UT: Use mongomock in MongoCredentialsRepository

This commit is contained in:
Ilija Lazoroski 2022-07-11 12:26:00 +02:00
parent 4226cb5b9e
commit 1b1bd7fcc2
1 changed files with 30 additions and 29 deletions

View File

@ -1,4 +1,4 @@
import mongoengine
import mongomock
import pytest
from common.credentials import Credentials
@ -53,80 +53,81 @@ CREDENTIALS_LIST = [
@pytest.fixture
def fake_mongo_repository(monkeypatch):
mongo = mongoengine.connection.get_connection()
def mongo_repository():
mongo = mongomock.MongoClient()
return MongoCredentialsRepository(mongo)
def test_mongo_repository_get_configured(fake_mongo_repository):
def test_mongo_repository_get_configured(mongo_repository):
actual_configured_credentials = fake_mongo_repository.get_configured_credentials()
actual_configured_credentials = mongo_repository.get_configured_credentials()
assert actual_configured_credentials == []
def test_mongo_repository_get_stolen(fake_mongo_repository):
def test_mongo_repository_get_stolen(mongo_repository):
actual_stolen_credentials = fake_mongo_repository.get_stolen_credentials()
actual_stolen_credentials = mongo_repository.get_stolen_credentials()
assert actual_stolen_credentials == []
def test_mongo_repository_get_all(fake_mongo_repository):
def test_mongo_repository_get_all(mongo_repository):
actual_credentials = fake_mongo_repository.get_all_credentials()
actual_credentials = mongo_repository.get_all_credentials()
assert actual_credentials == []
def test_mongo_repository_configured(fake_mongo_repository):
def test_mongo_repository_configured(mongo_repository):
fake_mongo_repository.save_configured_credentials(CREDENTIALS_LIST)
mongo_repository.save_configured_credentials(CREDENTIALS_LIST)
actual_configured_credentials = fake_mongo_repository.get_configured_credentials()
actual_configured_credentials = mongo_repository.get_configured_credentials()
assert actual_configured_credentials == CREDENTIALS_LIST
fake_mongo_repository.remove_configured_credentials()
mongo_repository.remove_configured_credentials()
actual_configured_credentials = fake_mongo_repository.get_configured_credentials()
actual_configured_credentials = mongo_repository.get_configured_credentials()
assert actual_configured_credentials == []
def test_mongo_repository_stolen(fake_mongo_repository):
def test_mongo_repository_stolen(mongo_repository):
fake_mongo_repository.save_configured_credentials(CONFIGURED_CREDENTIALS)
fake_mongo_repository.save_stolen_credentials(STOLEN_CREDENTIALS)
mongo_repository.save_configured_credentials(CONFIGURED_CREDENTIALS)
mongo_repository.save_stolen_credentials(STOLEN_CREDENTIALS)
actual_stolen_credentials = fake_mongo_repository.get_stolen_credentials()
actual_stolen_credentials = mongo_repository.get_stolen_credentials()
assert actual_stolen_credentials == STOLEN_CREDENTIALS
fake_mongo_repository.remove_stolen_credentials()
mongo_repository.remove_stolen_credentials()
actual_stolen_credentials = fake_mongo_repository.get_stolen_credentials()
actual_stolen_credentials = mongo_repository.get_stolen_credentials()
assert actual_stolen_credentials == []
# Must remove configured also for the next tests
fake_mongo_repository.remove_configured_credentials()
mongo_repository.remove_configured_credentials()
def test_mongo_repository_all(fake_mongo_repository):
def test_mongo_repository_all(mongo_repository):
fake_mongo_repository.save_configured_credentials(CONFIGURED_CREDENTIALS)
fake_mongo_repository.save_stolen_credentials(STOLEN_CREDENTIALS)
mongo_repository.save_configured_credentials(CONFIGURED_CREDENTIALS)
mongo_repository.save_stolen_credentials(STOLEN_CREDENTIALS)
actual_credentials = fake_mongo_repository.get_all_credentials()
actual_credentials = mongo_repository.get_all_credentials()
assert actual_credentials == CREDENTIALS_LIST
fake_mongo_repository.remove_all_credentials()
mongo_repository.remove_all_credentials()
actual_credentials = fake_mongo_repository.get_all_credentials()
actual_stolen_credentials = fake_mongo_repository.get_stolen_credentials()
actual_configured_credentials = fake_mongo_repository.get_configured_credentials()
actual_credentials = mongo_repository.get_all_credentials()
actual_stolen_credentials = mongo_repository.get_stolen_credentials()
actual_configured_credentials = mongo_repository.get_configured_credentials()
assert actual_credentials == []
assert actual_stolen_credentials == []