Browse Source

Simplify test cases

pull/1349/head
Hironsan 3 years ago
parent
commit
5aae409f9a
4 changed files with 68 additions and 136 deletions
  1. 26
      backend/api/tests/api/test_label.py
  2. 80
      backend/api/tests/api/test_project.py
  3. 86
      backend/api/tests/api/test_tag.py
  4. 12
      backend/api/tests/api/utils.py

26
backend/api/tests/api/test_label.py

@ -6,19 +6,7 @@ from rest_framework.reverse import reverse
from rest_framework.test import APITestCase
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,
]
)
prepare_project)
class TestLabelList(APITestCase):
@ -84,10 +72,6 @@ class TestLabelCreate(APITestCase):
def test_disallows_unauthenticated_user_to_create_label(self):
self.assert_create_label(expected_status=status.HTTP_403_FORBIDDEN)
@classmethod
def doCleanups(cls):
remove_all_role_mappings()
class TestLabelDetailAPI(APITestCase):
@ -157,10 +141,6 @@ class TestLabelDetailAPI(APITestCase):
def test_disallows_unauthenticated_user_to_delete_label(self):
self.assert_delete_label(expected_status=status.HTTP_403_FORBIDDEN)
@classmethod
def doCleanups(cls):
remove_all_role_mappings()
class TestLabelUploadAPI(APITestCase):
@ -192,7 +172,3 @@ class TestLabelUploadAPI(APITestCase):
def test_try_to_upload_invalid_file(self):
self.assert_upload_file('invalid_labels.json', self.project.users[0], status.HTTP_400_BAD_REQUEST)
@classmethod
def doCleanups(cls):
remove_all_role_mappings()

80
backend/api/tests/api/test_project.py

@ -1,42 +1,32 @@
from django.conf import settings
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase
from ...models import DOCUMENT_CLASSIFICATION
from .utils import (create_default_roles, make_project, make_user,
remove_all_role_mappings)
from .utils import create_default_roles, make_user, prepare_project
class TestProjectList(APITestCase):
@classmethod
def setUpTestData(cls):
cls.project = make_project(
task=DOCUMENT_CLASSIFICATION,
users=['admin'],
roles=[settings.ROLE_PROJECT_ADMIN]
)
cls.project = prepare_project()
cls.non_member = make_user(username='bob')
cls.url = reverse(viewname='project_list')
def test_return_projects_to_member(self):
self.client.force_login(self.project.users[0])
response = self.client.get(self.url)
project = response.data[0]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 1)
self.assertEqual(project['id'], self.project.item.id)
for member in self.project.users:
self.client.force_login(member)
response = self.client.get(self.url)
project = response.data[0]
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 1)
self.assertEqual(project['id'], self.project.item.id)
def test_does_not_return_project_to_non_member(self):
self.client.force_login(self.non_member)
response = self.client.get(self.url)
self.assertEqual(len(response.data), 0)
@classmethod
def doCleanups(cls):
remove_all_role_mappings()
class TestProjectCreate(APITestCase):
@ -63,24 +53,12 @@ class TestProjectCreate(APITestCase):
response = self.client.post(self.url, self.data)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@classmethod
def doCleanups(cls):
remove_all_role_mappings()
class TestProjectDetailAPI(APITestCase):
@classmethod
def setUpTestData(cls):
cls.project = make_project(
task=DOCUMENT_CLASSIFICATION,
users=['admin', 'approver', 'annotator'],
roles=[
settings.ROLE_PROJECT_ADMIN,
settings.ROLE_ANNOTATION_APPROVER,
settings.ROLE_ANNOTATOR,
]
)
cls.project = prepare_project()
cls.non_member = make_user(username='bob')
cls.url = reverse(viewname='project_detail', args=[cls.project.item.id])
cls.data = {'description': 'lorem'}
@ -96,38 +74,34 @@ class TestProjectDetailAPI(APITestCase):
response = self.client.get(self.url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_allows_admin_to_update_project(self):
self.client.force_login(self.project.users[0])
def assert_update_project(self, user, expected):
self.client.force_login(user)
response = self.client.patch(self.url, data=self.data)
self.assertEqual(response.status_code, expected)
return response
def test_allows_admin_to_update_project(self):
response = self.assert_update_project(self.project.users[0], status.HTTP_200_OK)
self.assertEqual(response.data['description'], self.data['description'])
def test_disallows_non_admin_to_update_project(self):
for member in self.project.users[1:]:
self.client.force_login(member)
response = self.client.patch(self.url, data=self.data)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assert_update_project(member, status.HTTP_403_FORBIDDEN)
def test_disallows_non_member_to_update_project(self):
self.client.force_login(self.non_member)
response = self.client.patch(self.url, data=self.data)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assert_update_project(self.non_member, status.HTTP_403_FORBIDDEN)
def test_allows_admin_to_delete_project(self):
self.client.force_login(self.project.users[0])
def assert_delete_project(self, user, expected):
self.client.force_login(user)
response = self.client.delete(self.url)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(response.status_code, expected)
def test_allows_admin_to_delete_project(self):
self.assert_delete_project(self.project.users[0], status.HTTP_204_NO_CONTENT)
def test_disallows_non_admin_to_delete_project(self):
for member in self.project.users[1:]:
self.client.force_login(member)
response = self.client.delete(self.url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assert_delete_project(member, status.HTTP_403_FORBIDDEN)
def test_disallows_non_member_to_delete_project(self):
self.client.force_login(self.non_member)
response = self.client.delete(self.url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@classmethod
def doCleanups(cls):
remove_all_role_mappings()
self.assert_delete_project(self.non_member, status.HTTP_403_FORBIDDEN)

86
backend/api/tests/api/test_tag.py

@ -1,30 +1,25 @@
from django.conf import settings
from rest_framework import status
from rest_framework.reverse import reverse
from rest_framework.test import APITestCase
from ...models import DOCUMENT_CLASSIFICATION
from .utils import make_project, make_tag, make_user, remove_all_role_mappings
from .utils import make_tag, make_user, prepare_project
class TestTagList(APITestCase):
@classmethod
def setUpTestData(cls):
cls.project = make_project(
task=DOCUMENT_CLASSIFICATION,
users=['annotator'],
roles=[settings.ROLE_ANNOTATOR]
)
cls.project = prepare_project()
cls.non_member = make_user(username='bob')
make_tag(project=cls.project.item)
cls.url = reverse(viewname='tag_list', args=[cls.project.item.id])
def test_return_tags_to_member(self):
self.client.force_login(self.project.users[0])
response = self.client.get(self.url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 1)
for member in self.project.users:
self.client.force_login(member)
response = self.client.get(self.url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 1)
def test_does_not_return_tags_to_non_member(self):
self.client.force_login(self.non_member)
@ -35,83 +30,58 @@ class TestTagList(APITestCase):
response = self.client.get(self.url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@classmethod
def doCleanups(cls):
remove_all_role_mappings()
class TestTagCreate(APITestCase):
@classmethod
def setUpTestData(cls):
cls.project = make_project(
task=DOCUMENT_CLASSIFICATION,
users=['admin', 'approver', 'annotator'],
roles=[
settings.ROLE_PROJECT_ADMIN,
settings.ROLE_ANNOTATION_APPROVER,
settings.ROLE_ANNOTATOR,
]
)
cls.project = prepare_project()
cls.non_member = make_user(username='bob')
cls.url = reverse(viewname='tag_list', args=[cls.project.item.id])
cls.data = {'text': 'example'}
def test_allow_admin_to_create_tag(self):
self.client.force_login(self.project.users[0])
def assert_create_tag(self, user=None, expected=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)
return response
def test_allow_admin_to_create_tag(self):
response = self.assert_create_tag(self.project.users[0], status.HTTP_201_CREATED)
self.assertEqual(response.data['text'], self.data['text'])
def test_disallow_non_admin_to_create_tag(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_tag(member, status.HTTP_403_FORBIDDEN)
def test_disallow_unauthenticated_user_to_create_tag(self):
response = self.client.post(self.url, self.data)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@classmethod
def doCleanups(cls):
remove_all_role_mappings()
self.assert_create_tag(expected=status.HTTP_403_FORBIDDEN)
class TestTagDelete(APITestCase):
@classmethod
def setUpTestData(cls):
cls.project = make_project(
task=DOCUMENT_CLASSIFICATION,
users=['admin', 'approver', 'annotator'],
roles=[
settings.ROLE_PROJECT_ADMIN,
settings.ROLE_ANNOTATION_APPROVER,
settings.ROLE_ANNOTATOR,
]
)
cls.project = prepare_project()
cls.non_member = make_user(username='bob')
def setUp(self):
tag = make_tag(project=self.project.item)
self.url = reverse(viewname='tag_detail', args=[self.project.item.id, tag.id])
def test_allow_admin_to_delete_tag(self):
self.client.force_login(self.project.users[0])
def assert_delete_tag(self, user=None, expected=status.HTTP_403_FORBIDDEN):
if user:
self.client.force_login(user)
response = self.client.delete(self.url)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
self.assertEqual(response.status_code, expected)
def test_allow_admin_to_delete_tag(self):
self.assert_delete_tag(self.project.users[0], status.HTTP_204_NO_CONTENT)
def test_disallow_non_admin_to_delete_tag(self):
for member in self.project.users[1:]:
self.client.force_login(member)
response = self.client.delete(self.url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
self.assert_delete_tag(member, status.HTTP_403_FORBIDDEN)
def test_disallow_unauthenticated_user_to_delete_tag(self):
response = self.client.delete(self.url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
@classmethod
def doCleanups(cls):
remove_all_role_mappings()
self.assert_delete_tag(expected=status.HTTP_403_FORBIDDEN)

12
backend/api/tests/api/utils.py

@ -84,6 +84,18 @@ def make_label(project):
return mommy.make('Label', project=project)
def prepare_project(task: str = 'Any'):
return make_project(
task=task,
users=['admin', 'approver', 'annotator'],
roles=[
settings.ROLE_PROJECT_ADMIN,
settings.ROLE_ANNOTATION_APPROVER,
settings.ROLE_ANNOTATOR,
]
)
class TestUtilsMixin:
def _patch_project(self, project, attribute, value):
old_value = getattr(project, attribute, None)

Loading…
Cancel
Save