Nessuna descrizione

tests_rest_cases.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 uuid import uuid4
  20. from iris import Iris
  21. def _get_case_with_identifier(response, identifier):
  22. for case in response['data']:
  23. if identifier == case['case_id']:
  24. return case
  25. raise ValueError('Case not found')
  26. class TestsRestCases(TestCase):
  27. def setUp(self) -> None:
  28. self._subject = Iris()
  29. def tearDown(self):
  30. self._subject.clear_database()
  31. def test_manage_case_filter_api_rest_should_fail(self):
  32. self._subject.create_dummy_case()
  33. response = self._subject.get('/manage/cases/filter').json()
  34. self.assertEqual('success', response['status'])
  35. def test_create_case_should_return_201(self):
  36. response = self._subject.create('/api/v2/cases', {
  37. 'case_name': 'name',
  38. 'case_description': 'description',
  39. 'case_customer': 1,
  40. 'case_soc_id': ''
  41. })
  42. self.assertEqual(201, response.status_code)
  43. def test_create_case_with_spurious_slash_should_return_404(self):
  44. response = self._subject.create('/api/v2/cases/', {
  45. 'case_name': 'name',
  46. 'case_description': 'description',
  47. 'case_customer': 1,
  48. 'case_soc_id': ''
  49. })
  50. self.assertEqual(404, response.status_code)
  51. def test_create_case_with_missing_name_should_return_400(self):
  52. response = self._subject.create('/api/v2/cases', {
  53. 'case_description': 'description',
  54. 'case_customer': 1,
  55. 'case_soc_id': ''
  56. })
  57. self.assertEqual(400, response.status_code)
  58. def test_create_case_with_classification_id_should_set_classification_id(self):
  59. response = self._subject.create('/api/v2/cases', {
  60. 'case_name': 'name',
  61. 'case_description': 'description',
  62. 'case_customer': 1,
  63. 'case_soc_id': '',
  64. 'classification_id': 2
  65. }).json()
  66. self.assertEqual(2, response['classification_id'])
  67. def test_create_case_should_add_a_new_case(self):
  68. response = self._subject.get('/api/v2/cases').json()
  69. initial_case_count = len(response['data'])
  70. self._subject.create_dummy_case()
  71. response = self._subject.get('/api/v2/cases').json()
  72. case_count = len(response['data'])
  73. self.assertEqual(initial_case_count + 1, case_count)
  74. def test_get_case_should_return_case_data(self):
  75. response = self._subject.create('/api/v2/cases', {
  76. 'case_name': 'name',
  77. 'case_description': 'description',
  78. 'case_customer': 1,
  79. 'case_soc_id': ''
  80. }).json()
  81. identifier = response['case_id']
  82. response = self._subject.get(f'/api/v2/cases/{identifier}').json()
  83. self.assertEqual('description', response['case_description'])
  84. def test_delete_case_should_return_204(self):
  85. response = self._subject.create('/api/v2/cases', {
  86. 'case_name': 'name',
  87. 'case_description': 'description',
  88. 'case_customer': 1,
  89. 'case_soc_id': ''
  90. }).json()
  91. identifier = response['case_id']
  92. response = self._subject.delete(f'/api/v2/cases/{identifier}')
  93. self.assertEqual(204, response.status_code)
  94. def test_get_case_should_return_404_after_it_is_deleted(self):
  95. response = self._subject.create('/api/v2/cases', {
  96. 'case_name': 'name',
  97. 'case_description': 'description',
  98. 'case_customer': 1,
  99. 'case_soc_id': ''
  100. }).json()
  101. identifier = response['case_id']
  102. self._subject.delete(f'/api/v2/cases/{identifier}')
  103. response = self._subject.get(f'/api/v2/cases/{identifier}')
  104. self.assertEqual(404, response.status_code)
  105. def test_update_case_should_not_require_case_name_issue_358(self):
  106. case_identifier = self._subject.create_dummy_case()
  107. response = self._subject.create(f'/manage/cases/update/{case_identifier}', {'case_tags': 'test,example'}).json()
  108. self.assertEqual('success', response['status'])
  109. def test_get_cases_should_not_fail(self):
  110. response = self._subject.get('/api/v2/cases')
  111. self.assertEqual(200, response.status_code)
  112. def test_get_cases_should_filter_on_case_name(self):
  113. response = self._subject.create('/api/v2/cases', {
  114. 'case_name': 'test_get_cases_should_filter_on_case_name',
  115. 'case_description': 'description',
  116. 'case_customer': 1,
  117. 'case_soc_id': ''
  118. }).json()
  119. case_identifier = response['case_id']
  120. filters = {'case_name': 'test_get_cases_should_filter_on_case_name'}
  121. response = self._subject.get('/api/v2/cases', query_parameters=filters).json()
  122. identifiers = []
  123. for case in response['data']:
  124. identifiers.append(case['case_id'])
  125. self.assertIn(case_identifier, identifiers)
  126. def test_get_cases_should_filter_on_is_open(self):
  127. case_identifier = self._subject.create_dummy_case()
  128. self._subject.create(f'/manage/cases/close/{case_identifier}', {})
  129. filters = {'is_open': 'true'}
  130. response = self._subject.get('/api/v2/cases', query_parameters=filters).json()
  131. identifiers = []
  132. for case in response['data']:
  133. identifiers.append(case['case_id'])
  134. self.assertNotIn(case_identifier, identifiers)
  135. def test_get_cases_should_return_the_state_name(self):
  136. case_identifier = self._subject.create_dummy_case()
  137. response = self._subject.get('/api/v2/cases').json()
  138. case = _get_case_with_identifier(response, case_identifier)
  139. self.assertEqual('Open', case['state']['state_name'])
  140. def test_get_cases_should_return_the_owner_name(self):
  141. case_identifier = self._subject.create_dummy_case()
  142. response = self._subject.get('/api/v2/cases').json()
  143. case = _get_case_with_identifier(response, case_identifier)
  144. self.assertEqual('administrator', case['owner']['user_name'])
  145. def test_get_case_should_have_field_case_name(self):
  146. case_identifier = self._subject.create_dummy_case()
  147. response = self._subject.get(f'/api/v2/cases/{case_identifier}').json()
  148. self.assertIn('case_name', response)
  149. def test_get_case_should_have_field_case_customer_id(self):
  150. case_identifier = self._subject.create_dummy_case()
  151. response = self._subject.get(f'/api/v2/cases/{case_identifier}').json()
  152. self.assertIn('case_customer_id', response)
  153. def test_create_case_should_return_data_with_case_customer_when_case_customer_is_an_empty_string(self):
  154. body = {
  155. 'case_name': 'case name',
  156. 'case_description': 'description',
  157. 'case_customer': '',
  158. 'case_soc_id': ''
  159. }
  160. response = self._subject.create('/api/v2/cases', body).json()
  161. self.assertIn('case_customer', response['data'])
  162. def test_update_case_should_not_fail(self):
  163. identifier = self._subject.create_dummy_case()
  164. response = self._subject.update(f'/api/v2/cases/{identifier}', { 'case_name': 'new name' })
  165. self.assertEqual(200, response.status_code)
  166. def test_update_case_should_allow_to_update_severity(self):
  167. identifier = self._subject.create_dummy_case()
  168. response = self._subject.update(f'/api/v2/cases/{identifier}', { 'severity_id': 5 }).json()
  169. self.assertEqual(5, response['severity_id'])
  170. def test_update_case_should_allow_to_update_classification(self):
  171. identifier = self._subject.create_dummy_case()
  172. response = self._subject.update(f'/api/v2/cases/{identifier}', { 'classification_id': 3 }).json()
  173. self.assertEqual(3, response['classification_id'])
  174. def test_update_case_should_allow_to_update_owner(self):
  175. user = self._subject.create_dummy_user()
  176. identifier = self._subject.create_dummy_case()
  177. response = self._subject.update(f'/api/v2/cases/{identifier}', { 'owner_id': user.get_identifier() }).json()
  178. self.assertEqual(user.get_identifier(), response['owner']['id'])
  179. def test_update_case_should_allow_to_update_state(self):
  180. identifier = self._subject.create_dummy_case()
  181. response = self._subject.update(f'/api/v2/cases/{identifier}', { 'state_id': 2 }).json()
  182. self.assertEqual(2, response['state']['state_id'])
  183. def test_update_case_should_allow_to_update_status(self):
  184. identifier = self._subject.create_dummy_case()
  185. response = self._subject.update(f'/api/v2/cases/{identifier}', { 'status_id': 2 }).json()
  186. self.assertEqual(2, response['status_id'])
  187. def test_update_case_should_allow_to_update_customer(self):
  188. identifier = self._subject.create_dummy_case()
  189. response = self._subject.create('/manage/customers/add', { 'customer_name': f'customer{uuid4()}'}).json()
  190. customer_identifier = response['data']['customer_id']
  191. response = self._subject.update(f'/api/v2/cases/{identifier}', {'case_customer': customer_identifier}).json()
  192. self.assertEqual(customer_identifier, response['case_customer_id'])
  193. def test_update_case_should_allow_to_update_reviewer(self):
  194. identifier = self._subject.create_dummy_case()
  195. user = self._subject.create_dummy_user()
  196. response = self._subject.update(f'/api/v2/cases/{identifier}', {'reviewer_id': user.get_identifier()}).json()
  197. self.assertEqual(user.get_identifier(), response['reviewer_id'])
  198. def test_update_case_should_allow_to_update_tags(self):
  199. identifier = self._subject.create_dummy_case()
  200. response = self._subject.update(f'/api/v2/cases/{identifier}', {'case_tags': 'tag1,tag2'}).json()
  201. self.assertEqual('tag1,tag2', response['case_tags'])
  202. def test_update_case_should_return_invalid_integer_on_case_severity_update(self):
  203. identifier = self._subject.create_dummy_case()
  204. response = self._subject.update(f'/api/v2/cases/{identifier}', {'severity_id': 'invalid_integer'})
  205. self.assertEqual(400, response.status_code)
  206. self.assertEqual(['Not a valid integer.'], response.json()['data']['severity_id'])