From: Stephen Finucane Date: Sun, 28 Oct 2018 17:40:27 +0000 (+0000) Subject: tests: Add 'store_samples' decorator to 'test_check' X-Git-Tag: v2.2.0-rc1~205 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bb44420adc7c9e23aeea0fc60d48046824ce7feb;p=thirdparty%2Fpatchwork.git tests: Add 'store_samples' decorator to 'test_check' Signed-off-by: Stephen Finucane --- diff --git a/patchwork/tests/api/test_check.py b/patchwork/tests/api/test_check.py index 25a2a2c4..e784ca92 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,11 +73,17 @@ 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)) @@ -83,15 +93,15 @@ class TestCheckAPI(APITestCase): reverse('api-check-list', kwargs={'patch_id': '99999'})) self.assertEqual(status.HTTP_404_NOT_FOUND, resp.status_code) + @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', @@ -99,28 +109,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) - def test_create_no_permissions(self): - """Ensure creations are rejected by standard users.""" - check = { - 'state': 'success', - 'target_url': 'http://t.co', - 'description': 'description', - 'context': 'context', - } + @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_state(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', @@ -133,6 +152,7 @@ class TestCheckAPI(APITestCase): self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code) self.assertEqual(0, Check.objects.all().count()) + @utils.store_samples('check-create-error-not-found') def test_create_invalid_patch(self): """Ensure we handle non-existent patches.""" check = {