Skip to content

Commit

Permalink
[IMP] Added tests_rest_users
Browse files Browse the repository at this point in the history
  • Loading branch information
c8y3 committed Oct 2, 2024
1 parent 1fe9761 commit b1d5467
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 65 deletions.
65 changes: 0 additions & 65 deletions tests/tests_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,71 +138,6 @@ def test_update_settings_should_not_fail(self):
response = self._subject.create('/manage/settings/update', body)
self.assertEqual(200, response.status_code)

def test_create_ioc_should_return_good_ioc_type_id(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()
self.assertEqual(1, response['ioc_type_id'])

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

def test_get_ioc_with_missing_ioc_identifier_should_return_error(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()
test = self._subject.get('/api/v2/iocs/None').json()
self.assertEqual('error', test['status'])

def test_delete_ioc_should_return_204(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.delete(f'/api/v2/iocs/{ioc_identifier}')
self.assertEqual(204, response.status_code)

def test_delete_ioc_with_missing_ioc_identifier_should_return_404(self):
response = self._subject.delete(f'/api/v2/iocs/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
self.assertEqual(404, response.status_code)

def test_delete_asset_should_return_204(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.delete(f'/api/v2/assets/{asset_identifier}')
self.assertEqual(204, response.status_code)

def test_delete_asset_with_missing_asset_identifier_should_return_404(self):
response = self._subject.delete(f'/api/v2/assets/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
self.assertEqual(404, response.status_code)

def test_create_asset_should_work(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)
self.assertEqual(201, response.status_code)

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_get_timeline_state_should_return_200(self):
response = self._subject.get('/case/timeline/state', query_parameters={'cid': 1})
self.assertEqual(200, response.status_code)

def test_get_users_should_return_200(self):
response = self._subject.get('/manage/users/list')
self.assertEqual(200, response.status_code)

def test_get_users_should_return_403_for_user_without_rights(self):
user = self._subject.create_dummy_user()
response = user.get('/manage/users/list')
self.assertEqual(403, response.status_code)
23 changes: 23 additions & 0 deletions tests/tests_rest_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ def setUp(self) -> None:
def tearDown(self):
self._subject.clear_database()


def test_delete_asset_should_return_204(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.delete(f'/api/v2/assets/{asset_identifier}')
self.assertEqual(204, response.status_code)

def test_delete_asset_with_missing_asset_identifier_should_return_404(self):
response = self._subject.delete(f'/api/v2/assets/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
self.assertEqual(404, response.status_code)

def test_create_asset_should_work(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)
self.assertEqual(201, response.status_code)

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_asset_with_missing_case_identifier_should_return_404(self):
body = {'asset_type_id': '1', 'asset_name': 'admin_laptop_test'}
response = self._subject.create(f'/api/v2/cases/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}/assets', body)
Expand Down
37 changes: 37 additions & 0 deletions tests/tests_rest_iocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
from unittest import TestCase
from iris import Iris

# TODO should change None into 123456789 and maybe fix...
_IDENTIFIER_FOR_NONEXISTENT_OBJECT = None


class TestsRestIocs(TestCase):

Expand All @@ -28,6 +31,40 @@ def setUp(self) -> None:
def tearDown(self):
self._subject.clear_database()

def test_create_ioc_should_return_good_ioc_type_id(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()
self.assertEqual(1, response['ioc_type_id'])

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

def test_get_ioc_with_missing_ioc_identifier_should_return_error(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()
test = self._subject.get('/api/v2/iocs/None').json()
self.assertEqual('error', test['status'])

def test_delete_ioc_should_return_204(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.delete(f'/api/v2/iocs/{ioc_identifier}')
self.assertEqual(204, response.status_code)

def test_delete_ioc_with_missing_ioc_identifier_should_return_404(self):
response = self._subject.delete(f'/api/v2/iocs/{_IDENTIFIER_FOR_NONEXISTENT_OBJECT}')
self.assertEqual(404, response.status_code)

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')
Expand Down
38 changes: 38 additions & 0 deletions tests/tests_rest_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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 TestsRestUsers(TestCase):

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

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

def test_get_users_should_return_200(self):
response = self._subject.get('/manage/users/list')
self.assertEqual(200, response.status_code)

def test_get_users_should_return_403_for_user_without_rights(self):
user = self._subject.create_dummy_user()
response = user.get('/manage/users/list')
self.assertEqual(403, response.status_code)

0 comments on commit b1d5467

Please sign in to comment.