tests: Extract mocking to an autouse, function-scoped fixture to reduce

code in test_technique_reports.py
This commit is contained in:
Shreya Malviya 2021-09-24 17:23:59 +05:30
parent 6f903bd8f1
commit 85e54419f3
1 changed files with 21 additions and 67 deletions

View File

@ -1,5 +1,7 @@
from enum import Enum from enum import Enum
import pytest
from common.utils.attack_utils import ScanStatus from common.utils.attack_utils import ScanStatus
from monkey_island.cc.services.attack.technique_reports.__init__ import ( from monkey_island.cc.services.attack.technique_reports.__init__ import (
AttackTechnique, AttackTechnique,
@ -18,6 +20,17 @@ FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE = {
} }
@pytest.fixture(scope="function", autouse=True)
def mock_config_schema_per_attack_technique(monkeypatch):
monkeypatch.setattr(
(
"monkey_island.cc.services.attack.technique_reports."
"__init__.CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE"
),
FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE,
)
class FakeAttackTechnique_TwoRelevantSystems(AttackTechnique): class FakeAttackTechnique_TwoRelevantSystems(AttackTechnique):
tech_id = "T0001" tech_id = "T0001"
relevant_systems = ["System 1", "System 2"] relevant_systems = ["System 1", "System 2"]
@ -63,115 +76,56 @@ class ExpectedMsgs_OneRelevantSystem(Enum):
USED: str = "USED" USED: str = "USED"
def test_get_message_by_status_disabled_two_relevant_systems(monkeypatch): def test_get_message_by_status_disabled_two_relevant_systems():
monkeypatch.setattr(
(
"monkey_island.cc.services.attack.technique_reports."
"__init__.CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE"
),
FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE,
)
technique_msg = FakeAttackTechnique_TwoRelevantSystems.get_message_by_status( technique_msg = FakeAttackTechnique_TwoRelevantSystems.get_message_by_status(
ScanStatus.DISABLED.value ScanStatus.DISABLED.value
) )
assert technique_msg == disabled_msg assert technique_msg == disabled_msg
def test_get_message_by_status_unscanned_two_relevant_systems(monkeypatch): def test_get_message_by_status_unscanned_two_relevant_systems():
monkeypatch.setattr(
(
"monkey_island.cc.services.attack.technique_reports."
"__init__.CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE"
),
FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE,
)
technique_msg = FakeAttackTechnique_TwoRelevantSystems.get_message_by_status( technique_msg = FakeAttackTechnique_TwoRelevantSystems.get_message_by_status(
ScanStatus.UNSCANNED.value ScanStatus.UNSCANNED.value
) )
assert technique_msg == ExpectedMsgs_TwoRelevantSystems.UNSCANNED.value assert technique_msg == ExpectedMsgs_TwoRelevantSystems.UNSCANNED.value
def test_get_message_by_status_scanned_two_relevant_systems(monkeypatch): def test_get_message_by_status_scanned_two_relevant_systems():
monkeypatch.setattr(
(
"monkey_island.cc.services.attack.technique_reports."
"__init__.CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE"
),
FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE,
)
technique_msg = FakeAttackTechnique_TwoRelevantSystems.get_message_by_status( technique_msg = FakeAttackTechnique_TwoRelevantSystems.get_message_by_status(
ScanStatus.SCANNED.value ScanStatus.SCANNED.value
) )
assert technique_msg == ExpectedMsgs_TwoRelevantSystems.SCANNED.value assert technique_msg == ExpectedMsgs_TwoRelevantSystems.SCANNED.value
def test_get_message_by_status_used_two_relevant_systems(monkeypatch): def test_get_message_by_status_used_two_relevant_systems():
monkeypatch.setattr(
(
"monkey_island.cc.services.attack.technique_reports."
"__init__.CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE"
),
FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE,
)
technique_msg = FakeAttackTechnique_TwoRelevantSystems.get_message_by_status( technique_msg = FakeAttackTechnique_TwoRelevantSystems.get_message_by_status(
ScanStatus.USED.value ScanStatus.USED.value
) )
assert technique_msg == ExpectedMsgs_TwoRelevantSystems.USED.value assert technique_msg == ExpectedMsgs_TwoRelevantSystems.USED.value
### def test_get_message_by_status_disabled_one_relevant_system():
def test_get_message_by_status_disabled_one_relevant_system(monkeypatch):
monkeypatch.setattr(
(
"monkey_island.cc.services.attack.technique_reports."
"__init__.CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE"
),
FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE,
)
technique_msg = FakeAttackTechnique_OneRelevantSystem.get_message_by_status( technique_msg = FakeAttackTechnique_OneRelevantSystem.get_message_by_status(
ScanStatus.DISABLED.value ScanStatus.DISABLED.value
) )
assert technique_msg == disabled_msg assert technique_msg == disabled_msg
def test_get_message_by_status_unscanned_one_relevant_system(monkeypatch): def test_get_message_by_status_unscanned_one_relevant_system():
monkeypatch.setattr(
(
"monkey_island.cc.services.attack.technique_reports."
"__init__.CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE"
),
FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE,
)
technique_msg = FakeAttackTechnique_OneRelevantSystem.get_message_by_status( technique_msg = FakeAttackTechnique_OneRelevantSystem.get_message_by_status(
ScanStatus.UNSCANNED.value ScanStatus.UNSCANNED.value
) )
assert technique_msg == ExpectedMsgs_OneRelevantSystem.UNSCANNED.value assert technique_msg == ExpectedMsgs_OneRelevantSystem.UNSCANNED.value
def test_get_message_by_status_scanned_one_relevant_system(monkeypatch): def test_get_message_by_status_scanned_one_relevant_system():
monkeypatch.setattr(
(
"monkey_island.cc.services.attack.technique_reports."
"__init__.CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE"
),
FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE,
)
technique_msg = FakeAttackTechnique_OneRelevantSystem.get_message_by_status( technique_msg = FakeAttackTechnique_OneRelevantSystem.get_message_by_status(
ScanStatus.SCANNED.value ScanStatus.SCANNED.value
) )
assert technique_msg == ExpectedMsgs_OneRelevantSystem.SCANNED.value assert technique_msg == ExpectedMsgs_OneRelevantSystem.SCANNED.value
def test_get_message_by_status_used_one_relevant_system(monkeypatch): def test_get_message_by_status_used_one_relevant_system():
monkeypatch.setattr(
(
"monkey_island.cc.services.attack.technique_reports."
"__init__.CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE"
),
FAKE_CONFIG_SCHEMA_PER_ATTACK_TECHNIQUE,
)
technique_msg = FakeAttackTechnique_OneRelevantSystem.get_message_by_status( technique_msg = FakeAttackTechnique_OneRelevantSystem.get_message_by_status(
ScanStatus.USED.value ScanStatus.USED.value
) )