Skip to content

Commit

Permalink
Merge pull request #24 from nulib/3137-remove-collections-token
Browse files Browse the repository at this point in the history
Remove searchToken from /collections route
  • Loading branch information
mbklein authored Oct 10, 2022
2 parents 1fcd63a + 3942a73 commit 0f3a4e9
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
17 changes: 13 additions & 4 deletions src/api/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ async function encodeSearchToken(models, body, format, options) {
function from(body) {
return body?.from || 0;
}

function size(body) {
return body?.size || 10;
}
Expand Down Expand Up @@ -55,10 +56,18 @@ class Paginator {

async pageInfo(count) {
let url = new URL(this.route, this.baseUrl);
url.searchParams.set(
"searchToken",
await encodeSearchToken(this.models, this.body, this.format, this.options)
);

if (this.options?.includeToken != false) {
url.searchParams.set(
"searchToken",
await encodeSearchToken(
this.models,
this.body,
this.format,
this.options
)
);
}

const prev = prevPage(this.body, count);
const next = nextPage(this.body, count);
Expand Down
35 changes: 19 additions & 16 deletions src/handlers/get-collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,36 @@ const { baseUrl } = require("../helpers");
const { modelsToTargets } = require("../api/request/models");
const { search } = require("../api/opensearch");
const opensearchResponse = require("../api/response/opensearch");
const { decodeSearchToken, Paginator } = require("../api/pagination");
const { Paginator } = require("../api/pagination");
const RequestPipeline = require("../api/request/pipeline");

const numberParam = (value, defaultValue) => {
if (value === undefined) return defaultValue;
return Number(value);
};

/**
* A simple function to get Collections
*/
exports.handler = async (event) => {
event = middleware(event);

let token = event?.queryStringParameters?.searchToken;
const page = numberParam(event.queryStringParameters?.page, 1);
if (isNaN(page) || page < 1) return invalidRequest("page must be >= 1");
const size = numberParam(event.queryStringParameters?.size, 10);
if (isNaN(size) || size < 1) return invalidRequest("size must be >= 1");

let body = { size: size, from: size * (page - 1) };
let models = ["collections"];
let body = "";

if (token) {
try {
const request = await decodeSearchToken(token);
const page = Number(event.queryStringParameters.page || 1);
request.body.from = request.body.size * (page - 1);
models = request.models;
body = request.body;
} catch (err) {
return invalidRequest("searchToken is invalid");
}
}

const pager = new Paginator(baseUrl(event), "collections", models, body);
const pager = new Paginator(
baseUrl(event),
"collections",
models,
body,
null,
{ includeToken: false }
);
const filteredBody = new RequestPipeline(body).authFilter().toJson();
let esResponse = await search(modelsToTargets(models), filteredBody);
let transformedResponse = await opensearchResponse.transform(
Expand Down
37 changes: 19 additions & 18 deletions test/integration/get-collections.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,33 @@ describe("Collections route", () => {
const handler = getCollectionsHandler.handler;
const originalQuery = { size: 10, from: 0 };
const authQuery = new RequestPipeline(originalQuery).authFilter().toJson();
const searchToken =
"N4IgRg9gJgniBcoDOBLAXgUwQRgAwF8AaEAW2gwBskEBtEAYwgoo3oBcUIA7agXXyA";
const baseEvent = helpers
.mockEvent("GET", "/collections")
.pathPrefix("/api/v2");

it("requires a valid searchToken", async () => {
const event = helpers
.mockEvent("GET", "/collections")
.pathPrefix("/api/v2")
.queryParams({ searchToken: "Ceci n'est pas une searchToken" })
.render();
describe("validates parameters", () => {
it("page", async () => {
const event = baseEvent.queryParams({ page: 0 }).render();
const result = await handler(event);
expect(result.statusCode).to.eq(400);
const resultBody = JSON.parse(result.body);
expect(resultBody.message).to.eq("page must be >= 1");
});

const result = await handler(event);
expect(result.statusCode).to.eq(400);
const resultBody = JSON.parse(result.body);
expect(resultBody.message).to.eq("searchToken is invalid");
it("size", async () => {
const event = baseEvent.queryParams({ size: 0 }).render();
const result = await handler(event);
expect(result.statusCode).to.eq(400);
const resultBody = JSON.parse(result.body);
expect(resultBody.message).to.eq("size must be >= 1");
});
});

it("paginates results using a searchToken and page number", async () => {
mock
.post("/dc-v2-collection/_search", authQuery)
.reply(200, helpers.testFixture("mocks/collections.json"));
const event = helpers
.mockEvent("GET", "/collections")
.pathPrefix("/api/v2")
.queryParams({ searchToken, page: 1 })
.body(authQuery)
.render();
const event = baseEvent.queryParams({ page: 1 }).render();
const result = await handler(event);
expect(result.statusCode).to.eq(200);
expect(result.headers).to.include({ "content-type": "application/json" });
Expand Down

0 comments on commit 0f3a4e9

Please sign in to comment.