Skip to content

Commit 28dc4aa

Browse files
committed
Fixes tests folder formatting
1 parent 2a244a3 commit 28dc4aa

File tree

11 files changed

+130
-108
lines changed

11 files changed

+130
-108
lines changed

.github/workflows/pipeline.yml

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ jobs:
3535
run: |
3636
pip install flake8
3737
flake8 deltascan/
38+
flake8 tests/

deltascan/core/deltascan.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ def __find_removed(self, changed_scan, old_scan):
652652
else:
653653
diffs[key] = "_"
654654
return diffs
655-
655+
656656
# ------------------------------------------------------------- DIFFS END ------------------------------------------------------------- #
657657

658658
def view(self):

deltascan/core/parser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def extract_port_scan_dict_results(cls, results):
9191
Exception: If an error occurs during the scan parser.
9292
9393
"""
94-
# TODO: We can add here as many fields as we want!
94+
# TODO: We can add here as many fields as we want!
9595
try:
9696
scan_results = []
9797
for host in results.hosts:
@@ -108,7 +108,7 @@ def extract_port_scan_dict_results(cls, results):
108108
"servicefp": "none" if isinstance(s.servicefp, str) and s.servicefp == "" else s.servicefp,
109109
"service_product": "none" if isinstance(s.banner, str) and s.banner == "" else s.banner,
110110
})
111-
111+
112112
scan["os"] = {}
113113
try:
114114
for _idx, _match in enumerate(host._extras["os"]["osmatches"][:3]):

tests/conftest.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"datefmt": "%Y-%m-%d %H:%M:%S",
3232
}
3333

34+
3435
@dataclass
3536
class Config:
3637
is_interactive: bool
@@ -51,24 +52,29 @@ class Config:
5152
port_type: str
5253
host: str
5354

55+
5456
conf_module.CONFIG_FILE_PATH = f"{TEST_DATA}/config.yaml"
5557
conf_module.APP_DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
5658
conf_module.Config = Config
5759

60+
5861
sys.modules['deltascan.core.config'] = conf_module
5962

63+
6064
# Add here whatever you need to execute before the tests
6165
def init():
6266
if os.path.exists(DATABASE_PATH):
6367
os.remove(DATABASE_PATH)
64-
68+
69+
6570
# Run cleanup actions
6671
def cleanup():
6772
if os.path.exists(DATABASE_PATH):
6873
os.remove(DATABASE_PATH)
6974

75+
7076
@pytest.fixture(autouse=True, scope="session")
7177
def session_mgmt():
7278
init()
7379
yield
74-
cleanup()
80+
cleanup()

tests/unit/test_data/mock_data.py

+34-33
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,80 @@
33
from dotmap import DotMap
44

55
SCANS_FROM_DB_TEST_V1 = [
6-
{"id": 1, "uuid": "uuid_1", "host_subnet": "0.0.0.0/24", "host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv" ,"results": {
6+
{"id": 1, "uuid": "uuid_1", "host_subnet": "0.0.0.0/24", "host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
77
"host": "0.0.0.0", "status": "up", "last_boot": "none", "os": ["none"], "osfingerprint": "none", "hops": [
88
{"ipaddr": "10.0.0.0", "host": "host_1"},
9-
{"ipaddr": "10.0.0.1", "host": "host_2"}],"ports": [
9+
{"ipaddr": "10.0.0.1", "host": "host_2"}], "ports": [
1010
{"portid": "80", "proto": "tcp", "state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
11-
{"portid": "22", "proto": "tcp","state": {"state": "closed"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
12-
{"portid": "443", "proto": "tcp","state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
11+
{"portid": "22", "proto": "tcp", "state": {"state": "closed"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
12+
{"portid": "443", "proto": "tcp", "state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
1313
]
1414
}, "result_hash": "d41d8cd98f00b204e9800998ecf8427e", "created_at": "2021-01-01 00:00:00"},
15-
{"id": 2, "uuid": "uuid_2", "host_subnet": "0.0.0.0/24","host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
15+
{"id": 2, "uuid": "uuid_2", "host_subnet": "0.0.0.0/24", "host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
1616
"host": "0.0.0.0", "status": "up", "last_boot": "none", "os": ["none"], "osfingerprint": "none", "hops": [
1717
{"ipaddr": "10.0.0.0", "host": "host_1"},
18-
{"ipaddr": "10.0.0.1", "host": "host_2"}],"ports": [
19-
{"portid": "80", "proto": "tcp","state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
20-
{"portid": "22", "proto": "tcp","state": {"state": "open"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
21-
{"portid": "443", "proto": "tcp","state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
18+
{"ipaddr": "10.0.0.1", "host": "host_2"}], "ports": [
19+
{"portid": "80", "proto": "tcp", "state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
20+
{"portid": "22", "proto": "tcp", "state": {"state": "open"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
21+
{"portid": "443", "proto": "tcp", "state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
2222
]
2323
}, "result_hash": "d41d8cd98asw0b204e9800998ecf8427e", "created_at": "2021-01-02 00:00:00"},
24-
{"id": 3, "uuid": "uuid_3", "host_subnet": "0.0.0.0/24","host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
24+
{"id": 3, "uuid": "uuid_3", "host_subnet": "0.0.0.0/24", "host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
2525
"host": "0.0.0.0", "status": "up", "last_boot": "none", "os": ["none"], "osfingerprint": "none", "hops": [
2626
{"ipaddr": "10.0.0.0", "host": "host_1"},
27-
{"ipaddr": "10.0.0.1", "host": "host_2"}],"ports": [
28-
{"portid": "80", "proto": "tcp","state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
29-
{"portid": "22", "proto": "tcp","state": {"state": "closed"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
30-
{"portid": "443", "proto": "tcp","state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
27+
{"ipaddr": "10.0.0.1", "host": "host_2"}], "ports": [
28+
{"portid": "80", "proto": "tcp", "state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
29+
{"portid": "22", "proto": "tcp", "state": {"state": "closed"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
30+
{"portid": "443", "proto": "tcp", "state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
3131
]
3232
}, "result_hash": "d41d8cd98f00b204e9800998ecf8427e", "created_at": "2021-01-03 00:00:00"}
3333
]
3434

3535

3636
SCANS_FROM_DB_TEST_V1_PORTS_KEYS = [
37-
{"id": 1, "uuid": "uuid_1", "host_subnet": "0.0.0.0/24","host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
37+
{"id": 1, "uuid": "uuid_1", "host_subnet": "0.0.0.0/24", "host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
3838
"host": "0.0.0.0", "status": "up", "last_boot": "none", "os": ["none"], "osfingerprint": "none", "hops": [
3939
{"ipaddr": "10.0.0.0", "host": "host_1"},
40-
{"ipaddr": "10.0.0.1", "host": "host_2"}],"ports": {
41-
"80": {"portid": "80", "proto": "tcp","state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
42-
"22": {"portid": "22", "proto": "tcp","state": {"state": "closed"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
43-
"443": {"portid": "443", "proto": "tcp","state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
40+
{"ipaddr": "10.0.0.1", "host": "host_2"}], "ports": {
41+
"80": {"portid": "80", "proto": "tcp", "state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
42+
"22": {"portid": "22", "proto": "tcp", "state": {"state": "closed"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
43+
"443": {"portid": "443", "proto": "tcp", "state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
4444
}}, "result_hash": "d41d8cd98f00b204e9800998ecf8427e", "created_at": "2021-01-01 00:00:00"},
4545

46-
{"id": 2, "uuid": "uuid_2", "host_subnet": "0.0.0.0/24","host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
46+
{"id": 2, "uuid": "uuid_2", "host_subnet": "0.0.0.0/24", "host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
4747
"host": "0.0.0.0", "status": "up", "last_boot": "none", "os": ["none"], "osfingerprint": "none", "hops": [
4848
{"ipaddr": "10.0.0.0", "host": "host_1"},
49-
{"ipaddr": "10.0.0.1", "host": "host_2"}],"ports": {
50-
"80": {"portid": "80", "proto": "tcp","state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
51-
"22": {"portid": "22", "proto": "tcp","state": {"state": "open"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
52-
"443": {"portid": "443", "proto": "tcp","state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
49+
{"ipaddr": "10.0.0.1", "host": "host_2"}], "ports": {
50+
"80": {"portid": "80", "proto": "tcp", "state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
51+
"22": {"portid": "22", "proto": "tcp", "state": {"state": "open"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
52+
"443": {"portid": "443", "proto": "tcp", "state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
5353
}}, "result_hash": "d41d8cd98asw0b204e9800998ecf8427e", "created_at": "2021-01-01 00:00:00"},
54-
{"id": 3, "uuid": "uuid_3", "host_subnet": "0.0.0.0/24","host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
54+
{"id": 3, "uuid": "uuid_3", "host_subnet": "0.0.0.0/24", "host": "0.0.0.0", "profile_name": "TEST_V1", "arguments": "-vv", "results": {
5555
"host": "0.0.0.0", "status": "up", "last_boot": "none", "os": ["none"], "osfingerprint": "none", "hops": [
5656
{"ipaddr": "10.0.0.0", "host": "host_1"},
57-
{"ipaddr": "10.0.0.1", "host": "host_2"}],"ports": {
58-
"80": {"portid": "80", "proto": "tcp","state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
59-
"22": {"portid": "22", "proto": "tcp","state": {"state": "closed"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
60-
"443": {"portid": "443", "proto": "tcp","state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
57+
{"ipaddr": "10.0.0.1", "host": "host_2"}], "ports": {
58+
"80": {"portid": "80", "proto": "tcp", "state": {"state": "open"}, "service": "http", "servicefp": "s_fp_test", "service_product": "Apache"},
59+
"22": {"portid": "22", "proto": "tcp", "state": {"state": "closed"}, "service": "ssh", "servicefp": "s_fp_test", "service_product": "OpenSSH"},
60+
"443": {"portid": "443", "proto": "tcp", "state": {"state": "open"}, "service": "https", "servicefp": "s_fp_test", "service_product": "Nginx"}
6161
}}, "result_hash": "d41d8cd98f00b204e9800998ecf8427e", "created_at": "2021-01-01 00:00:00"},
6262
]
6363

64+
6465
def mock_data_with_real_hash(test_data):
6566
for scan in test_data:
6667
scan["result_hash"] = hash_string(json.dumps(scan["results"]))
6768
return test_data
6869

70+
6971
DIFFS = [
7072
{
71-
"ids": [1,2],
73+
"ids": [1, 2],
7274
"dates": ["2024-02-01 00:00:00", "2024-01-01 00:00:00"],
7375
"generic": [{
7476
"host": "0.0.0.0",
7577
"arguments": "-vv",
7678
"profile_name": "PROFILE_1"
77-
},{
79+
}, {
7880
"host": "0.0.0.0",
7981
"arguments": "-vv",
8082
"profile_name": "PROFILE_1"
@@ -93,7 +95,7 @@ def mock_data_with_real_hash(test_data):
9395
"result_hashes": ["a123456", "a123411"]
9496
},
9597
{
96-
"ids": [1,2],
98+
"ids": [1, 2],
9799
"dates": ["2024-02-06 00:00:00", "2024-02-04 00:00:00"],
98100
"generic": [
99101
{
@@ -132,7 +134,6 @@ def mock_data_with_real_hash(test_data):
132134
"from": "open",
133135
"to": "closed"
134136
}
135-
136137
}
137138
}
138139
}

tests/unit/test_db.py

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from unittest import TestCase
2-
from unittest.mock import MagicMock, patch
3-
import os
42
from deltascan.core.db.manager import RDBMS
53

4+
65
class TestSQLiteDatabase(TestCase):
76
def setUp(self):
87
self.manager = RDBMS()
9-
8+
109
# WARNING: the tests run in order they appear here due to their name
1110
# Their names are ordered alphabetically: test_a_<name>, test_b_<name>, etc.
1211
def test_a_profile_create_and_get_database_success(self):
@@ -30,27 +29,30 @@ def test_a_profile_create_and_get_database_success(self):
3029
"arguments": "test_args",
3130
"created_at": None}
3231
])
33-
32+
3433
r = self.manager.get_profile("TEST_1")
3534
r["created_at"] = None
36-
self.assertEqual(r, {"id": 1,
37-
"profile_name": "TEST_1",
38-
"arguments": "test_args",
39-
"created_at": None},
35+
self.assertEqual(
36+
r, {
37+
"id": 1,
38+
"profile_name": "TEST_1",
39+
"arguments": "test_args",
40+
"created_at": None
41+
},
4042
)
4143

4244
def test_b_port_scans_create_and_get_database_success(self):
4345
self.manager.create_profile("TEST_3", "test_args")
4446
result = self.manager.create_port_scan(
4547
"uuid_1", "0.0.0.0", "0.0.0.0/24", "unknown", "TEST_3", '{"data": "test_data"}', "hash", None
4648
)
47-
self.assertEqual(1,result.id)
49+
self.assertEqual(1, result.id)
4850

4951
self.manager.create_profile("TEST_4", "test_args")
5052
result = self.manager.create_port_scan(
51-
"uuid_2", "0.0.0.0", "0.0.0.0/24","unknown", "TEST_4", '{"data": "test_data"}', "hash", None
53+
"uuid_2", "0.0.0.0", "0.0.0.0/24", "unknown", "TEST_4", '{"data": "test_data"}', "hash", None
5254
)
53-
self.assertEqual(2,result.id)
55+
self.assertEqual(2, result.id)
5456

5557
r1 = list(self.manager.get_scans(None, "0.0.0.0", 1, "TEST_3"))
5658
self.assertEqual(1, len(r1))

tests/unit/test_importer.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
import unittest
2-
from unittest.mock import MagicMock, patch
2+
from unittest.mock import MagicMock
33
from deltascan.core.importer import Importer
44

5+
56
class TestImporter(unittest.TestCase):
67
def setUp(self):
78
self.file = "test.csv"
89
self.logger = MagicMock()
910
self.importer = Importer(self.file, self.logger)
1011

1112
def test_compare_nmap_arguments(self):
12-
self.assertEqual(True,
13+
self.assertEqual(
14+
True,
1315
self.importer._compare_nmap_arguments(
1416
["-sV", "-p1-100"],
15-
["-sV", "-p1-100"]))
16-
self.assertEqual(True,
17+
["-sV", "-p1-100"]
18+
)
19+
)
20+
self.assertEqual(
21+
True,
1722
self.importer._compare_nmap_arguments(
1823
["--osscan", "--version-all", "-sS"],
19-
["-sS","--version-all", "--osscan"]))
20-
self.assertEqual(False,
24+
["-sS", "--version-all", "--osscan"]
25+
)
26+
)
27+
self.assertEqual(
28+
False,
2129
self.importer._compare_nmap_arguments(
2230
["--osscan", "--version-all", "-sS"],
23-
["-sV", "--osscan"]))
31+
["-sV", "--osscan"]
32+
)
33+
)

0 commit comments

Comments
 (0)