-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from nulib/integration-tests
Add integration tests and improve mocha config
- Loading branch information
Showing
28 changed files
with
375 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
ignore: ["test/test-helpers/**/*"], | ||
recursive: true, | ||
require: "test/test-helpers" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
tests/* | ||
test/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const base64 = require("base64-js"); | ||
|
||
function decodeEventBody(event) { | ||
if (!event.isBase64Encoded) return event; | ||
event.body = new Buffer.from(base64.toByteArray(event.body)).toString(); | ||
event.isBase64Encoded = false; | ||
return event; | ||
} | ||
|
||
module.exports = { decodeEventBody }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
"use strict"; | ||
|
||
const chai = require("chai"); | ||
const expect = chai.expect; | ||
|
||
describe("Doc retrieval routes", () => { | ||
helpers.saveEnvironment(); | ||
const mock = helpers.mockIndex(); | ||
|
||
describe("GET /works/{id}", () => { | ||
const { handler } = require("../../src/handlers/get-work-by-id"); | ||
|
||
it("retrieves a single work", async () => { | ||
mock | ||
.get("/dc-v2-work/_doc/1234") | ||
.reply(200, helpers.testFixture("mocks/work-1234.json")); | ||
|
||
const { event } = helpers | ||
.mockEvent("GET", "/works/{id}") | ||
.pathParams({ id: 1234 }); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(200); | ||
|
||
const resultBody = JSON.parse(result.body); | ||
expect(resultBody.data.api_model).to.eq("Work"); | ||
expect(resultBody.data.id).to.eq("1234"); | ||
}); | ||
|
||
it("404s a missing work", async () => { | ||
mock | ||
.get("/dc-v2-work/_doc/1234") | ||
.reply(200, helpers.testFixture("mocks/missing-work-1234.json")); | ||
|
||
const { event } = helpers | ||
.mockEvent("GET", "/works/{id}") | ||
.pathParams({ id: 1234 }); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(404); | ||
}); | ||
|
||
it("404s an unpublished work", async () => { | ||
mock | ||
.get("/dc-v2-work/_doc/1234") | ||
.reply(200, helpers.testFixture("mocks/unpublished-work-1234.json")); | ||
|
||
const { event } = helpers | ||
.mockEvent("GET", "/works/{id}") | ||
.pathParams({ id: 1234 }); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(404); | ||
}); | ||
}); | ||
|
||
describe("GET /collections/{id}", () => { | ||
const { handler } = require("../../src/handlers/get-collection-by-id"); | ||
|
||
it("retrieves a single collection", async () => { | ||
mock | ||
.get("/dc-v2-collection/_doc/1234") | ||
.reply(200, helpers.testFixture("mocks/collection-1234.json")); | ||
|
||
const { event } = helpers | ||
.mockEvent("GET", "/collections/{id}") | ||
.pathParams({ id: 1234 }); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(200); | ||
|
||
const resultBody = JSON.parse(result.body); | ||
expect(resultBody.data.api_model).to.eq("Collection"); | ||
expect(resultBody.data.id).to.eq("1234"); | ||
}); | ||
}); | ||
|
||
describe("GET /file-sets/{id}", () => { | ||
const { handler } = require("../../src/handlers/get-file-set-by-id"); | ||
|
||
it("retrieves a single file-set", async () => { | ||
mock | ||
.get("/dc-v2-file-set/_doc/1234") | ||
.reply(200, helpers.testFixture("mocks/fileset-1234.json")); | ||
|
||
const { event } = helpers | ||
.mockEvent("GET", "/file-sets/{id}") | ||
.pathParams({ id: 1234 }); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(200); | ||
|
||
const resultBody = JSON.parse(result.body); | ||
expect(resultBody.data.api_model).to.eq("FileSet"); | ||
expect(resultBody.data.id).to.eq("1234"); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
"use strict"; | ||
|
||
const chai = require("chai"); | ||
const expect = chai.expect; | ||
const searchHandlers = require("../../src/handlers/search"); | ||
const RequestPipeline = require("../../src/api/request/pipeline"); | ||
|
||
describe("Search routes", () => { | ||
helpers.saveEnvironment(); | ||
const mock = helpers.mockIndex(); | ||
|
||
describe("POST /search/{targets}", () => { | ||
const handler = searchHandlers.postSearch; | ||
const originalQuery = { query: { match_all: {} } }; | ||
const authQuery = new RequestPipeline(originalQuery).authFilter().toJson(); | ||
|
||
it("performs a works search by default", async () => { | ||
mock | ||
.post("/dc-v2-work/_search", authQuery) | ||
.reply(200, helpers.testFixture("mocks/search.json")); | ||
const { event } = helpers | ||
.mockEvent("POST", "/search") | ||
.body(originalQuery); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(200); | ||
|
||
const resultBody = JSON.parse(result.body); | ||
expect(resultBody).to.include.keys(["data", "pagination"]); | ||
expect(resultBody.data.length).to.eq(10); | ||
}); | ||
|
||
it("performs a search on specified models", async () => { | ||
mock | ||
.post("/dc-v2-work,dc-v2-collection/_search", authQuery) | ||
.reply(200, helpers.testFixture("mocks/search-multiple-targets.json")); | ||
const { event } = helpers | ||
.mockEvent("POST", "/search/{models}") | ||
.pathParams({ models: "works,collections" }) | ||
.body(originalQuery) | ||
.base64Encode(); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(200); | ||
|
||
const resultBody = JSON.parse(result.body); | ||
expect(resultBody).to.include.keys(["data", "pagination"]); | ||
expect(resultBody.data.length).to.eq(10); | ||
}); | ||
|
||
it("errors if invalid models specified", async () => { | ||
const { event } = helpers | ||
.mockEvent("POST", "/search/{models}") | ||
.pathParams({ models: "works,collections,blargh" }) | ||
.body(originalQuery); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(400); | ||
|
||
const resultBody = JSON.parse(result.body); | ||
expect(resultBody.message).to.eq( | ||
"Invalid models requested: works,collections,blargh" | ||
); | ||
}); | ||
}); | ||
|
||
describe("GET /search", () => { | ||
const handler = searchHandlers.getSearch; | ||
const originalQuery = { query: { match_all: {} }, size: 10, from: 0 }; | ||
const authQuery = new RequestPipeline(originalQuery).authFilter().toJson(); | ||
const searchToken = | ||
"N4IgRg9gJgniBcoCOBXApgJzokBbAhgC4DGAFgPr4A2VCwAvvQDQgDOAlgF5oICMADMzzQ0VVggDaIAO4QMAa3EBdekA"; | ||
|
||
it("requires a searchToken", async () => { | ||
const { event } = helpers.mockEvent("GET", "/search"); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(400); | ||
const resultBody = JSON.parse(result.body); | ||
expect(resultBody.message).to.eq("searchToken parameter is required"); | ||
}); | ||
|
||
it("requires a valid searchToken", async () => { | ||
const { event } = helpers | ||
.mockEvent("GET", "/search") | ||
.queryParams({ searchToken: "Ceci n'est pas une searchToken" }); | ||
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("performs a search using a searchToken and page number", async () => { | ||
mock | ||
.post("/dc-v2-work/_search", authQuery) | ||
.reply(200, helpers.testFixture("mocks/search.json")); | ||
|
||
const { event } = helpers | ||
.mockEvent("GET", "/search") | ||
.queryParams({ searchToken, page: 1 }); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(200); | ||
}); | ||
|
||
it("defaults to page 1", async () => { | ||
mock | ||
.post("/dc-v2-work/_search", authQuery) | ||
.reply(200, helpers.testFixture("mocks/search.json")); | ||
|
||
const { event } = helpers | ||
.mockEvent("GET", "/search") | ||
.queryParams({ searchToken }); | ||
const result = await handler(event); | ||
expect(result.statusCode).to.eq(200); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.