Skip to content

Commit

Permalink
[IMP] Added tests_rest_cases
Browse files Browse the repository at this point in the history
  • Loading branch information
c8y3 committed Oct 2, 2024
1 parent b94d3f3 commit af7e098
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 68 deletions.
68 changes: 0 additions & 68 deletions tests/tests_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
_IDENTIFIER_FOR_NONEXISTENT_OBJECT = None


def _get_case_with_identifier(response, identifier):
for case in response['cases']:
if identifier == case['case_id']:
return case
raise ValueError('Case not found')


class TestsRest(TestCase):

def setUp(self) -> None:
Expand Down Expand Up @@ -197,11 +190,6 @@ def test_create_asset_should_work(self):
response = self._subject.create(f'/api/v2/cases/{case_identifier}/assets', body)
self.assertEqual(201, 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)
self.assertEqual(404, 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)
Expand All @@ -210,35 +198,6 @@ 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_cases_should_not_fail(self):
response = self._subject.get('/api/v2/cases')
self.assertEqual(200, response.status_code)

def test_get_cases_should_filter_on_case_name(self):
response = self._subject.create('/api/v2/cases', {
'case_name': 'test_get_cases_should_filter_on_case_name',
'case_description': 'description',
'case_customer': 1,
'case_soc_id': ''
}).json()
case_identifier = response['case_id']
filters = {'case_name': 'test_get_cases_should_filter_on_case_name'}
response = self._subject.get('/api/v2/cases', query_parameters=filters).json()
identifiers = []
for case in response['cases']:
identifiers.append(case['case_id'])
self.assertIn(case_identifier, identifiers)

def test_get_cases_should_filter_on_is_open(self):
case_identifier = self._subject.create_dummy_case()
self._subject.create(f'/manage/cases/close/{case_identifier}', {})
filters = {'is_open': 'true'}
response = self._subject.get('/api/v2/cases', query_parameters=filters).json()
identifiers = []
for case in response['cases']:
identifiers.append(case['case_id'])
self.assertNotIn(case_identifier, identifiers)

def test_get_users_should_return_200(self):
response = self._subject.get('/manage/users/list')
self.assertEqual(200, response.status_code)
Expand All @@ -247,30 +206,3 @@ 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)

def test_get_cases_should_return_the_state_name(self):
case_identifier = self._subject.create_dummy_case()
response = self._subject.get('/api/v2/cases').json()
case = _get_case_with_identifier(response, case_identifier)
self.assertEqual('Open', case['state']['state_name'])

def test_get_cases_should_return_the_owner_name(self):
case_identifier = self._subject.create_dummy_case()
response = self._subject.get('/api/v2/cases').json()
case = _get_case_with_identifier(response, case_identifier)
self.assertEqual('administrator', case['owner']['user_name'])

def test_get_case_should_have_field_case_name(self):
case_identifier = self._subject.create_dummy_case()
response = self._subject.get(f'/api/v2/cases/{case_identifier}').json()
self.assertIn('case_name', response)

def test_create_case_should_return_data_with_case_customer_when_case_customer_is_an_empty_string(self):
body = {
'case_name': 'case name',
'case_description': 'description',
'case_customer': '',
'case_soc_id': ''
}
response = self._subject.create('/api/v2/cases', body).json()
self.assertIn('case_customer', response['data'])
8 changes: 8 additions & 0 deletions tests/tests_rest_assets.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 TestsRestAssets(TestCase):

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

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

def test_create_asset_in_old_api_with_same_type_and_name_should_return_400(self):
case_identifier = self._subject.create_dummy_case()
body = {'asset_type_id': '1', 'asset_name': 'admin_laptop_test'}
Expand Down
92 changes: 92 additions & 0 deletions tests/tests_rest_cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# IRIS Source Code
# Copyright (C) 2023 - DFIR-IRIS
# contact@dfir-iris.org
#
# 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


def _get_case_with_identifier(response, identifier):
for case in response['cases']:
if identifier == case['case_id']:
return case
raise ValueError('Case not found')


class TestsRestCases(TestCase):

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

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

def test_get_cases_should_not_fail(self):
response = self._subject.get('/api/v2/cases')
self.assertEqual(200, response.status_code)

def test_get_cases_should_filter_on_case_name(self):
response = self._subject.create('/api/v2/cases', {
'case_name': 'test_get_cases_should_filter_on_case_name',
'case_description': 'description',
'case_customer': 1,
'case_soc_id': ''
}).json()
case_identifier = response['case_id']
filters = {'case_name': 'test_get_cases_should_filter_on_case_name'}
response = self._subject.get('/api/v2/cases', query_parameters=filters).json()
identifiers = []
for case in response['cases']:
identifiers.append(case['case_id'])
self.assertIn(case_identifier, identifiers)

def test_get_cases_should_filter_on_is_open(self):
case_identifier = self._subject.create_dummy_case()
self._subject.create(f'/manage/cases/close/{case_identifier}', {})
filters = {'is_open': 'true'}
response = self._subject.get('/api/v2/cases', query_parameters=filters).json()
identifiers = []
for case in response['cases']:
identifiers.append(case['case_id'])
self.assertNotIn(case_identifier, identifiers)

def test_get_cases_should_return_the_state_name(self):
case_identifier = self._subject.create_dummy_case()
response = self._subject.get('/api/v2/cases').json()
case = _get_case_with_identifier(response, case_identifier)
self.assertEqual('Open', case['state']['state_name'])

def test_get_cases_should_return_the_owner_name(self):
case_identifier = self._subject.create_dummy_case()
response = self._subject.get('/api/v2/cases').json()
case = _get_case_with_identifier(response, case_identifier)
self.assertEqual('administrator', case['owner']['user_name'])

def test_get_case_should_have_field_case_name(self):
case_identifier = self._subject.create_dummy_case()
response = self._subject.get(f'/api/v2/cases/{case_identifier}').json()
self.assertIn('case_name', response)

def test_create_case_should_return_data_with_case_customer_when_case_customer_is_an_empty_string(self):
body = {
'case_name': 'case name',
'case_description': 'description',
'case_customer': '',
'case_soc_id': ''
}
response = self._subject.create('/api/v2/cases', body).json()
self.assertIn('case_customer', response['data'])

0 comments on commit af7e098

Please sign in to comment.