Skip to content

Commit

Permalink
Add a couple more test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen committed Sep 6, 2024
1 parent 83251bd commit 40bf9d5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
12 changes: 12 additions & 0 deletions tests/manager/api/odl/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,18 @@ def test_update_loan_still_active(
assert 6 == opds2_with_odl_api_fixture.pool.licenses_available
assert 1 == db.session.query(Loan).count()

def test_update_loan_bad_status(
self,
db: DatabaseTransactionFixture,
opds2_with_odl_api_fixture: OPDS2WithODLApiFixture,
) -> None:
status_doc = {
"status": "foo",
}

with pytest.raises(RemoteIntegrationException, match="unknown status value"):
opds2_with_odl_api_fixture.api.update_loan(MagicMock(), status_doc)

def test_update_loan_removes_loan(
self,
db: DatabaseTransactionFixture,
Expand Down
50 changes: 49 additions & 1 deletion tests/manager/core/test_opds2_import.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import json
from collections.abc import Generator
from contextlib import nullcontext
from unittest.mock import MagicMock, patch

import pytest
Expand All @@ -11,7 +12,12 @@

from palace.manager.api.circulation import CirculationAPI, FulfillmentInfo
from palace.manager.api.circulation_exceptions import CannotFulfill
from palace.manager.core.opds2_import import OPDS2API, OPDS2Importer, RWPMManifestParser
from palace.manager.core.opds2_import import (
OPDS2API,
OPDS2Importer,
OPDS2ImportMonitor,
RWPMManifestParser,
)
from palace.manager.sqlalchemy.constants import (
EditionConstants,
IdentifierType,
Expand All @@ -29,8 +35,10 @@
from palace.manager.sqlalchemy.model.patron import Loan
from palace.manager.sqlalchemy.model.work import Work
from palace.manager.util.datetime_helpers import utc_now
from palace.manager.util.http import BadResponseException
from tests.fixtures.database import DatabaseTransactionFixture
from tests.fixtures.files import OPDS2FilesFixture
from tests.mocks.mock import MockRequestsResponse


class OPDS2Test:
Expand Down Expand Up @@ -893,3 +901,43 @@ def test_get_authentication_token_bad_response(
OPDS2API.get_authentication_token(
opds2_api_fixture.patron, opds2_api_fixture.data_source, ""
)


class TestOPDS2ImportMonitor:
@pytest.mark.parametrize(
"content_type,exception",
[
("application/json", False),
("application/opds+json", False),
("application/xml", True),
("foo/xyz", True),
],
)
def test__verify_media_type(
self, db: DatabaseTransactionFixture, content_type: str, exception: bool
) -> None:
collection = db.collection(
protocol=OPDS2API.label(),
data_source_name="test",
external_account_id="http://test.com",
)
monitor = OPDS2ImportMonitor(
db.session,
collection,
OPDS2Importer,
parser=RWPMManifestParser(OPDS2FeedParserFactory()),
)

ctx_manager = (
nullcontext()
if not exception
else pytest.raises(
BadResponseException, match="Bad response from http://test.com"
)
)

mock_response = MockRequestsResponse(
status_code=200, headers={"Content-Type": content_type}
)
with ctx_manager:
monitor._verify_media_type("http://test.com", mock_response)

0 comments on commit 40bf9d5

Please sign in to comment.