Skip to content

Commit

Permalink
Fix the changes and add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhwaniartefact committed Oct 22, 2024
1 parent 6283b79 commit bb89610
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
3 changes: 1 addition & 2 deletions fixity/storage_service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import calendar
import json
from datetime import datetime
from typing import Dict

import requests
from sqlalchemy.orm.exc import NoResultFound
Expand All @@ -24,7 +23,7 @@ class StorageServiceError(Exception):
itself will not return, but the caller still needs access to it.
"""

def __init__(self, message: str, report: Dict[str, str] = None) -> None:
def __init__(self, message, report = None):
self.report = report
super().__init__(message)

Expand Down
48 changes: 42 additions & 6 deletions tests/test_fixity.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,16 +643,15 @@ 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
aip_id = uuid.uuid4()
stream = io.StringIO()
response = fixity.main(["scan", str(aip_id)], stream=stream)

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_is_not_connected(
def test_scanall_handles_exception_if_storage_service_raises_exception(
_get: mock.Mock, environment: None
) -> None:
_get.side_effect = [
Expand All @@ -663,12 +662,49 @@ def test_scanall_handles_exception_if_storage_service_is_not_connected(
spec=requests.Response,
)
]
stream = io.StringIO()

response = fixity.main(["scanall"], stream=stream)
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,
mock_check_fixity: List[mock.Mock],
monkeypatch: pytest.MonkeyPatch,
) -> None:
_get.side_effect = mock_check_fixity
aip_id = uuid.uuid4()
stream = io.StringIO()
monkeypatch.setenv("STORAGE_SERVICE_URL", "http://foo")
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 == 0

_assert_stream_content_matches(
stream,
[
f"Unable to POST pre-scan report to {report_url}/",
f"Fixity scan succeeded for AIP: {aip_id}",
f"Unable to POST report for AIP {aip_id} to remote service",
],
)


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

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

0 comments on commit bb89610

Please sign in to comment.