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.

137 lines
4.9 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 User, Label
  6. class TestLabelAPI(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 add_user_to_project(self, user, project):
  21. project.users.add(user)
  22. def create_label(self):
  23. label = mixer.blend('server.Label')
  24. return label
  25. def test_get_labels_by_project_user(self):
  26. """
  27. Ensure we can get label objects by project user.
  28. """
  29. user = self.create_user()
  30. project = self.create_project()
  31. self.add_user_to_project(user, project)
  32. label = self.create_label()
  33. project.labels.add(label)
  34. url = reverse('labels', args=[project.id])
  35. self.client.login(username=self.username, password=self.password)
  36. response = self.client.get(url, format='json')
  37. r = response.data[0]
  38. self.assertEqual(response.status_code, status.HTTP_200_OK)
  39. self.assertIsInstance(response.data, list)
  40. self.assertEqual(r['id'], label.id)
  41. def test_get_labels_by_other(self):
  42. """
  43. Ensure we cannot get label objects by other.
  44. """
  45. user = self.create_user() # noqa: F841
  46. project = self.create_project()
  47. label = self.create_label()
  48. project.labels.add(label)
  49. url = reverse('labels', args=[project.id])
  50. self.client.login(username=self.username, password=self.password)
  51. response = self.client.get(url, format='json')
  52. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  53. def test_create_label_by_admin(self):
  54. """
  55. Ensure we can create a new project object by admin.
  56. """
  57. user = self.create_superuser()
  58. project = self.create_project()
  59. self.add_user_to_project(user, project)
  60. data = {'text': 'label1',
  61. 'shortcut': 'a',
  62. 'background_color': '#ffffff',
  63. 'text_color': '#ffffff'}
  64. url = reverse('labels', args=[project.id])
  65. self.client.login(username=self.username, password=self.password)
  66. response = self.client.post(url, data, format='json')
  67. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  68. self.assertEqual(Label.objects.count(), 1)
  69. self.assertEqual(Label.objects.get().text, 'label1')
  70. def test_create_label_by_user(self):
  71. """
  72. Ensure we cannot create a new project object by user.
  73. """
  74. user = self.create_user()
  75. project = self.create_project()
  76. self.add_user_to_project(user, project)
  77. data = {'text': 'label1',
  78. 'shortcut': 'a',
  79. 'background_color': '#ffffff',
  80. 'text_color': '#ffffff'}
  81. url = reverse('labels', args=[project.id])
  82. self.client.login(username=self.username, password=self.password)
  83. response = self.client.post(url, data, format='json')
  84. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  85. def test_delete_label_by_admin(self):
  86. """
  87. Ensure we can delete a label by superuser.
  88. """
  89. user = self.create_superuser()
  90. project = self.create_project()
  91. label = self.create_label()
  92. self.add_user_to_project(user, project)
  93. project.labels.add(label)
  94. self.assertEqual(Label.objects.count(), 1)
  95. self.client.login(username=self.username, password=self.password)
  96. url = reverse('label', args=[project.id, label.id])
  97. response = self.client.delete(url, format='json')
  98. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  99. self.assertEqual(Label.objects.count(), 0)
  100. def test_delete_label_by_user(self):
  101. """
  102. Ensure we cannot delete a label by user.
  103. """
  104. user = self.create_user()
  105. project = self.create_project()
  106. label = self.create_label()
  107. self.add_user_to_project(user, project)
  108. project.labels.add(label)
  109. self.assertEqual(Label.objects.count(), 1)
  110. self.client.login(username=self.username, password=self.password)
  111. url = reverse('label', args=[project.id, label.id])
  112. response = self.client.delete(url, format='json')
  113. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  114. self.assertEqual(Label.objects.count(), 1)