From 84c8de6a7cf88d76e775fea354d22f00e825bcd4 Mon Sep 17 00:00:00 2001 From: Mike Salvatore Date: Thu, 15 Sep 2022 11:55:48 -0400 Subject: [PATCH] UT: Test error conditions in MongoCredentialsRepository Increases test coverage to 100% for MongoCredentialsRepository --- .../test_mongo_credentials_repository.py | 53 ++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_credentials_repository.py b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_credentials_repository.py index ffd3c3bfa..88c584534 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_credentials_repository.py +++ b/monkey/tests/unit_tests/monkey_island/cc/repository/test_mongo_credentials_repository.py @@ -9,7 +9,13 @@ from pymongo.database import Database from tests.data_for_tests.propagation_credentials import CREDENTIALS from common.credentials import Credentials -from monkey_island.cc.repository import MongoCredentialsRepository +from monkey_island.cc.repository import ( + ICredentialsRepository, + MongoCredentialsRepository, + RemovalError, + RetrievalError, + StorageError, +) from monkey_island.cc.server_utils.encryption import ILockableEncryptor CONFIGURED_CREDENTIALS = CREDENTIALS[0:3] @@ -40,6 +46,36 @@ def mongo_repository(mongo_client, repository_encryptor): return MongoCredentialsRepository(mongo_client, repository_encryptor) +@pytest.fixture +def error_raising_mock_mongo_client() -> mongomock.MongoClient: + mongo_client = MagicMock(spec=mongomock.MongoClient) + mongo_client.monkey_island = MagicMock(spec=mongomock.Database) + mongo_client.monkey_island.stolen_credentials = MagicMock(spec=mongomock.Collection) + mongo_client.monkey_island.configured_credentials = MagicMock(spec=mongomock.Collection) + + mongo_client.monkey_island.configured_credentials.find = MagicMock( + side_effect=Exception("some exception") + ) + mongo_client.monkey_island.stolen_credentials.find = MagicMock( + side_effect=Exception("some exception") + ) + mongo_client.monkey_island.stolen_credentials.insert_one = MagicMock( + side_effect=Exception("some exception") + ) + mongo_client.monkey_island.stolen_credentials.drop = MagicMock( + side_effect=Exception("some exception") + ) + + return mongo_client + + +@pytest.fixture +def error_raising_credentials_repository( + error_raising_mock_mongo_client: mongomock.MongoClient, repository_encryptor: ILockableEncryptor +) -> ICredentialsRepository: + return MongoCredentialsRepository(error_raising_mock_mongo_client, repository_encryptor) + + def test_mongo_repository_get_configured(mongo_repository): actual_configured_credentials = mongo_repository.get_configured_credentials() @@ -91,6 +127,21 @@ def test_mongo_repository_all(mongo_repository): assert mongo_repository.get_configured_credentials() == [] +def test_mongo_repository_get__retrieval_error(error_raising_credentials_repository): + with pytest.raises(RetrievalError): + error_raising_credentials_repository.get_all_credentials() + + +def test_mongo_repository_save__storage_error(error_raising_credentials_repository): + with pytest.raises(StorageError): + error_raising_credentials_repository.save_stolen_credentials(STOLEN_CREDENTIALS) + + +def test_mongo_repository_remove_credentials__removal_error(error_raising_credentials_repository): + with pytest.raises(RemovalError): + error_raising_credentials_repository.remove_stolen_credentials() + + @pytest.mark.parametrize("credentials", CREDENTIALS) def test_configured_secrets_encrypted( mongo_repository: MongoCredentialsRepository,