forked from p15670423/monkey
UT: Rename variables and fixtures to file repository
This commit is contained in:
parent
7caba8e399
commit
027c53a090
|
@ -12,7 +12,7 @@ FILE_NAME = "test_file"
|
|||
FILE_CONTENTS = b"HelloWorld!"
|
||||
|
||||
|
||||
class MockFileStorageService(IFileRepository):
|
||||
class MockFileRepository(IFileRepository):
|
||||
def __init__(self):
|
||||
self._file = io.BytesIO(FILE_CONTENTS)
|
||||
|
||||
|
@ -35,7 +35,7 @@ class MockFileStorageService(IFileRepository):
|
|||
@pytest.fixture
|
||||
def flask_client(build_flask_client):
|
||||
container = StubDIContainer()
|
||||
container.register(IFileRepository, MockFileStorageService)
|
||||
container.register(IFileRepository, MockFileRepository)
|
||||
|
||||
with build_flask_client(container) as flask_client:
|
||||
yield flask_client
|
||||
|
|
|
@ -40,7 +40,7 @@ def mock_get_config_value(monkeypatch):
|
|||
)
|
||||
|
||||
|
||||
class MockFileStorageService(IFileRepository):
|
||||
class MockFileRepository(IFileRepository):
|
||||
def __init__(self):
|
||||
self._file = None
|
||||
|
||||
|
@ -60,14 +60,14 @@ class MockFileStorageService(IFileRepository):
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def file_storage_service():
|
||||
return MockFileStorageService()
|
||||
def file_repository():
|
||||
return MockFileRepository()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def flask_client(build_flask_client, file_storage_service):
|
||||
def flask_client(build_flask_client, file_repository):
|
||||
container = StubDIContainer()
|
||||
container.register_instance(IFileRepository, file_storage_service)
|
||||
container.register_instance(IFileRepository, file_repository)
|
||||
|
||||
with build_flask_client(container) as flask_client:
|
||||
yield flask_client
|
||||
|
@ -98,9 +98,9 @@ def test_pba_file_upload_post__invalid(flask_client, mock_set_config_value):
|
|||
|
||||
@pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE])
|
||||
def test_pba_file_upload_post__internal_server_error(
|
||||
flask_client, pba_os, mock_set_config_value, file_storage_service
|
||||
flask_client, pba_os, mock_set_config_value, file_repository
|
||||
):
|
||||
file_storage_service.save_file = lambda x, y: raise_(Exception())
|
||||
file_repository.save_file = lambda x, y: raise_(Exception())
|
||||
url = get_url_for_resource(FileUpload, target_os=pba_os)
|
||||
|
||||
resp = flask_client.post(
|
||||
|
|
|
@ -9,18 +9,18 @@ from monkey_island.cc.services.post_breach_files import PostBreachFilesService
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def file_storage_service(tmp_path):
|
||||
def local_storage_file_repository(tmp_path):
|
||||
return LocalStorageFileRepository(tmp_path)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def post_breach_files_service(file_storage_service):
|
||||
PostBreachFilesService.initialize(file_storage_service)
|
||||
def post_breach_files_service(local_storage_file_repository):
|
||||
PostBreachFilesService.initialize(local_storage_file_repository)
|
||||
|
||||
|
||||
def test_remove_pba_files(file_storage_service, tmp_path):
|
||||
file_storage_service.save_file("linux_file", io.BytesIO(b""))
|
||||
file_storage_service.save_file("windows_file", io.BytesIO(b""))
|
||||
def test_remove_pba_files(local_storage_file_repository, tmp_path):
|
||||
local_storage_file_repository.save_file("linux_file", io.BytesIO(b""))
|
||||
local_storage_file_repository.save_file("windows_file", io.BytesIO(b""))
|
||||
assert not dir_is_empty(tmp_path)
|
||||
|
||||
PostBreachFilesService.remove_PBA_files()
|
||||
|
@ -33,11 +33,11 @@ def dir_is_empty(dir_path):
|
|||
return len(dir_contents) == 0
|
||||
|
||||
|
||||
def test_remove_failure(file_storage_service, monkeypatch):
|
||||
def test_remove_failure(local_storage_file_repository, monkeypatch):
|
||||
monkeypatch.setattr(os, "remove", lambda x: raise_(OSError("Permission denied")))
|
||||
|
||||
try:
|
||||
file_storage_service.save_file("windows_file", io.BytesIO(b""))
|
||||
local_storage_file_repository.save_file("windows_file", io.BytesIO(b""))
|
||||
PostBreachFilesService.remove_PBA_files()
|
||||
except Exception as ex:
|
||||
pytest.fail(f"Unxepected exception: {ex}")
|
||||
|
|
Loading…
Reference in New Issue