Skip to content

Commit

Permalink
Private collections should be visible from reading room
Browse files Browse the repository at this point in the history
  • Loading branch information
kdid committed Jan 30, 2023
1 parent 9306258 commit 406ca03
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/handlers/get-collection-by-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ const { getCollection } = require("../api/opensearch");
const { doSearch } = require("./search-runner");
const opensearchResponse = require("../api/response/opensearch");

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

const getCollectionById = async (event) => {
const id = event.pathParameters.id;
const esResponse = await getCollection(id);
const esResponse = await getCollection(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"
}
}
}
76 changes: 76 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 @@ -86,5 +125,42 @@ describe("Retrieve collection by id", () => {
expect(result.statusCode).to.eq(301);
expect(result).to.have.header("location", "/collections");
});

it("returns a IIIF collection if the user is in the reading room", async () => {
const originalQuery = {
query: { query_string: { query: "collection.id:1234" } },
};
const authQuery = new RequestPipeline(originalQuery)
.authFilter(helpers.preprocess({}))
.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 event = helpers
.mockEvent("GET", "/collections/{id}")
.pathParams({ id: 1234 })
.queryParams({ as: "iiif" })
.render();

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

const result = await handler(event);
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");
});
});
});

0 comments on commit 406ca03

Please sign in to comment.