From 48d6bb4b85682cc77ee44dd076e1384665a9a026 Mon Sep 17 00:00:00 2001 From: Hironsan Date: Fri, 7 May 2021 17:48:28 +0900 Subject: [PATCH] Update test_label.py --- backend/api/tests/api/test_label.py | 105 +++++++++++----------------- 1 file changed, 42 insertions(+), 63 deletions(-) diff --git a/backend/api/tests/api/test_label.py b/backend/api/tests/api/test_label.py index 70830fd0..77c96b73 100644 --- a/backend/api/tests/api/test_label.py +++ b/backend/api/tests/api/test_label.py @@ -9,20 +9,24 @@ from .utils import (DATA_DIR, make_label, make_project, make_user, remove_all_role_mappings) +def prepare_project(): + return make_project( + task='Any', + users=['admin', 'approver', 'annotator'], + roles=[ + settings.ROLE_PROJECT_ADMIN, + settings.ROLE_ANNOTATION_APPROVER, + settings.ROLE_ANNOTATOR, + ] + ) + + class TestLabelList(APITestCase): @classmethod def setUpTestData(cls): cls.non_member = make_user(username='bob') - cls.project_a = make_project( - task='Any', - users=['admin', 'approver', 'annotator'], - roles=[ - settings.ROLE_PROJECT_ADMIN, - settings.ROLE_ANNOTATION_APPROVER, - settings.ROLE_ANNOTATOR, - ] - ) + cls.project_a = prepare_project() cls.label = make_label(cls.project_a.item) cls.url = reverse(viewname='label_list', args=[cls.project_a.item.id]) @@ -57,37 +61,28 @@ class TestLabelCreate(APITestCase): @classmethod def setUpTestData(cls): cls.non_member = make_user(username='bob') - cls.project = make_project( - task='Any', - users=['admin', 'approver', 'annotator'], - roles=[ - settings.ROLE_PROJECT_ADMIN, - settings.ROLE_ANNOTATION_APPROVER, - settings.ROLE_ANNOTATOR, - ] - ) + cls.project = prepare_project() cls.url = reverse(viewname='label_list', args=[cls.project.item.id]) cls.data = {'text': 'example'} - def test_allows_admin_to_create_label(self): - self.client.force_login(self.project.users[0]) + def assert_create_label(self, user=None, expected_status=status.HTTP_403_FORBIDDEN): + if user: + self.client.force_login(user) response = self.client.post(self.url, data=self.data) - self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(response.status_code, expected_status) + + def test_allows_admin_to_create_label(self): + self.assert_create_label(self.project.users[0], status.HTTP_201_CREATED) def test_disallows_non_admin_to_create_label(self): for member in self.project.users[1:]: - self.client.force_login(member) - response = self.client.post(self.url, data=self.data) - self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + self.assert_create_label(member, status.HTTP_403_FORBIDDEN) def test_disallows_non_project_member_to_create_label(self): - self.client.force_login(self.non_member) - response = self.client.post(self.url, data=self.data) - self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + self.assert_create_label(self.non_member, status.HTTP_403_FORBIDDEN) def test_disallows_unauthenticated_user_to_create_label(self): - response = self.client.post(self.url, data=self.data) - self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) + self.assert_create_label(expected_status=status.HTTP_403_FORBIDDEN) @classmethod def doCleanups(cls): @@ -99,15 +94,7 @@ class TestLabelDetailAPI(APITestCase): @classmethod def setUpTestData(cls): cls.non_member = make_user(username='bob') - cls.project = make_project( - task='Any', - users=['admin', 'approver', 'annotator'], - roles=[ - settings.ROLE_PROJECT_ADMIN, - settings.ROLE_ANNOTATION_APPROVER, - settings.ROLE_ANNOTATOR, - ] - ) + cls.project = prepare_project() cls.label = make_label(cls.project.item) cls.url = reverse(viewname='label_detail', args=[cls.project.item.id, cls.label.id]) cls.data = {'text': 'example'} @@ -127,7 +114,7 @@ class TestLabelDetailAPI(APITestCase): response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - def update_label(self, user=None, expected_status=status.HTTP_403_FORBIDDEN): + def assert_update_label(self, user=None, expected_status=status.HTTP_403_FORBIDDEN): if user: self.client.force_login(user) response = self.client.patch(self.url, data=self.data) @@ -135,7 +122,7 @@ class TestLabelDetailAPI(APITestCase): return response def test_allows_admin_to_update_label(self): - response = self.update_label( + response = self.assert_update_label( user=self.project.users[0], expected_status=status.HTTP_200_OK ) @@ -143,32 +130,32 @@ class TestLabelDetailAPI(APITestCase): def test_disallows_non_admin_to_update_label(self): for member in self.project.users[1:]: - self.update_label(member, status.HTTP_403_FORBIDDEN) + self.assert_update_label(member, status.HTTP_403_FORBIDDEN) def test_disallows_non_project_member_to_update_label(self): - self.update_label(self.non_member, status.HTTP_403_FORBIDDEN) + self.assert_update_label(self.non_member, status.HTTP_403_FORBIDDEN) def test_disallows_unauthenticated_user_to_update_label(self): - self.update_label(expected_status=status.HTTP_403_FORBIDDEN) + self.assert_update_label(expected_status=status.HTTP_403_FORBIDDEN) - def delete_label(self, user=None, expected_status=status.HTTP_403_FORBIDDEN): + def assert_delete_label(self, user=None, expected_status=status.HTTP_403_FORBIDDEN): if user: self.client.force_login(user) response = self.client.delete(self.url) self.assertEqual(response.status_code, expected_status) def test_allows_admin_to_delete_label(self): - self.delete_label(self.project.users[0], status.HTTP_204_NO_CONTENT) + self.assert_delete_label(self.project.users[0], status.HTTP_204_NO_CONTENT) def test_disallows_non_admin_to_delete_label(self): for member in self.project.users[1:]: - self.delete_label(member, status.HTTP_403_FORBIDDEN) + self.assert_delete_label(member, status.HTTP_403_FORBIDDEN) def test_disallows_non_project_member_to_delete_label(self): - self.delete_label(self.non_member, status.HTTP_403_FORBIDDEN) + self.assert_delete_label(self.non_member, status.HTTP_403_FORBIDDEN) def test_disallows_unauthenticated_user_to_delete_label(self): - self.delete_label(expected_status=status.HTTP_403_FORBIDDEN) + self.assert_delete_label(expected_status=status.HTTP_403_FORBIDDEN) @classmethod def doCleanups(cls): @@ -180,18 +167,10 @@ class TestLabelUploadAPI(APITestCase): @classmethod def setUpTestData(cls): cls.non_member = make_user(username='bob') - cls.project = make_project( - task='Any', - users=['admin', 'approver', 'annotator'], - roles=[ - settings.ROLE_PROJECT_ADMIN, - settings.ROLE_ANNOTATION_APPROVER, - settings.ROLE_ANNOTATOR, - ] - ) + cls.project = prepare_project() cls.url = reverse(viewname='label_upload', args=[cls.project.item.id]) - def upload_file(self, filename, user=None, expected_status=status.HTTP_403_FORBIDDEN): + def assert_upload_file(self, filename, user=None, expected_status=status.HTTP_403_FORBIDDEN): if user: self.client.force_login(user) with open(os.path.join(DATA_DIR, filename), 'rb') as f: @@ -199,20 +178,20 @@ class TestLabelUploadAPI(APITestCase): self.assertEqual(response.status_code, expected_status) def test_allows_project_admin_to_upload_label(self): - self.upload_file('valid_labels.json', self.project.users[0], status.HTTP_201_CREATED) + self.assert_upload_file('valid_labels.json', self.project.users[0], status.HTTP_201_CREATED) def test_disallows_project_member_to_upload_label(self): for member in self.project.users[1:]: - self.upload_file('valid_labels.json', member, status.HTTP_403_FORBIDDEN) + self.assert_upload_file('valid_labels.json', member, status.HTTP_403_FORBIDDEN) def test_disallows_non_project_member_to_upload_label(self): - self.upload_file('valid_labels.json', self.non_member, status.HTTP_403_FORBIDDEN) + self.assert_upload_file('valid_labels.json', self.non_member, status.HTTP_403_FORBIDDEN) def test_disallows_unauthenticated_user_to_upload_label(self): - self.upload_file('valid_labels.json', expected_status=status.HTTP_403_FORBIDDEN) + self.assert_upload_file('valid_labels.json', expected_status=status.HTTP_403_FORBIDDEN) def test_try_to_upload_invalid_file(self): - self.upload_file('invalid_labels.json', self.project.users[0], status.HTTP_400_BAD_REQUEST) + self.assert_upload_file('invalid_labels.json', self.project.users[0], status.HTTP_400_BAD_REQUEST) @classmethod def doCleanups(cls):