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.

97 lines
3.3 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 TestDocAPI(APITestCase):
  7. def setUp(self):
  8. self.username, self.password = 'user', 'pass'
  9. def create_user(self):
  10. user = User.objects.create_user(username=self.username, password=self.password)
  11. return user
  12. def create_superuser(self):
  13. user = User.objects.create_superuser(username=self.username,
  14. password=self.password,
  15. email='hoge@example.com')
  16. return user
  17. def create_project(self):
  18. project = mixer.blend('server.Project')
  19. return project
  20. def create_doc(self):
  21. doc = mixer.blend('server.Document')
  22. return doc
  23. def test_get_docs_by_project_user(self):
  24. """
  25. Ensure we can get document objects by project user.
  26. """
  27. user = self.create_user()
  28. project = self.create_project()
  29. project.users.add(user)
  30. doc = self.create_doc()
  31. project.documents.add(doc)
  32. url = reverse('docs', args=[project.id])
  33. self.client.login(username=self.username, password=self.password)
  34. response = self.client.get(url, format='json')
  35. self.assertEqual(response.status_code, status.HTTP_200_OK)
  36. self.assertIsInstance(response.data, dict)
  37. self.assertEqual(response.data['count'], 1)
  38. self.assertEqual(response.data['results'][0]['id'], doc.id)
  39. def test_get_docs_by_other(self):
  40. """
  41. Ensure we cannot get label objects by other.
  42. """
  43. user = self.create_user()
  44. project = self.create_project()
  45. doc = self.create_doc()
  46. project.documents.add(doc)
  47. url = reverse('docs', args=[project.id])
  48. self.client.login(username=self.username, password=self.password)
  49. response = self.client.get(url, format='json')
  50. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  51. def test_create_doc_by_admin(self):
  52. """
  53. Ensure we can create a new document object by admin.
  54. """
  55. # user = self.create_superuser()
  56. # project = self.create_project()
  57. # project.users.add(user)
  58. #
  59. # data = {'text': 'doc', 'labels': []}
  60. # url = reverse('docs', args=[project.id])
  61. # self.client.login(username=self.username, password=self.password)
  62. # response = self.client.post(url, data, format='json')
  63. #
  64. # self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  65. # self.assertEqual(Document.objects.count(), 1)
  66. # self.assertEqual(Document.objects.get().text, 'doc')
  67. def test_create_doc_by_user(self):
  68. """
  69. Ensure we cannot create a new project object by user.
  70. """
  71. user = self.create_user()
  72. project = self.create_project()
  73. project.users.add(user)
  74. data = {'text': 'doc', 'labels': []}
  75. url = reverse('docs', args=[project.id])
  76. self.client.login(username=self.username, password=self.password)
  77. response = self.client.post(url, data, format='json')
  78. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)