Refactored to use fixtures without enum, to be consistent with other UT code

This commit is contained in:
VakarisZ 2021-05-19 09:38:12 +03:00
parent c85ac0f610
commit 4f25e1b6c8
11 changed files with 20 additions and 32 deletions

View File

@ -1,2 +0,0 @@
class FixtureEnum:
USES_DATABASE = "uses_database"

View File

@ -3,7 +3,6 @@ import uuid
from time import sleep
import pytest
from tests.monkey_island.cc.fixture_enum import FixtureEnum
from monkey_island.cc.models.monkey import Monkey, MonkeyNotFoundError
from monkey_island.cc.models.monkey_ttl import MonkeyTtl
@ -12,7 +11,7 @@ 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()

View File

@ -1,6 +1,5 @@
import pytest
from mongoengine import ValidationError
from tests.monkey_island.cc.fixture_enum import FixtureEnum
import common.common_consts.zero_trust_consts as zero_trust_consts
from monkey_island.cc.models.zero_trust.event import Event
@ -13,7 +12,7 @@ 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

View File

@ -1,6 +1,5 @@
import pytest
from mongoengine import ValidationError
from tests.monkey_island.cc.fixture_enum import FixtureEnum
import common.common_consts.zero_trust_consts as zero_trust_consts
from monkey_island.cc.models.zero_trust.finding import Finding
@ -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

View File

@ -2,7 +2,6 @@ import logging
import pytest
from mongomock import ObjectId
from tests.monkey_island.cc.fixture_enum import FixtureEnum
from monkey_island.cc.models.edge import Edge
from monkey_island.cc.services.edge.edge import EdgeService
@ -11,7 +10,7 @@ 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()

View File

@ -1,7 +1,6 @@
from datetime import datetime
import pytest
from tests.monkey_island.cc.fixture_enum import FixtureEnum
from common.common_consts import zero_trust_consts
from monkey_island.cc.models.zero_trust.event import 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(

View File

@ -2,7 +2,6 @@ from unittest.mock import MagicMock
import dpath.util
import pytest
from tests.monkey_island.cc.fixture_enum import FixtureEnum
from common.config_value_paths import AWS_KEYS_PATH
from monkey_island.cc.database import mongo
@ -17,7 +16,7 @@ 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()

View File

@ -1,5 +1,4 @@
import pytest
from tests.monkey_island.cc.fixture_enum import FixtureEnum
from monkey_island.cc.models.zero_trust.finding import Finding
from monkey_island.cc.models.zero_trust.scoutsuite_finding import ScoutSuiteFinding
@ -13,7 +12,7 @@ from monkey_island.cc.services.zero_trust.test_common.scoutsuite_finding_data im
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])

View File

@ -1,7 +1,6 @@
from unittest.mock import MagicMock
import pytest
from tests.monkey_island.cc.fixture_enum import FixtureEnum
from common.common_consts.zero_trust_consts import (
DEVICES,
@ -25,7 +24,7 @@ from monkey_island.cc.services.zero_trust.zero_trust_report.finding_service impo
)
@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()

View File

@ -1,7 +1,6 @@
from typing import List
import pytest
from tests.monkey_island.cc.fixture_enum import FixtureEnum
from common.common_consts import zero_trust_consts
from common.common_consts.zero_trust_consts import (
@ -19,7 +18,7 @@ from monkey_island.cc.services.zero_trust.zero_trust_report.test_common.example_
)
@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 = {

View File

@ -1,5 +1,4 @@
import pytest
from tests.monkey_island.cc.fixture_enum import FixtureEnum
from common.common_consts import zero_trust_consts
from monkey_island.cc.services.zero_trust.test_common.finding_data import (
@ -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"