UT: Use mock encryptor

This commit is contained in:
Kekoa Kaaikala 2022-09-20 11:37:11 +00:00
parent 875a54aa8f
commit e0cebd144a
3 changed files with 23 additions and 27 deletions

View File

@ -1,10 +1,26 @@
from unittest.mock import MagicMock
import pytest
from tests.unit_tests.monkey_island.cc.mongomock_fixtures import * # noqa: F401,F403,E402
from monkey_island.cc.server_utils.encryption import unlock_datastore_encryptor
from monkey_island.cc.server_utils.encryption import ILockableEncryptor, unlock_datastore_encryptor
@pytest.fixture
def uses_encryptor(data_for_tests_dir):
secret = "m0nk3y_u53r:3cr3t_p455w0rd"
unlock_datastore_encryptor(data_for_tests_dir, secret)
def reverse(data: bytes) -> bytes:
return bytes(reversed(data))
@pytest.fixture
def repository_encryptor():
# NOTE: Tests will fail if any inputs to this mock encryptor are palindromes.
repository_encryptor = MagicMock(spec=ILockableEncryptor)
repository_encryptor.encrypt = MagicMock(side_effect=reverse)
repository_encryptor.decrypt = MagicMock(side_effect=reverse)
return repository_encryptor

View File

@ -18,7 +18,6 @@ from monkey_island.cc.repository import (
RetrievalError,
StorageError,
)
from monkey_island.cc.server_utils.encryption import RepositoryEncryptor
class FakeAgentEvent(AbstractAgentEvent):
@ -60,15 +59,10 @@ def key_file(tmp_path):
@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 MongoAgentEventRepository(mongo_client, event_serializer_registry, encryptor)
def mongo_repository(
mongo_client, event_serializer_registry, repository_encryptor
) -> IAgentEventRepository:
return MongoAgentEventRepository(mongo_client, event_serializer_registry, repository_encryptor)
@pytest.fixture
@ -88,10 +82,10 @@ 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, repository_encryptor
) -> IAgentEventRepository:
return MongoAgentEventRepository(
error_raising_mongo_client, event_serializer_registry, encryptor
error_raising_mongo_client, event_serializer_registry, repository_encryptor
)

View File

@ -22,20 +22,6 @@ CONFIGURED_CREDENTIALS = CREDENTIALS[0:3]
STOLEN_CREDENTIALS = CREDENTIALS[3:]
def reverse(data: bytes) -> bytes:
return bytes(reversed(data))
@pytest.fixture
def repository_encryptor():
# NOTE: Tests will fail if any inputs to this mock encryptor are palindromes.
repository_encryptor = MagicMock(spec=ILockableEncryptor)
repository_encryptor.encrypt = MagicMock(side_effect=reverse)
repository_encryptor.decrypt = MagicMock(side_effect=reverse)
return repository_encryptor
@pytest.fixture
def mongo_client():
return mongomock.MongoClient()