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

Private collections should be visible from reading room #87

Merged
merged 1 commit into from
Jan 31, 2023
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
17 changes: 14 additions & 3 deletions src/handlers/get-collection-by-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ const { getCollection } = require("../api/opensearch");
const { wrap } = require("./middleware");
const opensearchResponse = require("../api/response/opensearch");

const getCollectionById = async (event) => {
const getOpts = (event) => {
const id = event.pathParameters.id;
const esResponse = await getCollection(id);

const allowPrivate =
event.userToken.isReadingRoom() || event.userToken.hasEntitlement(id);
const allowUnpublished = event.userToken.hasEntitlement(id);
return { allowPrivate, allowUnpublished };
};

const getCollectionById = async (event) => {
const esResponse = await getCollection(
event.pathParameters.id,
getOpts(event)
);
return await opensearchResponse.transform(esResponse);
};

const getIiifCollectionById = async (event) => {
const id = event.pathParameters.id;
const esResponse = await getCollection(id);
const esResponse = await getCollection(id, getOpts(event));
const collection = JSON.parse(esResponse.body)?._source;
if (!collection) return { statusCode: 404, body: "Not Found" };
const parameterOverrides = { ...event.queryStringParameters };
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/mocks/collection-1234-private-published.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"_index": "dev-dc-v2-collection",
"_type": "_doc",
"_id": "1234",
"_version": 1,
"found": true,
"_source": {
"id": "1234",
"title": "Collection Title",
"api_model": "Collection",
"published": true,
"visibility": "Private",
"representative_image": {
"work_id": "1234"
}
}
}
79 changes: 79 additions & 0 deletions test/integration/get-collection-by-id.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ describe("Retrieve collection by id", () => {
const mock = helpers.mockIndex();

describe("GET /collections/{id}", () => {
beforeEach(() => {
process.env.READING_ROOM_IPS = "";
});

const { handler } = requireSource("handlers/get-collection-by-id");

it("retrieves a single collection link document", async () => {
Expand Down Expand Up @@ -46,6 +50,41 @@ describe("Retrieve collection by id", () => {
expect(result.statusCode).to.eq(404);
});

it("404's if the collection is private", async () => {
mock
.get("/dc-v2-collection/_doc/1234")
.reply(
200,
helpers.testFixture("mocks/collection-1234-private-published.json")
);

const event = helpers
.mockEvent("GET", "/collections/{id}")
.pathParams({ id: 1234 })
.render();

const result = await handler(event);
expect(result.statusCode).to.eq(404);
});

it("200's if the collection is private but the user is in the reading room", async () => {
mock
.get("/dc-v2-collection/_doc/1234")
.reply(
200,
helpers.testFixture("mocks/collection-1234-private-published.json")
);

const event = helpers
.mockEvent("GET", "/collections/{id}")
.pathParams({ id: 1234 })
.render();

process.env.READING_ROOM_IPS = event.requestContext.http.sourceIp;
const result = await handler(event);
expect(result.statusCode).to.eq(200);
});

it("returns a single collection as a IIIF collection", async () => {
const originalQuery = {
query: { query_string: { query: "collection.id:1234" } },
Expand Down Expand Up @@ -90,5 +129,45 @@ describe("Retrieve collection by id", () => {
"https://api.test.library.northwestern.edu/api/v2/collections"
);
});

it("returns a private IIIF collection if the user is in the reading room", async () => {
const event = helpers
.mockEvent("GET", "/collections/{id}")
.pathParams({ id: 1234 })
.queryParams({ as: "iiif" })
.render();

process.env.READING_ROOM_IPS = event.requestContext.http.sourceIp;

const originalQuery = {
query: { query_string: { query: "collection.id:1234" } },
};

let preProcessedEvent = helpers.preprocess(event);

const authQuery = new RequestPipeline(originalQuery)
.authFilter(preProcessedEvent)
.toJson();

mock
.get("/dc-v2-collection/_doc/1234")
.reply(
200,
helpers.testFixture("mocks/collection-1234-private-published.json")
);
mock
.post("/dc-v2-work/_search", authQuery)
.reply(200, helpers.testFixture("mocks/search.json"));

const result = await handler(preProcessedEvent);
expect(result.statusCode).to.eq(200);
expect(result).to.have.header(
"content-type",
/application\/json;.*charset=UTF-8/
);
const resultBody = JSON.parse(result.body);
expect(resultBody.type).to.eq("Collection");
expect(resultBody.label.none[0]).to.eq("Collection Title");
});
});
});