Skip to content

Commit

Permalink
Log auth document URL when Catalog Root link missing. (PP-1209) (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdilauro authored Jan 28, 2025
1 parent a370ff3 commit 77d03df
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### UNRELEASED CHANGES

- Log auth document URL when Catalog Root link missing. (PP-1209)
- Update default libraries config.
- Remove no longer needed node option: -openssl-legacy-provider.
- Fix a couple of vulnerable dependencies.
Expand Down
28 changes: 28 additions & 0 deletions src/dataflow/__tests__/getLibraryData.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ describe("buildLibraryData", () => {
});
});

test("throws ApplicationError with auth doc URL, if auth doc has no catalog root url", () => {
// Make sure that the error is actually thrown.
expect.assertions(2);

try {
buildLibraryData(fixtures.authDocNoCatalogRoot, "librarySlug");
} catch (error) {
expect(error).toBeInstanceOf(ApplicationError);
expect(error.message).toBe(
"Application Error: No Catalog Root URL present in Auth Document at /auth-doc."
);
}
});

test("throws ApplicationError without auth doc URL, if auth doc has no self url", () => {
// Make sure that the error is actually thrown.
expect.assertions(2);

try {
buildLibraryData(fixtures.authDocNoLinks, "librarySlug");
} catch (error) {
expect(error).toBeInstanceOf(ApplicationError);
expect(error.message).toBe(
"Application Error: No Catalog Root URL present in Auth Document at (unknown: missing auth doc 'self' link or href)."
);
}
});

test("correctly parses web_color_scheme", () => {
const library = buildLibraryData(
{
Expand Down
17 changes: 10 additions & 7 deletions src/dataflow/getLibraryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,21 @@ function getShelfUrl(authDoc: OPDS1.AuthDocument): string | null {
}

/**
* Extracts the catalot root url from an auth document
* Extracts the catalog root url from an auth document
*/
function getCatalogUrl(authDoc: OPDS1.AuthDocument): string {
const url =
authDoc.links?.find(link => {
return link.rel === OPDS1.CatalogRootRel;
})?.href ?? null;
const url: string | undefined = authDoc.links?.find(
link => link.rel === OPDS1.CatalogRootRel
)?.href;

if (!url)
if (!url) {
const selfUrl =
authDoc.links?.find(link => link.rel === OPDS1.SelfRel)?.href ??
"(unknown: missing auth doc 'self' link or href)";
throw new ApplicationError({
detail: "No Catalog Root Url present in Auth Document."
detail: `No Catalog Root URL present in Auth Document at ${selfUrl}.`
});
}

return url;
}
Expand Down
45 changes: 42 additions & 3 deletions src/test-utils/fixtures/auth-document.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
import { OPDS1 } from "interfaces";

export const authDoc: OPDS1.AuthDocument = {
const baseAuthDoc: OPDS1.AuthDocument = {
id: "auth-doc-id",
title: "auth doc title",
description: "auth doc description",
links: [],
authentication: []
};

export const authDoc: OPDS1.AuthDocument = {
...baseAuthDoc,
links: [
...(baseAuthDoc.links ?? []),
{
rel: OPDS1.SelfRel,
href: "/auth-doc"
},
{
rel: OPDS1.CatalogRootRel,
href: "/catalog-root"
}
],
authentication: []
]
};

export const authDocNoLinks: OPDS1.AuthDocument = { ...baseAuthDoc };

export const authDocNoCatalogRoot: OPDS1.AuthDocument = {
...baseAuthDoc,
links: [
...(baseAuthDoc.links ?? []),
{
rel: OPDS1.SelfRel,
href: "/auth-doc"
}
]
};

// This is not a valid auth doc, since the `href` is missing for the catalog
// root link. But we don't control these, so it could happen.
export const authDocNoCatalogRootHref: OPDS1.AuthDocument = {
...baseAuthDoc,
links: [
...(baseAuthDoc.links ?? []),
{
rel: OPDS1.SelfRel,
href: "/auth-doc"
},
{
rel: OPDS1.CatalogRootRel
}
]
} as OPDS1.AuthDocument;
3 changes: 3 additions & 0 deletions src/types/opds1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
/**
* Link Relations
*/
export const SelfRel = "self";
export const AuthDocLinkRelation = "http://opds-spec.org/auth/document";
export const AcquisitionLinkRel = "http://opds-spec.org/acquisition";
export const BorrowLinkRel = "http://opds-spec.org/acquisition/borrow";
export const RevokeLinkRel = "http://librarysimplified.org/terms/rel/revoke";
export const TrackOpenBookRel =
"http://librarysimplified.org/terms/rel/analytics/open-book";
export type AnyLinkRelation =
| typeof SelfRel
| typeof AuthDocLinkRelation
| typeof AcquisitionLinkRel
| typeof BorrowLinkRel
Expand Down Expand Up @@ -135,6 +137,7 @@ export interface Link {
export const CatalogRootRel = "start";
export const ShelfLinkRel = "http://opds-spec.org/shelf";
type AuthDocLinkRelations =
| typeof SelfRel
| typeof CatalogRootRel
| typeof ShelfLinkRel
| "navigation"
Expand Down

0 comments on commit 77d03df

Please sign in to comment.