UT: Fix test_principle_service tests

And modify data in test_pillar_service.py accordingly
This commit is contained in:
Mike Salvatore 2022-02-02 13:33:30 -05:00 committed by Shreya Malviya
parent b23360db37
commit 788641b7d6
4 changed files with 12 additions and 72 deletions

View File

@ -1,35 +1,17 @@
from tests.unit_tests.monkey_island.cc.services.zero_trust.test_common.finding_data import ( from tests.unit_tests.monkey_island.cc.services.zero_trust.test_common.finding_data import (
get_monkey_finding_dto, get_monkey_finding_dto,
get_scoutsuite_finding_dto,
) )
from common.common_consts import zero_trust_consts from common.common_consts import zero_trust_consts
def save_example_findings(): def save_example_findings():
# devices passed = 1
_save_finding_with_status(
"scoutsuite",
zero_trust_consts.TEST_ENDPOINT_SECURITY_EXISTS,
zero_trust_consts.STATUS_PASSED,
)
# devices passed = 2
_save_finding_with_status(
"scoutsuite",
zero_trust_consts.TEST_ENDPOINT_SECURITY_EXISTS,
zero_trust_consts.STATUS_PASSED,
)
# devices failed = 1 # devices failed = 1
_save_finding_with_status( _save_finding_with_status(
"monkey", zero_trust_consts.TEST_ENDPOINT_SECURITY_EXISTS, zero_trust_consts.STATUS_FAILED "monkey", zero_trust_consts.TEST_ENDPOINT_SECURITY_EXISTS, zero_trust_consts.STATUS_FAILED
) )
# people verify = 1 # people verify = 1
# networks verify = 1 # networks verify = 1
_save_finding_with_status(
"scoutsuite", zero_trust_consts.TEST_SCHEDULED_EXECUTION, zero_trust_consts.STATUS_VERIFY
)
# people verify = 2
# networks verify = 2
_save_finding_with_status( _save_finding_with_status(
"monkey", zero_trust_consts.TEST_SCHEDULED_EXECUTION, zero_trust_consts.STATUS_VERIFY "monkey", zero_trust_consts.TEST_SCHEDULED_EXECUTION, zero_trust_consts.STATUS_VERIFY
) )
@ -39,24 +21,12 @@ def save_example_findings():
) )
# data failed 2 # data failed 2
_save_finding_with_status( _save_finding_with_status(
"scoutsuite", "monkey", zero_trust_consts.TEST_DATA_ENDPOINT_HTTP, zero_trust_consts.STATUS_FAILED
zero_trust_consts.TEST_SCOUTSUITE_UNENCRYPTED_DATA,
zero_trust_consts.STATUS_FAILED,
) )
# data failed 3 # data failed 3
_save_finding_with_status( _save_finding_with_status(
"monkey", zero_trust_consts.TEST_DATA_ENDPOINT_HTTP, zero_trust_consts.STATUS_FAILED "monkey", zero_trust_consts.TEST_DATA_ENDPOINT_HTTP, zero_trust_consts.STATUS_FAILED
) )
# data failed 4
_save_finding_with_status(
"monkey", zero_trust_consts.TEST_DATA_ENDPOINT_HTTP, zero_trust_consts.STATUS_FAILED
)
# data failed 5
_save_finding_with_status(
"scoutsuite",
zero_trust_consts.TEST_SCOUTSUITE_UNENCRYPTED_DATA,
zero_trust_consts.STATUS_FAILED,
)
# data verify 1 # data verify 1
_save_finding_with_status( _save_finding_with_status(
"monkey", zero_trust_consts.TEST_DATA_ENDPOINT_HTTP, zero_trust_consts.STATUS_VERIFY "monkey", zero_trust_consts.TEST_DATA_ENDPOINT_HTTP, zero_trust_consts.STATUS_VERIFY
@ -65,18 +35,9 @@ def save_example_findings():
_save_finding_with_status( _save_finding_with_status(
"monkey", zero_trust_consts.TEST_DATA_ENDPOINT_HTTP, zero_trust_consts.STATUS_VERIFY "monkey", zero_trust_consts.TEST_DATA_ENDPOINT_HTTP, zero_trust_consts.STATUS_VERIFY
) )
# data passed 1
_save_finding_with_status(
"scoutsuite",
zero_trust_consts.TEST_SCOUTSUITE_UNENCRYPTED_DATA,
zero_trust_consts.STATUS_PASSED,
)
def _save_finding_with_status(finding_type: str, test: str, status: str): def _save_finding_with_status(finding_type: str, test: str, status: str):
if finding_type == "scoutsuite":
finding = get_scoutsuite_finding_dto()
else:
finding = get_monkey_finding_dto() finding = get_monkey_finding_dto()
finding.test = test finding.test = test
finding.status = status finding.status = status

View File

@ -1,27 +1,14 @@
from tests.unit_tests.monkey_island.cc.services.zero_trust.test_common.monkey_finding_data import ( from tests.unit_tests.monkey_island.cc.services.zero_trust.test_common.monkey_finding_data import (
get_monkey_details_dto, get_monkey_details_dto,
) )
from tests.unit_tests.monkey_island.cc.services.zero_trust.test_common.scoutsuite_finding_data import ( # noqa: E501
get_scoutsuite_details_dto,
)
from common.common_consts.zero_trust_consts import ( from common.common_consts.zero_trust_consts import (
STATUS_FAILED, STATUS_FAILED,
STATUS_PASSED, STATUS_PASSED,
TEST_ENDPOINT_SECURITY_EXISTS, TEST_ENDPOINT_SECURITY_EXISTS,
TEST_SCOUTSUITE_SERVICE_SECURITY,
) )
from monkey_island.cc.models.zero_trust.finding import Finding from monkey_island.cc.models.zero_trust.finding import Finding
from monkey_island.cc.models.zero_trust.monkey_finding import MonkeyFinding from monkey_island.cc.models.zero_trust.monkey_finding import MonkeyFinding
from monkey_island.cc.models.zero_trust.scoutsuite_finding import ScoutSuiteFinding
def get_scoutsuite_finding_dto() -> Finding:
scoutsuite_details = get_scoutsuite_details_dto()
scoutsuite_details.save()
return ScoutSuiteFinding(
test=TEST_SCOUTSUITE_SERVICE_SECURITY, status=STATUS_FAILED, details=scoutsuite_details
)
def get_monkey_finding_dto() -> Finding: def get_monkey_finding_dto() -> Finding:

View File

@ -29,16 +29,16 @@ def test_get_pillars_grades():
def _get_expected_pillar_grades() -> List[dict]: def _get_expected_pillar_grades() -> List[dict]:
return [ return [
{ {
zero_trust_consts.STATUS_FAILED: 5, zero_trust_consts.STATUS_FAILED: 3,
zero_trust_consts.STATUS_VERIFY: 2, zero_trust_consts.STATUS_VERIFY: 2,
zero_trust_consts.STATUS_PASSED: 1, zero_trust_consts.STATUS_PASSED: 0,
# 2 different tests of DATA pillar were executed in _save_findings() # 1 test of DATA pillar was executed in _save_findings()
zero_trust_consts.STATUS_UNEXECUTED: _get_cnt_of_tests_in_pillar(DATA) - 2, zero_trust_consts.STATUS_UNEXECUTED: _get_cnt_of_tests_in_pillar(DATA) - 1,
"pillar": "Data", "pillar": "Data",
}, },
{ {
zero_trust_consts.STATUS_FAILED: 0, zero_trust_consts.STATUS_FAILED: 0,
zero_trust_consts.STATUS_VERIFY: 2, zero_trust_consts.STATUS_VERIFY: 1,
zero_trust_consts.STATUS_PASSED: 0, zero_trust_consts.STATUS_PASSED: 0,
# 1 test of PEOPLE pillar were executed in _save_findings() # 1 test of PEOPLE pillar were executed in _save_findings()
zero_trust_consts.STATUS_UNEXECUTED: _get_cnt_of_tests_in_pillar(PEOPLE) - 1, zero_trust_consts.STATUS_UNEXECUTED: _get_cnt_of_tests_in_pillar(PEOPLE) - 1,
@ -46,7 +46,7 @@ def _get_expected_pillar_grades() -> List[dict]:
}, },
{ {
zero_trust_consts.STATUS_FAILED: 0, zero_trust_consts.STATUS_FAILED: 0,
zero_trust_consts.STATUS_VERIFY: 2, zero_trust_consts.STATUS_VERIFY: 1,
zero_trust_consts.STATUS_PASSED: 0, zero_trust_consts.STATUS_PASSED: 0,
# 1 different tests of NETWORKS pillar were executed in _save_findings() # 1 different tests of NETWORKS pillar were executed in _save_findings()
zero_trust_consts.STATUS_UNEXECUTED: _get_cnt_of_tests_in_pillar(NETWORKS) - 1, zero_trust_consts.STATUS_UNEXECUTED: _get_cnt_of_tests_in_pillar(NETWORKS) - 1,
@ -55,7 +55,7 @@ def _get_expected_pillar_grades() -> List[dict]:
{ {
zero_trust_consts.STATUS_FAILED: 1, zero_trust_consts.STATUS_FAILED: 1,
zero_trust_consts.STATUS_VERIFY: 0, zero_trust_consts.STATUS_VERIFY: 0,
zero_trust_consts.STATUS_PASSED: 2, zero_trust_consts.STATUS_PASSED: 0,
# 1 different tests of DEVICES pillar were executed in _save_findings() # 1 different tests of DEVICES pillar were executed in _save_findings()
zero_trust_consts.STATUS_UNEXECUTED: _get_cnt_of_tests_in_pillar(DEVICES) - 1, zero_trust_consts.STATUS_UNEXECUTED: _get_cnt_of_tests_in_pillar(DEVICES) - 1,
"pillar": "Devices", "pillar": "Devices",

View File

@ -1,7 +1,6 @@
import pytest import pytest
from tests.unit_tests.monkey_island.cc.services.zero_trust.test_common.finding_data import ( from tests.unit_tests.monkey_island.cc.services.zero_trust.test_common.finding_data import (
get_monkey_finding_dto, get_monkey_finding_dto,
get_scoutsuite_finding_dto,
) )
from common.common_consts import zero_trust_consts from common.common_consts import zero_trust_consts
@ -13,10 +12,9 @@ EXPECTED_DICT = {
"test_pillar1": [ "test_pillar1": [
{ {
"principle": "Test principle description2", "principle": "Test principle description2",
"status": zero_trust_consts.STATUS_FAILED, "status": zero_trust_consts.STATUS_PASSED,
"tests": [ "tests": [
{"status": zero_trust_consts.STATUS_PASSED, "test": "You ran a test2"}, {"status": zero_trust_consts.STATUS_PASSED, "test": "You ran a test2"},
{"status": zero_trust_consts.STATUS_FAILED, "test": "You ran a test3"},
], ],
} }
], ],
@ -28,10 +26,9 @@ EXPECTED_DICT = {
}, },
{ {
"principle": "Test principle description2", "principle": "Test principle description2",
"status": zero_trust_consts.STATUS_FAILED, "status": zero_trust_consts.STATUS_PASSED,
"tests": [ "tests": [
{"status": zero_trust_consts.STATUS_PASSED, "test": "You ran a test2"}, {"status": zero_trust_consts.STATUS_PASSED, "test": "You ran a test2"},
{"status": zero_trust_consts.STATUS_FAILED, "test": "You ran a test3"},
], ],
}, },
], ],
@ -46,7 +43,7 @@ def test_get_principles_status():
principles_to_tests = { principles_to_tests = {
"network_policies": ["segmentation"], "network_policies": ["segmentation"],
"endpoint_security": ["tunneling", "scoutsuite_service_security"], "endpoint_security": ["tunneling"],
} }
zero_trust_consts.PRINCIPLES_TO_TESTS = principles_to_tests zero_trust_consts.PRINCIPLES_TO_TESTS = principles_to_tests
@ -65,7 +62,6 @@ def test_get_principles_status():
tests_map = { tests_map = {
"segmentation": {"explanation": "You ran a test1"}, "segmentation": {"explanation": "You ran a test1"},
"tunneling": {"explanation": "You ran a test2"}, "tunneling": {"explanation": "You ran a test2"},
"scoutsuite_service_security": {"explanation": "You ran a test3"},
} }
zero_trust_consts.TESTS_MAP = tests_map zero_trust_consts.TESTS_MAP = tests_map
@ -77,10 +73,6 @@ def test_get_principles_status():
monkey_finding.test = "tunneling" monkey_finding.test = "tunneling"
monkey_finding.save() monkey_finding.save()
scoutsuite_finding = get_scoutsuite_finding_dto()
scoutsuite_finding.test = "scoutsuite_service_security"
scoutsuite_finding.save()
expected = dict(EXPECTED_DICT) # new mutable expected = dict(EXPECTED_DICT) # new mutable
result = PrincipleService.get_principles_status() result = PrincipleService.get_principles_status()