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.

127 lines
4.6 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 TestProjects(APITestCase):
  7. def setUp(self):
  8. self.username, self.password = 'user', 'pass'
  9. self.url = reverse('project-list')
  10. def create_user(self):
  11. user = User.objects.create_user(username=self.username, password=self.password)
  12. return user
  13. def create_superuser(self):
  14. user = User.objects.create_superuser(username=self.username,
  15. password=self.password,
  16. email='hoge@example.com')
  17. return user
  18. def create_project(self):
  19. project = mixer.blend('server.Project')
  20. return project
  21. def add_user_to_project(self, user, project):
  22. project.users.add(user)
  23. def test_get_projects(self):
  24. """
  25. Ensure we can get project objects.
  26. """
  27. user = self.create_user()
  28. project = self.create_project()
  29. self.add_user_to_project(user, project)
  30. self.client.login(username=self.username, password=self.password)
  31. response = self.client.get(self.url, format='json')
  32. p = response.data[0]
  33. self.assertEqual(response.status_code, status.HTTP_200_OK)
  34. self.assertIsInstance(response.data, list)
  35. self.assertEqual(p['id'], project.id)
  36. self.assertEqual(p['name'], project.name)
  37. self.assertEqual(p['description'], project.description)
  38. self.assertEqual(p['project_type'], project.project_type)
  39. self.assertEqual(p['image'], project.image)
  40. def test_get_progress(self):
  41. """
  42. Ensure we can get project's progress.
  43. """
  44. user = self.create_user()
  45. project = self.create_project()
  46. self.add_user_to_project(user, project)
  47. url = '{}{}/progress/'.format(self.url, 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_200_OK)
  51. self.assertIn('total', response.data)
  52. self.assertIn('remaining', response.data)
  53. self.assertIsInstance(response.data['total'], int)
  54. self.assertIsInstance(response.data['remaining'], int)
  55. def test_create_project_by_admin(self):
  56. """
  57. Ensure we can create a new project object by admin.
  58. """
  59. user = self.create_superuser()
  60. data = {'name': 'DabApps',
  61. 'description': 'desc',
  62. 'project_type': Project.DOCUMENT_CLASSIFICATION,
  63. 'users': [user.id]}
  64. self.client.login(username=self.username, password=self.password)
  65. response = self.client.post(self.url, data, format='json')
  66. self.assertEqual(response.status_code, status.HTTP_201_CREATED)
  67. self.assertEqual(Project.objects.count(), 1)
  68. self.assertEqual(Project.objects.get().name, 'DabApps')
  69. def test_create_project_by_user(self):
  70. """
  71. Ensure we cannot create a new project object by user.
  72. """
  73. user = self.create_user()
  74. data = {'name': 'DabApps',
  75. 'description': 'desc',
  76. 'project_type': Project.DOCUMENT_CLASSIFICATION,
  77. 'users': [user.id]}
  78. self.client.login(username=self.username, password=self.password)
  79. response = self.client.post(self.url, data, format='json')
  80. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  81. def test_delete_project_by_superuser(self):
  82. """
  83. Ensure we can delete a project by superuser.
  84. """
  85. user = self.create_superuser()
  86. project = self.create_project()
  87. self.assertEqual(Project.objects.count(), 1)
  88. self.client.login(username=self.username, password=self.password)
  89. url = '{}{}/'.format(self.url, project.id)
  90. response = self.client.delete(url, format='json')
  91. self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
  92. self.assertEqual(Project.objects.count(), 0)
  93. def test_delete_project_by_user(self):
  94. """
  95. Ensure we cannot delete a project by user.
  96. """
  97. user = self.create_user()
  98. project = self.create_project()
  99. self.assertEqual(Project.objects.count(), 1)
  100. self.client.login(username=self.username, password=self.password)
  101. url = '{}{}/'.format(self.url, project.id)
  102. response = self.client.delete(url, format='json')
  103. self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
  104. self.assertEqual(Project.objects.count(), 1)