-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96c4339
commit 2f255d2
Showing
4 changed files
with
216 additions
and
15 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
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,19 +1,90 @@ | ||
import request from "supertest"; | ||
import sinon from "sinon"; | ||
import { DataSource, Repository } from "typeorm"; | ||
import { lambdaHandler } from "../handler"; | ||
import assert from "assert"; | ||
import { StatusCodes } from "http-status-codes"; | ||
|
||
describe("test endpoint", () => { | ||
const server = request("http://localhost:1337/"); | ||
let sandbox: sinon.SinonSandbox; | ||
let userRepositoryStub: sinon.SinonStub; | ||
let dataSourceStub: sinon.SinonStub; | ||
let metadataStub: sinon.SinonStub; | ||
|
||
describe("Test obligatory query parameter", () => { | ||
it("GET `dev/?exampleParam=test` returns 200", () => { | ||
return server.get("dev/users").query("exampleParam=test").expect(200); | ||
}); | ||
const data = [ | ||
{ | ||
id: "50ece88a-61ac-4435-8457-051693716276", | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "[email protected]", | ||
}, | ||
{ | ||
id: "8eb91b5f-63ef-4ca8-9db4-3fa6f173f1ae", | ||
firstName: "Mark", | ||
lastName: "Smith", | ||
email: "[email protected]", | ||
} | ||
]; | ||
|
||
const pagination = { | ||
page: 1, | ||
limit: 3, | ||
total: 2, | ||
totalPages: 1 | ||
}; | ||
|
||
const params = { | ||
exampleParam: "example", | ||
page: "1", | ||
limit: "3", | ||
}; | ||
|
||
it("GET `dev` returns bad request", () => { | ||
return server.get("dev/users").expect(400); | ||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
userRepositoryStub = sandbox.stub(Repository.prototype, "findAndCount").resolves([data, 2]); | ||
dataSourceStub = sandbox.stub(DataSource.prototype, "initialize").resolves(); | ||
metadataStub = sandbox.stub(Repository.prototype, "metadata"); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe("GET dev/users", () => { | ||
it("should return users", async () => { | ||
metadataStub.value({ | ||
columns: [{ propertyName: "email", }, { propertyName: "firstName" }] | ||
}); | ||
const filters = { | ||
sort: { email: "ASC" }, | ||
filter: { firstName: "John" }, | ||
search: "Doe", | ||
}; | ||
|
||
const response = await lambdaHandler({ | ||
queryStringParameters: { ...params, ...filters } | ||
}); | ||
|
||
const parshedResponse = JSON.parse(response.body); | ||
assert.equal(response.statusCode, StatusCodes.OK); | ||
assert.deepEqual(parshedResponse, { meta: { pagination, ...filters }, data }); | ||
}); | ||
|
||
it("GET 'any' returns not found", () => { | ||
return server.get("any").expect(404); | ||
it("shouldn't return filters if they are not available", async () => { | ||
metadataStub.value({ | ||
columns: [{ propertyName: "id", }, { propertyName: "lastName" }] | ||
}); | ||
|
||
const response = await lambdaHandler({ | ||
queryStringParameters: { | ||
...params, | ||
sort: { email: "ASC", id: "ASC" }, | ||
filter: { firstName: "John", lastName: "Doe" }, | ||
} | ||
}); | ||
|
||
const parshedResponse = JSON.parse(response.body); | ||
assert.equal(response.statusCode, StatusCodes.OK); | ||
assert.deepEqual(parshedResponse, { meta: { pagination, sort: { id: "ASC" }, filter: { lastName: "Doe" }}, data }); | ||
}); | ||
}); | ||
}); |
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