[damned-lies: 1/4] Separate api tests with uploading file




commit a806c24a67a6b24195f8355d5edbe962ae009b70
Author: Claude Paroz <claude 2xlibre net>
Date:   Wed Sep 7 18:39:38 2022 +0200

    Separate api tests with uploading file

 api/tests.py | 71 +++++++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 46 insertions(+), 25 deletions(-)
---
diff --git a/api/tests.py b/api/tests.py
index 482497ab..d18eb1a9 100644
--- a/api/tests.py
+++ b/api/tests.py
@@ -150,53 +150,74 @@ class APITests(TestCase):
         response = self.client.post(url, data={})
         self.assertEqual(response.status_code, 403)
 
-    def test_upload_translation(self):
-        translator = Person.objects.create(
+class APIUploadTests(TestCase):
+    fixtures = ['sample_data.json']
+
+    @classmethod
+    def setUpTestData(cls):
+        super().setUpTestData()
+        cls.translator = Person.objects.create(
             first_name='John', last_name='Translator',
             email='jt devnull com', username='translator'
         )
-        somebody = Person.objects.create(
-            email='some devnull com', username='somebody'
-        )
-        team = Team.objects.get(name='fr')
-        Role.objects.create(team=team, person=translator)
-        Role.objects.create(team=team, person=somebody)
+        cls.team = Team.objects.get(name='fr')
+        Role.objects.create(team=cls.team, person=cls.translator)
+        cls.url = reverse('api-upload', args=['gnome-hello', 'master', 'po', 'fr'])
+        cls.test_po = Path(__file__).parent.parent / "stats" / "tests" / "test.po"
 
+    def _get_module_state(self):
+        """Create basic data ready for testing file upload."""
         _, _, state = get_vertimus_state(
             Branch.objects.get(module__name='gnome-hello'),
             Domain.objects.get(module__name='gnome-hello', name='po'),
             Language.objects.get(locale='fr')
         )
-        url = reverse('api-upload', args=['gnome-hello', 'master', 'po', 'fr'])
-        test_po = Path(__file__).parent.parent / "stats" / "tests" / "test.po"
-        # Test anonymous cannot post
-        with test_po.open('rb') as fh:
-            response = self.client.post(url, data={'file': File(fh)})
-            self.assertRedirects(response, reverse('login') + f'?next={url}')
+        return state
 
-        state.change_state(StateTranslating, person=translator)
+    def test_upload_translation_anonymously(self):
+        """Non-logged in user cannot submit file."""
+        somebody = Person.objects.create(
+            email='some devnull com', username='somebody'
+        )
+        Role.objects.create(team=self.team, person=somebody)
+
+        # Anonymous cannot post
+        with self.test_po.open('rb') as fh:
+            response = self.client.post(self.url, data={'file': File(fh)})
+            self.assertRedirects(response, reverse('login') + f'?next={self.url}')
 
-        # somebody cannot post translation if reserved by translator.
+    def test_upload_translation_translating(self):
+        """If module is already reserved by another translator, uploading will be refused."""
+        state = self._get_module_state()
+        state.change_state(StateTranslating, person=self.translator)
+
+        somebody = Person.objects.create(
+            email='some devnull com', username='somebody'
+        )
+        Role.objects.create(team=self.team, person=somebody)
         self.client.force_login(somebody)
-        with test_po.open('rb') as fh:
-            response = self.client.post(url, data={'file': File(fh)})
+        with self.test_po.open('rb') as fh:
+            response = self.client.post(self.url, data={'file': File(fh)})
             self.assertEqual(response.status_code, 403)
 
-        self.client.force_login(translator)
-        with test_po.open('rb') as fh:
-            response = self.client.post(url, data={'file': File(fh)})
+    def test_upload_translation(self):
+        state = self._get_module_state()
+        state.change_state(StateTranslating, person=self.translator)
+        self.client.force_login(self.translator)
+        with self.test_po.open('rb') as fh:
+            response = self.client.post(self.url, data={'file': File(fh)})
         self.assertEqual(response.json(), {'result': 'OK'})
         self.assertEqual(len(mail.outbox), 1)
-        self.assertEqual(mail.outbox[0].recipients(), [team.mailing_list])
+        self.assertEqual(mail.outbox[0].recipients(), [self.team.mailing_list])
 
         # Test upload with comment
-        state.change_state(StateTranslating, person=translator)
-        with test_po.open('rb') as fh:
+        state.change_state(StateTranslating, person=self.translator)
+        with self.test_po.open('rb') as fh:
             data = {
                 'file': File(fh),
                 'comment': 'The comment',
             }
-            response = self.client.post(url, data=data)
+            response = self.client.post(self.url, data=data)
         self.assertEqual(response.json(), {'result': 'OK'})
         action = Action.objects.last()
         self.assertEqual(action.comment, "The comment")


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]