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!"
|
FILE_CONTENTS = b"HelloWorld!"
|
||||||
|
|
||||||
|
|
||||||
class MockFileStorageService(IFileRepository):
|
class MockFileRepository(IFileRepository):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._file = io.BytesIO(FILE_CONTENTS)
|
self._file = io.BytesIO(FILE_CONTENTS)
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ class MockFileStorageService(IFileRepository):
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def flask_client(build_flask_client):
|
def flask_client(build_flask_client):
|
||||||
container = StubDIContainer()
|
container = StubDIContainer()
|
||||||
container.register(IFileRepository, MockFileStorageService)
|
container.register(IFileRepository, MockFileRepository)
|
||||||
|
|
||||||
with build_flask_client(container) as flask_client:
|
with build_flask_client(container) as flask_client:
|
||||||
yield flask_client
|
yield flask_client
|
||||||
|
|
|
@ -40,7 +40,7 @@ def mock_get_config_value(monkeypatch):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class MockFileStorageService(IFileRepository):
|
class MockFileRepository(IFileRepository):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._file = None
|
self._file = None
|
||||||
|
|
||||||
|
@ -60,14 +60,14 @@ class MockFileStorageService(IFileRepository):
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def file_storage_service():
|
def file_repository():
|
||||||
return MockFileStorageService()
|
return MockFileRepository()
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def flask_client(build_flask_client, file_storage_service):
|
def flask_client(build_flask_client, file_repository):
|
||||||
container = StubDIContainer()
|
container = StubDIContainer()
|
||||||
container.register_instance(IFileRepository, file_storage_service)
|
container.register_instance(IFileRepository, file_repository)
|
||||||
|
|
||||||
with build_flask_client(container) as flask_client:
|
with build_flask_client(container) as flask_client:
|
||||||
yield 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])
|
@pytest.mark.parametrize("pba_os", [LINUX_PBA_TYPE, WINDOWS_PBA_TYPE])
|
||||||
def test_pba_file_upload_post__internal_server_error(
|
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)
|
url = get_url_for_resource(FileUpload, target_os=pba_os)
|
||||||
|
|
||||||
resp = flask_client.post(
|
resp = flask_client.post(
|
||||||
|
|
|
@ -9,18 +9,18 @@ from monkey_island.cc.services.post_breach_files import PostBreachFilesService
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def file_storage_service(tmp_path):
|
def local_storage_file_repository(tmp_path):
|
||||||
return LocalStorageFileRepository(tmp_path)
|
return LocalStorageFileRepository(tmp_path)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
@pytest.fixture(autouse=True)
|
||||||
def post_breach_files_service(file_storage_service):
|
def post_breach_files_service(local_storage_file_repository):
|
||||||
PostBreachFilesService.initialize(file_storage_service)
|
PostBreachFilesService.initialize(local_storage_file_repository)
|
||||||
|
|
||||||
|
|
||||||
def test_remove_pba_files(file_storage_service, tmp_path):
|
def test_remove_pba_files(local_storage_file_repository, tmp_path):
|
||||||
file_storage_service.save_file("linux_file", io.BytesIO(b""))
|
local_storage_file_repository.save_file("linux_file", io.BytesIO(b""))
|
||||||
file_storage_service.save_file("windows_file", io.BytesIO(b""))
|
local_storage_file_repository.save_file("windows_file", io.BytesIO(b""))
|
||||||
assert not dir_is_empty(tmp_path)
|
assert not dir_is_empty(tmp_path)
|
||||||
|
|
||||||
PostBreachFilesService.remove_PBA_files()
|
PostBreachFilesService.remove_PBA_files()
|
||||||
|
@ -33,11 +33,11 @@ def dir_is_empty(dir_path):
|
||||||
return len(dir_contents) == 0
|
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")))
|
monkeypatch.setattr(os, "remove", lambda x: raise_(OSError("Permission denied")))
|
||||||
|
|
||||||
try:
|
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()
|
PostBreachFilesService.remove_PBA_files()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
pytest.fail(f"Unxepected exception: {ex}")
|
pytest.fail(f"Unxepected exception: {ex}")
|
||||||
|
|
Loading…
Reference in New Issue