Skip to content

Commit

Permalink
[IMP] Added tests_rest_alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
c8y3 committed Oct 2, 2024
1 parent b279aa8 commit b94d3f3
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 95 deletions.
95 changes: 0 additions & 95 deletions tests/tests_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,43 +206,6 @@ def test_get_asset_with_missing_asset_identifier_should_return_404(self):
response = self._subject.get('/api/v2/asset/None')
self.assertEqual(404, response.status_code)

def test_create_alert_should_not_fail(self):
body = {
'alert_title': 'title',
'alert_severity_id': 4,
'alert_status_id': 3,
'alert_customer_id': 1
}
response = self._subject.create('/alerts/add', body)
self.assertEqual(200, response.status_code)

def test_alerts_filter_with_alerts_filter_should_not_fail(self):
response = self._subject.get('/alerts/filter', query_parameters={'alert_assets': 'some assert name'})
self.assertEqual(200, response.status_code)

def test_alerts_filter_with_iocs_filter_should_not_fail(self):
response = self._subject.get('/alerts/filter', query_parameters={'alert_iocs': 'some ioc value'})
self.assertEqual(200, response.status_code)

def test_merge_alert_into_a_case_should_not_fail(self):
case_identifier = self._subject.create_dummy_case()
body = {
'alert_title': 'title',
'alert_severity_id': 4,
'alert_status_id': 3,
'alert_customer_id': 1
}
response = self._subject.create('/alerts/add', body).json()
alert_identifier = response['data']['alert_id']
body = {
'target_case_id': case_identifier,
'iocs_import_list': [],
'assets_import_list': []
}
response = self._subject.create(f'/alerts/merge/{alert_identifier}', body)
# TODO should be 201
self.assertEqual(200, response.status_code)

def test_get_timeline_state_should_return_200(self):
response = self._subject.get('/case/timeline/state', query_parameters={'cid': 1})
self.assertEqual(200, response.status_code)
Expand Down Expand Up @@ -311,61 +274,3 @@ def test_create_case_should_return_data_with_case_customer_when_case_customer_is
}
response = self._subject.create('/api/v2/cases', body).json()
self.assertIn('case_customer', response['data'])

def test_get_iocs_should_not_fail(self):
case_identifier = self._subject.create_dummy_case()
response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs')
self.assertEqual(200, response.status_code)

def test_create_ioc_should_add_the_ioc_in_the_correct_case(self):
case_identifier = self._subject.create_dummy_case()
body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs').json()
self.assertEqual(1, response['total'])

def test_get_iocs_should_filter_and_return_ioc_type_identifier(self):
case_identifier = self._subject.create_dummy_case()
ioc_type_identifier = 2
self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', {
'ioc_type_id': ioc_type_identifier,
'ioc_tlp_id': 2,
'ioc_value': 'test_get_iocs_should_filter_on_ioc_value',
'ioc_description': 'rewrw',
'ioc_tags': '',
'custom_attributes': {}
}).json()
self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', {
'ioc_type_id': 1,
'ioc_tlp_id': 2,
'ioc_value': 'wrong_test',
'ioc_description': 'rewrw',
'ioc_tags': '',
'custom_attributes': {}
}).json()
filters = {'ioc_value': 'test_get_iocs_should_filter_on_ioc_value'}
response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs', query_parameters=filters).json()
identifiers = []
for ioc in response['iocs']:
identifiers.append(ioc['ioc_type_id'])
self.assertIn(ioc_type_identifier, identifiers)

def test_get_ioc_should_return_404_when_not_present(self):
response = self._subject.get(f'/api/v2/iocs/137')
self.assertEqual(404, response.status_code)

def test_get_ioc_should_return_200_on_success(self):
case_identifier = self._subject.create_dummy_case()
body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
ioc_identifier = response['ioc_id']
response = self._subject.get(f'/api/v2/iocs/{ioc_identifier}')
self.assertEqual(200, response.status_code)

def test_get_asset_should_return_200(self):
case_identifier = self._subject.create_dummy_case()
body = {'asset_type_id': '1', 'asset_name': 'admin_laptop_test'}
response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
asset_identifier = response['asset_id']
response = self._subject.get(f'/api/v2/assets/{asset_identifier}')
self.assertEqual(200, response.status_code)
66 changes: 66 additions & 0 deletions tests/tests_rest_alerts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# IRIS Source Code
# Copyright (C) 2023 - DFIR-IRIS
# [email protected]
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from unittest import TestCase
from iris import Iris


class TestsRestAlerts(TestCase):

def setUp(self) -> None:
self._subject = Iris()

def tearDown(self):
self._subject.clear_database()

def test_create_alert_should_not_fail(self):
body = {
'alert_title': 'title',
'alert_severity_id': 4,
'alert_status_id': 3,
'alert_customer_id': 1
}
response = self._subject.create('/alerts/add', body)
self.assertEqual(200, response.status_code)

def test_alerts_filter_with_alerts_filter_should_not_fail(self):
response = self._subject.get('/alerts/filter', query_parameters={'alert_assets': 'some assert name'})
self.assertEqual(200, response.status_code)

def test_alerts_filter_with_iocs_filter_should_not_fail(self):
response = self._subject.get('/alerts/filter', query_parameters={'alert_iocs': 'some ioc value'})
self.assertEqual(200, response.status_code)

def test_merge_alert_into_a_case_should_not_fail(self):
case_identifier = self._subject.create_dummy_case()
body = {
'alert_title': 'title',
'alert_severity_id': 4,
'alert_status_id': 3,
'alert_customer_id': 1
}
response = self._subject.create('/alerts/add', body).json()
alert_identifier = response['data']['alert_id']
body = {
'target_case_id': case_identifier,
'iocs_import_list': [],
'assets_import_list': []
}
response = self._subject.create(f'/alerts/merge/{alert_identifier}', body)
# TODO should be 201
self.assertEqual(200, response.status_code)
8 changes: 8 additions & 0 deletions tests/tests_rest_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ def test_create_asset_with_same_type_and_name_should_return_400(self):
self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body)
response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body)
self.assertEqual(400, response.status_code)

def test_get_asset_should_return_200(self):
case_identifier = self._subject.create_dummy_case()
body = {'asset_type_id': '1', 'asset_name': 'admin_laptop_test'}
response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body).json()
asset_identifier = response['asset_id']
response = self._subject.get(f'/api/v2/assets/{asset_identifier}')
self.assertEqual(200, response.status_code)
50 changes: 50 additions & 0 deletions tests/tests_rest_iocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,56 @@ def setUp(self) -> None:
def tearDown(self):
self._subject.clear_database()

def test_get_iocs_should_not_fail(self):
case_identifier = self._subject.create_dummy_case()
response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs')
self.assertEqual(200, response.status_code)

def test_create_ioc_should_add_the_ioc_in_the_correct_case(self):
case_identifier = self._subject.create_dummy_case()
body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs').json()
self.assertEqual(1, response['total'])

def test_get_iocs_should_filter_and_return_ioc_type_identifier(self):
case_identifier = self._subject.create_dummy_case()
ioc_type_identifier = 2
self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', {
'ioc_type_id': ioc_type_identifier,
'ioc_tlp_id': 2,
'ioc_value': 'test_get_iocs_should_filter_on_ioc_value',
'ioc_description': 'rewrw',
'ioc_tags': '',
'custom_attributes': {}
}).json()
self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', {
'ioc_type_id': 1,
'ioc_tlp_id': 2,
'ioc_value': 'wrong_test',
'ioc_description': 'rewrw',
'ioc_tags': '',
'custom_attributes': {}
}).json()
filters = {'ioc_value': 'test_get_iocs_should_filter_on_ioc_value'}
response = self._subject.get(f'/api/v2/cases/{case_identifier}/iocs', query_parameters=filters).json()
identifiers = []
for ioc in response['iocs']:
identifiers.append(ioc['ioc_type_id'])
self.assertIn(ioc_type_identifier, identifiers)

def test_get_ioc_should_return_404_when_not_present(self):
response = self._subject.get(f'/api/v2/iocs/137')
self.assertEqual(404, response.status_code)

def test_get_ioc_should_return_200_on_success(self):
case_identifier = self._subject.create_dummy_case()
body = {'ioc_type_id': 1, 'ioc_tlp_id': 2, 'ioc_value': '8.8.8.8', 'ioc_description': 'rewrw', 'ioc_tags': ''}
response = self._subject.create(f'/api/v2/cases/{case_identifier}/iocs', body).json()
ioc_identifier = response['ioc_id']
response = self._subject.get(f'/api/v2/iocs/{ioc_identifier}')
self.assertEqual(200, response.status_code)

def test_get_iocs_should_include_tlp_information(self):
case_identifier = self._subject.create_dummy_case()
tlp_identifier = 2
Expand Down

0 comments on commit b94d3f3

Please sign in to comment.