[PATCH 06/13] tests: Add 'store_samples' decorator to 'test_patch'

Stephen Finucane stephen at that.guru
Tue Oct 30 22:31:46 AEDT 2018


Signed-off-by: Stephen Finucane <stephen at that.guru>
---
 patchwork/tests/api/test_patch.py | 145 +++++++++++++++++++++---------
 1 file changed, 102 insertions(+), 43 deletions(-)

diff --git a/patchwork/tests/api/test_patch.py b/patchwork/tests/api/test_patch.py
index df30d510..7d34ba09 100644
--- a/patchwork/tests/api/test_patch.py
+++ b/patchwork/tests/api/test_patch.py
@@ -11,6 +11,7 @@ from django.conf import settings
 from django.urls import reverse
 
 from patchwork.models import Patch
+from patchwork.tests.api import utils
 from patchwork.tests.utils import create_maintainer
 from patchwork.tests.utils import create_patch
 from patchwork.tests.utils import create_person
@@ -58,73 +59,98 @@ class TestPatchAPI(APITestCase):
         self.assertEqual(patch_obj.project.id,
                          patch_json['project']['id'])
 
-    def test_list(self):
-        """Validate we can list a patch."""
+    def test_list_empty(self):
+        """List patches when none are present."""
         resp = self.client.get(self.api_url())
         self.assertEqual(status.HTTP_200_OK, resp.status_code)
         self.assertEqual(0, len(resp.data))
 
+    def _create_patch(self):
         person_obj = create_person(email='test at example.com')
         project_obj = create_project(linkname='myproject')
         state_obj = create_state(name='Under Review')
         patch_obj = create_patch(state=state_obj, project=project_obj,
                                  submitter=person_obj)
 
-        # anonymous user
+        return patch_obj
+
+    def test_list_anonymous(self):
+        """List patches as anonymous user."""
+        patch = self._create_patch()
+
         resp = self.client.get(self.api_url())
         self.assertEqual(status.HTTP_200_OK, resp.status_code)
         self.assertEqual(1, len(resp.data))
         patch_rsp = resp.data[0]
-        self.assertSerialized(patch_obj, patch_rsp)
+        self.assertSerialized(patch, patch_rsp)
         self.assertNotIn('headers', patch_rsp)
         self.assertNotIn('content', patch_rsp)
         self.assertNotIn('diff', patch_rsp)
 
-        # authenticated user
+    @utils.store_samples('patch-list')
+    def test_list_authenticated(self):
+        """List patches as an authenticated user."""
+        patch = self._create_patch()
         user = create_user()
+
         self.client.force_authenticate(user=user)
         resp = self.client.get(self.api_url())
         self.assertEqual(status.HTTP_200_OK, resp.status_code)
         self.assertEqual(1, len(resp.data))
         patch_rsp = resp.data[0]
-        self.assertSerialized(patch_obj, patch_rsp)
+        self.assertSerialized(patch, patch_rsp)
 
-        # test filtering by state
-        resp = self.client.get(self.api_url(), {'state': 'under-review'})
-        self.assertEqual([patch_obj.id], [x['id'] for x in resp.data])
-        resp = self.client.get(self.api_url(), {'state': 'missing-state'})
-        self.assertEqual(0, len(resp.data))
+    def test_list_filter_state(self):
+        """Filter patches by state."""
+        self._create_patch()
+        user = create_user()
+
+        state_obj_b = create_state(name='New')
+        create_patch(state=state_obj_b)
+        state_obj_c = create_state(name='RFC')
+        create_patch(state=state_obj_c)
+
+        self.client.force_authenticate(user=user)
+        resp = self.client.get(self.api_url(), [('state', 'under-review'),
+                                                ('state', 'new')])
+        self.assertEqual(2, len(resp.data))
+
+    def test_list_filter_project(self):
+        """Filter patches by project."""
+        patch = self._create_patch()
+        user = create_user()
+
+        self.client.force_authenticate(user=user)
 
-        # test filtering by project
         resp = self.client.get(self.api_url(), {'project': 'myproject'})
-        self.assertEqual([patch_obj.id], [x['id'] for x in resp.data])
+        self.assertEqual([patch.id], [x['id'] for x in resp.data])
+
         resp = self.client.get(self.api_url(), {'project': 'invalidproject'})
         self.assertEqual(0, len(resp.data))
 
+    def test_list_filter_submitter(self):
+        """Filter patches by submitter."""
+        patch = self._create_patch()
+        submitter = patch.submitter
+        user = create_user()
+
+        self.client.force_authenticate(user=user)
+
         # test filtering by submitter, both ID and email
-        resp = self.client.get(self.api_url(), {'submitter': person_obj.id})
-        self.assertEqual([patch_obj.id], [x['id'] for x in resp.data])
+        resp = self.client.get(self.api_url(), {'submitter': submitter.id})
+        self.assertEqual([patch.id], [x['id'] for x in resp.data])
+
         resp = self.client.get(self.api_url(), {
             'submitter': 'test at example.com'})
-        self.assertEqual([patch_obj.id], [x['id'] for x in resp.data])
+        self.assertEqual([patch.id], [x['id'] for x in resp.data])
+
         resp = self.client.get(self.api_url(), {
             'submitter': 'test at example.org'})
         self.assertEqual(0, len(resp.data))
 
-        state_obj_b = create_state(name='New')
-        create_patch(state=state_obj_b)
-        state_obj_c = create_state(name='RFC')
-        create_patch(state=state_obj_c)
-
-        resp = self.client.get(self.api_url())
-        self.assertEqual(3, len(resp.data))
-        resp = self.client.get(self.api_url(), [('state', 'under-review')])
-        self.assertEqual(1, len(resp.data))
-        resp = self.client.get(self.api_url(), [('state', 'under-review'),
-                                                ('state', 'new')])
-        self.assertEqual(2, len(resp.data))
-
+    @utils.store_samples('patch-list-1-0')
     def test_list_version_1_0(self):
+        """List patches using API v1.0."""
         create_patch()
 
         resp = self.client.get(self.api_url(version='1.0'))
@@ -133,8 +159,9 @@ class TestPatchAPI(APITestCase):
         self.assertIn('url', resp.data[0])
         self.assertNotIn('web_url', resp.data[0])
 
+    @utils.store_samples('patch-detail')
     def test_detail(self):
-        """Validate we can get a specific patch."""
+        """Show a specific patch."""
         patch = create_patch(
             content='Reviewed-by: Test User <test at example.com>\n',
             headers='Received: from somewhere\nReceived: from another place'
@@ -154,6 +181,7 @@ class TestPatchAPI(APITestCase):
         self.assertEqual(patch.diff, resp.data['diff'])
         self.assertEqual(0, len(resp.data['tags']))
 
+    @utils.store_samples('patch-detail-1-0')
     def test_detail_version_1_0(self):
         patch = create_patch()
 
@@ -185,27 +213,45 @@ class TestPatchAPI(APITestCase):
         resp = self.client.post(self.api_url(), patch)
         self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code)
 
-    def test_update(self):
-        """Ensure updates can be performed by maintainers."""
-        project = create_project()
-        patch = create_patch(project=project)
+    @utils.store_samples('patch-update-error-forbidden')
+    def test_update_anonymous(self):
+        """Update patch as anonymous user.
+
+        Ensure updates can be performed by maintainers.
+        """
+        patch = create_patch()
         state = create_state()
 
-        # anonymous user
         resp = self.client.patch(self.api_url(patch.id), {'state': state.name})
         self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
 
-        # authenticated user
+    def test_update_non_maintainer(self):
+        """Update patch as non-maintainer.
+
+        Ensure updates can be performed by maintainers.
+        """
+        patch = create_patch()
+        state = create_state()
         user = create_user()
+
         self.client.force_authenticate(user=user)
         resp = self.client.patch(self.api_url(patch.id), {'state': state.name})
         self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
 
-        # maintainer
+    @utils.store_samples('patch-update')
+    def test_update_maintainer(self):
+        """Update patch as maintainer.
+
+        Ensure updates can be performed by maintainers.
+        """
+        project = create_project()
+        patch = create_patch(project=project)
+        state = create_state()
         user = create_maintainer(project)
+
         self.client.force_authenticate(user=user)
-        resp = self.client.patch(self.api_url(patch.id), {
-            'state': state.name, 'delegate': user.id})
+        resp = self.client.patch(self.api_url(patch.id),
+                                 {'state': state.name, 'delegate': user.id})
         self.assertEqual(status.HTTP_200_OK, resp.status_code, resp)
         self.assertEqual(Patch.objects.get(id=patch.id).state, state)
         self.assertEqual(Patch.objects.get(id=patch.id).delegate, user)
@@ -217,22 +263,35 @@ class TestPatchAPI(APITestCase):
         self.assertEqual(status.HTTP_200_OK, resp.status_code, resp)
         self.assertIsNone(Patch.objects.get(id=patch.id).delegate)
 
-    def test_update_invalid(self):
-        """Ensure we handle invalid Patch updates."""
+    @utils.store_samples('patch-update-error-bad-request')
+    def test_update_invalid_state(self):
+        """Update patch with invalid fields.
+
+        Ensure we handle invalid Patch updates.
+        """
         project = create_project()
         state = create_state()
         patch = create_patch(project=project, state=state)
         user = create_maintainer(project)
 
-        # invalid state
         self.client.force_authenticate(user=user)
         resp = self.client.patch(self.api_url(patch.id), {'state': 'foobar'})
         self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
         self.assertContains(resp, 'Expected one of: %s.' % state.name,
                             status_code=status.HTTP_400_BAD_REQUEST)
 
-        # invalid delegate
+    def test_update_invalid_delegate(self):
+        """Update patch with invalid fields.
+
+        Ensure we handle invalid Patch updates.
+        """
+        project = create_project()
+        state = create_state()
+        patch = create_patch(project=project, state=state)
+        user_a = create_maintainer(project)
         user_b = create_user()
+
+        self.client.force_authenticate(user=user_a)
         resp = self.client.patch(self.api_url(patch.id),
                                  {'delegate': user_b.id})
         self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code)
-- 
2.17.2



More information about the Patchwork mailing list