forked from p15670423/monkey
UT: Add StubDIContainer
This commit is contained in:
parent
528ca76c32
commit
68f31db03a
|
@ -1 +1 @@
|
||||||
from .di_container import DIContainer
|
from .di_container import DIContainer, UnregisteredTypeError
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
from .stub_di_container import StubDIContainer
|
|
@ -0,0 +1,20 @@
|
||||||
|
from typing import Any, Sequence, Type, TypeVar
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
|
from common import DIContainer, UnregisteredTypeError
|
||||||
|
|
||||||
|
T = TypeVar("T")
|
||||||
|
|
||||||
|
|
||||||
|
class StubDIContainer(DIContainer):
|
||||||
|
def resolve(self, type_: Type[T]) -> T:
|
||||||
|
try:
|
||||||
|
return super().resolve(type_)
|
||||||
|
except UnregisteredTypeError:
|
||||||
|
return MagicMock()
|
||||||
|
|
||||||
|
def resolve_dependencies(self, type_: Type[T]) -> Sequence[Any]:
|
||||||
|
try:
|
||||||
|
return super().resolve_dependencies(type_)
|
||||||
|
except UnregisteredTypeError:
|
||||||
|
return MagicMock()
|
|
@ -2,8 +2,8 @@ import io
|
||||||
from typing import BinaryIO
|
from typing import BinaryIO
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from tests.common import StubDIContainer
|
||||||
|
|
||||||
from common import DIContainer
|
|
||||||
from monkey_island.cc.services import FileRetrievalError, IFileStorageService
|
from monkey_island.cc.services import FileRetrievalError, IFileStorageService
|
||||||
|
|
||||||
FILE_NAME = "test_file"
|
FILE_NAME = "test_file"
|
||||||
|
@ -32,7 +32,7 @@ class MockFileStorageService(IFileStorageService):
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def flask_client(build_flask_client, tmp_path):
|
def flask_client(build_flask_client, tmp_path):
|
||||||
container = DIContainer()
|
container = StubDIContainer()
|
||||||
container.register(IFileStorageService, MockFileStorageService)
|
container.register(IFileStorageService, MockFileStorageService)
|
||||||
|
|
||||||
with build_flask_client(container) as flask_client:
|
with build_flask_client(container) as flask_client:
|
||||||
|
|
|
@ -2,9 +2,9 @@ import io
|
||||||
from typing import BinaryIO
|
from typing import BinaryIO
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from tests.common import StubDIContainer
|
||||||
from tests.utils import raise_
|
from tests.utils import raise_
|
||||||
|
|
||||||
from common import DIContainer
|
|
||||||
from monkey_island.cc.resources.pba_file_upload import LINUX_PBA_TYPE, WINDOWS_PBA_TYPE
|
from monkey_island.cc.resources.pba_file_upload import LINUX_PBA_TYPE, WINDOWS_PBA_TYPE
|
||||||
from monkey_island.cc.services import FileRetrievalError, IFileStorageService
|
from monkey_island.cc.services import FileRetrievalError, IFileStorageService
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ def file_storage_service():
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def flask_client(build_flask_client, file_storage_service):
|
def flask_client(build_flask_client, file_storage_service):
|
||||||
container = DIContainer()
|
container = StubDIContainer()
|
||||||
container.register_instance(IFileStorageService, file_storage_service)
|
container.register_instance(IFileStorageService, file_storage_service)
|
||||||
|
|
||||||
with build_flask_client(container) as flask_client:
|
with build_flask_client(container) as flask_client:
|
||||||
|
|
Loading…
Reference in New Issue