暂无描述

tests_rest_assets.py 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. _CASE_ACCESS_LEVEL_FULL_ACCESS = 4
  22. class TestsRestAssets(TestCase):
  23. def setUp(self) -> None:
  24. self._subject = Iris()
  25. def tearDown(self):
  26. self._subject.clear_database()
  27. def test_create_asset_should_work(self):
  28. case_identifier = self._subject.create_dummy_case()
  29. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  30. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body)
  31. self.assertEqual(201, response.status_code)
  32. def test_get_asset_with_missing_asset_identifier_should_return_404(self):
  33. response = self._subject.get(f'/api/v2/asset/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
  34. self.assertEqual(404, response.status_code)
  35. def test_create_asset_with_missing_case_identifier_should_return_404(self):
  36. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  37. response = self._subject.create(f'/api/v2/cases/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}/assets', body)
  38. self.assertEqual(404, response.status_code)
  39. def test_create_asset_in_old_api_with_same_type_and_name_should_return_400(self):
  40. case_identifier = self._subject.create_dummy_case()
  41. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  42. self._subject.create('/case/assets/add', body, {'cid': case_identifier})
  43. response = self._subject.create('/case/assets/add', body, {'cid': case_identifier})
  44. self.assertEqual(400, response.status_code)
  45. def test_create_asset_with_same_type_and_name_should_return_400(self):
  46. case_identifier = self._subject.create_dummy_case()
  47. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  48. self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body)
  49. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body)
  50. self.assertEqual(400, response.status_code)
  51. def test_create_asset_with_asset_compromise_status_id_should_not_fail(self):
  52. case_identifier = self._subject.create_dummy_case()
  53. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test', 'asset_compromise_status_id': 1}
  54. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  55. self.assertEqual(1, response['asset_compromise_status_id'])
  56. def test_get_asset_should_return_200(self):
  57. case_identifier = self._subject.create_dummy_case()
  58. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  59. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  60. asset_identifier = response['asset_id']
  61. response = self._subject.get(f'/api/v2/cases/{case_identifier}/assets/{asset_identifier}')
  62. self.assertEqual(200, response.status_code)
  63. def test_get_asset_should_return_404_after_it_was_deleted(self):
  64. case_identifier = self._subject.create_dummy_case()
  65. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  66. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  67. asset_identifier = response['asset_id']
  68. self._subject.delete(f'/api/v2/cases/{case_identifier}/assets/{asset_identifier}')
  69. response = self._subject.get(f'/api/v2/cases/{case_identifier}/assets/{asset_identifier}')
  70. self.assertEqual(404, response.status_code)
  71. def test_update_asset_should_not_fail(self):
  72. case_identifier = self._subject.create_dummy_case()
  73. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  74. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  75. identifier = response['asset_id']
  76. response = self._subject.update(f'/api/v2/cases/{case_identifier}/assets/{identifier}',
  77. {'asset_type_id': 1, 'asset_name': 'new_asset_name'})
  78. self.assertEqual(200, response.status_code)
  79. def test_update_asset_should_return_correct_asset_uuid(self):
  80. case_identifier = self._subject.create_dummy_case()
  81. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  82. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  83. identifier = response['asset_id']
  84. asset_uuid = response['asset_uuid']
  85. response = self._subject.update(f'/api/v2/cases/{case_identifier}/assets/{identifier}',
  86. {'asset_type_id': 1, 'asset_name': 'new_asset_name'}).json()
  87. self.assertEqual(asset_uuid, response['asset_uuid'])
  88. def test_update_asset_should_return_404_when_asset_not_found(self):
  89. case_identifier = self._subject.create_dummy_case()
  90. response = self._subject.update(f'/api/v2/cases/{case_identifier}/assets/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}',
  91. {'asset_type_id': 1, 'asset_name': 'new_asset_name'})
  92. self.assertEqual(404, response.status_code)
  93. def test_update_asset_should_allow_to_update_analysis_status(self):
  94. case_identifier = self._subject.create_dummy_case()
  95. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  96. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  97. identifier = response['asset_id']
  98. response = self._subject.update(f'/api/v2/cases/{case_identifier}/assets/{identifier}',
  99. {'asset_type_id': 1, 'asset_name': 'admin_laptop_test', 'analysis_status_id': 2}).json()
  100. self.assertEqual(2, response['analysis_status_id'])
  101. def test_update_asset_should_allow_to_update_asset_compromise_status_id(self):
  102. case_identifier = self._subject.create_dummy_case()
  103. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test', 'asset_compromise_status_id': 1}
  104. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  105. identifier = response['asset_id']
  106. response = self._subject.update(f'/api/v2/cases/{case_identifier}/assets/{identifier}',
  107. {'asset_type_id': 1, 'asset_name': 'admin_laptop_test', 'asset_compromise_status_id': 2}).json()
  108. self.assertEqual(2, response['asset_compromise_status_id'])
  109. def test_delete_asset_should_return_204(self):
  110. case_identifier = self._subject.create_dummy_case()
  111. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  112. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  113. asset_identifier = response['asset_id']
  114. response = self._subject.delete(f'/api/v2/cases/{case_identifier}/assets/{asset_identifier}')
  115. self.assertEqual(204, response.status_code)
  116. def test_delete_asset_with_missing_asset_identifier_should_return_404(self):
  117. case_identifier = self._subject.create_dummy_case()
  118. response = self._subject.delete(f'/api/v2/cases/{case_identifier}/assets/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
  119. self.assertEqual(404, response.status_code)
  120. def test_delete_asset_should_increment_asset_state(self):
  121. case_identifier = self._subject.create_dummy_case()
  122. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  123. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  124. asset_identifier = response['asset_id']
  125. response = self._subject.get('/case/assets/state', {'cid': case_identifier}).json()
  126. state = response['data']['object_state']
  127. self._subject.delete(f'/api/v2/cases/{case_identifier}/assets/{asset_identifier}')
  128. response = self._subject.get('/case/assets/state', {'cid': case_identifier}).json()
  129. self.assertEqual(state + 1, response['data']['object_state'])
  130. def test_delete_asset_should_not_fail_when_it_is_linked_to_an_ioc(self):
  131. case_identifier = self._subject.create_dummy_case()
  132. body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
  133. response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
  134. ioc_identifier = response['ioc_id']
  135. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test', 'ioc_links': [ioc_identifier]}
  136. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  137. asset_identifier = response['asset_id']
  138. response = self._subject.delete(f'/api/v2/cases/{case_identifier}/assets/{asset_identifier}')
  139. self.assertEqual(204, response.status_code)
  140. def test_delete_asset_should_not_fail_when_it_has_associated_comments(self):
  141. case_identifier = self._subject.create_dummy_case()
  142. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  143. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  144. asset_identifier = response['asset_id']
  145. self._subject.create(f'/case/assets/{asset_identifier}/comments/add', {'comment_text': 'comment text'})
  146. response = self._subject.delete(f'/api/v2/cases/{case_identifier}/assets/{asset_identifier}')
  147. self.assertEqual(204, response.status_code)
  148. def test_delete_asset_should_delete_associated_comments(self):
  149. case_identifier = self._subject.create_dummy_case()
  150. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  151. response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  152. asset_identifier = response['asset_id']
  153. response = self._subject.create(f'/case/assets/{asset_identifier}/comments/add', {'comment_text': 'comment text'}).json()
  154. comment_identifier = response['data']['comment_id']
  155. self._subject.delete(f'/api/v2/cases/{case_identifier}/assets/{asset_identifier}')
  156. response = self._subject.create(f'/case/assets/{case_identifier}/comments/{comment_identifier}/edit', {'comment_text': 'new comment text'})
  157. # TODO should ideally rather be 404 here
  158. self.assertEqual(400, response.status_code)
  159. def test_user_should_not_change_comment_of_others(self):
  160. user1 = self._subject.create_dummy_user()
  161. user2 = self._subject.create_dummy_user()
  162. # Create a case
  163. case_identifier = self._subject.create_dummy_case()
  164. # Give access to users
  165. body = {
  166. 'cases_list': [case_identifier],
  167. 'access_level': _CASE_ACCESS_LEVEL_FULL_ACCESS
  168. }
  169. self._subject.create(f'/manage/users/{user2.get_identifier()}/cases-access/update', body)
  170. self._subject.create(f'/manage/users/{user1.get_identifier()}/cases-access/update', body)
  171. # Create a new asset
  172. body = {'asset_type_id': 1, 'asset_name': 'admin_laptop_test'}
  173. response = user1.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  174. asset_identifier = response['asset_id']
  175. # Create a comment in the new asset
  176. response = user1.create(f'/case/assets/{asset_identifier}/comments/add?cid={case_identifier}', {'comment_text': 'comment text'}).json()
  177. comment_identifier = response['data']['comment_id']
  178. # Try to update the comment from user 2
  179. response = user2.create(f'/case/assets/{asset_identifier}/comments/{comment_identifier}/edit?cid={case_identifier}', {'comment_text': 'updated comment'})
  180. self.assertEqual(400, response.status_code)
  181. def test_get_assets_should_not_fail(self):
  182. case_identifier = self._subject.create_dummy_case()
  183. response = self._subject.get(f'/api/v2/cases/{case_identifier}/assets')
  184. self.assertEqual(200, response.status_code)
  185. def test_get_assets_should_return_404_when_case_does_not_exist(self):
  186. response = self._subject.get(f'/api/v2/cases/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}/assets')
  187. self.assertEqual(404, response.status_code)
  188. def test_get_assets_should_return_current_page(self):
  189. case_identifier = self._subject.create_dummy_case()
  190. response = self._subject.get(f'/api/v2/cases/{case_identifier}/assets').json()
  191. self.assertEqual(1, response['current_page'])
  192. def test_get_assets_should_return_existing_assets(self):
  193. case_identifier = self._subject.create_dummy_case()
  194. body = {'asset_type_id': 1, 'asset_name': 'asset'}
  195. self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  196. response = self._subject.get(f'/api/v2/cases/{case_identifier}/assets').json()
  197. self.assertEqual(1, len(response['data']))
  198. def test_get_assets_should_accept_per_page_query_parameter(self):
  199. case_identifier = self._subject.create_dummy_case()
  200. body = {'asset_type_id': 1, 'asset_name': 'asset1'}
  201. self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  202. body = {'asset_type_id': 1, 'asset_name': 'asset2'}
  203. self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  204. response = self._subject.get(f'/api/v2/cases/{case_identifier}/assets', { 'per_page': 1 }).json()
  205. self.assertEqual(1, len(response['data']))
  206. def test_get_assets_should_accept_order_by_query_parameter(self):
  207. case_identifier = self._subject.create_dummy_case()
  208. body = {'asset_type_id': 1, 'asset_name': 'asset2'}
  209. self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  210. body = {'asset_type_id': 1, 'asset_name': 'asset1'}
  211. self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
  212. response = self._subject.get(f'/api/v2/cases/{case_identifier}/assets', { 'order_by': 'asset_name' }).json()
  213. self.assertEqual('asset1', response['data'][0]['asset_name'])