Skip to content

Commit

Permalink
Edit tests, add null checks and fix merge bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
ttuovinen committed Jan 24, 2024
1 parent 2ceeeb4 commit 3217e3f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 73 deletions.
3 changes: 1 addition & 2 deletions api/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ def from_config(
# Finland
if authenticator.ekirjasto_provider:
authenticator.ekirjasto_provider.set_secrets(_db)

authenticator.assert_ready_for_token_signing()

return authenticator
Expand Down Expand Up @@ -499,7 +498,7 @@ def authenticated_patron(
provider_token = provider.validate_ekirjasto_delegate_token(auth.token)
if isinstance(provider_token, ProblemDetail):
return provider_token
elif self.saml_providers_by_name and auth.type.lower() == "bearer":
elif auth.type.lower() == "bearer":
# The patron wants to use an
# SAMLAuthenticationProvider. Figure out which one.
if auth.token is None:
Expand Down
99 changes: 34 additions & 65 deletions api/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from api.circulation_exceptions import *
from api.config import CannotLoadConfiguration, Configuration
from api.ekirjasto_controller import EkirjastoController # Finland
from api.opensearch_analytics_search import OpenSearchAnalyticsSearch
from api.opensearch_analytics_search import OpenSearchAnalyticsSearch # Finland
from api.custom_index import CustomIndexView
from api.lanes import (
ContributorFacets,
Expand Down Expand Up @@ -84,8 +84,6 @@
DeliveryMechanism,
Hold,
Identifier,
IntegrationConfiguration,
IntegrationLibraryConfiguration,
Library,
LicensePool,
LicensePoolDeliveryMechanism,
Expand Down Expand Up @@ -180,7 +178,6 @@ class CirculationManager:
odl_notification_controller: ODLNotificationController
static_files: StaticFileController
playtime_entries: PlaytimeEntriesController
catalog_descriptions: CatalogDescriptionsController

# Admin controllers
admin_sign_in_controller: SignInController
Expand Down Expand Up @@ -450,7 +447,6 @@ def setup_one_time_controllers(self):
self.static_files = StaticFileController(self)
self.patron_auth_token = PatronAuthTokenController(self)
self.playtime_entries = PlaytimeEntriesController(self)
self.catalog_descriptions = CatalogDescriptionsController(self)

def setup_configuration_dependent_controllers(self):
"""Set up all the controllers that depend on the
Expand Down Expand Up @@ -689,27 +685,13 @@ def load_licensepools(self, library, identifier_type, identifier):
"""
_db = Session.object_session(library)
pools = (
_db.scalars(
select(LicensePool)
.join(Collection, LicensePool.collection_id == Collection.id)
.join(Identifier, LicensePool.identifier_id == Identifier.id)
.join(
IntegrationConfiguration,
Collection.integration_configuration_id
== IntegrationConfiguration.id,
)
.join(
IntegrationLibraryConfiguration,
IntegrationConfiguration.id
== IntegrationLibraryConfiguration.parent_id,
)
.where(
Identifier.type == identifier_type,
Identifier.identifier == identifier,
IntegrationLibraryConfiguration.library_id == library.id,
)
)
.unique()
_db.query(LicensePool)
.join(LicensePool.collection)
.join(LicensePool.identifier)
.join(Collection.libraries)
.filter(Identifier.type == identifier_type)
.filter(Identifier.identifier == identifier)
.filter(Library.id == library.id)
.all()
)
if not pools:
Expand Down Expand Up @@ -1024,7 +1006,7 @@ def crawlable_collection_feed(self, collection_name):
"""Build or retrieve a crawlable acquisition feed for the
requested collection.
"""
collection = Collection.by_name(self._db, collection_name)
collection = get_one(self._db, Collection, name=collection_name)
if not collection:
return NO_SUCH_COLLECTION
title = collection.name
Expand Down Expand Up @@ -1437,7 +1419,7 @@ def borrow(self, identifier_type, identifier, mechanism_id=None):
"""
patron = flask.request.patron
library = flask.request.library

header = self.authorization_header()
credential = self.manager.auth.get_credential_from_header(header)

Expand Down Expand Up @@ -2443,13 +2425,13 @@ def image(self, filename):
)
return self.static_file(directory, filename)


# Finland
class CatalogDescriptionsController(CirculationManagerController):
def get_catalogs(self, library_uuid=None):
catalogs = []
libraries = []



if library_uuid != None:
try:
libraries = [
Expand All @@ -2468,87 +2450,74 @@ def get_catalogs(self, library_uuid=None):
{
"rel": "http://opds-spec.org/image/thumbnail",
"href": library.logo.data_url,
"type": "image/png"
"type": "image/png",
}
]

authentication_document_url = url_for(
"authentication_document",
library_short_name=library.short_name,
_external=True
library_short_name=library.short_name,
_external=True,
)

catalog_url = url_for(
"acquisition_groups",
library_short_name=library.short_name,
_external=True
library_short_name=library.short_name,
_external=True,
)
timenow = utc_now().strftime('%Y-%m-%dT%H:%M:%SZ')

timenow = utc_now().strftime("%Y-%m-%dT%H:%M:%SZ")

metadata = {
"id": "urn:uuid:" + library.uuid,
"title": library.name,
"short_name": library.short_name,
"modified": timenow,
"updated": timenow,
"isAutomatic": False
"isAutomatic": False,
}

if "library_description" in settings:
metadata["description"] = settings["library_description"]

links = [
{
"rel": "http://opds-spec.org/catalog",
"href": catalog_url,
"type": "application/atom+xml;profile=opds-catalog;kind=acquisition"
"type": "application/atom+xml;profile=opds-catalog;kind=acquisition",
},
{
"href": authentication_document_url,
"type": "application/vnd.opds.authentication.v1.0+json"
}
"type": "application/vnd.opds.authentication.v1.0+json",
},
]

if "help_web" in settings:
links += [{
"href": settings["help_web"],
"rel": "help"
}]
links += [{"href": settings["help_web"], "rel": "help"}]
elif "help_email" in settings:
links += [{
"href": "mailto:"+settings["help_email"],
"rel": "help"
}]
links += [{"href": "mailto:" + settings["help_email"], "rel": "help"}]

catalogs += [{"metadata": metadata, "links": links, "images": images}]

catalogs += [
{
"metadata": metadata,
"links": links,
"images": images
}
]

response_json = {
"metadata": {
"title": "Libraries"
},
"metadata": {"title": "Libraries"},
"catalogs": catalogs,
"links": [
{
"rel": "self",
"href": url_for("client_libraries", _external=True),
"type": "application/opds+json"
"type": "application/opds+json",
}
]
],
}

return Response(
json_serializer(response_json),
status=200,
mimetype="application/json",
)


class PatronAuthTokenController(CirculationManagerController):
def get_token(self):
"""Create a Patron Auth access token for an authenticated patron"""
Expand Down
14 changes: 10 additions & 4 deletions api/opensearch_analytics_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,23 @@ def _create_event_object(
"authors": [
contribution.contributor.sort_name
for contribution in edition.contributions
if contribution.role == Contributor.AUTHOR_ROLE
if getattr(contribution.contributor, "role", None)
== Contributor.AUTHOR_ROLE
]
if edition
else None,
"contributions": [
": ".join(
contribution.contributor.role,
contribution.contributor.sort_name,
[
getattr(contribution.contributor, "role", ""),
contribution.contributor.sort_name,
]
)
for contribution in edition.contributions
if contribution.role != Contributor.AUTHOR_ROLE
if (
not getattr(contribution.contributor, "role", None)
or contribution.contributor.role != Contributor.AUTHOR_ROLE
)
]
if edition
else None,
Expand Down
4 changes: 2 additions & 2 deletions tests/api/admin/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ def test_package_url(
[
None,
None,
"/my-base-dir/node_modules/@natlibfi/circulation-admin",
"/my-base-dir/node_modules/@natlibfi/ekirjasto-circulation-admin",
],
[
None,
"1.0.0",
"/my-base-dir/node_modules/@natlibfi/circulation-admin",
"/my-base-dir/node_modules/@natlibfi/ekirjasto-circulation-admin",
],
["some-package", "1.0.0", "/my-base-dir/node_modules/some-package"],
],
Expand Down

0 comments on commit 3217e3f

Please sign in to comment.