Geen omschrijving

tests_rest_tasks.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # IRIS Source Code
  2. # Copyright (C) 2023 - DFIR-IRIS
  3. # contact@dfir-iris.org
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. from unittest import TestCase
  19. from iris import Iris
  20. _IDENTIFIER_FOR_NONEXISTENT_OBJECT = 123456789
  21. class TestsRestTasks(TestCase):
  22. def setUp(self) -> None:
  23. self._subject = Iris()
  24. def tearDown(self):
  25. self._subject.clear_database()
  26. def test_add_task_should_return_201(self):
  27. case_identifier = self._subject.create_dummy_case()
  28. body = {'task_assignees_id': [], 'task_description': '', 'task_status_id': 1, 'task_tags': '',
  29. 'task_title': 'dummy title', 'custom_attributes': {}}
  30. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body)
  31. self.assertEqual(201, response.status_code)
  32. def test_add_task_with_missing_task_title_identifier_should_return_400(self):
  33. case_identifier = self._subject.create_dummy_case()
  34. body = {'task_assignees_id': [], 'task_description': '', 'task_status_id': 1, 'task_tags': '',
  35. 'custom_attributes': {}}
  36. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body)
  37. self.assertEqual(400, response.status_code)
  38. def test_create_case_with_spurious_slash_should_return_404(self):
  39. case_identifier = self._subject.create_dummy_case()
  40. body = {'task_assignees_id': [], 'task_description': '', 'task_status_id': 1, 'task_tags': '',
  41. 'task_title': 'dummy title', 'custom_attributes': {}}
  42. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks/', body)
  43. self.assertEqual(404, response.status_code)
  44. def test_get_task_should_return_200(self):
  45. case_identifier = self._subject.create_dummy_case()
  46. body = {'task_assignees_id': [], 'task_description': '', 'task_status_id': 1, 'task_tags': '',
  47. 'task_title': 'dummy title',
  48. 'custom_attributes': {}}
  49. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  50. task_identifier = response['id']
  51. response = self._subject.get(f'/api/v2/cases/{case_identifier}/tasks/{task_identifier}')
  52. self.assertEqual(200, response.status_code)
  53. def test_get_task_with_missing_task_identifier_should_return_error(self):
  54. case_identifier = self._subject.create_dummy_case()
  55. body = {'task_assignees_id': [], 'task_description': '', 'task_status_id': 1, 'task_tags': '',
  56. 'task_title': 'dummy title', 'custom_attributes': {}}
  57. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body)
  58. response = self._subject.get(f'/api/v2/cases/{case_identifier}/tasks/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
  59. self.assertEqual(404, response.status_code)
  60. def test_delete_task_should_return_204(self):
  61. case_identifier = self._subject.create_dummy_case()
  62. body = {'task_assignees_id': [], 'task_description': '', 'task_status_id': 1, 'task_tags': '',
  63. 'task_title': 'dummy title',
  64. 'custom_attributes': {}}
  65. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  66. task_identifier = response['id']
  67. test = self._subject.delete(f'/api/v2/cases/{case_identifier}/tasks/{task_identifier}')
  68. self.assertEqual(204, test.status_code)
  69. def test_delete_task_with_missing_task_identifier_should_return_404(self):
  70. case_identifier = self._subject.create_dummy_case()
  71. body = {'task_assignees_id': [], 'task_description': '', 'task_status_id': 1, 'task_tags': '', 'task_title': 'dummy title',
  72. 'custom_attributes': {}}
  73. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body)
  74. test = self._subject.delete(f'/api/v2/cases/{case_identifier}/tasks/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
  75. self.assertEqual(404, test.status_code)
  76. def test_get_user_task_should_not_fail(self):
  77. case_identifier = self._subject.create_dummy_case()
  78. user = self._subject.create_dummy_user()
  79. body = {'task_assignees_id': [user.get_identifier()], 'task_description': '', 'task_status_id': 1, 'task_tags': '', 'task_title': 'dummy title',
  80. 'custom_attributes': {}}
  81. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body)
  82. response = user.get('/user/tasks/list')
  83. self.assertEqual(200, response.status_code)
  84. def test_get_user_task_should_contain_task_case_field(self):
  85. case_identifier = self._subject.create_dummy_case()
  86. user = self._subject.create_dummy_user()
  87. body = {'task_assignees_id': [user.get_identifier()], 'task_description': '', 'task_status_id': 1, 'task_tags': '', 'task_title': 'dummy title',
  88. 'custom_attributes': {}}
  89. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body)
  90. response = user.get('/user/tasks/list').json()
  91. self.assertEqual(f'#{case_identifier} - case name', response['data']['tasks'][0]['task_case'])
  92. def test_update_task_should_not_fail(self):
  93. case_identifier = self._subject.create_dummy_case()
  94. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'dummy title'}
  95. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  96. identifier = response['id']
  97. response = self._subject.update(f'/api/v2/cases/{case_identifier}/tasks/{identifier}',
  98. {'task_title': 'new title', 'task_status_id': 1, 'task_assignees_id': []})
  99. self.assertEqual(200, response.status_code)
  100. def test_update_task_should_update_assignees(self):
  101. case_identifier = self._subject.create_dummy_case()
  102. user = self._subject.create_dummy_user()
  103. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'dummy title'}
  104. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  105. identifier = response['id']
  106. self._subject.update(f'/api/v2/cases/{case_identifier}/tasks/{identifier}',
  107. {'task_title': 'dummy title', 'task_status_id': 1, 'task_assignees_id': [user.get_identifier()]})
  108. response = self._subject.get('/case/tasks/list', query_parameters={'cid': case_identifier}).json()
  109. self.assertEqual(user.get_identifier(), response['data']['tasks'][0]['task_assignees'][0]['id'])
  110. def test_update_task_without_task_status_id_should_return_400(self):
  111. case_identifier = self._subject.create_dummy_case()
  112. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'dummy title'}
  113. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  114. identifier = response['id']
  115. response = self._subject.update(f'/api/v2/cases/{case_identifier}/tasks/{identifier}',
  116. {'task_title': 'new title', 'task_assignees_id': []})
  117. self.assertEqual(400, response.status_code)
  118. def test_update_task_should_return_a_task(self):
  119. case_identifier = self._subject.create_dummy_case()
  120. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'dummy title'}
  121. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  122. identifier = response['id']
  123. response = self._subject.update(f'/api/v2/cases/{case_identifier}/tasks/{identifier}',
  124. {'task_title': 'new title', 'task_status_id': 1, 'task_assignees_id': []}).json()
  125. self.assertEqual('new title', response['task_title'])
  126. def test_get_tasks_should_return_200(self):
  127. case_identifier = self._subject.create_dummy_case()
  128. response = self._subject.get(f'/api/v2/cases/{case_identifier}/tasks')
  129. self.assertEqual(200, response.status_code)
  130. def test_get_tasks_should_return_empty_list_for_field_data_when_there_are_no_tasks(self):
  131. case_identifier = self._subject.create_dummy_case()
  132. response = self._subject.get(f'/api/v2/cases/{case_identifier}/tasks').json()
  133. self.assertEqual([], response['data'])
  134. def test_get_tasks_should_return_total(self):
  135. case_identifier = self._subject.create_dummy_case()
  136. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'dummy title'}
  137. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  138. response = self._subject.get(f'/api/v2/cases/{case_identifier}/tasks').json()
  139. self.assertEqual(1, response['total'])
  140. def test_get_tasks_should_honour_per_page_pagination_parameter(self):
  141. case_identifier = self._subject.create_dummy_case()
  142. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'task1'}
  143. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  144. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'task2'}
  145. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  146. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'task3'}
  147. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  148. response = self._subject.get(f'/api/v2/cases/{case_identifier}/tasks', { 'per_page': 2 }).json()
  149. self.assertEqual(2, len(response['data']))
  150. def test_get_tasks_should_return_current_page(self):
  151. case_identifier = self._subject.create_dummy_case()
  152. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'task1'}
  153. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  154. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'task2'}
  155. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  156. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'task3'}
  157. self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  158. response = self._subject.get(f'/api/v2/cases/{case_identifier}/tasks', { 'page': 2, 'per_page': 2 }).json()
  159. self.assertEqual(2, response['current_page'])
  160. def test_get_tasks_should_return_correct_task_uuid(self):
  161. case_identifier = self._subject.create_dummy_case()
  162. body = {'task_assignees_id': [], 'task_status_id': 1, 'task_title': 'title'}
  163. response = self._subject.create(f'/api/v2/cases/{case_identifier}/tasks', body).json()
  164. identifier = response['id']
  165. response = self._subject.get(f'/api/v2/tasks/{identifier}').json()
  166. expected_uuid = response['task_uuid']
  167. response = self._subject.get(f'/api/v2/cases/{case_identifier}/tasks').json()
  168. self.assertEqual(expected_uuid, response['data'][0]['task_uuid'])