From c19e50b7f120f41845b4841892a4701e31d7472b Mon Sep 17 00:00:00 2001 From: Kekoa Kaaikala Date: Fri, 16 Sep 2022 13:43:10 +0000 Subject: [PATCH] UT: Fix tests for event encryption --- .../repository/test_agent_event_encryption.py | 2 +- .../repository/test_mongo_event_repository.py | 21 +++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_agent_event_encryption.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_agent_event_encryption.py index 0ebd0a5c6..ff3b78b89 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_agent_event_encryption.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_agent_event_encryption.py @@ -30,7 +30,7 @@ def encryptor(key_file): @pytest.fixture def serializer(): - return PydanticAgentEventSerializer() + return PydanticAgentEventSerializer(FakeAgentEvent) def test_agent_event_encryption__encrypts(encryptor, serializer): diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py index 99a780b3d..576f4a3de 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_event_repository.py @@ -18,6 +18,7 @@ from monkey_island.cc.repository import ( RetrievalError, StorageError, ) +from monkey_island.cc.server_utils.encryption import RepositoryEncryptor class FakeAgentEvent(AbstractAgentEvent): @@ -54,8 +55,20 @@ def mongo_client(event_serializer_registry): @pytest.fixture -def mongo_repository(mongo_client, event_serializer_registry) -> IAgentEventRepository: - return MongoEventRepository(mongo_client, event_serializer_registry) +def key_file(tmp_path): + return tmp_path / "test_key.bin" + + +@pytest.fixture +def encryptor(key_file): + encryptor = RepositoryEncryptor(key_file) + encryptor.unlock(b"password") + return encryptor + + +@pytest.fixture +def mongo_repository(mongo_client, event_serializer_registry, encryptor) -> IAgentEventRepository: + return MongoEventRepository(mongo_client, event_serializer_registry, encryptor) @pytest.fixture @@ -75,9 +88,9 @@ def error_raising_mongo_client(mongo_client) -> mongomock.MongoClient: @pytest.fixture def error_raising_mongo_repository( - error_raising_mongo_client, event_serializer_registry + error_raising_mongo_client, event_serializer_registry, encryptor ) -> IAgentEventRepository: - return MongoEventRepository(error_raising_mongo_client, event_serializer_registry) + return MongoEventRepository(error_raising_mongo_client, event_serializer_registry, encryptor) def assert_same_contents(a, b):