No Description

tests_rest_iocs.py 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 TestsRestIocs(TestCase):
  22. def setUp(self) -> None:
  23. self._subject = Iris()
  24. def tearDown(self):
  25. self._subject.clear_database()
  26. def test_get_ioc_should_not_fail(self):
  27. response = self._subject.get('/case/ioc/list').json()
  28. self.assertEqual('success', response['status'])
  29. def test_create_ioc_should_return_correct_ioc_type_id(self):
  30. case_identifier = self._subject.create_dummy_case()
  31. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  32. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  33. self.assertEqual(1, response['ioc_type_id'])
  34. def test_get_ioc_should_return_ioc_type_id(self):
  35. ioc_type_id = 1
  36. case_identifier = self._subject.create_dummy_case()
  37. body = {'ioc_type_id': ioc_type_id, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  38. test = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  39. current_id = test['ioc_id']
  40. response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs/{current_id}').json()
  41. self.assertEqual(ioc_type_id, response['ioc_type_id'])
  42. def test_get_ioc_with_missing_ioc_identifier_should_return_error(self):
  43. case_identifier = self._subject.create_dummy_case()
  44. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  45. self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  46. test = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs/None').json()
  47. self.assertEqual('error', test['status'])
  48. def test_delete_ioc_should_return_204(self):
  49. case_identifier = self._subject.create_dummy_case()
  50. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  51. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  52. ioc_identifier = response['ioc_id']
  53. response = self._subject.delete(f'/api/v2/cases/{case_identifier}/iocs/{ioc_identifier}')
  54. self.assertEqual(204, response.status_code)
  55. def test_delete_ioc_with_missing_ioc_identifier_should_return_404(self):
  56. case_identifier = self._subject.create_dummy_case()
  57. response = self._subject.delete(f'/api/v2/cases/{case_identifier}/iocs/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
  58. self.assertEqual(404, response.status_code)
  59. def test_get_iocs_should_not_fail(self):
  60. case_identifier = self._subject.create_dummy_case()
  61. response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs')
  62. self.assertEqual(200, response.status_code)
  63. def test_create_ioc_should_add_the_ioc_in_the_correct_case(self):
  64. case_identifier = self._subject.create_dummy_case()
  65. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  66. self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  67. response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs').json()
  68. self.assertEqual(1, response['total'])
  69. def test_get_iocs_should_filter_and_return_ioc_type_identifier(self):
  70. case_identifier = self._subject.create_dummy_case()
  71. ioc_type_identifier = 2
  72. self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', {
  73. 'ioc_type_id': ioc_type_identifier,
  74. 'ioc_tlp_id': 2,
  75. 'ioc_value': 'test_get_iocs_should_filter_on_ioc_value',
  76. 'ioc_description': 'rewrw',
  77. 'ioc_tags': '',
  78. 'custom_attributes': {}
  79. }).json()
  80. self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', {
  81. 'ioc_type_id': 1,
  82. 'ioc_tlp_id': 2,
  83. 'ioc_value': 'wrong_test',
  84. 'ioc_description': 'rewrw',
  85. 'ioc_tags': '',
  86. 'custom_attributes': {}
  87. }).json()
  88. filters = {'ioc_value': 'test_get_iocs_should_filter_on_ioc_value'}
  89. response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs', query_parameters=filters).json()
  90. identifiers = []
  91. for ioc in response['data']:
  92. identifiers.append(ioc['ioc_type_id'])
  93. self.assertIn(ioc_type_identifier, identifiers)
  94. def test_get_ioc_should_return_404_when_not_present(self):
  95. case_identifier = self._subject.create_dummy_case()
  96. response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
  97. self.assertEqual(404, response.status_code)
  98. def test_get_ioc_should_return_200_on_success(self):
  99. case_identifier = self._subject.create_dummy_case()
  100. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  101. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  102. ioc_identifier = response['ioc_id']
  103. response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs/{ioc_identifier}')
  104. self.assertEqual(200, response.status_code)
  105. def test_get_iocs_should_include_tlp_information(self):
  106. case_identifier = self._subject.create_dummy_case()
  107. tlp_identifier = 2
  108. body = {'ioc_type_id': 1, 'ioc_tlp_id': tlp_identifier, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  109. self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  110. response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs').json()
  111. self.assertEqual(tlp_identifier, response['data'][0]['tlp']['tlp_id'])
  112. def test_get_iocs_should_include_link_to_other_cases_with_same_value_type_ioc(self):
  113. case_identifier1 = self._subject.create_dummy_case()
  114. case_identifier2 = self._subject.create_dummy_case()
  115. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  116. self._subject.create(f'/api/v2/cases/{case_identifier1}/iocs', body).json()
  117. body = {'ioc_type_id': 1, 'ioc_tlp_id': 1, 'ioc_value': '8.8.8.8', 'ioc_description': 'another', 'ioc_tags': ''}
  118. self._subject.create(f'/api/v2/cases/{case_identifier2}/iocs', body).json()
  119. response = self._subject.get(f'/api/v2/cases/{case_identifier2}/iocs').json()
  120. self.assertEqual(case_identifier1, response['data'][0]['link'][0]['case_id'])
  121. def test_create_ioc_should_include_field_link(self):
  122. case_identifier = self._subject.create_dummy_case()
  123. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  124. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  125. self.assertEqual([], response['link'])
  126. def test_get_ioc_should_include_field_link(self):
  127. case_identifier = self._subject.create_dummy_case()
  128. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  129. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  130. ioc_identifier = response['ioc_id']
  131. response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs/{ioc_identifier}').json()
  132. self.assertEqual([], response['link'])
  133. def test_create_ioc_should_not_create_two_iocs_with_identical_type_and_value(self):
  134. case_identifier = self._subject.create_dummy_case()
  135. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  136. self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body)
  137. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body)
  138. self.assertEqual(400, response.status_code)
  139. def test_delete_ioc_should_not_prevent_case_deletion(self):
  140. case_identifier = self._subject.create_dummy_case()
  141. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  142. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  143. ioc_identifier = response['ioc_id']
  144. self._subject.create(f'/case/ioc/{ioc_identifier}/comments/add', {'comment_text': 'comment text'})
  145. self._subject.delete(f'/api/v2/cases/{case_identifier}/iocs/{ioc_identifier}')
  146. response = self._subject.delete(f'/api/v2/cases/{case_identifier}')
  147. self.assertEqual(204, response.status_code)
  148. def test_update_ioc_should_not_fail(self):
  149. case_identifier = self._subject.create_dummy_case()
  150. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  151. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  152. ioc_identifier = response['ioc_id']
  153. response = self._subject.update(f'/api/v2/cases/{case_identifier}/iocs/{ioc_identifier}', {'ioc_value': '9.9.9.9'})
  154. self.assertEqual(200, response.status_code)
  155. def test_update_ioc_should_return_updated_value(self):
  156. case_identifier = self._subject.create_dummy_case()
  157. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  158. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  159. ioc_identifier = response['ioc_id']
  160. new_value = '9.9.9.9'
  161. response = self._subject.update(f'/api/v2/cases/{case_identifier}/iocs/{ioc_identifier}', {'ioc_value': new_value}).json()
  162. self.assertEqual(new_value, response['ioc_value'])
  163. def test_update_ioc_should_return_an_error_when_ioc_type_identifier_is_out_of_range(self):
  164. case_identifier = self._subject.create_dummy_case()
  165. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  166. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  167. ioc_identifier = response['ioc_id']
  168. response = self._subject.update(f'/api/v2/cases/{case_identifier}/iocs/{ioc_identifier}', {'ioc_type_id': '123456789'})
  169. self.assertEqual(400, response.status_code)
  170. def test_rest_case_should_return_error_ioc_when_permission_denied(self):
  171. user = self._subject.create_dummy_user()
  172. case_identifier = self._subject.create_dummy_case()
  173. body = {'ioc_type_id': 1, 'ioc_tlp_id': 1, 'ioc_value': 'IOC value'}
  174. self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body)
  175. response = user.get(f'/api/v2/cases/{case_identifier}/iocs')
  176. self.assertEqual(403, response.status_code)