[10/13] tests: Add 'store_samples' decorator to 'test_check'

Message ID 20181030113153.7855-11-stephen@that.guru
State New
Headers show
Series
  • Start generating API examples from tests
Related show

Commit Message

Stephen Finucane Oct. 30, 2018, 11:31 a.m.
Signed-off-by: Stephen Finucane <stephen@that.guru>
---
 patchwork/tests/api/test_check.py | 54 +++++++++++++++++++++++--------
 1 file changed, 41 insertions(+), 13 deletions(-)

Patch

diff --git a/patchwork/tests/api/test_check.py b/patchwork/tests/api/test_check.py
index 783a6154..42a6231a 100644
--- a/patchwork/tests/api/test_check.py
+++ b/patchwork/tests/api/test_check.py
@@ -9,6 +9,7 @@  from django.conf import settings
 from django.urls import reverse
 
 from patchwork.models import Check
+from patchwork.tests.api import utils
 from patchwork.tests.utils import create_check
 from patchwork.tests.utils import create_patch
 from patchwork.tests.utils import create_maintainer
@@ -55,12 +56,15 @@  class TestCheckAPI(APITestCase):
         self.assertEqual(check_obj.description, check_json['description'])
         self.assertEqual(check_obj.user.id, check_json['user']['id'])
 
-    def test_list(self):
-        """Validate we can list checks on a patch."""
+    def test_list_empty(self):
+        """List checks 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))
 
+    @utils.store_samples('check-list')
+    def test_list(self):
+        """List checks."""
         check_obj = self._create_check()
         self._create_check(create_patch())  # second, unrelated patch
 
@@ -69,23 +73,29 @@  class TestCheckAPI(APITestCase):
         self.assertEqual(1, len(resp.data))
         self.assertSerialized(check_obj, resp.data[0])
 
+    def test_list_filter_user(self):
+        """Filter checks by user."""
+        check_obj = self._create_check()
+
         # test filtering by owner, both ID and username
         resp = self.client.get(self.api_url(), {'user': self.user.id})
         self.assertEqual([check_obj.id], [x['id'] for x in resp.data])
+
         resp = self.client.get(self.api_url(), {'user': self.user.username})
         self.assertEqual([check_obj.id], [x['id'] for x in resp.data])
+
         resp = self.client.get(self.api_url(), {'user': 'otheruser'})
         self.assertEqual(0, len(resp.data))
 
+    @utils.store_samples('check-detail')
     def test_detail(self):
-        """Validate we can get a specific check."""
+        """Show a check."""
         check = self._create_check()
         resp = self.client.get(self.api_url(check))
         self.assertEqual(status.HTTP_200_OK, resp.status_code)
         self.assertSerialized(check, resp.data)
 
-    def test_create(self):
-        """Ensure creations can be performed by user of patch."""
+    def _test_create(self, user):
         check = {
             'state': 'success',
             'target_url': 'http://t.co',
@@ -93,19 +103,37 @@  class TestCheckAPI(APITestCase):
             'context': 'context',
         }
 
-        self.client.force_authenticate(user=self.user)
-        resp = self.client.post(self.api_url(), check)
-        self.assertEqual(status.HTTP_201_CREATED, resp.status_code)
-        self.assertEqual(1, Check.objects.all().count())
-        self.assertSerialized(Check.objects.first(), resp.data)
+        self.client.force_authenticate(user=user)
+        return self.client.post(self.api_url(), check)
+
+    @utils.store_samples('check-create-error-forbidden')
+    def test_create_non_maintainer(self):
+        """Create a check as a non-maintainer.
 
+        Ensure creations can only be performed by maintainers.
+        """
         user = create_user()
-        self.client.force_authenticate(user=user)
-        resp = self.client.post(self.api_url(), check)
+
+        resp = self._test_create(user=user)
         self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
 
+    @utils.store_samples('check-create')
+    def test_create_maintainer(self):
+        """Create a check as a maintainer.
+
+        Ensure creations can only be performed by maintainers.
+        """
+        resp = self._test_create(user=self.user)
+        self.assertEqual(status.HTTP_201_CREATED, resp.status_code)
+        self.assertEqual(1, Check.objects.all().count())
+        self.assertSerialized(Check.objects.first(), resp.data)
+
+    @utils.store_samples('check-create-error-bad-request')
     def test_create_invalid(self):
-        """Ensure we handle invalid check states."""
+        """Create a check using invalid values.
+
+        Ensure we handle invalid check states.
+        """
         check = {
             'state': 'this-is-not-a-valid-state',
             'target_url': 'http://t.co',