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.

73 lines
3.2 KiB

  1. from django.urls import reverse
  2. from rest_framework import status
  3. from rest_framework.test import APITestCase
  4. from mixer.backend.django import mixer
  5. from ..models import *
  6. class TestAnnotationAPI(APITestCase):
  7. @classmethod
  8. def setUpTestData(cls):
  9. cls.username = 'user'
  10. cls.password = 'pass'
  11. cls.user1 = User.objects.create_user(username=cls.username, password=cls.password)
  12. cls.user2 = User.objects.create_user(username='user2', password='pass2')
  13. cls.project1 = mixer.blend('server.Project', project_type=Project.DOCUMENT_CLASSIFICATION,
  14. users=[cls.user1, cls.user2])
  15. cls.project2 = mixer.blend('server.Project', project_type=Project.DOCUMENT_CLASSIFICATION,
  16. users=[cls.user2])
  17. cls.doc1 = mixer.blend('server.Document', project=cls.project1)
  18. cls.doc2 = mixer.blend('server.Document', project=cls.project1)
  19. cls.label = mixer.blend('server.Label', project=cls.project1)
  20. cls.annotation1 = mixer.blend('server.DocumentAnnotation', document=cls.doc1, user=cls.user1)
  21. cls.annotation2 = mixer.blend('server.DocumentAnnotation', document=cls.doc1, user=cls.user2)
  22. def setUp(self):
  23. self.client.login(username=self.username, password=self.password)
  24. def test_fetch_own_annotation(self):
  25. """
  26. Ensure user can fetch only own annotation.
  27. """
  28. url = reverse('annotations', args=[self.project1.id, self.doc1.id])
  29. r = self.client.get(url, format='json')
  30. self.assertEqual(r.status_code, status.HTTP_200_OK)
  31. self.assertEqual(len(r.data), 1)
  32. self.assertEqual(r.data[0]['id'], self.annotation1.id)
  33. def test_fetch_other_projects_annotation(self):
  34. """
  35. Ensure user cannot fetch other project's annotation.
  36. """
  37. url = reverse('annotations', args=[self.project2.id, self.doc1.id])
  38. r = self.client.get(url, format='json')
  39. self.assertEqual(r.status_code, status.HTTP_403_FORBIDDEN)
  40. def test_annotate_doc(self):
  41. """
  42. Ensure user can annotate a document.
  43. """
  44. # Try to annotate a empty document(doc2).
  45. data = {'label': self.label.id}
  46. url = reverse('annotations', args=[self.project1.id, self.doc2.id])
  47. r = self.client.post(url, data, format='json')
  48. self.assertEqual(r.status_code, status.HTTP_201_CREATED)
  49. self.assertEqual(len(self.doc2.doc_annotations.all()), 1)
  50. def test_delete_annotation(self):
  51. """
  52. Ensure user can delete only own annotation.
  53. """
  54. self.assertEqual(len(self.doc1.doc_annotations.all()), 2)
  55. # Try to delete own annotation.
  56. url = reverse('ann', args=[self.project1.id, self.doc1.id, self.annotation1.id])
  57. r = self.client.delete(url, format='json')
  58. self.assertEqual(r.status_code, status.HTTP_204_NO_CONTENT)
  59. self.assertEqual(len(self.doc1.doc_annotations.all()), 1)
  60. # Try to delete other's annotation.
  61. url = reverse('ann', args=[self.project1.id, self.doc1.id, self.annotation2.id])
  62. r = self.client.delete(url, format='json')
  63. self.assertEqual(r.status_code, status.HTTP_403_FORBIDDEN)