forked from p15670423/monkey
Merge pull request #1178 from guardicore/test_common_refactoring
Test common refactoring
This commit is contained in:
commit
b02c006c51
|
@ -1,4 +0,0 @@
|
|||
# Without these imports pytests can't use fixtures,
|
||||
# because they are not found
|
||||
from .fixture_enum import FixtureEnum # noqa: F401
|
||||
from .mongomock_fixtures import * # noqa: F401,F403
|
|
@ -1,2 +0,0 @@
|
|||
class FixtureEnum:
|
||||
USES_DATABASE = "uses_database"
|
|
@ -1,3 +0,0 @@
|
|||
# Without these imports pytests can't use fixtures,
|
||||
# because they are not found
|
||||
from monkey_island.cc.test_common.fixtures import * # noqa: F401,F403
|
|
@ -0,0 +1,3 @@
|
|||
# Without these imports pytests can't use fixtures,
|
||||
# because they are not found
|
||||
from tests.unit_tests.monkey_island.cc.mongomock_fixtures import * # noqa: F401,F403
|
|
@ -6,13 +6,12 @@ import pytest
|
|||
|
||||
from monkey_island.cc.models.monkey import Monkey, MonkeyNotFoundError
|
||||
from monkey_island.cc.models.monkey_ttl import MonkeyTtl
|
||||
from monkey_island.cc.test_common.fixtures import FixtureEnum
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestMonkey:
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_is_dead(self):
|
||||
# Arrange
|
||||
alive_monkey_ttl = MonkeyTtl.create_ttl_expire_in(30)
|
||||
|
@ -38,7 +37,7 @@ class TestMonkey:
|
|||
assert mia_monkey.is_dead()
|
||||
assert not alive_monkey.is_dead()
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_ttl_renewal(self):
|
||||
# Arrange
|
||||
monkey = Monkey(guid=str(uuid.uuid4()))
|
||||
|
@ -49,7 +48,7 @@ class TestMonkey:
|
|||
monkey.renew_ttl()
|
||||
assert monkey.ttl_ref
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_get_single_monkey_by_id(self):
|
||||
# Arrange
|
||||
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
||||
|
@ -63,7 +62,7 @@ class TestMonkey:
|
|||
with pytest.raises(MonkeyNotFoundError) as _:
|
||||
_ = Monkey.get_single_monkey_by_id("abcdefabcdefabcdefabcdef")
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_get_os(self):
|
||||
linux_monkey = Monkey(
|
||||
guid=str(uuid.uuid4()),
|
||||
|
@ -79,7 +78,7 @@ class TestMonkey:
|
|||
assert 1 == len([m for m in Monkey.objects() if m.get_os() == "linux"])
|
||||
assert 1 == len([m for m in Monkey.objects() if m.get_os() == "unknown"])
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_get_tunneled_monkeys(self):
|
||||
linux_monkey = Monkey(guid=str(uuid.uuid4()), description="Linux shay-Virtual-Machine")
|
||||
windows_monkey = Monkey(
|
||||
|
@ -100,7 +99,7 @@ class TestMonkey:
|
|||
)
|
||||
assert test
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_get_label_by_id(self):
|
||||
hostname_example = "a_hostname"
|
||||
ip_example = "1.1.1.1"
|
||||
|
@ -148,7 +147,7 @@ class TestMonkey:
|
|||
assert cache_info_after_query_3.hits == 1
|
||||
assert cache_info_after_query_3.misses == 2
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_is_monkey(self):
|
||||
a_monkey = Monkey(guid=str(uuid.uuid4()))
|
||||
a_monkey.save()
|
|
@ -6,14 +6,13 @@ from monkey_island.cc.models.zero_trust.event import Event
|
|||
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_details import MonkeyFindingDetails
|
||||
from monkey_island.cc.test_common.fixtures import FixtureEnum
|
||||
|
||||
MONKEY_FINDING_DETAIL_MOCK = MonkeyFindingDetails()
|
||||
MONKEY_FINDING_DETAIL_MOCK.events = ["mock1", "mock2"]
|
||||
|
||||
|
||||
class TestMonkeyFinding:
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_save_finding_validation(self):
|
||||
with pytest.raises(ValidationError):
|
||||
_ = MonkeyFinding.save_finding(
|
||||
|
@ -22,7 +21,7 @@ class TestMonkeyFinding:
|
|||
detail_ref=MONKEY_FINDING_DETAIL_MOCK,
|
||||
)
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_save_finding_sanity(self):
|
||||
assert len(Finding.objects(test=zero_trust_consts.TEST_SEGMENTATION)) == 0
|
||||
|
|
@ -7,7 +7,6 @@ from monkey_island.cc.models.zero_trust.monkey_finding_details import MonkeyFind
|
|||
from monkey_island.cc.models.zero_trust.scoutsuite_finding import ScoutSuiteFinding
|
||||
from monkey_island.cc.models.zero_trust.scoutsuite_finding_details import ScoutSuiteFindingDetails
|
||||
from monkey_island.cc.services.zero_trust.test_common.scoutsuite_finding_data import RULES
|
||||
from monkey_island.cc.test_common.fixtures import FixtureEnum
|
||||
|
||||
MONKEY_FINDING_DETAIL_MOCK = MonkeyFindingDetails()
|
||||
MONKEY_FINDING_DETAIL_MOCK.events = ["mock1", "mock2"]
|
||||
|
@ -16,7 +15,7 @@ SCOUTSUITE_FINDING_DETAIL_MOCK.scoutsuite_rules = []
|
|||
|
||||
|
||||
class TestScoutSuiteFinding:
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_save_finding_validation(self):
|
||||
with pytest.raises(ValidationError):
|
||||
_ = ScoutSuiteFinding.save_finding(
|
||||
|
@ -25,7 +24,7 @@ class TestScoutSuiteFinding:
|
|||
detail_ref=SCOUTSUITE_FINDING_DETAIL_MOCK,
|
||||
)
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_save_finding_sanity(self):
|
||||
assert len(Finding.objects(test=zero_trust_consts.TEST_SEGMENTATION)) == 0
|
||||
|
|
@ -6,7 +6,7 @@ from monkey_island.cc.models.edge import Edge
|
|||
from monkey_island.cc.models.zero_trust.finding import Finding
|
||||
|
||||
|
||||
@pytest.fixture(scope="session", autouse=True)
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def change_to_mongo_mock():
|
||||
# Make sure tests are working with mongomock
|
||||
mongoengine.disconnect()
|
|
@ -5,13 +5,12 @@ from mongomock import ObjectId
|
|||
|
||||
from monkey_island.cc.models.edge import Edge
|
||||
from monkey_island.cc.services.edge.edge import EdgeService
|
||||
from monkey_island.cc.test_common.fixtures import FixtureEnum
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestEdgeService:
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_get_or_create_edge(self):
|
||||
src_id = ObjectId()
|
||||
dst_id = ObjectId()
|
|
@ -9,7 +9,6 @@ from monkey_island.cc.models.zero_trust.monkey_finding import MonkeyFinding
|
|||
from monkey_island.cc.services.zero_trust.monkey_findings.monkey_zt_finding_service import (
|
||||
MonkeyZTFindingService,
|
||||
)
|
||||
from monkey_island.cc.test_common.fixtures import FixtureEnum
|
||||
|
||||
EVENTS = [
|
||||
Event.create_event(
|
||||
|
@ -40,7 +39,7 @@ STATUS = [
|
|||
|
||||
|
||||
class TestMonkeyZTFindingService:
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_create_or_add_to_existing_creation(self):
|
||||
# Create new finding
|
||||
MonkeyZTFindingService.create_or_add_to_existing(
|
||||
|
@ -55,7 +54,7 @@ class TestMonkeyZTFindingService:
|
|||
assert len(finding_details.events) == 1
|
||||
assert finding_details.events[0].message == EVENTS[0].message
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_create_or_add_to_existing_addition(self):
|
||||
# Create new finding
|
||||
MonkeyZTFindingService.create_or_add_to_existing(
|
|
@ -10,14 +10,13 @@ from monkey_island.cc.services.config import ConfigService
|
|||
from monkey_island.cc.services.zero_trust.scoutsuite.scoutsuite_auth_service import (
|
||||
is_aws_keys_setup,
|
||||
)
|
||||
from monkey_island.cc.test_common.fixtures import FixtureEnum
|
||||
|
||||
|
||||
class MockObject:
|
||||
pass
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_is_aws_keys_setup(tmp_path):
|
||||
# Mock default configuration
|
||||
ConfigService.init_default_config()
|
|
@ -9,11 +9,10 @@ from monkey_island.cc.services.zero_trust.test_common.scoutsuite_finding_data im
|
|||
RULES,
|
||||
SCOUTSUITE_FINDINGS,
|
||||
)
|
||||
from monkey_island.cc.test_common.fixtures import FixtureEnum
|
||||
|
||||
|
||||
class TestScoutSuiteZTFindingService:
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_process_rule(self):
|
||||
# Creates new PermissiveFirewallRules finding with a rule
|
||||
ScoutSuiteZTFindingService.process_rule(SCOUTSUITE_FINDINGS[0], RULES[0])
|
|
@ -22,10 +22,9 @@ from monkey_island.cc.services.zero_trust.zero_trust_report.finding_service impo
|
|||
EnrichedFinding,
|
||||
FindingService,
|
||||
)
|
||||
from monkey_island.cc.test_common.fixtures.fixture_enum import FixtureEnum
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_get_all_findings():
|
||||
get_scoutsuite_finding_dto().save()
|
||||
get_monkey_finding_dto().save()
|
|
@ -16,10 +16,9 @@ from monkey_island.cc.services.zero_trust.zero_trust_report.pillar_service impor
|
|||
from monkey_island.cc.services.zero_trust.zero_trust_report.test_common.example_finding_data import ( # noqa: E501
|
||||
save_example_findings,
|
||||
)
|
||||
from monkey_island.cc.test_common.fixtures import FixtureEnum
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_get_pillars_grades():
|
||||
save_example_findings()
|
||||
expected_grades = _get_expected_pillar_grades()
|
||||
|
@ -97,7 +96,7 @@ def _get_cnt_of_tests_in_pillar(pillar: str):
|
|||
return len(tests_in_pillar)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_get_pillars_to_statuses():
|
||||
# Test empty database
|
||||
expected = {
|
|
@ -8,7 +8,6 @@ from monkey_island.cc.services.zero_trust.test_common.finding_data import (
|
|||
from monkey_island.cc.services.zero_trust.zero_trust_report.principle_service import (
|
||||
PrincipleService,
|
||||
)
|
||||
from monkey_island.cc.test_common.fixtures import FixtureEnum
|
||||
|
||||
EXPECTED_DICT = {
|
||||
"test_pillar1": [
|
||||
|
@ -39,7 +38,7 @@ EXPECTED_DICT = {
|
|||
}
|
||||
|
||||
|
||||
@pytest.mark.usefixtures(FixtureEnum.USES_DATABASE)
|
||||
@pytest.mark.usefixtures("uses_database")
|
||||
def test_get_principles_status():
|
||||
TEST_PILLAR1 = "test_pillar1"
|
||||
TEST_PILLAR2 = "test_pillar2"
|
Loading…
Reference in New Issue