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.

65 lines
2.1 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)