You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1899 lines
97 KiB

3 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. import os
  2. from django.conf import settings
  3. from django.test import override_settings
  4. from model_mommy import mommy
  5. from rest_framework import status
  6. from rest_framework.reverse import reverse
  7. from rest_framework.test import APITestCase
  8. from ..exceptions import FileParseException
  9. from ..models import (DOCUMENT_CLASSIFICATION, SEQ2SEQ, SEQUENCE_LABELING,
  10. SPEECH2TEXT, Comment, Document, Role, RoleMapping,
  11. SequenceAnnotation, User)
  12. from ..utils import (CoNLLParser, CSVParser, FastTextParser, JSONParser,
  13. PlainTextParser)
  14. DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
  15. def create_default_roles():
  16. Role.objects.get_or_create(name=settings.ROLE_PROJECT_ADMIN)
  17. Role.objects.get_or_create(name=settings.ROLE_ANNOTATOR)
  18. Role.objects.get_or_create(name=settings.ROLE_ANNOTATION_APPROVER)
  19. def assign_user_to_role(project_member, project, role_name):
  20. role, _ = Role.objects.get_or_create(name=role_name)
  21. if RoleMapping.objects.filter(user=project_member, project=project).exists():
  22. mapping = RoleMapping.objects.get(user=project_member, project=project)
  23. mapping.role = role
  24. mapping.save()
  25. else:
  26. mapping = RoleMapping.objects.get_or_create(role_id=role.id, user_id=project_member.id, project_id=project.id)
  27. return mapping
  28. def remove_all_role_mappings():
  29. RoleMapping.objects.all().delete()
  30. class TestHealthEndpoint(APITestCase):
  31. @classmethod
  32. def setUpTestData(cls):
  33. cls.url = reverse(viewname='health')
  34. def test_returns_green_status_on_health_endpoint(self):
  35. response = self.client.get(self.url, format='json')
  36. self.assertEqual(response.data['status'], 'green')
  37. class TestUtilsMixin:
  38. def _patch_project(self, project, attribute, value):
  39. old_value = getattr(project, attribute, None)
  40. setattr(project, attribute, value)
  41. project.save()
  42. def cleanup_project():
  43. setattr(project, attribute, old_value)
  44. project.save()
  45. self.addCleanup(cleanup_project)
  46. @override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
  47. class TestProjectListAPI(APITestCase):
  48. @classmethod
  49. def setUpTestData(cls):
  50. cls.main_project_member_name = 'project_member_name'
  51. cls.main_project_member_pass = 'project_member_pass'
  52. cls.sub_project_member_name = 'sub_project_member_name'
  53. cls.sub_project_member_pass = 'sub_project_member_pass'
  54. cls.approver_name = 'approver_name_name'
  55. cls.approver_pass = 'approver_pass'
  56. cls.super_user_name = 'super_user_name'
  57. cls.super_user_pass = 'super_user_pass'
  58. create_default_roles()
  59. main_project_member = User.objects.create_user(username=cls.main_project_member_name,
  60. password=cls.main_project_member_pass)
  61. sub_project_member = User.objects.create_user(username=cls.sub_project_member_name,
  62. password=cls.sub_project_member_pass)
  63. approver = User.objects.create_user(username=cls.approver_name,
  64. password=cls.approver_pass)
  65. User.objects.create_superuser(username=cls.super_user_name,
  66. password=cls.super_user_pass,
  67. email='fizz@buzz.com')
  68. cls.main_project = mommy.make('TextClassificationProject', users=[main_project_member])
  69. cls.sub_project = mommy.make('TextClassificationProject', users=[sub_project_member])
  70. assign_user_to_role(project_member=main_project_member, project=cls.main_project,
  71. role_name=settings.ROLE_ANNOTATOR)
  72. assign_user_to_role(project_member=sub_project_member, project=cls.sub_project,
  73. role_name=settings.ROLE_ANNOTATOR)
  74. assign_user_to_role(project_member=approver, project=cls.main_project,
  75. role_name=settings.ROLE_ANNOTATION_APPROVER)
  76. cls.url = reverse(viewname='project_list')
  77. cls.data = {'name': 'example', 'project_type': 'DocumentClassification',
  78. 'description': 'example', 'guideline': 'example',
  79. 'resourcetype': 'TextClassificationProject'}
  80. cls.num_project = main_project_member.projects.count()
  81. def test_returns_main_project_to_approver(self):
  82. self.client.login(username=self.approver_name,
  83. password=self.approver_pass)
  84. response = self.client.get(self.url, format='json')
  85. project = response.data[0]
  86. num_project = len(response.data)
  87. self.assertEqual(num_project, self.num_project)
  88. self.assertEqual(project['id'], self.main_project.id)
  89. def test_returns_main_project_to_main_project_member(self):
  90. self.client.login(username=self.main_project_member_name,
  91. password=self.main_project_member_pass)
  92. response = self.client.get(self.url, format='json')
  93. project = response.data[0]
  94. num_project = len(response.data)
  95. self.assertEqual(num_project, self.num_project)
  96. self.assertEqual(project['id'], self.main_project.id)
  97. def test_do_not_return_main_project_to_sub_project_member(self):
  98. self.client.login(username=self.sub_project_member_name,
  99. password=self.sub_project_member_pass)
  100. response = self.client.get(self.url, format='json')
  101. project = response.data[0]
  102. num_project = len(response.data)
  103. self.assertEqual(num_project, self.num_project)
  104. self.assertNotEqual(project['id'], self.main_project.id)
  105. def test_allows_superuser_to_create_project(self):
  106. self.client.login(username=self.super_user_name,
  107. password=self.super_user_pass)
  108. response = self.client.post(self.url, format='json', data=self.data)
  109. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  110. self.assertFalse(response.json().get('collaborative_annotation'))
  111. self.assertFalse(response.json().get('randomize_document_order'))
  112. def test_allows_superuser_to_create_project_with_flags(self):
  113. self.client.login(username=self.super_user_name,
  114. password=self.super_user_pass)
  115. data = dict(self.data)
  116. data['collaborative_annotation'] = True
  117. data['randomize_document_order'] = True
  118. response = self.client.post(self.url, format='json', data=data)
  119. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  120. self.assertTrue(response.json().get('collaborative_annotation'))
  121. self.assertTrue(response.json().get('randomize_document_order'))
  122. @classmethod
  123. def doCleanups(cls):
  124. remove_all_role_mappings()
  125. @override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
  126. class TestProjectDetailAPI(APITestCase):
  127. @classmethod
  128. def setUpTestData(cls):
  129. cls.project_member_name = 'project_member_name'
  130. cls.project_member_pass = 'project_member_pass'
  131. cls.non_project_member_name = 'non_project_member_name'
  132. cls.non_project_member_pass = 'non_project_member_pass'
  133. cls.admin_user_name = 'admin_user_name'
  134. cls.admin_user_pass = 'admin_user_pass'
  135. create_default_roles()
  136. cls.project_member = User.objects.create_user(username=cls.project_member_name,
  137. password=cls.project_member_pass)
  138. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  139. password=cls.non_project_member_pass)
  140. project_admin = User.objects.create_superuser(username=cls.admin_user_name,
  141. password=cls.admin_user_pass,
  142. email='fizz@buzz.com')
  143. cls.main_project = mommy.make('TextClassificationProject', users=[cls.project_member, project_admin])
  144. mommy.make('TextClassificationProject', users=[non_project_member])
  145. cls.url = reverse(viewname='project_detail', args=[cls.main_project.id])
  146. cls.data = {'description': 'lorem'}
  147. assign_user_to_role(project_member=cls.project_member, project=cls.main_project,
  148. role_name=settings.ROLE_ANNOTATOR)
  149. assign_user_to_role(project_member=project_admin, project=cls.main_project,
  150. role_name=settings.ROLE_PROJECT_ADMIN)
  151. def test_returns_main_project_detail_to_main_project_member(self):
  152. self.client.login(username=self.project_member_name,
  153. password=self.project_member_pass)
  154. response = self.client.get(self.url, format='json')
  155. self.assertEqual(response.data['id'], self.main_project.id)
  156. def test_do_not_return_main_project_to_sub_project_member(self):
  157. self.client.login(username=self.non_project_member_name,
  158. password=self.non_project_member_pass)
  159. response = self.client.get(self.url, format='json')
  160. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  161. def test_allows_admin_to_update_project(self):
  162. self.client.login(username=self.admin_user_name,
  163. password=self.admin_user_pass)
  164. response = self.client.patch(self.url, format='json', data=self.data)
  165. self.assertEqual(response.data['description'], self.data['description'])
  166. def test_disallows_non_project_member_to_update_project(self):
  167. self.client.login(username=self.non_project_member_name,
  168. password=self.non_project_member_pass)
  169. response = self.client.patch(self.url, format='json', data=self.data)
  170. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  171. def test_allows_admin_to_delete_project(self):
  172. self.client.login(username=self.admin_user_name,
  173. password=self.admin_user_pass)
  174. response = self.client.delete(self.url, format='json')
  175. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  176. def test_disallows_non_project_member_to_delete_project(self):
  177. self.client.login(username=self.non_project_member_name,
  178. password=self.non_project_member_pass)
  179. response = self.client.delete(self.url, format='json')
  180. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  181. @classmethod
  182. def doCleanups(cls):
  183. remove_all_role_mappings()
  184. class TestLabelListAPI(APITestCase):
  185. @classmethod
  186. def setUpTestData(cls):
  187. cls.project_member_name = 'project_member_name'
  188. cls.project_member_pass = 'project_member_pass'
  189. cls.non_project_member_name = 'non_project_member_name'
  190. cls.non_project_member_pass = 'non_project_member_pass'
  191. cls.admin_user_name = 'admin_user_name'
  192. cls.admin_user_pass = 'admin_user_pass'
  193. create_default_roles()
  194. cls.project_member = User.objects.create_user(username=cls.project_member_name,
  195. password=cls.project_member_pass)
  196. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  197. password=cls.non_project_member_pass)
  198. project_admin = User.objects.create_superuser(username=cls.admin_user_name,
  199. password=cls.admin_user_pass,
  200. email='fizz@buzz.com')
  201. cls.main_project = mommy.make('Project', users=[cls.project_member, project_admin])
  202. cls.main_project_label = mommy.make('Label', project=cls.main_project)
  203. sub_project = mommy.make('Project', users=[non_project_member])
  204. other_project = mommy.make('Project', users=[project_admin])
  205. mommy.make('Label', project=sub_project)
  206. cls.url = reverse(viewname='label_list', args=[cls.main_project.id])
  207. cls.other_url = reverse(viewname='label_list', args=[other_project.id])
  208. cls.data = {'text': 'example'}
  209. assign_user_to_role(project_member=cls.project_member, project=cls.main_project,
  210. role_name=settings.ROLE_ANNOTATOR)
  211. def test_returns_labels_to_project_member(self):
  212. self.client.login(username=self.project_member_name,
  213. password=self.project_member_pass)
  214. response = self.client.get(self.url, format='json')
  215. self.assertEqual(response.status_code, status.HTTP_200_OK)
  216. def test_do_not_return_labels_to_non_project_member(self):
  217. self.client.login(username=self.non_project_member_name,
  218. password=self.non_project_member_pass)
  219. response = self.client.get(self.url, format='json')
  220. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  221. def test_do_not_return_labels_of_other_projects(self):
  222. self.client.login(username=self.project_member_name,
  223. password=self.project_member_pass)
  224. response = self.client.get(self.url, format='json')
  225. label = response.data[0]
  226. num_labels = len(response.data)
  227. self.assertEqual(num_labels, len(self.main_project.labels.all()))
  228. self.assertEqual(label['id'], self.main_project_label.id)
  229. def test_allows_admin_to_create_label(self):
  230. self.client.login(username=self.admin_user_name,
  231. password=self.admin_user_pass)
  232. response = self.client.post(self.url, format='json', data=self.data)
  233. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  234. def test_can_create_multiple_labels_without_shortcut_key(self):
  235. self.client.login(username=self.admin_user_name,
  236. password=self.admin_user_pass)
  237. labels = [
  238. {'text': 'Ruby', 'prefix_key': None, 'suffix_key': None},
  239. {'text': 'PHP', 'prefix_key': None, 'suffix_key': None}
  240. ]
  241. for label in labels:
  242. response = self.client.post(self.url, format='json', data=label)
  243. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  244. def test_can_create_same_label_in_multiple_projects(self):
  245. self.client.login(username=self.admin_user_name,
  246. password=self.admin_user_pass)
  247. label = {'text': 'LOC', 'prefix_key': None, 'suffix_key': 'l'}
  248. response = self.client.post(self.url, format='json', data=label)
  249. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  250. response = self.client.post(self.other_url, format='json', data=label)
  251. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  252. def test_can_create_same_suffix_with_different_prefix(self):
  253. self.client.login(username=self.admin_user_name,
  254. password=self.admin_user_pass)
  255. label = {'text': 'Person', 'prefix_key': None, 'suffix_key': 'p'}
  256. response = self.client.post(self.url, format='json', data=label)
  257. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  258. label = {'text': 'Percentage', 'prefix_key': 'ctrl', 'suffix_key': 'p'}
  259. response = self.client.post(self.url, format='json', data=label)
  260. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  261. def test_cannot_create_same_shortcut_key(self):
  262. self.client.login(username=self.admin_user_name,
  263. password=self.admin_user_pass)
  264. label = {'text': 'Person', 'prefix_key': None, 'suffix_key': 'p'}
  265. response = self.client.post(self.url, format='json', data=label)
  266. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  267. label = {'text': 'Percentage', 'prefix_key': None, 'suffix_key': 'p'}
  268. response = self.client.post(self.url, format='json', data=label)
  269. self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
  270. def test_disallows_project_member_to_create_label(self):
  271. self.client.login(username=self.project_member_name,
  272. password=self.project_member_pass)
  273. response = self.client.post(self.url, format='json', data=self.data)
  274. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  275. @classmethod
  276. def doCleanups(cls):
  277. remove_all_role_mappings()
  278. class TestLabelDetailAPI(APITestCase):
  279. @classmethod
  280. def setUpTestData(cls):
  281. cls.project_member_name = 'project_member_name'
  282. cls.project_member_pass = 'project_member_pass'
  283. cls.non_project_member_name = 'non_project_member_name'
  284. cls.non_project_member_pass = 'non_project_member_pass'
  285. cls.super_user_name = 'super_user_name'
  286. cls.super_user_pass = 'super_user_pass'
  287. create_default_roles()
  288. project_member = User.objects.create_user(username=cls.project_member_name,
  289. password=cls.project_member_pass)
  290. User.objects.create_user(username=cls.non_project_member_name, password=cls.non_project_member_pass)
  291. # Todo: change super_user to project_admin.
  292. super_user = User.objects.create_superuser(username=cls.super_user_name,
  293. password=cls.super_user_pass,
  294. email='fizz@buzz.com')
  295. project = mommy.make('Project', users=[project_member, super_user])
  296. cls.label = mommy.make('Label', project=project)
  297. cls.label_with_shortcut = mommy.make('Label', suffix_key='l', project=project)
  298. cls.url = reverse(viewname='label_detail', args=[project.id, cls.label.id])
  299. cls.url_with_shortcut = reverse(viewname='label_detail', args=[project.id, cls.label_with_shortcut.id])
  300. cls.data = {'text': 'example'}
  301. create_default_roles()
  302. assign_user_to_role(project_member=project_member, project=project,
  303. role_name=settings.ROLE_ANNOTATOR)
  304. def test_returns_label_to_project_member(self):
  305. self.client.login(username=self.project_member_name,
  306. password=self.project_member_pass)
  307. response = self.client.get(self.url, format='json')
  308. self.assertEqual(response.data['id'], self.label.id)
  309. def test_do_not_return_label_to_non_project_member(self):
  310. self.client.login(username=self.non_project_member_name,
  311. password=self.non_project_member_pass)
  312. response = self.client.get(self.url, format='json')
  313. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  314. def test_allows_superuser_to_update_label(self):
  315. self.client.login(username=self.super_user_name,
  316. password=self.super_user_pass)
  317. response = self.client.patch(self.url, format='json', data=self.data)
  318. self.assertEqual(response.data['text'], self.data['text'])
  319. def test_allows_superuser_to_update_label_with_shortcut(self):
  320. self.client.login(username=self.super_user_name,
  321. password=self.super_user_pass)
  322. response = self.client.patch(self.url_with_shortcut, format='json', data={'suffix_key': 's'})
  323. self.assertEqual(response.data['suffix_key'], 's')
  324. def test_disallows_project_member_to_update_label(self):
  325. self.client.login(username=self.project_member_name,
  326. password=self.project_member_pass)
  327. response = self.client.patch(self.url, format='json', data=self.data)
  328. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  329. def test_allows_superuser_to_delete_label(self):
  330. self.client.login(username=self.super_user_name,
  331. password=self.super_user_pass)
  332. response = self.client.delete(self.url, format='json')
  333. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  334. def test_disallows_project_member_to_delete_label(self):
  335. self.client.login(username=self.project_member_name,
  336. password=self.project_member_pass)
  337. response = self.client.delete(self.url, format='json')
  338. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  339. @classmethod
  340. def doCleanups(cls):
  341. remove_all_role_mappings()
  342. class TestLabelUploadAPI(APITestCase):
  343. @classmethod
  344. def setUpTestData(cls):
  345. cls.project_member_name = 'project_member_name'
  346. cls.project_member_pass = 'project_member_pass'
  347. cls.non_project_member_name = 'non_project_member_name'
  348. cls.non_project_member_pass = 'non_project_member_pass'
  349. cls.super_user_name = 'super_user_name'
  350. cls.super_user_pass = 'super_user_pass'
  351. create_default_roles()
  352. project_member = User.objects.create_user(username=cls.project_member_name,
  353. password=cls.project_member_pass)
  354. User.objects.create_user(username=cls.non_project_member_name, password=cls.non_project_member_pass)
  355. project_admin = User.objects.create_user(username=cls.super_user_name,
  356. password=cls.super_user_pass)
  357. project = mommy.make('Project', users=[project_member, project_admin])
  358. cls.url = reverse(viewname='label_upload', args=[project.id])
  359. create_default_roles()
  360. assign_user_to_role(project_member=project_admin, project=project, role_name=settings.ROLE_PROJECT_ADMIN)
  361. assign_user_to_role(project_member=project_member, project=project, role_name=settings.ROLE_ANNOTATOR)
  362. def help_to_upload_file(self, filename, expected_status):
  363. with open(os.path.join(DATA_DIR, filename), 'rb') as f:
  364. response = self.client.post(self.url, data={'file': f})
  365. self.assertEqual(response.status_code, expected_status)
  366. def test_allows_project_admin_to_upload_label(self):
  367. self.client.login(username=self.super_user_name,
  368. password=self.super_user_pass)
  369. self.help_to_upload_file('valid_labels.json', status.HTTP_201_CREATED)
  370. def test_disallows_project_member_to_upload_label(self):
  371. self.client.login(username=self.project_member_name,
  372. password=self.project_member_pass)
  373. self.help_to_upload_file('valid_labels.json', status.HTTP_403_FORBIDDEN)
  374. def test_try_to_upload_invalid_file(self):
  375. self.client.login(username=self.super_user_name,
  376. password=self.super_user_pass)
  377. self.help_to_upload_file('invalid_labels.json', status.HTTP_400_BAD_REQUEST)
  378. @classmethod
  379. def doCleanups(cls):
  380. remove_all_role_mappings()
  381. class TestDocumentListAPI(APITestCase, TestUtilsMixin):
  382. @classmethod
  383. def setUpTestData(cls):
  384. cls.project_member_name = 'project_member_name'
  385. cls.project_member_pass = 'project_member_pass'
  386. cls.non_project_member_name = 'non_project_member_name'
  387. cls.non_project_member_pass = 'non_project_member_pass'
  388. cls.super_user_name = 'super_user_name'
  389. cls.super_user_pass = 'super_user_pass'
  390. create_default_roles()
  391. project_member = User.objects.create_user(username=cls.project_member_name,
  392. password=cls.project_member_pass)
  393. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  394. password=cls.non_project_member_pass)
  395. super_user = User.objects.create_superuser(username=cls.super_user_name,
  396. password=cls.super_user_pass,
  397. email='fizz@buzz.com')
  398. cls.main_project = mommy.make('TextClassificationProject', users=[project_member, super_user])
  399. doc1 = mommy.make('Document', project=cls.main_project)
  400. doc2 = mommy.make('Document', project=cls.main_project)
  401. mommy.make('Document', project=cls.main_project)
  402. cls.random_order_project = mommy.make('TextClassificationProject', users=[project_member, super_user],
  403. randomize_document_order=True)
  404. mommy.make('Document', 100, project=cls.random_order_project)
  405. sub_project = mommy.make('TextClassificationProject', users=[non_project_member])
  406. mommy.make('Document', project=sub_project)
  407. cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
  408. cls.random_order_project_url = reverse(viewname='doc_list', args=[cls.random_order_project.id])
  409. cls.data = {'text': 'example'}
  410. assign_user_to_role(project_member=project_member, project=cls.main_project,
  411. role_name=settings.ROLE_ANNOTATOR)
  412. assign_user_to_role(project_member=project_member, project=cls.random_order_project,
  413. role_name=settings.ROLE_ANNOTATOR)
  414. mommy.make('DocumentAnnotation', document=doc1, user=project_member)
  415. mommy.make('DocumentAnnotation', document=doc2, user=project_member)
  416. def _test_list(self, url, username, password, expected_num_results):
  417. self.client.login(username=username, password=password)
  418. response = self.client.get(url, format='json')
  419. self.assertEqual(response.status_code, status.HTTP_200_OK)
  420. self.assertEqual(len(response.json().get('results')), expected_num_results)
  421. def test_returns_docs_to_project_member(self):
  422. self._test_list(self.url,
  423. username=self.project_member_name,
  424. password=self.project_member_pass,
  425. expected_num_results=3)
  426. def test_returns_docs_to_project_member_filtered_to_active(self):
  427. self._test_list('{}?doc_annotations__isnull=true'.format(self.url),
  428. username=self.project_member_name,
  429. password=self.project_member_pass,
  430. expected_num_results=1)
  431. def test_returns_docs_to_project_member_filtered_to_completed(self):
  432. self._test_list('{}?doc_annotations__isnull=false'.format(self.url),
  433. username=self.project_member_name,
  434. password=self.project_member_pass,
  435. expected_num_results=2)
  436. def test_returns_docs_to_project_member_filtered_to_active_with_collaborative_annotation(self):
  437. self._test_list('{}?doc_annotations__isnull=true'.format(self.url),
  438. username=self.super_user_name,
  439. password=self.super_user_pass,
  440. expected_num_results=3)
  441. self._patch_project(self.main_project, 'collaborative_annotation', True)
  442. self._test_list('{}?doc_annotations__isnull=true'.format(self.url),
  443. username=self.super_user_name,
  444. password=self.super_user_pass,
  445. expected_num_results=1)
  446. def test_returns_docs_to_project_member_filtered_to_completed_with_collaborative_annotation(self):
  447. self._test_list('{}?doc_annotations__isnull=false'.format(self.url),
  448. username=self.super_user_name,
  449. password=self.super_user_pass,
  450. expected_num_results=0)
  451. self._patch_project(self.main_project, 'collaborative_annotation', True)
  452. self._test_list('{}?doc_annotations__isnull=false'.format(self.url),
  453. username=self.super_user_name,
  454. password=self.super_user_pass,
  455. expected_num_results=2)
  456. def test_returns_docs_in_consistent_order_for_all_users(self):
  457. self.client.login(username=self.project_member_name, password=self.project_member_pass)
  458. user1_documents = self.client.get(self.url, format='json').json().get('results')
  459. self.client.logout()
  460. self.client.login(username=self.super_user_name, password=self.super_user_pass)
  461. user2_documents = self.client.get(self.url, format='json').json().get('results')
  462. self.client.logout()
  463. self.assertEqual([doc['id'] for doc in user1_documents], [doc['id'] for doc in user2_documents])
  464. def test_can_return_docs_in_consistent_random_order(self):
  465. self.client.login(username=self.project_member_name, password=self.project_member_pass)
  466. user1_documents1 = self.client.get(self.random_order_project_url, format='json').json().get('results')
  467. user1_documents2 = self.client.get(self.random_order_project_url, format='json').json().get('results')
  468. self.client.logout()
  469. self.assertEqual(user1_documents1, user1_documents2)
  470. self.client.login(username=self.super_user_name, password=self.super_user_pass)
  471. user2_documents1 = self.client.get(self.random_order_project_url, format='json').json().get('results')
  472. user2_documents2 = self.client.get(self.random_order_project_url, format='json').json().get('results')
  473. self.client.logout()
  474. self.assertEqual(user2_documents1, user2_documents2)
  475. self.assertNotEqual(user1_documents1, user2_documents1)
  476. self.assertNotEqual(user1_documents2, user2_documents2)
  477. def test_do_not_return_docs_to_non_project_member(self):
  478. self.client.login(username=self.non_project_member_name,
  479. password=self.non_project_member_pass)
  480. response = self.client.get(self.url, format='json')
  481. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  482. def test_do_not_return_docs_of_other_projects(self):
  483. self._test_list(self.url,
  484. username=self.project_member_name,
  485. password=self.project_member_pass,
  486. expected_num_results=self.main_project.documents.count())
  487. def test_allows_superuser_to_create_doc(self):
  488. self.client.login(username=self.super_user_name,
  489. password=self.super_user_pass)
  490. response = self.client.post(self.url, format='json', data=self.data)
  491. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  492. def test_disallows_project_member_to_create_doc(self):
  493. self.client.login(username=self.project_member_name,
  494. password=self.project_member_pass)
  495. response = self.client.post(self.url, format='json', data=self.data)
  496. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  497. @classmethod
  498. def doCleanups(cls):
  499. remove_all_role_mappings()
  500. class TestDocumentDetailAPI(APITestCase):
  501. @classmethod
  502. def setUpTestData(cls):
  503. cls.project_member_name = 'project_member_name'
  504. cls.project_member_pass = 'project_member_pass'
  505. cls.non_project_member_name = 'non_project_member_name'
  506. cls.non_project_member_pass = 'non_project_member_pass'
  507. cls.super_user_name = 'super_user_name'
  508. cls.super_user_pass = 'super_user_pass'
  509. create_default_roles()
  510. project_member = User.objects.create_user(username=cls.project_member_name,
  511. password=cls.project_member_pass)
  512. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  513. password=cls.non_project_member_pass)
  514. # Todo: change super_user to project_admin.
  515. super_user = User.objects.create_superuser(username=cls.super_user_name,
  516. password=cls.super_user_pass,
  517. email='fizz@buzz.com')
  518. project = mommy.make('TextClassificationProject', users=[project_member, super_user])
  519. cls.doc = mommy.make('Document', project=project)
  520. cls.url = reverse(viewname='doc_detail', args=[project.id, cls.doc.id])
  521. cls.data = {'text': 'example'}
  522. assign_user_to_role(project_member=project_member, project=project,
  523. role_name=settings.ROLE_ANNOTATOR)
  524. def test_returns_doc_to_project_member(self):
  525. self.client.login(username=self.project_member_name,
  526. password=self.project_member_pass)
  527. response = self.client.get(self.url, format='json')
  528. self.assertEqual(response.data['id'], self.doc.id)
  529. def test_do_not_return_doc_to_non_project_member(self):
  530. self.client.login(username=self.non_project_member_name,
  531. password=self.non_project_member_pass)
  532. response = self.client.get(self.url, format='json')
  533. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  534. def test_allows_superuser_to_update_doc(self):
  535. self.client.login(username=self.super_user_name,
  536. password=self.super_user_pass)
  537. response = self.client.patch(self.url, format='json', data=self.data)
  538. self.assertEqual(response.data['text'], self.data['text'])
  539. def test_disallows_project_member_to_update_doc(self):
  540. self.client.login(username=self.project_member_name,
  541. password=self.project_member_pass)
  542. response = self.client.patch(self.url, format='json', data=self.data)
  543. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  544. def test_allows_superuser_to_delete_doc(self):
  545. self.client.login(username=self.super_user_name,
  546. password=self.super_user_pass)
  547. response = self.client.delete(self.url, format='json')
  548. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  549. def test_disallows_project_member_to_delete_doc(self):
  550. self.client.login(username=self.project_member_name,
  551. password=self.project_member_pass)
  552. response = self.client.delete(self.url, format='json')
  553. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  554. @classmethod
  555. def doCleanups(cls):
  556. remove_all_role_mappings()
  557. class TestApproveLabelsAPI(APITestCase):
  558. @classmethod
  559. def setUpTestData(cls):
  560. cls.annotator_name = 'annotator_name'
  561. cls.annotator_pass = 'annotator_pass'
  562. cls.approver_name = 'approver_name_name'
  563. cls.approver_pass = 'approver_pass'
  564. cls.project_admin_name = 'project_admin_name'
  565. cls.project_admin_pass = 'project_admin_pass'
  566. create_default_roles()
  567. annotator = User.objects.create_user(username=cls.annotator_name,
  568. password=cls.annotator_pass)
  569. approver = User.objects.create_user(username=cls.approver_name,
  570. password=cls.approver_pass)
  571. project_admin = User.objects.create_user(username=cls.project_admin_name,
  572. password=cls.project_admin_pass)
  573. project = mommy.make('TextClassificationProject', users=[annotator, approver, project_admin])
  574. cls.doc = mommy.make('Document', project=project)
  575. cls.url = reverse(viewname='approve_labels', args=[project.id, cls.doc.id])
  576. assign_user_to_role(project_member=annotator, project=project,
  577. role_name=settings.ROLE_ANNOTATOR)
  578. assign_user_to_role(project_member=approver, project=project,
  579. role_name=settings.ROLE_ANNOTATION_APPROVER)
  580. assign_user_to_role(project_member=project_admin, project=project,
  581. role_name=settings.ROLE_PROJECT_ADMIN)
  582. def test_allow_project_admin_to_approve_and_disapprove_labels(self):
  583. self.client.login(username=self.project_admin_name, password=self.project_admin_pass)
  584. response = self.client.post(self.url, format='json', data={'approved': True})
  585. self.assertEqual(response.data['annotation_approver'], self.project_admin_name)
  586. response = self.client.post(self.url, format='json', data={'approved': False})
  587. self.assertIsNone(response.data['annotation_approver'])
  588. def test_allow_approver_to_approve_and_disapprove_labels(self):
  589. self.client.login(username=self.approver_name, password=self.approver_pass)
  590. response = self.client.post(self.url, format='json', data={'approved': True})
  591. self.assertEqual(response.data['annotation_approver'], self.approver_name)
  592. response = self.client.post(self.url, format='json', data={'approved': False})
  593. self.assertIsNone(response.data['annotation_approver'])
  594. def test_disallows_non_annotation_approver_to_approve_and_disapprove_labels(self):
  595. self.client.login(username=self.annotator_name, password=self.annotator_pass)
  596. response = self.client.post(self.url, format='json', data={'approved': True})
  597. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  598. @classmethod
  599. def doCleanups(cls):
  600. remove_all_role_mappings()
  601. class TestCommentListAPI(APITestCase):
  602. @classmethod
  603. def setUpTestData(cls):
  604. cls.project_member_name = 'project_member_name'
  605. cls.project_member_pass = 'project_member_pass'
  606. cls.another_project_member_name = 'another_project_member_name'
  607. cls.another_project_member_pass = 'another_project_member_pass'
  608. cls.non_project_member_name = 'non_project_member_name'
  609. cls.non_project_member_pass = 'non_project_member_pass'
  610. create_default_roles()
  611. cls.project_member = User.objects.create_user(username=cls.project_member_name,
  612. password=cls.project_member_pass)
  613. another_project_member = User.objects.create_user(username=cls.another_project_member_name,
  614. password=cls.another_project_member_pass)
  615. User.objects.create_user(username=cls.non_project_member_name, password=cls.non_project_member_pass)
  616. main_project = mommy.make('SequenceLabelingProject', users=[cls.project_member, another_project_member])
  617. main_project_doc = mommy.make('Document', project=main_project)
  618. cls.comment = mommy.make('Comment', document=main_project_doc, text='comment 1', user=cls.project_member)
  619. mommy.make('Comment', document=main_project_doc, text='comment 2', user=cls.project_member)
  620. mommy.make('Comment', document=main_project_doc, text='comment 3', user=another_project_member)
  621. cls.url = reverse(viewname='comment_list_doc', args=[main_project.id, main_project_doc.id])
  622. cls.url_project = reverse(viewname='comment_list_project', args=[main_project.id])
  623. assign_user_to_role(project_member=cls.project_member, project=main_project,
  624. role_name=settings.ROLE_ANNOTATOR)
  625. assign_user_to_role(project_member=another_project_member, project=main_project,
  626. role_name=settings.ROLE_ANNOTATOR)
  627. def test_returns_comments_to_project_member(self):
  628. self.client.login(username=self.project_member_name,
  629. password=self.project_member_pass)
  630. response = self.client.get(self.url, format='json')
  631. self.assertEqual(response.status_code, status.HTTP_200_OK)
  632. self.assertEqual(len(response.data), 3)
  633. self.client.login(username=self.another_project_member_name,
  634. password=self.another_project_member_pass)
  635. response = self.client.get(self.url, format='json')
  636. self.assertEqual(response.status_code, status.HTTP_200_OK)
  637. self.assertEqual(len(response.data), 3)
  638. def test_does_not_return_comments_to_non_project_member(self):
  639. self.client.login(username=self.non_project_member_name,
  640. password=self.non_project_member_pass)
  641. response = self.client.get(self.url, format='json')
  642. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  643. def test_does_not_allow_deletion_by_non_project_member(self):
  644. self.client.login(username=self.non_project_member_name,
  645. password=self.non_project_member_pass)
  646. response = self.client.delete('{}/{}'.format(self.url, self.comment.id), format='json')
  647. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  648. def test_does_not_allow_deletion_of_non_owned_comment(self):
  649. self.client.login(username=self.another_project_member_name,
  650. password=self.another_project_member_pass)
  651. response = self.client.delete('{}/{}'.format(self.url, self.comment.id), format='json')
  652. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  653. def test_create_update_delete_comment(self):
  654. self.client.login(username=self.project_member_name,
  655. password=self.project_member_pass)
  656. response = self.client.post(self.url, format='json', data={'text': 'comment'})
  657. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  658. self.assertEqual(response.data['user'], self.project_member.id)
  659. self.assertEqual(response.data['text'], 'comment')
  660. url = '{}/{}'.format(self.url, response.data['id'])
  661. # check if all comments are fetched
  662. response = self.client.get(self.url, format='json')
  663. self.assertEqual(response.status_code, status.HTTP_200_OK)
  664. self.assertEqual(len(response.data), 4)
  665. # update comment
  666. response = self.client.patch(url, format='json', data={'text': 'new comment'})
  667. self.assertEqual(response.status_code, status.HTTP_200_OK)
  668. self.assertEqual(response.data['text'], 'new comment')
  669. # delete comment
  670. response = self.client.delete(url)
  671. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  672. response = self.client.get(self.url, format='json')
  673. self.assertEqual(response.status_code, status.HTTP_200_OK)
  674. self.assertEqual(len(response.data), 3)
  675. def test_returns_project_comments_to_project_member(self):
  676. self.client.login(username=self.project_member_name,
  677. password=self.project_member_pass)
  678. response = self.client.get(self.url_project, format='json')
  679. self.assertEqual(response.status_code, status.HTTP_200_OK)
  680. self.assertEqual(len(response.data), 3)
  681. self.client.login(username=self.another_project_member_name,
  682. password=self.another_project_member_pass)
  683. response = self.client.get(self.url, format='json')
  684. self.assertEqual(response.status_code, status.HTTP_200_OK)
  685. self.assertEqual(len(response.data), 3)
  686. def test_does_not_return_project_comments_to_non_project_member(self):
  687. self.client.login(username=self.non_project_member_name,
  688. password=self.non_project_member_pass)
  689. response = self.client.get(self.url_project, format='json')
  690. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  691. @classmethod
  692. def doCleanups(cls):
  693. remove_all_role_mappings()
  694. class TestAnnotationListAPI(APITestCase, TestUtilsMixin):
  695. @classmethod
  696. def setUpTestData(cls):
  697. cls.project_member_name = 'project_member_name'
  698. cls.project_member_pass = 'project_member_pass'
  699. cls.another_project_member_name = 'another_project_member_name'
  700. cls.another_project_member_pass = 'another_project_member_pass'
  701. cls.non_project_member_name = 'non_project_member_name'
  702. cls.non_project_member_pass = 'non_project_member_pass'
  703. create_default_roles()
  704. project_member = User.objects.create_user(username=cls.project_member_name,
  705. password=cls.project_member_pass)
  706. another_project_member = User.objects.create_user(username=cls.another_project_member_name,
  707. password=cls.another_project_member_pass)
  708. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  709. password=cls.non_project_member_pass)
  710. main_project = mommy.make('SequenceLabelingProject', users=[project_member, another_project_member])
  711. main_project_label = mommy.make('Label', project=main_project)
  712. main_project_doc = mommy.make('Document', project=main_project)
  713. mommy.make('SequenceAnnotation', document=main_project_doc, user=project_member)
  714. mommy.make('SequenceAnnotation', document=main_project_doc, user=another_project_member)
  715. sub_project = mommy.make('SequenceLabelingProject', users=[non_project_member])
  716. sub_project_doc = mommy.make('Document', project=sub_project)
  717. mommy.make('SequenceAnnotation', document=sub_project_doc)
  718. cls.classification_project = mommy.make('TextClassificationProject',
  719. users=[project_member, another_project_member])
  720. cls.classification_project_label_1 = mommy.make('Label', project=cls.classification_project)
  721. cls.classification_project_label_2 = mommy.make('Label', project=cls.classification_project)
  722. cls.classification_project_document = mommy.make('Document', project=cls.classification_project)
  723. cls.classification_project_url = reverse(
  724. viewname='annotation_list', args=[cls.classification_project.id, cls.classification_project_document.id])
  725. assign_user_to_role(project_member=project_member, project=cls.classification_project,
  726. role_name=settings.ROLE_ANNOTATOR)
  727. assign_user_to_role(project_member=another_project_member, project=cls.classification_project,
  728. role_name=settings.ROLE_ANNOTATOR)
  729. cls.url = reverse(viewname='annotation_list', args=[main_project.id, main_project_doc.id])
  730. cls.post_data = {'start_offset': 0, 'end_offset': 1, 'label': main_project_label.id}
  731. cls.num_entity_of_project_member = SequenceAnnotation.objects.filter(document=main_project_doc,
  732. user=project_member).count()
  733. cls.num_entity_of_another_project_member = SequenceAnnotation.objects.filter(
  734. document=main_project_doc,
  735. user=another_project_member).count()
  736. cls.main_project = main_project
  737. assign_user_to_role(project_member=project_member, project=main_project,
  738. role_name=settings.ROLE_ANNOTATOR)
  739. def test_returns_annotations_to_project_member(self):
  740. self.client.login(username=self.project_member_name,
  741. password=self.project_member_pass)
  742. response = self.client.get(self.url, format='json')
  743. self.assertEqual(response.status_code, status.HTTP_200_OK)
  744. def test_do_not_return_annotations_to_non_project_member(self):
  745. self.client.login(username=self.non_project_member_name,
  746. password=self.non_project_member_pass)
  747. response = self.client.get(self.url, format='json')
  748. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  749. def test_do_not_return_annotations_of_another_project_member(self):
  750. self.client.login(username=self.project_member_name,
  751. password=self.project_member_pass)
  752. response = self.client.get(self.url, format='json')
  753. self.assertEqual(len(response.data), self.num_entity_of_project_member)
  754. def test_returns_annotations_of_another_project_member_if_collaborative_project(self):
  755. self._patch_project(self.main_project, 'collaborative_annotation', True)
  756. self.client.login(username=self.project_member_name,
  757. password=self.project_member_pass)
  758. response = self.client.get(self.url, format='json')
  759. self.assertEqual(len(response.data),
  760. self.num_entity_of_project_member + self.num_entity_of_another_project_member)
  761. def test_allows_project_member_to_create_annotation(self):
  762. self.client.login(username=self.project_member_name,
  763. password=self.project_member_pass)
  764. response = self.client.post(self.url, format='json', data=self.post_data)
  765. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  766. def test_disallows_non_project_member_to_create_annotation(self):
  767. self.client.login(username=self.non_project_member_name,
  768. password=self.non_project_member_pass)
  769. response = self.client.post(self.url, format='json', data=self.post_data)
  770. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  771. def test_allow_replace_annotation_for_single_class_project(self):
  772. self._patch_project(self.classification_project, 'single_class_classification', True)
  773. self.client.login(username=self.project_member_name, password=self.project_member_pass)
  774. response = self.client.post(self.classification_project_url, format='json',
  775. data={'label': self.classification_project_label_1.id})
  776. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  777. response = self.client.post(self.classification_project_url, format='json',
  778. data={'label': self.classification_project_label_2.id})
  779. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  780. def test_allow_replace_annotation_for_single_class_shared_project(self):
  781. self._patch_project(self.classification_project, 'single_class_classification', True)
  782. self._patch_project(self.classification_project, 'collaborative_annotation', True)
  783. self.client.login(username=self.project_member_name, password=self.project_member_pass)
  784. response = self.client.post(self.classification_project_url, format='json',
  785. data={'label': self.classification_project_label_1.id})
  786. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  787. self.client.login(username=self.another_project_member_name, password=self.another_project_member_pass)
  788. response = self.client.post(self.classification_project_url, format='json',
  789. data={'label': self.classification_project_label_2.id})
  790. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  791. def _patch_project(self, project, attribute, value):
  792. old_value = getattr(project, attribute, None)
  793. setattr(project, attribute, value)
  794. project.save()
  795. def cleanup_project():
  796. setattr(project, attribute, old_value)
  797. project.save()
  798. self.addCleanup(cleanup_project)
  799. @classmethod
  800. def doCleanups(cls):
  801. remove_all_role_mappings()
  802. class TestAnnotationDetailAPI(APITestCase):
  803. @classmethod
  804. def setUpTestData(cls):
  805. cls.super_user_name = 'super_user_name'
  806. cls.super_user_pass = 'super_user_pass'
  807. cls.project_member_name = 'project_member_name'
  808. cls.project_member_pass = 'project_member_pass'
  809. cls.another_project_member_name = 'another_project_member_name'
  810. cls.another_project_member_pass = 'another_project_member_pass'
  811. cls.non_project_member_name = 'non_project_member_name'
  812. cls.non_project_member_pass = 'non_project_member_pass'
  813. # Todo: change super_user to project_admin.
  814. super_user = User.objects.create_superuser(username=cls.super_user_name,
  815. password=cls.super_user_pass,
  816. email='fizz@buzz.com')
  817. create_default_roles()
  818. project_member = User.objects.create_user(username=cls.project_member_name,
  819. password=cls.project_member_pass)
  820. another_project_member = User.objects.create_user(username=cls.another_project_member_name,
  821. password=cls.another_project_member_pass)
  822. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  823. password=cls.non_project_member_pass)
  824. main_project = mommy.make('SequenceLabelingProject',
  825. users=[super_user, project_member, another_project_member])
  826. main_project_doc = mommy.make('Document', project=main_project)
  827. main_project_entity = mommy.make('SequenceAnnotation',
  828. document=main_project_doc, user=project_member)
  829. another_entity = mommy.make('SequenceAnnotation',
  830. document=main_project_doc, user=another_project_member)
  831. shared_project = mommy.make('SequenceLabelingProject',
  832. collaborative_annotation=True,
  833. users=[project_member, another_project_member])
  834. shared_project_doc = mommy.make('Document', project=shared_project)
  835. shared_entity = mommy.make('SequenceAnnotation', document=shared_project_doc, user=another_project_member)
  836. cls.url = reverse(viewname='annotation_detail', args=[main_project.id,
  837. main_project_doc.id,
  838. main_project_entity.id])
  839. cls.another_url = reverse(viewname='annotation_detail', args=[main_project.id,
  840. main_project_doc.id,
  841. another_entity.id])
  842. cls.shared_url = reverse(viewname='annotation_detail', args=[shared_project.id,
  843. shared_project_doc.id,
  844. shared_entity.id])
  845. cls.post_data = {'start_offset': 0, 'end_offset': 10}
  846. assign_user_to_role(project_member=project_member, project=main_project, role_name=settings.ROLE_ANNOTATOR)
  847. assign_user_to_role(project_member=project_member, project=shared_project, role_name=settings.ROLE_ANNOTATOR)
  848. def test_returns_annotation_to_project_member(self):
  849. self.client.login(username=self.project_member_name,
  850. password=self.project_member_pass)
  851. response = self.client.get(self.url, format='json')
  852. self.assertEqual(response.status_code, status.HTTP_200_OK)
  853. def test_do_not_return_annotation_to_non_project_member(self):
  854. self.client.login(username=self.non_project_member_name,
  855. password=self.non_project_member_pass)
  856. response = self.client.get(self.url, format='json')
  857. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  858. def test_do_not_return_annotation_by_another_project_member(self):
  859. self.client.login(username=self.project_member_name,
  860. password=self.project_member_pass)
  861. response = self.client.get(self.another_url, format='json')
  862. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  863. def test_allows_project_member_to_update_annotation(self):
  864. self.client.login(username=self.project_member_name,
  865. password=self.project_member_pass)
  866. response = self.client.patch(self.url, format='json', data=self.post_data)
  867. self.assertEqual(response.status_code, status.HTTP_200_OK)
  868. def test_disallows_non_project_member_to_update_annotation(self):
  869. self.client.login(username=self.non_project_member_name,
  870. password=self.non_project_member_pass)
  871. response = self.client.patch(self.url, format='json', data=self.post_data)
  872. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  873. def test_disallows_project_member_to_update_annotation_of_another_member(self):
  874. self.client.login(username=self.project_member_name,
  875. password=self.project_member_pass)
  876. response = self.client.patch(self.another_url, format='json', data=self.post_data)
  877. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  878. def test_allows_superuser_to_delete_annotation_of_another_member(self):
  879. self.client.login(username=self.super_user_name,
  880. password=self.super_user_pass)
  881. response = self.client.delete(self.another_url, format='json', data=self.post_data)
  882. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  883. def test_allows_project_member_to_delete_annotation(self):
  884. self.client.login(username=self.project_member_name,
  885. password=self.project_member_pass)
  886. response = self.client.delete(self.url, format='json')
  887. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  888. def test_disallows_project_member_to_delete_annotation(self):
  889. self.client.login(username=self.non_project_member_name,
  890. password=self.non_project_member_pass)
  891. response = self.client.delete(self.url, format='json')
  892. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  893. def test_disallows_project_member_to_delete_annotation_of_another_member(self):
  894. self.client.login(username=self.project_member_name,
  895. password=self.project_member_pass)
  896. response = self.client.delete(self.another_url, format='json', data=self.post_data)
  897. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  898. def test_allow_member_to_update_others_annotation_in_shared_project(self):
  899. self.client.login(username=self.project_member_name,
  900. password=self.project_member_pass)
  901. response = self.client.patch(self.shared_url, format='json', data=self.post_data)
  902. self.assertEqual(response.status_code, status.HTTP_200_OK)
  903. def test_allow_member_to_delete_others_annotation_in_shared_project(self):
  904. self.client.login(username=self.project_member_name,
  905. password=self.project_member_pass)
  906. response = self.client.delete(self.shared_url, format='json')
  907. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  908. @classmethod
  909. def doCleanups(cls):
  910. remove_all_role_mappings()
  911. class TestSearch(APITestCase):
  912. @classmethod
  913. def setUpTestData(cls):
  914. cls.project_member_name = 'project_member_name'
  915. cls.project_member_pass = 'project_member_pass'
  916. cls.non_project_member_name = 'non_project_member_name'
  917. cls.non_project_member_pass = 'non_project_member_pass'
  918. create_default_roles()
  919. project_member = User.objects.create_user(username=cls.project_member_name,
  920. password=cls.project_member_pass)
  921. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  922. password=cls.non_project_member_pass)
  923. cls.main_project = mommy.make('TextClassificationProject', users=[project_member])
  924. cls.search_term = 'example'
  925. doc1 = mommy.make('Document', text=cls.search_term, project=cls.main_project)
  926. doc2 = mommy.make('Document', text='Lorem', project=cls.main_project)
  927. label1 = mommy.make('Label', project=cls.main_project)
  928. label2 = mommy.make('Label', project=cls.main_project)
  929. mommy.make('SequenceAnnotation', document=doc1, user=project_member, label=label1)
  930. mommy.make('SequenceAnnotation', document=doc2, user=project_member, label=label2)
  931. sub_project = mommy.make('TextClassificationProject', users=[non_project_member])
  932. mommy.make('Document', text=cls.search_term, project=sub_project)
  933. cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
  934. cls.data = {'q': cls.search_term}
  935. assign_user_to_role(project_member=project_member, project=cls.main_project,
  936. role_name=settings.ROLE_ANNOTATOR)
  937. def test_can_filter_doc_by_term(self):
  938. self.client.login(username=self.project_member_name,
  939. password=self.project_member_pass)
  940. response = self.client.get(self.url, format='json', data=self.data)
  941. count = Document.objects.filter(text__contains=self.search_term,
  942. project=self.main_project).count()
  943. self.assertEqual(response.data['count'], count)
  944. def test_can_order_doc_by_created_at_ascending(self):
  945. params = {'ordering': 'created_at'}
  946. self.client.login(username=self.project_member_name,
  947. password=self.project_member_pass)
  948. response = self.client.get(self.url, format='json', data=params)
  949. docs = Document.objects.filter(project=self.main_project).order_by('created_at').values()
  950. for d1, d2 in zip(response.data['results'], docs):
  951. self.assertEqual(d1['id'], d2['id'])
  952. def test_can_order_doc_by_created_at_descending(self):
  953. params = {'ordering': '-created_at'}
  954. self.client.login(username=self.project_member_name,
  955. password=self.project_member_pass)
  956. response = self.client.get(self.url, format='json', data=params)
  957. docs = Document.objects.filter(project=self.main_project).order_by('-created_at').values()
  958. for d1, d2 in zip(response.data['results'], docs):
  959. self.assertEqual(d1['id'], d2['id'])
  960. def test_can_order_doc_by_annotation_updated_at_ascending(self):
  961. params = {'ordering': 'seq_annotations__updated_at'}
  962. self.client.login(username=self.project_member_name,
  963. password=self.project_member_pass)
  964. response = self.client.get(self.url, format='json', data=params)
  965. docs = Document.objects.filter(project=self.main_project).order_by('seq_annotations__updated_at').values()
  966. for d1, d2 in zip(response.data['results'], docs):
  967. self.assertEqual(d1['id'], d2['id'])
  968. def test_can_order_doc_by_annotation_updated_at_descending(self):
  969. params = {'ordering': '-seq_annotations__updated_at'}
  970. self.client.login(username=self.project_member_name,
  971. password=self.project_member_pass)
  972. response = self.client.get(self.url, format='json', data=params)
  973. docs = Document.objects.filter(project=self.main_project).order_by('-seq_annotations__updated_at').values()
  974. for d1, d2 in zip(response.data['results'], docs):
  975. self.assertEqual(d1['id'], d2['id'])
  976. @classmethod
  977. def doCleanups(cls):
  978. remove_all_role_mappings()
  979. class TestFilter(APITestCase):
  980. @classmethod
  981. def setUpTestData(cls):
  982. cls.project_member_name = 'project_member_name'
  983. cls.project_member_pass = 'project_member_pass'
  984. create_default_roles()
  985. project_member = User.objects.create_user(username=cls.project_member_name,
  986. password=cls.project_member_pass)
  987. cls.main_project = mommy.make('SequenceLabelingProject', users=[project_member])
  988. cls.label1 = mommy.make('Label', project=cls.main_project)
  989. cls.label2 = mommy.make('Label', project=cls.main_project)
  990. doc1 = mommy.make('Document', project=cls.main_project)
  991. doc2 = mommy.make('Document', project=cls.main_project)
  992. mommy.make('Document', project=cls.main_project)
  993. mommy.make('SequenceAnnotation', document=doc1, user=project_member, label=cls.label1)
  994. mommy.make('SequenceAnnotation', document=doc2, user=project_member, label=cls.label2)
  995. cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
  996. cls.params = {'seq_annotations__label__id': cls.label1.id}
  997. assign_user_to_role(project_member=project_member, project=cls.main_project,
  998. role_name=settings.ROLE_ANNOTATOR)
  999. def test_can_filter_by_label(self):
  1000. self.client.login(username=self.project_member_name,
  1001. password=self.project_member_pass)
  1002. response = self.client.get(self.url, format='json', data=self.params)
  1003. docs = Document.objects.filter(project=self.main_project,
  1004. seq_annotations__label__id=self.label1.id).values()
  1005. for d1, d2 in zip(response.data['results'], docs):
  1006. self.assertEqual(d1['id'], d2['id'])
  1007. def test_can_filter_doc_with_annotation(self):
  1008. params = {'seq_annotations__isnull': False}
  1009. self.client.login(username=self.project_member_name,
  1010. password=self.project_member_pass)
  1011. response = self.client.get(self.url, format='json', data=params)
  1012. docs = Document.objects.filter(project=self.main_project, seq_annotations__isnull=False).values()
  1013. self.assertEqual(response.data['count'], docs.count())
  1014. for d1, d2 in zip(response.data['results'], docs):
  1015. self.assertEqual(d1['id'], d2['id'])
  1016. def test_can_filter_doc_without_anotation(self):
  1017. params = {'seq_annotations__isnull': True}
  1018. self.client.login(username=self.project_member_name,
  1019. password=self.project_member_pass)
  1020. response = self.client.get(self.url, format='json', data=params)
  1021. docs = Document.objects.filter(project=self.main_project, seq_annotations__isnull=True).values()
  1022. self.assertEqual(response.data['count'], docs.count())
  1023. for d1, d2 in zip(response.data['results'], docs):
  1024. self.assertEqual(d1['id'], d2['id'])
  1025. @classmethod
  1026. def doCleanups(cls):
  1027. remove_all_role_mappings()
  1028. class TestUploader(APITestCase):
  1029. @classmethod
  1030. def setUpTestData(cls):
  1031. cls.super_user_name = 'super_user_name'
  1032. cls.super_user_pass = 'super_user_pass'
  1033. # Todo: change super_user to project_admin.
  1034. create_default_roles()
  1035. super_user = User.objects.create_superuser(username=cls.super_user_name,
  1036. password=cls.super_user_pass,
  1037. email='fizz@buzz.com')
  1038. cls.classification_project = mommy.make('TextClassificationProject',
  1039. users=[super_user], project_type=DOCUMENT_CLASSIFICATION)
  1040. cls.labeling_project = mommy.make('SequenceLabelingProject',
  1041. users=[super_user], project_type=SEQUENCE_LABELING)
  1042. cls.seq2seq_project = mommy.make('Seq2seqProject', users=[super_user], project_type=SEQ2SEQ)
  1043. assign_user_to_role(project_member=super_user, project=cls.classification_project,
  1044. role_name=settings.ROLE_PROJECT_ADMIN)
  1045. assign_user_to_role(project_member=super_user, project=cls.labeling_project,
  1046. role_name=settings.ROLE_PROJECT_ADMIN)
  1047. assign_user_to_role(project_member=super_user, project=cls.seq2seq_project,
  1048. role_name=settings.ROLE_PROJECT_ADMIN)
  1049. def setUp(self):
  1050. self.client.login(username=self.super_user_name,
  1051. password=self.super_user_pass)
  1052. def upload_test_helper(self, project_id, filename, file_format, expected_status, **kwargs):
  1053. url = reverse(viewname='doc_uploader', args=[project_id])
  1054. with open(os.path.join(DATA_DIR, filename), 'rb') as f:
  1055. response = self.client.post(url, data={'file': f, 'format': file_format})
  1056. self.assertEqual(response.status_code, expected_status)
  1057. def label_test_helper(self, project_id, expected_labels, expected_label_keys):
  1058. url = reverse(viewname='label_list', args=[project_id])
  1059. expected_keys = {key for label in expected_labels for key in label}
  1060. response = self.client.get(url).json()
  1061. actual_labels = [{key: value for (key, value) in label.items() if key in expected_keys}
  1062. for label in response]
  1063. self.assertCountEqual(actual_labels, expected_labels)
  1064. for label in response:
  1065. for expected_label_key in expected_label_keys:
  1066. self.assertIsNotNone(label.get(expected_label_key))
  1067. def test_can_upload_conll_format_file(self):
  1068. self.upload_test_helper(project_id=self.labeling_project.id,
  1069. filename='labeling.conll',
  1070. file_format='conll',
  1071. expected_status=status.HTTP_201_CREATED)
  1072. def test_cannot_upload_wrong_conll_format_file(self):
  1073. self.upload_test_helper(project_id=self.labeling_project.id,
  1074. filename='labeling.invalid.conll',
  1075. file_format='conll',
  1076. expected_status=status.HTTP_400_BAD_REQUEST)
  1077. def test_can_upload_classification_csv(self):
  1078. self.upload_test_helper(project_id=self.classification_project.id,
  1079. filename='example.csv',
  1080. file_format='csv',
  1081. expected_status=status.HTTP_201_CREATED)
  1082. def test_can_upload_classification_csv_with_out_of_order_columns(self):
  1083. self.upload_test_helper(project_id=self.classification_project.id,
  1084. filename='example_out_of_order_columns.csv',
  1085. file_format='csv',
  1086. expected_status=status.HTTP_201_CREATED)
  1087. self.label_test_helper(
  1088. project_id=self.classification_project.id,
  1089. expected_labels=[
  1090. {'text': 'Positive'},
  1091. {'text': 'Negative'},
  1092. ],
  1093. expected_label_keys=[],
  1094. )
  1095. def test_can_upload_csv_with_non_utf8_encoding(self):
  1096. self.upload_test_helper(project_id=self.classification_project.id,
  1097. filename='example.utf16.csv',
  1098. file_format='csv',
  1099. expected_status=status.HTTP_201_CREATED)
  1100. def test_can_upload_seq2seq_csv(self):
  1101. self.upload_test_helper(project_id=self.seq2seq_project.id,
  1102. filename='example.csv',
  1103. file_format='csv',
  1104. expected_status=status.HTTP_201_CREATED)
  1105. def test_can_upload_single_column_csv(self):
  1106. self.upload_test_helper(project_id=self.seq2seq_project.id,
  1107. filename='example_one_column.csv',
  1108. file_format='csv',
  1109. expected_status=status.HTTP_201_CREATED)
  1110. def test_can_upload_csv_file_does_not_match_column_and_row(self):
  1111. self.upload_test_helper(project_id=self.classification_project.id,
  1112. filename='example_column_and_row_not_matching.csv',
  1113. file_format='csv',
  1114. expected_status=status.HTTP_201_CREATED)
  1115. def test_cannot_upload_csv_file_has_too_many_columns(self):
  1116. self.upload_test_helper(project_id=self.classification_project.id,
  1117. filename='example.invalid.2.csv',
  1118. file_format='csv',
  1119. expected_status=status.HTTP_400_BAD_REQUEST)
  1120. def test_can_upload_classification_excel(self):
  1121. self.upload_test_helper(project_id=self.classification_project.id,
  1122. filename='example.xlsx',
  1123. file_format='excel',
  1124. expected_status=status.HTTP_201_CREATED)
  1125. def test_can_upload_seq2seq_excel(self):
  1126. self.upload_test_helper(project_id=self.seq2seq_project.id,
  1127. filename='example.xlsx',
  1128. file_format='excel',
  1129. expected_status=status.HTTP_201_CREATED)
  1130. def test_can_upload_single_column_excel(self):
  1131. self.upload_test_helper(project_id=self.seq2seq_project.id,
  1132. filename='example_one_column.xlsx',
  1133. file_format='excel',
  1134. expected_status=status.HTTP_201_CREATED)
  1135. def test_can_upload_excel_file_does_not_match_column_and_row(self):
  1136. self.upload_test_helper(project_id=self.classification_project.id,
  1137. filename='example_column_and_row_not_matching.xlsx',
  1138. file_format='excel',
  1139. expected_status=status.HTTP_201_CREATED)
  1140. def test_cannot_upload_excel_file_has_too_many_columns(self):
  1141. self.upload_test_helper(project_id=self.classification_project.id,
  1142. filename='example.invalid.2.xlsx',
  1143. file_format='excel',
  1144. expected_status=status.HTTP_400_BAD_REQUEST)
  1145. @override_settings(IMPORT_BATCH_SIZE=1)
  1146. def test_can_upload_small_batch_size(self):
  1147. self.upload_test_helper(project_id=self.seq2seq_project.id,
  1148. filename='example_one_column_no_header.xlsx',
  1149. file_format='excel',
  1150. expected_status=status.HTTP_201_CREATED)
  1151. def test_can_upload_classification_jsonl(self):
  1152. self.upload_test_helper(project_id=self.classification_project.id,
  1153. filename='classification.jsonl',
  1154. file_format='json',
  1155. expected_status=status.HTTP_201_CREATED)
  1156. self.label_test_helper(
  1157. project_id=self.classification_project.id,
  1158. expected_labels=[
  1159. {'text': 'positive', 'suffix_key': 'p', 'prefix_key': None},
  1160. {'text': 'negative', 'suffix_key': 'n', 'prefix_key': None},
  1161. {'text': 'neutral', 'suffix_key': 'n', 'prefix_key': 'ctrl'},
  1162. ],
  1163. expected_label_keys=[
  1164. 'background_color',
  1165. 'text_color',
  1166. ])
  1167. def test_can_upload_labeling_jsonl(self):
  1168. self.upload_test_helper(project_id=self.labeling_project.id,
  1169. filename='labeling.jsonl',
  1170. file_format='json',
  1171. expected_status=status.HTTP_201_CREATED)
  1172. self.label_test_helper(
  1173. project_id=self.labeling_project.id,
  1174. expected_labels=[
  1175. {'text': 'LOC', 'suffix_key': 'l', 'prefix_key': None},
  1176. {'text': 'ORG', 'suffix_key': 'o', 'prefix_key': None},
  1177. {'text': 'PER', 'suffix_key': 'p', 'prefix_key': None},
  1178. ],
  1179. expected_label_keys=[
  1180. 'background_color',
  1181. 'text_color',
  1182. ])
  1183. def test_can_upload_seq2seq_jsonl(self):
  1184. self.upload_test_helper(project_id=self.seq2seq_project.id,
  1185. filename='seq2seq.jsonl',
  1186. file_format='json',
  1187. expected_status=status.HTTP_201_CREATED)
  1188. def test_can_upload_plain_text(self):
  1189. self.upload_test_helper(project_id=self.classification_project.id,
  1190. filename='example.txt',
  1191. file_format='plain',
  1192. expected_status=status.HTTP_201_CREATED)
  1193. def test_can_upload_data_without_label(self):
  1194. self.upload_test_helper(project_id=self.classification_project.id,
  1195. filename='example.jsonl',
  1196. file_format='json',
  1197. expected_status=status.HTTP_201_CREATED)
  1198. @classmethod
  1199. def doCleanups(cls):
  1200. remove_all_role_mappings()
  1201. @override_settings(CLOUD_BROWSER_APACHE_LIBCLOUD_PROVIDER='LOCAL')
  1202. @override_settings(CLOUD_BROWSER_APACHE_LIBCLOUD_ACCOUNT=os.path.dirname(DATA_DIR))
  1203. @override_settings(CLOUD_BROWSER_APACHE_LIBCLOUD_SECRET_KEY='not-used')
  1204. class TestCloudUploader(TestUploader):
  1205. def upload_test_helper(self, project_id, filename, file_format, expected_status, **kwargs):
  1206. query_params = {
  1207. 'project_id': project_id,
  1208. 'upload_format': file_format,
  1209. 'container': kwargs.pop('container', os.path.basename(DATA_DIR)),
  1210. 'object': filename,
  1211. }
  1212. query_params.update(kwargs)
  1213. response = self.client.get(reverse('cloud_uploader'), query_params)
  1214. self.assertEqual(response.status_code, expected_status)
  1215. def test_cannot_upload_with_missing_file(self):
  1216. self.upload_test_helper(project_id=self.classification_project.id,
  1217. filename='does-not-exist',
  1218. file_format='json',
  1219. expected_status=status.HTTP_400_BAD_REQUEST)
  1220. def test_cannot_upload_with_missing_container(self):
  1221. self.upload_test_helper(project_id=self.classification_project.id,
  1222. filename='example.jsonl',
  1223. container='does-not-exist',
  1224. file_format='json',
  1225. expected_status=status.HTTP_400_BAD_REQUEST)
  1226. def test_cannot_upload_with_missing_query_parameters(self):
  1227. response = self.client.get(reverse('cloud_uploader'), {'project_id': self.classification_project.id})
  1228. self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
  1229. def test_can_upload_with_redirect(self):
  1230. self.upload_test_helper(project_id=self.classification_project.id,
  1231. filename='example.jsonl',
  1232. next='http://somewhere',
  1233. file_format='json',
  1234. expected_status=status.HTTP_302_FOUND)
  1235. def test_can_upload_with_redirect_to_blank(self):
  1236. self.upload_test_helper(project_id=self.classification_project.id,
  1237. filename='example.jsonl',
  1238. next='about:blank',
  1239. file_format='json',
  1240. expected_status=status.HTTP_201_CREATED)
  1241. class TestFeatures(APITestCase):
  1242. @classmethod
  1243. def setUpTestData(cls):
  1244. cls.user_name = 'user_name'
  1245. cls.user_pass = 'user_pass'
  1246. create_default_roles()
  1247. cls.user = User.objects.create_user(username=cls.user_name, password=cls.user_pass, email='fizz@buzz.com')
  1248. def setUp(self):
  1249. self.client.login(username=self.user_name, password=self.user_pass)
  1250. @override_settings(CLOUD_BROWSER_APACHE_LIBCLOUD_PROVIDER=None)
  1251. def test_no_cloud_upload(self):
  1252. response = self.client.get(reverse('features'))
  1253. self.assertFalse(response.json().get('cloud_upload'))
  1254. @override_settings(IMPORT_BATCH_SIZE=2)
  1255. class TestParser(APITestCase):
  1256. def parser_helper(self, filename, parser, include_label=True):
  1257. with open(os.path.join(DATA_DIR, filename), mode='rb') as f:
  1258. result = list(parser.parse(f))
  1259. for data in result:
  1260. for r in data:
  1261. self.assertIn('text', r)
  1262. if include_label:
  1263. self.assertIn('labels', r)
  1264. return result
  1265. def test_give_valid_data_to_conll_parser(self):
  1266. self.parser_helper(filename='labeling.conll', parser=CoNLLParser())
  1267. def test_give_valid_data_to_conll_parser_with_trailing_newlines(self):
  1268. result = self.parser_helper(filename='labeling.trailing.conll', parser=CoNLLParser())
  1269. self.assertEqual(len(result), 1)
  1270. self.assertEqual(len(result[0]), 1)
  1271. def test_plain_parser(self):
  1272. self.parser_helper(filename='example.txt', parser=PlainTextParser(), include_label=False)
  1273. def test_give_invalid_data_to_conll_parser(self):
  1274. with self.assertRaises(FileParseException):
  1275. self.parser_helper(filename='labeling.invalid.conll',
  1276. parser=CoNLLParser())
  1277. def test_give_classification_data_to_csv_parser(self):
  1278. self.parser_helper(filename='example.csv', parser=CSVParser(), include_label=False)
  1279. def test_give_seq2seq_data_to_csv_parser(self):
  1280. self.parser_helper(filename='example.csv', parser=CSVParser(), include_label=False)
  1281. def test_give_classification_data_to_json_parser(self):
  1282. self.parser_helper(filename='classification.jsonl', parser=JSONParser())
  1283. def test_give_labeling_data_to_json_parser(self):
  1284. self.parser_helper(filename='labeling.jsonl', parser=JSONParser())
  1285. def test_give_seq2seq_data_to_json_parser(self):
  1286. self.parser_helper(filename='seq2seq.jsonl', parser=JSONParser())
  1287. def test_give_data_without_label_to_json_parser(self):
  1288. self.parser_helper(filename='example.jsonl', parser=JSONParser(), include_label=False)
  1289. def test_give_labeling_data_to_fasttext_parser(self):
  1290. self.parser_helper(filename='example_fasttext.txt', parser=FastTextParser())
  1291. def test_give_data_without_label_name_to_fasttext_parser(self):
  1292. with self.assertRaises(FileParseException):
  1293. self.parser_helper(filename='example_fasttext_label_tag_without_name.txt', parser=FastTextParser())
  1294. def test_give_data_without_text_to_fasttext_parser(self):
  1295. with self.assertRaises(FileParseException):
  1296. self.parser_helper(filename='example_fasttext_without_text.txt', parser=FastTextParser())
  1297. class TestDownloader(APITestCase):
  1298. @classmethod
  1299. def setUpTestData(cls):
  1300. cls.super_user_name = 'super_user_name'
  1301. cls.super_user_pass = 'super_user_pass'
  1302. # Todo: change super_user to project_admin.
  1303. create_default_roles()
  1304. super_user = User.objects.create_superuser(username=cls.super_user_name,
  1305. password=cls.super_user_pass,
  1306. email='fizz@buzz.com')
  1307. cls.classification_project = mommy.make('TextClassificationProject',
  1308. users=[super_user], project_type=DOCUMENT_CLASSIFICATION)
  1309. cls.labeling_project = mommy.make('SequenceLabelingProject',
  1310. users=[super_user], project_type=SEQUENCE_LABELING)
  1311. cls.seq2seq_project = mommy.make('Seq2seqProject', users=[super_user], project_type=SEQ2SEQ)
  1312. cls.speech2text_project = mommy.make('Speech2textProject', users=[super_user], project_type=SPEECH2TEXT)
  1313. cls.classification_url = reverse(viewname='doc_downloader', args=[cls.classification_project.id])
  1314. cls.labeling_url = reverse(viewname='doc_downloader', args=[cls.labeling_project.id])
  1315. cls.seq2seq_url = reverse(viewname='doc_downloader', args=[cls.seq2seq_project.id])
  1316. cls.speech2text_url = reverse(viewname='doc_downloader', args=[cls.speech2text_project.id])
  1317. def setUp(self):
  1318. self.client.login(username=self.super_user_name,
  1319. password=self.super_user_pass)
  1320. def download_test_helper(self, url, format, expected_status):
  1321. response = self.client.get(url, data={'q': format})
  1322. self.assertEqual(response.status_code, expected_status)
  1323. def test_cannot_download_conll_format_file(self):
  1324. self.download_test_helper(url=self.labeling_url,
  1325. format='conll',
  1326. expected_status=status.HTTP_400_BAD_REQUEST)
  1327. def test_can_download_classification_csv(self):
  1328. self.download_test_helper(url=self.classification_url,
  1329. format='csv',
  1330. expected_status=status.HTTP_200_OK)
  1331. def test_can_download_labeling_csv(self):
  1332. self.download_test_helper(url=self.labeling_url,
  1333. format='csv',
  1334. expected_status=status.HTTP_200_OK)
  1335. def test_can_download_seq2seq_csv(self):
  1336. self.download_test_helper(url=self.seq2seq_url,
  1337. format='csv',
  1338. expected_status=status.HTTP_200_OK)
  1339. def test_can_download_classification_jsonl(self):
  1340. self.download_test_helper(url=self.classification_url,
  1341. format='json',
  1342. expected_status=status.HTTP_200_OK)
  1343. def test_can_download_labeling_jsonl(self):
  1344. self.download_test_helper(url=self.labeling_url,
  1345. format='json',
  1346. expected_status=status.HTTP_200_OK)
  1347. def test_can_download_seq2seq_jsonl(self):
  1348. self.download_test_helper(url=self.seq2seq_url,
  1349. format='json',
  1350. expected_status=status.HTTP_200_OK)
  1351. def test_can_download_speech2text_jsonl(self):
  1352. self.download_test_helper(url=self.speech2text_url,
  1353. format='json',
  1354. expected_status=status.HTTP_200_OK)
  1355. def test_can_download_labelling_jsonl(self):
  1356. self.download_test_helper(url=self.labeling_url,
  1357. format='jsonl',
  1358. expected_status=status.HTTP_200_OK)
  1359. def test_can_download_plain_text(self):
  1360. self.download_test_helper(url=self.classification_url,
  1361. format='plain',
  1362. expected_status=status.HTTP_400_BAD_REQUEST)
  1363. def test_can_download_classification_fasttext(self):
  1364. self.download_test_helper(url=self.classification_url,
  1365. format='txt',
  1366. expected_status=status.HTTP_200_OK)
  1367. class TestStatisticsAPI(APITestCase, TestUtilsMixin):
  1368. @classmethod
  1369. def setUpTestData(cls):
  1370. cls.super_user_name = 'super_user_name'
  1371. cls.super_user_pass = 'super_user_pass'
  1372. cls.other_user_name = 'other_user_name'
  1373. cls.other_user_pass = 'other_user_pass'
  1374. create_default_roles()
  1375. # Todo: change super_user to project_admin.
  1376. super_user = User.objects.create_superuser(username=cls.super_user_name,
  1377. password=cls.super_user_pass,
  1378. email='fizz@buzz.com')
  1379. other_user = User.objects.create_user(username=cls.other_user_name,
  1380. password=cls.other_user_pass,
  1381. email='bar@buzz.com')
  1382. cls.project = mommy.make('TextClassificationProject', users=[super_user])
  1383. doc1 = mommy.make('Document', project=cls.project)
  1384. doc2 = mommy.make('Document', project=cls.project)
  1385. mommy.make('DocumentAnnotation', document=doc1, user=super_user)
  1386. mommy.make('DocumentAnnotation', document=doc2, user=other_user)
  1387. cls.url = reverse(viewname='statistics', args=[cls.project.id])
  1388. cls.doc = Document.objects.filter(project=cls.project)
  1389. assign_user_to_role(project_member=other_user, project=cls.project,
  1390. role_name=settings.ROLE_ANNOTATOR)
  1391. @classmethod
  1392. def doCleanups(cls):
  1393. remove_all_role_mappings()
  1394. def test_returns_exact_progress(self):
  1395. self.client.login(username=self.super_user_name,
  1396. password=self.super_user_pass)
  1397. response = self.client.get(self.url, format='json')
  1398. self.assertEqual(response.data['total'], 2)
  1399. self.assertEqual(response.data['remaining'], 1)
  1400. def test_returns_exact_progress_with_collaborative_annotation(self):
  1401. self._patch_project(self.project, 'collaborative_annotation', True)
  1402. self.client.login(username=self.other_user_name,
  1403. password=self.other_user_pass)
  1404. response = self.client.get(self.url, format='json')
  1405. self.assertEqual(response.data['total'], 2)
  1406. self.assertEqual(response.data['remaining'], 0)
  1407. def test_returns_user_count(self):
  1408. self.client.login(username=self.super_user_name,
  1409. password=self.super_user_pass)
  1410. response = self.client.get(self.url, format='json')
  1411. self.assertIn('label', response.data)
  1412. self.assertIsInstance(response.data['label'], dict)
  1413. def test_returns_label_count(self):
  1414. self.client.login(username=self.super_user_name,
  1415. password=self.super_user_pass)
  1416. response = self.client.get(self.url, format='json')
  1417. self.assertIn('user', response.data)
  1418. self.assertIsInstance(response.data['user'], dict)
  1419. def test_returns_partial_response(self):
  1420. self.client.login(username=self.super_user_name,
  1421. password=self.super_user_pass)
  1422. response = self.client.get(f'{self.url}?include=user', format='json')
  1423. self.assertEqual(list(response.data.keys()), ['user'])
  1424. class TestUserAPI(APITestCase):
  1425. @classmethod
  1426. def setUpTestData(cls):
  1427. cls.super_user_name = 'super_user_name'
  1428. cls.super_user_pass = 'super_user_pass'
  1429. create_default_roles()
  1430. User.objects.create_superuser(username=cls.super_user_name,
  1431. password=cls.super_user_pass,
  1432. email='fizz@buzz.com')
  1433. cls.url = reverse(viewname='user_list')
  1434. def test_returns_user_count(self):
  1435. self.client.login(username=self.super_user_name,
  1436. password=self.super_user_pass)
  1437. response = self.client.get(self.url, format='json')
  1438. self.assertEqual(1, len(response.data))
  1439. class TestRoleAPI(APITestCase):
  1440. @classmethod
  1441. def setUpTestData(cls):
  1442. cls.user_name = 'user_name'
  1443. cls.user_pass = 'user_pass'
  1444. cls.project_admin_name = 'project_admin_name'
  1445. cls.project_admin_pass = 'project_admin_pass'
  1446. create_default_roles()
  1447. cls.user = User.objects.create_user(username=cls.user_name,
  1448. password=cls.user_pass)
  1449. User.objects.create_superuser(username=cls.project_admin_name,
  1450. password=cls.project_admin_pass,
  1451. email='fizz@buzz.com')
  1452. cls.url = reverse(viewname='roles')
  1453. def test_cannot_create_multiple_roles_with_same_name(self):
  1454. self.client.login(username=self.project_admin_name,
  1455. password=self.project_admin_pass)
  1456. roles = [
  1457. {'name': 'examplerole', 'description': 'example'},
  1458. {'name': 'examplerole', 'description': 'example'}
  1459. ]
  1460. self.client.post(self.url, format='json', data=roles[0])
  1461. second_response = self.client.post(self.url, format='json', data=roles[1])
  1462. self.assertEqual(second_response.status_code, status.HTTP_400_BAD_REQUEST)
  1463. def test_nonadmin_cannot_create_role(self):
  1464. self.client.login(username=self.user_name,
  1465. password=self.user_pass)
  1466. data = {'name': 'testrole', 'description': 'example'}
  1467. response = self.client.post(self.url, format='json', data=data)
  1468. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  1469. def test_admin_can_create_role(self):
  1470. self.client.login(username=self.project_admin_name,
  1471. password=self.project_admin_pass)
  1472. data = {'name': 'testrole', 'description': 'example'}
  1473. response = self.client.post(self.url, format='json', data=data)
  1474. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  1475. def test_admin_can_get_roles(self):
  1476. self.client.login(username=self.project_admin_name,
  1477. password=self.project_admin_pass)
  1478. response = self.client.get(self.url, format='json')
  1479. self.assertEqual(response.status_code, status.HTTP_200_OK)
  1480. class TestRoleMappingListAPI(APITestCase):
  1481. @classmethod
  1482. def setUpTestData(cls):
  1483. cls.project_member_name = 'project_member_name'
  1484. cls.project_member_pass = 'project_member_pass'
  1485. cls.second_project_member_name = 'second_project_member_name'
  1486. cls.second_project_member_pass = 'second_project_member_pass'
  1487. cls.project_admin_name = 'project_admin_name'
  1488. cls.project_admin_pass = 'project_admin_pass'
  1489. create_default_roles()
  1490. project_member = User.objects.create_user(username=cls.project_member_name,
  1491. password=cls.project_member_pass)
  1492. cls.second_project_member = User.objects.create_user(username=cls.second_project_member_name,
  1493. password=cls.second_project_member_pass)
  1494. project_admin = User.objects.create_user(username=cls.project_admin_name,
  1495. password=cls.project_admin_pass)
  1496. cls.main_project = mommy.make('Project', users=[project_member, project_admin, cls.second_project_member])
  1497. cls.other_project = mommy.make('Project', users=[cls.second_project_member, project_admin])
  1498. cls.admin_role = Role.objects.get(name=settings.ROLE_PROJECT_ADMIN)
  1499. cls.role = mommy.make('Role', name='otherrole')
  1500. assign_user_to_role(project_admin, cls.main_project, cls.admin_role)
  1501. cls.data = {'user': project_member.id, 'role': cls.admin_role.id, 'project': cls.main_project.id}
  1502. cls.other_url = reverse(viewname='rolemapping_list', args=[cls.other_project.id])
  1503. cls.url = reverse(viewname='rolemapping_list', args=[cls.main_project.id])
  1504. def test_returns_mappings_to_project_admin(self):
  1505. self.client.login(username=self.project_admin_name,
  1506. password=self.project_admin_pass)
  1507. response = self.client.get(self.url, format='json')
  1508. self.assertEqual(response.status_code, status.HTTP_200_OK)
  1509. # Todo: refactoring testing.
  1510. # def test_allows_superuser_to_create_mapping(self):
  1511. # self.client.login(username=self.project_admin_name,
  1512. # password=self.project_admin_pass)
  1513. # response = self.client.post(self.url, format='json', data=self.data)
  1514. # self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  1515. #
  1516. # def test_do_not_allow_nonadmin_to_create_mapping(self):
  1517. # self.client.login(username=self.project_member_name,
  1518. # password=self.project_member_pass)
  1519. # response = self.client.post(self.url, format='json', data=self.data)
  1520. # self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  1521. #
  1522. # def test_do_not_return_mappings_to_nonadmin(self):
  1523. # self.client.login(username=self.project_member_name,
  1524. # password=self.project_member_pass)
  1525. # response = self.client.get(self.url, format='json')
  1526. # self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  1527. class TestRoleMappingDetailAPI(APITestCase):
  1528. @classmethod
  1529. def setUpTestData(cls):
  1530. cls.project_admin_name = 'project_admin_name'
  1531. cls.project_admin_pass = 'project_admin_pass'
  1532. cls.project_member_name = 'project_member_name'
  1533. cls.project_member_pass = 'project_member_pass'
  1534. cls.non_project_member_name = 'non_project_member_name'
  1535. cls.non_project_member_pass = 'non_project_member_pass'
  1536. create_default_roles()
  1537. project_admin = User.objects.create_user(username=cls.project_admin_name,
  1538. password=cls.project_admin_pass)
  1539. project_member = User.objects.create_user(username=cls.project_member_name,
  1540. password=cls.project_member_pass)
  1541. User.objects.create_user(username=cls.non_project_member_name, password=cls.non_project_member_pass)
  1542. project = mommy.make('Project', users=[project_admin, project_member])
  1543. admin_role = Role.objects.get(name=settings.ROLE_PROJECT_ADMIN)
  1544. annotator_role = Role.objects.get(name=settings.ROLE_ANNOTATOR)
  1545. cls.rolemapping = assign_user_to_role(project_admin, project, admin_role)
  1546. assign_user_to_role(project_member, project, annotator_role)
  1547. cls.url = reverse(viewname='rolemapping_detail', args=[project.id, cls.rolemapping.id])
  1548. cls.data = {'role': admin_role.id}
  1549. def test_returns_rolemapping_to_project_member(self):
  1550. self.client.login(username=self.project_admin_name,
  1551. password=self.project_admin_pass)
  1552. response = self.client.get(self.url, format='json')
  1553. self.assertEqual(response.data['id'], self.rolemapping.id)
  1554. def test_do_not_return_mapping_to_non_project_member(self):
  1555. self.client.login(username=self.non_project_member_name,
  1556. password=self.non_project_member_pass)
  1557. response = self.client.get(self.url, format='json')
  1558. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  1559. def test_allows_admin_to_update_mapping(self):
  1560. self.client.login(username=self.project_admin_name,
  1561. password=self.project_admin_pass)
  1562. response = self.client.patch(self.url, format='json', data=self.data)
  1563. self.assertEqual(response.data['role'], self.data['role'])
  1564. def test_disallows_project_member_to_update_mapping(self):
  1565. self.client.login(username=self.project_member_name,
  1566. password=self.project_member_pass)
  1567. response = self.client.patch(self.url, format='json', data=self.data)
  1568. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  1569. def test_allows_admin_to_delete_mapping(self):
  1570. self.client.login(username=self.project_admin_name,
  1571. password=self.project_admin_pass)
  1572. response = self.client.delete(self.url, format='json')
  1573. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  1574. def test_disallows_project_member_to_delete_mapping(self):
  1575. self.client.login(username=self.project_member_name,
  1576. password=self.project_member_pass)
  1577. response = self.client.delete(self.url, format='json')
  1578. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)