Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend test coverage of the fixity module #56

Merged
merged 7 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fixity/fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ def get_handler(stream, timestamps, log_level=None):

def main(
argv: Optional[List[str]] = None,
logger: Union[logging.Logger] = None,
logger: Optional[logging.Logger] = None,
stream: Optional[TextIO] = None,
) -> Union[int, bool, Type[Exception]]:
) -> Optional[Union[int, bool, Type[Exception]]]:
if logger is None:
logger = get_logger()
if stream is None:
Expand Down
76 changes: 76 additions & 0 deletions tests/test_fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

from fixity import fixity
from fixity import reporting
from fixity.fixity import ArgumentError
from fixity.models import Report
from fixity.models import Session
from fixity.storage_service import StorageServiceError

SESSION = Session()
STORAGE_SERVICE_URL = "http://localhost:8000/"
Expand Down Expand Up @@ -634,3 +636,77 @@ def test_scanall_if_sort_argument_is_passed(
params={"username": STORAGE_SERVICE_USER, "api_key": STORAGE_SERVICE_KEY},
),
]


@mock.patch("requests.get")
def test_main_handles_exception_if_environment_key_is_missing(
_get: mock.Mock, mock_check_fixity: List[mock.Mock]
) -> None:
_get.side_effect = mock_check_fixity

response = fixity.main(["scan", str(uuid.uuid4())])

assert str(response) == "Missing environment variable: STORAGE_SERVICE_URL"
assert isinstance(response, ArgumentError)


@mock.patch("requests.get")
def test_scanall_handles_exception_if_storage_service_raises_exception(
_get: mock.Mock, environment: None
) -> None:
_get.side_effect = [
mock.Mock(
**{
"status_code": 401,
},
spec=requests.Response,
)
]

response = fixity.main(["scanall"])

assert (
str(response)
== f'Storage service at "{STORAGE_SERVICE_URL}" failed authentication while requesting AIPs'
)
assert isinstance(response, StorageServiceError)


@mock.patch("requests.get")
def test_main_verifies_urls_with_trailing_slash(
_get: mock.Mock,
monkeypatch: pytest.MonkeyPatch,
) -> None:
_get.side_effect = [
mock.Mock(**{"status_code": 200}, spec=requests.Response),
mock.Mock(**{"status_code": 401}, spec=requests.Response),
]
aip_id = uuid.uuid4()
stream = io.StringIO()
ss_url = "http://foo"
monkeypatch.setenv("STORAGE_SERVICE_URL", ss_url)
monkeypatch.setenv("STORAGE_SERVICE_USER", STORAGE_SERVICE_USER)
monkeypatch.setenv("STORAGE_SERVICE_KEY", STORAGE_SERVICE_KEY)
report_url = "http://bar"
monkeypatch.setenv("REPORT_URL", report_url)
monkeypatch.setenv("REPORT_USERNAME", "test")
monkeypatch.setenv("REPORT_PASSWORD", "test123")

response = fixity.main(["scan", str(aip_id)], stream=stream)

assert response is None

_assert_stream_content_matches(
stream,
[
f"Unable to POST pre-scan report to {report_url}/",
f'Storage service at "{ss_url}/" failed authentication while scanning AIP {aip_id}',
],
)


def test_main_validates_arguments() -> None:
response = fixity.main(["scan"])

assert str(response) == "An AIP UUID must be specified when scanning a single AIP"
assert isinstance(response, ArgumentError)