Browse Source

Update test_label.py

pull/1349/head
Hironsan 3 years ago
parent
commit
48d6bb4b85
1 changed files with 42 additions and 63 deletions
  1. 105
      backend/api/tests/api/test_label.py

105
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) 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): class TestLabelList(APITestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.non_member = make_user(username='bob') 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.label = make_label(cls.project_a.item)
cls.url = reverse(viewname='label_list', args=[cls.project_a.item.id]) cls.url = reverse(viewname='label_list', args=[cls.project_a.item.id])
@ -57,37 +61,28 @@ class TestLabelCreate(APITestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.non_member = make_user(username='bob') 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.url = reverse(viewname='label_list', args=[cls.project.item.id])
cls.data = {'text': 'example'} 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) 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): def test_disallows_non_admin_to_create_label(self):
for member in self.project.users[1:]: 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): 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): 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 @classmethod
def doCleanups(cls): def doCleanups(cls):
@ -99,15 +94,7 @@ class TestLabelDetailAPI(APITestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.non_member = make_user(username='bob') 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.label = make_label(cls.project.item)
cls.url = reverse(viewname='label_detail', args=[cls.project.item.id, cls.label.id]) cls.url = reverse(viewname='label_detail', args=[cls.project.item.id, cls.label.id])
cls.data = {'text': 'example'} cls.data = {'text': 'example'}
@ -127,7 +114,7 @@ class TestLabelDetailAPI(APITestCase):
response = self.client.get(self.url) response = self.client.get(self.url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) 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: if user:
self.client.force_login(user) self.client.force_login(user)
response = self.client.patch(self.url, data=self.data) response = self.client.patch(self.url, data=self.data)
@ -135,7 +122,7 @@ class TestLabelDetailAPI(APITestCase):
return response return response
def test_allows_admin_to_update_label(self): def test_allows_admin_to_update_label(self):
response = self.update_label(
response = self.assert_update_label(
user=self.project.users[0], user=self.project.users[0],
expected_status=status.HTTP_200_OK expected_status=status.HTTP_200_OK
) )
@ -143,32 +130,32 @@ class TestLabelDetailAPI(APITestCase):
def test_disallows_non_admin_to_update_label(self): def test_disallows_non_admin_to_update_label(self):
for member in self.project.users[1:]: 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): 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): 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: if user:
self.client.force_login(user) self.client.force_login(user)
response = self.client.delete(self.url) response = self.client.delete(self.url)
self.assertEqual(response.status_code, expected_status) self.assertEqual(response.status_code, expected_status)
def test_allows_admin_to_delete_label(self): 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): def test_disallows_non_admin_to_delete_label(self):
for member in self.project.users[1:]: 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): 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): 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 @classmethod
def doCleanups(cls): def doCleanups(cls):
@ -180,18 +167,10 @@ class TestLabelUploadAPI(APITestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.non_member = make_user(username='bob') 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]) 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: if user:
self.client.force_login(user) self.client.force_login(user)
with open(os.path.join(DATA_DIR, filename), 'rb') as f: 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) self.assertEqual(response.status_code, expected_status)
def test_allows_project_admin_to_upload_label(self): 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): def test_disallows_project_member_to_upload_label(self):
for member in self.project.users[1:]: 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): 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): 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): 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 @classmethod
def doCleanups(cls): def doCleanups(cls):

Loading…
Cancel
Save