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.

615 lines
34 KiB

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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. from rest_framework import status
  2. from rest_framework.reverse import reverse
  3. from rest_framework.test import APITestCase
  4. from mixer.backend.django import mixer
  5. from ..models import User, SequenceAnnotation, Document
  6. class TestProjectListAPI(APITestCase):
  7. @classmethod
  8. def setUpTestData(cls):
  9. cls.main_project_member_name = 'project_member_name'
  10. cls.main_project_member_pass = 'project_member_pass'
  11. cls.sub_project_member_name = 'sub_project_member_name'
  12. cls.sub_project_member_pass = 'sub_project_member_pass'
  13. cls.super_user_name = 'super_user_name'
  14. cls.super_user_pass = 'super_user_pass'
  15. main_project_member = User.objects.create_user(username=cls.main_project_member_name,
  16. password=cls.main_project_member_pass)
  17. sub_project_member = User.objects.create_user(username=cls.sub_project_member_name,
  18. password=cls.sub_project_member_pass)
  19. # Todo: change super_user to project_admin.
  20. super_user = User.objects.create_superuser(username=cls.super_user_name,
  21. password=cls.super_user_pass,
  22. email='fizz@buzz.com')
  23. cls.main_project = mixer.blend('server.Project', users=[main_project_member])
  24. cls.sub_project = mixer.blend('server.Project', users=[sub_project_member])
  25. cls.url = reverse(viewname='project_list')
  26. cls.data = {'name': 'example', 'project_type': 'Seq2seq',
  27. 'description': 'example', 'guideline': 'example'}
  28. cls.num_project = main_project_member.projects.count()
  29. def test_returns_main_project_to_main_project_member(self):
  30. self.client.login(username=self.main_project_member_name,
  31. password=self.main_project_member_pass)
  32. response = self.client.get(self.url, format='json')
  33. project = response.data[0]
  34. num_project = len(response.data)
  35. self.assertEqual(num_project, self.num_project)
  36. self.assertEqual(project['id'], self.main_project.id)
  37. def test_do_not_return_main_project_to_sub_project_member(self):
  38. self.client.login(username=self.sub_project_member_name,
  39. password=self.sub_project_member_pass)
  40. response = self.client.get(self.url, format='json')
  41. project = response.data[0]
  42. num_project = len(response.data)
  43. self.assertEqual(num_project, self.num_project)
  44. self.assertNotEqual(project['id'], self.main_project.id)
  45. def test_allows_superuser_to_create_project(self):
  46. self.client.login(username=self.super_user_name,
  47. password=self.super_user_pass)
  48. response = self.client.post(self.url, format='json', data=self.data)
  49. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  50. def test_disallows_project_member_to_create_project(self):
  51. self.client.login(username=self.main_project_member_name,
  52. password=self.main_project_member_pass)
  53. response = self.client.post(self.url, format='json', data=self.data)
  54. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  55. class TestProjectDetailAPI(APITestCase):
  56. @classmethod
  57. def setUpTestData(cls):
  58. cls.project_member_name = 'project_member_name'
  59. cls.project_member_pass = 'project_member_pass'
  60. cls.non_project_member_name = 'non_project_member_name'
  61. cls.non_project_member_pass = 'non_project_member_pass'
  62. cls.super_user_name = 'super_user_name'
  63. cls.super_user_pass = 'super_user_pass'
  64. cls.project_member = User.objects.create_user(username=cls.project_member_name,
  65. password=cls.project_member_pass)
  66. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  67. password=cls.non_project_member_pass)
  68. # Todo: change super_user to project_admin.
  69. super_user = User.objects.create_superuser(username=cls.super_user_name,
  70. password=cls.super_user_pass,
  71. email='fizz@buzz.com')
  72. cls.main_project = mixer.blend('server.Project', users=[cls.project_member, super_user])
  73. sub_project = mixer.blend('server.Project', users=[non_project_member])
  74. cls.url = reverse(viewname='project_detail', args=[cls.main_project.id])
  75. cls.data = {'description': 'lorem'}
  76. def test_returns_main_project_detail_to_main_project_member(self):
  77. self.client.login(username=self.project_member_name,
  78. password=self.project_member_pass)
  79. response = self.client.get(self.url, format='json')
  80. self.assertEqual(response.data['id'], self.main_project.id)
  81. def test_do_not_return_main_project_to_sub_project_member(self):
  82. self.client.login(username=self.non_project_member_name,
  83. password=self.non_project_member_pass)
  84. response = self.client.get(self.url, format='json')
  85. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  86. def test_allows_superuser_to_update_project(self):
  87. self.client.login(username=self.super_user_name,
  88. password=self.super_user_pass)
  89. response = self.client.patch(self.url, format='json', data=self.data)
  90. self.assertEqual(response.data['description'], self.data['description'])
  91. def test_disallows_project_member_to_update_project(self):
  92. self.client.login(username=self.project_member_name,
  93. password=self.project_member_pass)
  94. response = self.client.patch(self.url, format='json', data=self.data)
  95. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  96. def test_allows_superuser_to_delete_project(self):
  97. self.client.login(username=self.super_user_name,
  98. password=self.super_user_pass)
  99. response = self.client.delete(self.url, format='json')
  100. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  101. def test_disallows_project_member_to_delete_project(self):
  102. self.client.login(username=self.project_member_name,
  103. password=self.project_member_pass)
  104. response = self.client.delete(self.url, format='json')
  105. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  106. class TestLabelListAPI(APITestCase):
  107. @classmethod
  108. def setUpTestData(cls):
  109. cls.project_member_name = 'project_member_name'
  110. cls.project_member_pass = 'project_member_pass'
  111. cls.non_project_member_name = 'non_project_member_name'
  112. cls.non_project_member_pass = 'non_project_member_pass'
  113. cls.super_user_name = 'super_user_name'
  114. cls.super_user_pass = 'super_user_pass'
  115. project_member = User.objects.create_user(username=cls.project_member_name,
  116. password=cls.project_member_pass)
  117. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  118. password=cls.non_project_member_pass)
  119. # Todo: change super_user to project_admin.
  120. super_user = User.objects.create_superuser(username=cls.super_user_name,
  121. password=cls.super_user_pass,
  122. email='fizz@buzz.com')
  123. cls.main_project = mixer.blend('server.Project', users=[project_member, super_user])
  124. cls.main_project_label = mixer.blend('server.Label', project=cls.main_project)
  125. sub_project = mixer.blend('server.Project', users=[non_project_member])
  126. mixer.blend('server.Label', project=sub_project)
  127. cls.url = reverse(viewname='label_list', args=[cls.main_project.id])
  128. cls.data = {'text': 'example'}
  129. def test_returns_labels_to_project_member(self):
  130. self.client.login(username=self.project_member_name,
  131. password=self.project_member_pass)
  132. response = self.client.get(self.url, format='json')
  133. self.assertEqual(response.status_code, status.HTTP_200_OK)
  134. def test_do_not_return_labels_to_non_project_member(self):
  135. self.client.login(username=self.non_project_member_name,
  136. password=self.non_project_member_pass)
  137. response = self.client.get(self.url, format='json')
  138. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  139. def test_do_not_return_labels_of_other_projects(self):
  140. self.client.login(username=self.project_member_name,
  141. password=self.project_member_pass)
  142. response = self.client.get(self.url, format='json')
  143. label = response.data[0]
  144. num_labels = len(response.data)
  145. self.assertEqual(num_labels, len(self.main_project.labels.all()))
  146. self.assertEqual(label['id'], self.main_project_label.id)
  147. def test_allows_superuser_to_create_label(self):
  148. self.client.login(username=self.super_user_name,
  149. password=self.super_user_pass)
  150. response = self.client.post(self.url, format='json', data=self.data)
  151. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  152. def test_disallows_project_member_to_create_label(self):
  153. self.client.login(username=self.project_member_name,
  154. password=self.project_member_pass)
  155. response = self.client.post(self.url, format='json', data=self.data)
  156. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  157. class TestLabelDetailAPI(APITestCase):
  158. @classmethod
  159. def setUpTestData(cls):
  160. cls.project_member_name = 'project_member_name'
  161. cls.project_member_pass = 'project_member_pass'
  162. cls.non_project_member_name = 'non_project_member_name'
  163. cls.non_project_member_pass = 'non_project_member_pass'
  164. cls.super_user_name = 'super_user_name'
  165. cls.super_user_pass = 'super_user_pass'
  166. project_member = User.objects.create_user(username=cls.project_member_name,
  167. password=cls.project_member_pass)
  168. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  169. password=cls.non_project_member_pass)
  170. # Todo: change super_user to project_admin.
  171. super_user = User.objects.create_superuser(username=cls.super_user_name,
  172. password=cls.super_user_pass,
  173. email='fizz@buzz.com')
  174. project = mixer.blend('server.Project', users=[project_member, super_user])
  175. cls.label = mixer.blend('server.Label', project=project)
  176. cls.url = reverse(viewname='label_detail', args=[project.id, cls.label.id])
  177. cls.data = {'text': 'example'}
  178. def test_returns_label_to_project_member(self):
  179. self.client.login(username=self.project_member_name,
  180. password=self.project_member_pass)
  181. response = self.client.get(self.url, format='json')
  182. self.assertEqual(response.data['id'], self.label.id)
  183. def test_do_not_return_label_to_non_project_member(self):
  184. self.client.login(username=self.non_project_member_name,
  185. password=self.non_project_member_pass)
  186. response = self.client.get(self.url, format='json')
  187. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  188. def test_allows_superuser_to_update_label(self):
  189. self.client.login(username=self.super_user_name,
  190. password=self.super_user_pass)
  191. response = self.client.patch(self.url, format='json', data=self.data)
  192. self.assertEqual(response.data['text'], self.data['text'])
  193. def test_disallows_project_member_to_update_label(self):
  194. self.client.login(username=self.project_member_name,
  195. password=self.project_member_pass)
  196. response = self.client.patch(self.url, format='json', data=self.data)
  197. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  198. def test_allows_superuser_to_delete_label(self):
  199. self.client.login(username=self.super_user_name,
  200. password=self.super_user_pass)
  201. response = self.client.delete(self.url, format='json')
  202. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  203. def test_disallows_project_member_to_delete_label(self):
  204. self.client.login(username=self.project_member_name,
  205. password=self.project_member_pass)
  206. response = self.client.delete(self.url, format='json')
  207. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  208. class TestDocumentListAPI(APITestCase):
  209. @classmethod
  210. def setUpTestData(cls):
  211. cls.project_member_name = 'project_member_name'
  212. cls.project_member_pass = 'project_member_pass'
  213. cls.non_project_member_name = 'non_project_member_name'
  214. cls.non_project_member_pass = 'non_project_member_pass'
  215. cls.super_user_name = 'super_user_name'
  216. cls.super_user_pass = 'super_user_pass'
  217. project_member = User.objects.create_user(username=cls.project_member_name,
  218. password=cls.project_member_pass)
  219. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  220. password=cls.non_project_member_pass)
  221. # Todo: change super_user to project_admin.
  222. super_user = User.objects.create_superuser(username=cls.super_user_name,
  223. password=cls.super_user_pass,
  224. email='fizz@buzz.com')
  225. cls.main_project = mixer.blend('server.Project', users=[project_member, super_user])
  226. mixer.blend('server.Document', project=cls.main_project)
  227. sub_project = mixer.blend('server.Project', users=[non_project_member])
  228. mixer.blend('server.Document', project=sub_project)
  229. cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
  230. cls.data = {'text': 'example'}
  231. def test_returns_docs_to_project_member(self):
  232. self.client.login(username=self.project_member_name,
  233. password=self.project_member_pass)
  234. response = self.client.get(self.url, format='json')
  235. self.assertEqual(response.status_code, status.HTTP_200_OK)
  236. def test_do_not_return_docs_to_non_project_member(self):
  237. self.client.login(username=self.non_project_member_name,
  238. password=self.non_project_member_pass)
  239. response = self.client.get(self.url, format='json')
  240. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  241. def test_do_not_return_docs_of_other_projects(self):
  242. self.client.login(username=self.project_member_name,
  243. password=self.project_member_pass)
  244. response = self.client.get(self.url, format='json')
  245. self.assertEqual(response.data['count'], self.main_project.documents.count())
  246. def test_allows_superuser_to_create_doc(self):
  247. self.client.login(username=self.super_user_name,
  248. password=self.super_user_pass)
  249. response = self.client.post(self.url, format='json', data=self.data)
  250. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  251. def test_disallows_project_member_to_create_doc(self):
  252. self.client.login(username=self.project_member_name,
  253. password=self.project_member_pass)
  254. response = self.client.post(self.url, format='json', data=self.data)
  255. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  256. class TestDocumentDetailAPI(APITestCase):
  257. @classmethod
  258. def setUpTestData(cls):
  259. cls.project_member_name = 'project_member_name'
  260. cls.project_member_pass = 'project_member_pass'
  261. cls.non_project_member_name = 'non_project_member_name'
  262. cls.non_project_member_pass = 'non_project_member_pass'
  263. cls.super_user_name = 'super_user_name'
  264. cls.super_user_pass = 'super_user_pass'
  265. project_member = User.objects.create_user(username=cls.project_member_name,
  266. password=cls.project_member_pass)
  267. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  268. password=cls.non_project_member_pass)
  269. # Todo: change super_user to project_admin.
  270. super_user = User.objects.create_superuser(username=cls.super_user_name,
  271. password=cls.super_user_pass,
  272. email='fizz@buzz.com')
  273. project = mixer.blend('server.Project', users=[project_member, super_user])
  274. cls.doc = mixer.blend('server.Document', project=project)
  275. cls.url = reverse(viewname='doc_detail', args=[project.id, cls.doc.id])
  276. cls.data = {'text': 'example'}
  277. def test_returns_doc_to_project_member(self):
  278. self.client.login(username=self.project_member_name,
  279. password=self.project_member_pass)
  280. response = self.client.get(self.url, format='json')
  281. self.assertEqual(response.data['id'], self.doc.id)
  282. def test_do_not_return_doc_to_non_project_member(self):
  283. self.client.login(username=self.non_project_member_name,
  284. password=self.non_project_member_pass)
  285. response = self.client.get(self.url, format='json')
  286. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  287. def test_allows_superuser_to_update_doc(self):
  288. self.client.login(username=self.super_user_name,
  289. password=self.super_user_pass)
  290. response = self.client.patch(self.url, format='json', data=self.data)
  291. self.assertEqual(response.data['text'], self.data['text'])
  292. def test_disallows_project_member_to_update_doc(self):
  293. self.client.login(username=self.project_member_name,
  294. password=self.project_member_pass)
  295. response = self.client.patch(self.url, format='json', data=self.data)
  296. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  297. def test_allows_superuser_to_delete_doc(self):
  298. self.client.login(username=self.super_user_name,
  299. password=self.super_user_pass)
  300. response = self.client.delete(self.url, format='json')
  301. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  302. def test_disallows_project_member_to_delete_doc(self):
  303. self.client.login(username=self.project_member_name,
  304. password=self.project_member_pass)
  305. response = self.client.delete(self.url, format='json')
  306. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  307. class TestEntityListAPI(APITestCase):
  308. @classmethod
  309. def setUpTestData(cls):
  310. cls.project_member_name = 'project_member_name'
  311. cls.project_member_pass = 'project_member_pass'
  312. cls.another_project_member_name = 'another_project_member_name'
  313. cls.another_project_member_pass = 'another_project_member_pass'
  314. cls.non_project_member_name = 'non_project_member_name'
  315. cls.non_project_member_pass = 'non_project_member_pass'
  316. project_member = User.objects.create_user(username=cls.project_member_name,
  317. password=cls.project_member_pass)
  318. another_project_member = User.objects.create_user(username=cls.another_project_member_name,
  319. password=cls.another_project_member_pass)
  320. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  321. password=cls.non_project_member_pass)
  322. main_project = mixer.blend('server.Project', users=[project_member, another_project_member])
  323. main_project_label = mixer.blend('server.Label', project=main_project)
  324. main_project_doc = mixer.blend('server.Document', project=main_project)
  325. mixer.blend('server.SequenceAnnotation', document=main_project_doc, user=project_member)
  326. mixer.blend('server.SequenceAnnotation', document=main_project_doc, user=another_project_member)
  327. sub_project = mixer.blend('server.Project', users=[non_project_member])
  328. sub_project_doc = mixer.blend('server.Document', project=sub_project)
  329. mixer.blend('server.SequenceAnnotation', document=sub_project_doc)
  330. cls.url = reverse(viewname='entity_list', args=[main_project.id, main_project_doc.id])
  331. cls.post_data = {'start_offset': 0, 'end_offset': 1, 'label': main_project_label.id}
  332. cls.num_entity_of_project_member = SequenceAnnotation.objects.filter(document=main_project_doc,
  333. user=project_member).count()
  334. def test_returns_entities_to_project_member(self):
  335. self.client.login(username=self.project_member_name,
  336. password=self.project_member_pass)
  337. response = self.client.get(self.url, format='json')
  338. self.assertEqual(response.status_code, status.HTTP_200_OK)
  339. def test_do_not_return_entities_to_non_project_member(self):
  340. self.client.login(username=self.non_project_member_name,
  341. password=self.non_project_member_pass)
  342. response = self.client.get(self.url, format='json')
  343. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  344. def test_do_not_return_entities_of_another_project_member(self):
  345. self.client.login(username=self.project_member_name,
  346. password=self.project_member_pass)
  347. response = self.client.get(self.url, format='json')
  348. self.assertEqual(len(response.data), self.num_entity_of_project_member)
  349. def test_allows_project_member_to_create_entity(self):
  350. self.client.login(username=self.project_member_name,
  351. password=self.project_member_pass)
  352. response = self.client.post(self.url, format='json', data=self.post_data)
  353. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  354. def test_disallows_non_project_member_to_create_entity(self):
  355. self.client.login(username=self.non_project_member_name,
  356. password=self.non_project_member_pass)
  357. response = self.client.post(self.url, format='json', data=self.post_data)
  358. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  359. class TestEntityDetailAPI(APITestCase):
  360. @classmethod
  361. def setUpTestData(cls):
  362. cls.project_member_name = 'project_member_name'
  363. cls.project_member_pass = 'project_member_pass'
  364. cls.another_project_member_name = 'another_project_member_name'
  365. cls.another_project_member_pass = 'another_project_member_pass'
  366. cls.non_project_member_name = 'non_project_member_name'
  367. cls.non_project_member_pass = 'non_project_member_pass'
  368. project_member = User.objects.create_user(username=cls.project_member_name,
  369. password=cls.project_member_pass)
  370. another_project_member = User.objects.create_user(username=cls.another_project_member_name,
  371. password=cls.another_project_member_pass)
  372. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  373. password=cls.non_project_member_pass)
  374. main_project = mixer.blend('server.Project', users=[project_member, another_project_member])
  375. main_project_doc = mixer.blend('server.Document', project=main_project)
  376. main_project_entity = mixer.blend('server.SequenceAnnotation',
  377. document=main_project_doc, user=project_member)
  378. another_entity = mixer.blend('server.SequenceAnnotation',
  379. document=main_project_doc, user=another_project_member)
  380. sub_project = mixer.blend('server.Project', users=[non_project_member])
  381. sub_project_doc = mixer.blend('server.Document', project=sub_project)
  382. mixer.blend('server.SequenceAnnotation', document=sub_project_doc)
  383. cls.url = reverse(viewname='entity_detail', args=[main_project.id,
  384. main_project_doc.id,
  385. main_project_entity.id])
  386. cls.another_url = reverse(viewname='entity_detail', args=[main_project.id,
  387. main_project_doc.id,
  388. another_entity.id])
  389. cls.post_data = {'start_offset': 0, 'end_offset': 10}
  390. def test_returns_entity_to_project_member(self):
  391. self.client.login(username=self.project_member_name,
  392. password=self.project_member_pass)
  393. response = self.client.get(self.url, format='json')
  394. self.assertEqual(response.status_code, status.HTTP_200_OK)
  395. def test_do_not_return_entity_to_non_project_member(self):
  396. self.client.login(username=self.non_project_member_name,
  397. password=self.non_project_member_pass)
  398. response = self.client.get(self.url, format='json')
  399. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  400. def test_do_not_return_entity_by_another_project_member(self):
  401. self.client.login(username=self.project_member_name,
  402. password=self.project_member_pass)
  403. response = self.client.get(self.another_url, format='json')
  404. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  405. def test_allows_project_member_to_update_entity(self):
  406. self.client.login(username=self.project_member_name,
  407. password=self.project_member_pass)
  408. response = self.client.patch(self.url, format='json', data=self.post_data)
  409. self.assertEqual(response.status_code, status.HTTP_200_OK)
  410. def test_disallows_non_project_member_to_update_entity(self):
  411. self.client.login(username=self.non_project_member_name,
  412. password=self.non_project_member_pass)
  413. response = self.client.patch(self.url, format='json', data=self.post_data)
  414. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  415. def test_disallows_project_member_to_update_entity_of_another_member(self):
  416. self.client.login(username=self.project_member_name,
  417. password=self.project_member_pass)
  418. response = self.client.patch(self.another_url, format='json', data=self.post_data)
  419. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  420. def test_allows_project_member_to_delete_entity(self):
  421. self.client.login(username=self.project_member_name,
  422. password=self.project_member_pass)
  423. response = self.client.delete(self.url, format='json')
  424. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  425. def test_disallows_project_member_to_delete_entity(self):
  426. self.client.login(username=self.non_project_member_name,
  427. password=self.non_project_member_pass)
  428. response = self.client.delete(self.url, format='json')
  429. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  430. def test_disallows_project_member_to_delete_entity_of_another_member(self):
  431. self.client.login(username=self.project_member_name,
  432. password=self.project_member_pass)
  433. response = self.client.delete(self.another_url, format='json', data=self.post_data)
  434. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  435. class TestSearch(APITestCase):
  436. @classmethod
  437. def setUpTestData(cls):
  438. cls.project_member_name = 'project_member_name'
  439. cls.project_member_pass = 'project_member_pass'
  440. cls.non_project_member_name = 'non_project_member_name'
  441. cls.non_project_member_pass = 'non_project_member_pass'
  442. project_member = User.objects.create_user(username=cls.project_member_name,
  443. password=cls.project_member_pass)
  444. non_project_member = User.objects.create_user(username=cls.non_project_member_name,
  445. password=cls.non_project_member_pass)
  446. cls.main_project = mixer.blend('server.Project', users=[project_member])
  447. cls.search_term = 'example'
  448. doc1 = mixer.blend('server.Document', text=cls.search_term, project=cls.main_project)
  449. doc2 = mixer.blend('server.Document', text='Lorem', project=cls.main_project)
  450. label1 = mixer.blend('server.Label', project=cls.main_project)
  451. label2 = mixer.blend('server.Label', project=cls.main_project)
  452. mixer.blend('server.SequenceAnnotation', document=doc1, user=project_member, label=label1)
  453. mixer.blend('server.SequenceAnnotation', document=doc2, user=project_member, label=label2)
  454. sub_project = mixer.blend('server.Project', users=[non_project_member])
  455. mixer.blend('server.Document', text=cls.search_term, project=sub_project)
  456. cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
  457. cls.data = {'q': cls.search_term}
  458. def test_can_filter_doc_by_term(self):
  459. self.client.login(username=self.project_member_name,
  460. password=self.project_member_pass)
  461. response = self.client.get(self.url, format='json', data=self.data)
  462. count = Document.objects.filter(text__contains=self.search_term,
  463. project=self.main_project).count()
  464. self.assertEqual(response.data['count'], count)
  465. def test_can_order_doc_by_created_at_ascending(self):
  466. params = {'ordering': 'created_at'}
  467. self.client.login(username=self.project_member_name,
  468. password=self.project_member_pass)
  469. response = self.client.get(self.url, format='json', data=params)
  470. docs = Document.objects.filter(project=self.main_project).order_by('created_at').values()
  471. for d1, d2 in zip(response.data['results'], docs):
  472. self.assertEqual(d1['id'], d2['id'])
  473. def test_can_order_doc_by_created_at_descending(self):
  474. params = {'ordering': '-created_at'}
  475. self.client.login(username=self.project_member_name,
  476. password=self.project_member_pass)
  477. response = self.client.get(self.url, format='json', data=params)
  478. docs = Document.objects.filter(project=self.main_project).order_by('-created_at').values()
  479. for d1, d2 in zip(response.data['results'], docs):
  480. self.assertEqual(d1['id'], d2['id'])
  481. def test_can_order_doc_by_annotation_updated_at_ascending(self):
  482. params = {'ordering': 'seq_annotations__updated_at'}
  483. self.client.login(username=self.project_member_name,
  484. password=self.project_member_pass)
  485. response = self.client.get(self.url, format='json', data=params)
  486. docs = Document.objects.filter(project=self.main_project).order_by('seq_annotations__updated_at').values()
  487. for d1, d2 in zip(response.data['results'], docs):
  488. self.assertEqual(d1['id'], d2['id'])
  489. def test_can_order_doc_by_annotation_updated_at_descending(self):
  490. params = {'ordering': '-seq_annotations__updated_at'}
  491. self.client.login(username=self.project_member_name,
  492. password=self.project_member_pass)
  493. response = self.client.get(self.url, format='json', data=params)
  494. docs = Document.objects.filter(project=self.main_project).order_by('-seq_annotations__updated_at').values()
  495. for d1, d2 in zip(response.data['results'], docs):
  496. self.assertEqual(d1['id'], d2['id'])
  497. class TestFilter(APITestCase):
  498. @classmethod
  499. def setUpTestData(cls):
  500. cls.project_member_name = 'project_member_name'
  501. cls.project_member_pass = 'project_member_pass'
  502. project_member = User.objects.create_user(username=cls.project_member_name,
  503. password=cls.project_member_pass)
  504. cls.main_project = mixer.blend('server.Project', users=[project_member])
  505. cls.label1 = mixer.blend('server.Label', project=cls.main_project)
  506. cls.label2 = mixer.blend('server.Label', project=cls.main_project)
  507. doc1 = mixer.blend('server.Document', project=cls.main_project)
  508. doc2 = mixer.blend('server.Document', project=cls.main_project)
  509. mixer.blend('server.SequenceAnnotation', document=doc1, user=project_member, label=cls.label1)
  510. mixer.blend('server.SequenceAnnotation', document=doc2, user=project_member, label=cls.label2)
  511. cls.url = reverse(viewname='doc_list', args=[cls.main_project.id])
  512. cls.params = {'seq_annotations__label__id': cls.label1.id}
  513. def test_can_filter_by_label(self):
  514. self.client.login(username=self.project_member_name,
  515. password=self.project_member_pass)
  516. response = self.client.get(self.url, format='json', data=self.params)
  517. docs = Document.objects.filter(project=self.main_project,
  518. seq_annotations__label__id=self.label1.id).values()
  519. for d1, d2 in zip(response.data['results'], docs):
  520. self.assertEqual(d1['id'], d2['id'])