This commit is contained in:
0
iris-web/source/tests/__init__.py
Normal file
0
iris-web/source/tests/__init__.py
Normal file
36
iris-web/source/tests/clean_database.py
Normal file
36
iris-web/source/tests/clean_database.py
Normal file
@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# IRIS Source Code
|
||||
# Copyright (C) 2021 - Airbus CyberSecurity (SAS)
|
||||
# ir@cyberactionlab.net
|
||||
#
|
||||
# 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.
|
||||
|
||||
|
||||
import logging
|
||||
|
||||
from app import db
|
||||
|
||||
|
||||
def clean_db():
|
||||
db.session.commit()
|
||||
db.drop_all()
|
||||
db.create_all()
|
||||
|
||||
logging.info("CleanUp completed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
clean_db()
|
0
iris-web/source/tests/performance/__init__.py
Normal file
0
iris-web/source/tests/performance/__init__.py
Normal file
186
iris-web/source/tests/performance/test_burst_db_interaction.py
Normal file
186
iris-web/source/tests/performance/test_burst_db_interaction.py
Normal file
@ -0,0 +1,186 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# IRIS Source Code
|
||||
# Copyright (C) 2021 - Airbus CyberSecurity (SAS)
|
||||
# ir@cyberactionlab.net
|
||||
#
|
||||
# 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
|
||||
|
||||
import logging
|
||||
import random
|
||||
from datetime import datetime
|
||||
from datetime import timedelta
|
||||
|
||||
from app import db
|
||||
from app.datamgmt.case.case_assets_db import create_asset
|
||||
from app.datamgmt.case.case_notes_db import add_note
|
||||
from app.datamgmt.case.case_notes_db import add_note_group
|
||||
from app.datamgmt.manage.manage_users_db import create_user
|
||||
from app.models.cases import Cases
|
||||
from app.models.cases import CasesEvent
|
||||
from app.models.cases import Client
|
||||
from app.models.models import CaseEventsAssets
|
||||
from app.models.authorization import User
|
||||
from app.post_init import run_post_init
|
||||
from tests.clean_database import clean_db
|
||||
|
||||
|
||||
class TestBurstDBInteraction(TestCase):
|
||||
def setUp(self) -> None:
|
||||
logging.info('SetUp called')
|
||||
clean_db()
|
||||
run_post_init()
|
||||
|
||||
def tearDown(self) -> None:
|
||||
logging.info('Teardown called')
|
||||
clean_db()
|
||||
|
||||
@staticmethod
|
||||
def _create_burst_users(random_nb: int):
|
||||
user_name = "User "
|
||||
user_login = "user_"
|
||||
user_password = "user_"
|
||||
user_email = "user_"
|
||||
for i in range(0, random_nb):
|
||||
create_user(
|
||||
user_name=f"{user_name}{str(i)}",
|
||||
user_login=f"{user_login}{str(i)}",
|
||||
user_password=f"{user_password}{str(i)}",
|
||||
user_email=f"{user_email}{str(i)}",
|
||||
user_isadmin=(i % 2 == 0)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _create_burst_clients(clients_nb: int):
|
||||
for i in range(clients_nb):
|
||||
client = Client(f"client_{str(i)}")
|
||||
db.session.add(client)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
@staticmethod
|
||||
def _create_burst_cases(users_nb: int, client_nb: int, cases_nb: int):
|
||||
for i in range(cases_nb):
|
||||
logging.info(f"Creating case #{str(i)}")
|
||||
asset_l = []
|
||||
|
||||
case = Cases(
|
||||
name=f"Test {str(i)}",
|
||||
description=f"Testing case number {str(i)}",
|
||||
soc_id=f"SOC{str(i)}",
|
||||
gen_report=False,
|
||||
user=(User.query.filter(User.id == random.randrange(1, users_nb)).first()),
|
||||
client_name=f"client_{str(random.randrange(1, client_nb))}"
|
||||
)
|
||||
|
||||
case.validate_on_build()
|
||||
case.save()
|
||||
|
||||
for ii in range(random.randrange(5, 10)):
|
||||
ng = add_note_group(
|
||||
group_title=f"Group #{str(ii)}",
|
||||
caseid=case.case_id,
|
||||
userid=random.randrange(1, users_nb),
|
||||
creationdate=datetime.utcnow()
|
||||
)
|
||||
|
||||
for iii in range(random.randrange(2, 8)):
|
||||
add_note(
|
||||
note_title=f"Note #{str(ii)}",
|
||||
creation_date=datetime.utcnow(),
|
||||
user_id=random.randrange(1, users_nb),
|
||||
caseid=case.case_id,
|
||||
group_id=ng.group_id
|
||||
)
|
||||
|
||||
for ii in range(random.randrange(6, 140)):
|
||||
asset = create_asset(
|
||||
asset_name=f"asset_{str(ii)}",
|
||||
asset_description=f"My asset {str(i)}",
|
||||
asset_ip='',
|
||||
asset_info='',
|
||||
asset_compromised=(ii % 2 == 0),
|
||||
asset_type=random.randrange(1, 19),
|
||||
asset_domain='',
|
||||
date_added=datetime.utcnow(),
|
||||
date_update=datetime.utcnow(),
|
||||
caseid=case.case_id,
|
||||
user_id=random.randrange(1, users_nb),
|
||||
analysis_status=random.randrange(1, 5)
|
||||
)
|
||||
|
||||
asset_l.append(asset.asset_id)
|
||||
|
||||
for ii in range(random.randrange(10, 350)):
|
||||
event = CasesEvent()
|
||||
event.case_id = case.case_id
|
||||
event.user_id = random.randrange(1, users_nb)
|
||||
event.event_raw = ""
|
||||
event.event_content = f"My event content @{str(ii)}"
|
||||
event.event_title = f"My event title @{str(ii)}"
|
||||
event.event_tags = ''
|
||||
event.event_color = ''
|
||||
event.event_date = datetime.utcnow()
|
||||
event.event_added = datetime.utcnow()
|
||||
|
||||
db.session.add(event)
|
||||
db.session.commit()
|
||||
for iii in range(random.randrange(0, 5)):
|
||||
cea = CaseEventsAssets()
|
||||
cea.asset_id = asset_l[random.randrange(len(asset_l))]
|
||||
cea.event_id = event.event_id
|
||||
cea.case_id = case.case_id
|
||||
|
||||
db.session.add(cea)
|
||||
|
||||
db.session.commit()
|
||||
|
||||
@staticmethod
|
||||
def random_date(start, end):
|
||||
delta = end - start
|
||||
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
|
||||
random_second = random.randrange(int_delta)
|
||||
return start + timedelta(seconds=random_second)
|
||||
|
||||
@staticmethod
|
||||
def update_dates():
|
||||
d1 = datetime.strptime('1/1/2008 1:30 PM', '%m/%d/%Y %I:%M %p')
|
||||
d2 = datetime.strptime('12/12/2021 4:50 AM', '%m/%d/%Y %I:%M %p')
|
||||
events = CasesEvent.query.all()
|
||||
for event in events:
|
||||
event.event_date = datetime.utcnow()
|
||||
logging.info(f"Updating event {event.event_title}")
|
||||
db.session.commit()
|
||||
|
||||
def test_burst_creation(self):
|
||||
start_time = datetime.utcnow()
|
||||
logging.info(f"Test started at: {start_time.__str__()}")
|
||||
|
||||
logging.info('Creating random users')
|
||||
self._create_burst_users(154)
|
||||
|
||||
logging.info('Creating random clients')
|
||||
self._create_burst_clients(68)
|
||||
|
||||
logging.info('Creating random cases')
|
||||
self._create_burst_cases(154, 68, 1489)
|
||||
|
||||
end_time = datetime.utcnow()
|
||||
logging.info(f"Test ended at: {end_time.__str__()}")
|
||||
logging.info(f"Elapsed time: {(end_time - start_time).__str__()}")
|
||||
|
63
iris-web/source/tests/test_helper.py
Normal file
63
iris-web/source/tests/test_helper.py
Normal file
@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# IRIS Source Code
|
||||
# Copyright (C) 2021 - Airbus CyberSecurity (SAS)
|
||||
# ir@cyberactionlab.net
|
||||
#
|
||||
# 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 os import environ
|
||||
from unittest import TestCase
|
||||
|
||||
import re
|
||||
from flask import url_for
|
||||
from flask.testing import FlaskClient
|
||||
from random import randrange
|
||||
|
||||
from app import app
|
||||
from app.datamgmt.client.client_db import create_client
|
||||
from app.models import Client
|
||||
|
||||
|
||||
class TestHelper(TestCase):
|
||||
@staticmethod
|
||||
def log_in(test_app: FlaskClient) -> None:
|
||||
login_page = test_app.get('/login')
|
||||
|
||||
csrf_token = re.search(r'id="csrf_token" name="csrf_token" type="hidden" value="(.*?)"', str(login_page.data)).group(1)
|
||||
|
||||
test_app.post('/login', data=dict(username='administrator', password=environ.get("IRIS_ADM_PASSWORD", ""), csrf_token=csrf_token), follow_redirects=True)
|
||||
|
||||
def verify_path_without_cid_redirects_correctly(self, path: str, assert_string: str):
|
||||
with app.test_client() as test_app:
|
||||
self.log_in(test_app)
|
||||
|
||||
result = test_app.get(url_for(path))
|
||||
|
||||
self.assertEqual(302, result.status_code)
|
||||
self.assertIn(assert_string, str(result.data))
|
||||
|
||||
result2 = test_app.get(url_for(path), follow_redirects=True)
|
||||
|
||||
self.assertEqual(200, result2.status_code)
|
||||
|
||||
@staticmethod
|
||||
def create_client(client_name: str = None) -> Client:
|
||||
client_name = client_name if client_name is not None else f"client_name_{randrange(1,10000)}"
|
||||
|
||||
new_client = create_client(client_name)
|
||||
|
||||
return new_client
|
0
iris-web/source/tests/unit/__init__.py
Normal file
0
iris-web/source/tests/unit/__init__.py
Normal file
0
iris-web/source/tests/unit/blueprints/__init__.py
Normal file
0
iris-web/source/tests/unit/blueprints/__init__.py
Normal file
@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# IRIS Source Code
|
||||
# Copyright (C) 2021 - Airbus CyberSecurity (SAS)
|
||||
# ir@cyberactionlab.net
|
||||
#
|
||||
# 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 app import app
|
||||
from tests.test_helper import TestHelper
|
||||
|
||||
app.testing = True
|
||||
|
||||
|
||||
class TestCaseRfilesRoutes(TestCase):
|
||||
def setUp(self) -> None:
|
||||
self._test_helper = TestHelper()
|
||||
|
||||
def test_case_get_case_rfiles_should_redirect_to_cid_1_if_no_cid_is_provided(self):
|
||||
self._test_helper.verify_path_without_cid_redirects_correctly(
|
||||
'case_rfiles.case_rfile',
|
||||
'You should be redirected automatically to target URL: <a href="/case/evidences?cid=1">/case/evidences?cid=1</a>'
|
||||
)
|
@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# IRIS Source Code
|
||||
# Copyright (C) 2021 - Airbus CyberSecurity (SAS)
|
||||
# ir@cyberactionlab.net
|
||||
#
|
||||
# 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 app import app
|
||||
from tests.test_helper import TestHelper
|
||||
|
||||
app.testing = True
|
||||
|
||||
|
||||
class TestCaseTasksRoutes(TestCase):
|
||||
def setUp(self) -> None:
|
||||
self._test_helper = TestHelper()
|
||||
|
||||
def test_case_get_tasks_should_redirect_to_cid_1_if_no_cid_is_provided(self):
|
||||
self._test_helper.verify_path_without_cid_redirects_correctly(
|
||||
'case_tasks.case_tasks',
|
||||
'You should be redirected automatically to target URL: <a href="/case/tasks?cid=1">/case/tasks?cid=1</a>'
|
||||
)
|
@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# IRIS Source Code
|
||||
# Copyright (C) 2021 - Airbus CyberSecurity (SAS)
|
||||
# ir@cyberactionlab.net
|
||||
#
|
||||
# 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 app import app
|
||||
from tests.test_helper import TestHelper
|
||||
|
||||
app.testing = True
|
||||
|
||||
|
||||
class TestManageModulesRoutes(TestCase):
|
||||
def setUp(self) -> None:
|
||||
self._test_helper = TestHelper()
|
||||
|
||||
def test_case_get_manage_modules_index_should_redirect_to_cid_1_if_no_cid_is_provided(self):
|
||||
self._test_helper.verify_path_without_cid_redirects_correctly(
|
||||
'manage_module.manage_modules_index',
|
||||
'You should be redirected automatically to target URL: <a href="/manage/modules?cid=1">/manage/modules?cid=1</a>'
|
||||
)
|
0
iris-web/source/tests/unit/datamgmt/__init__.py
Normal file
0
iris-web/source/tests/unit/datamgmt/__init__.py
Normal file
117
iris-web/source/tests/unit/datamgmt/client/test_client_db.py
Normal file
117
iris-web/source/tests/unit/datamgmt/client/test_client_db.py
Normal file
@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env python3
|
||||
#
|
||||
# IRIS Source Code
|
||||
# Copyright (C) 2021 - Airbus CyberSecurity (SAS)
|
||||
# ir@cyberactionlab.net
|
||||
#
|
||||
# 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 app.datamgmt.client.client_db import get_client, get_client_list, update_client, delete_client
|
||||
from app.datamgmt.exceptions.ElementExceptions import ElementNotFoundException
|
||||
from tests.clean_database import clean_db
|
||||
from tests.test_helper import TestHelper
|
||||
|
||||
|
||||
class TestClientDB(TestCase):
|
||||
def setUp(self) -> None:
|
||||
self._test_helper = TestHelper()
|
||||
clean_db()
|
||||
|
||||
def tearDown(self) -> None:
|
||||
clean_db()
|
||||
|
||||
# CREATE CLIENT
|
||||
def test_create_client_should_return_client_object(self):
|
||||
new_client = self._test_helper.create_client("new_client_name")
|
||||
|
||||
self.assertIsNotNone(new_client)
|
||||
self.assertEqual("new_client_name", new_client.name)
|
||||
|
||||
# GET CLIENT
|
||||
def test_get_client_should_return_client_object(self):
|
||||
# Create 2 clients
|
||||
client1 = self._test_helper.create_client()
|
||||
self._test_helper.create_client()
|
||||
|
||||
# Get client1
|
||||
returned_client = get_client(client1.client_id)
|
||||
|
||||
self.assertIsNotNone(returned_client)
|
||||
self.assertEqual(returned_client.client_id, client1.client_id)
|
||||
|
||||
# GET CLIENT LIST
|
||||
def test_get_client_list_should_return_list_of_client_object(self):
|
||||
# Create 3 clients
|
||||
client1 = self._test_helper.create_client()
|
||||
client2 = self._test_helper.create_client()
|
||||
client3 = self._test_helper.create_client()
|
||||
|
||||
# Get client list
|
||||
returned_client_list = get_client_list()
|
||||
|
||||
self.assertEqual(3, len(returned_client_list))
|
||||
returned_client_id_list = [el['client_id'] for el in returned_client_list]
|
||||
self.assertTrue(client1.client_id in returned_client_id_list)
|
||||
self.assertTrue(client2.client_id in returned_client_id_list)
|
||||
self.assertTrue(client3.client_id in returned_client_id_list)
|
||||
|
||||
def test_get_client_list_should_return_list_of_client_object_for_api(self):
|
||||
# Create 3 clients
|
||||
client1 = self._test_helper.create_client()
|
||||
client2 = self._test_helper.create_client()
|
||||
client3 = self._test_helper.create_client()
|
||||
|
||||
# Get client list
|
||||
returned_client_list = get_client_list(True)
|
||||
|
||||
self.assertEqual(3, len(returned_client_list))
|
||||
returned_client_id_list = [client_id for _, client_id in returned_client_list]
|
||||
self.assertTrue(client1.client_id in returned_client_id_list)
|
||||
self.assertTrue(client2.client_id in returned_client_id_list)
|
||||
self.assertTrue(client3.client_id in returned_client_id_list)
|
||||
|
||||
# UPDATE CLIENT
|
||||
def test_update_client_should_correctly_modify_client(self):
|
||||
client1 = self._test_helper.create_client()
|
||||
|
||||
new_name = 'updated name'
|
||||
update_client(client1.client_id, new_name)
|
||||
|
||||
returned_client = get_client(client1.client_id)
|
||||
|
||||
self.assertEqual(returned_client.name, new_name)
|
||||
|
||||
def test_update_client_should_raise_error_if_client_id_not_found(self):
|
||||
with self.assertRaises(ElementNotFoundException):
|
||||
update_client(0, 'new_name')
|
||||
|
||||
# DELETE CLIENT
|
||||
def test_delete_client_should_correctly_remove_client(self):
|
||||
client1 = self._test_helper.create_client()
|
||||
client2 = self._test_helper.create_client()
|
||||
|
||||
delete_client(client1.client_id)
|
||||
|
||||
client_list = get_client_list()
|
||||
|
||||
self.assertEqual(1, len(client_list))
|
||||
self.assertEqual(client2.client_id, client_list[0]['client_id'])
|
||||
|
||||
def test_delete_client_should_raise_error_if_client_id_not_found(self):
|
||||
with self.assertRaises(ElementNotFoundException):
|
||||
delete_client(0)
|
Reference in New Issue
Block a user