Skip to content

Commit

Permalink
test: add example lambda tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelTshDev committed Apr 8, 2024
1 parent 96c4339 commit 58456ff
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ jobs:
- name: SonarQube Scan
uses: sonarsource/sonarqube-scan-action@master
env:
SONAR_TOKEN: sqp_cdf796a422338de063410c70229532ccd1326375
SONAR_TOKEN: sqp_b71429573ea73ed4c73e5fe20749c635ed7b611d
SONAR_HOST_URL: https://sonarqube.aws.tshdev.io
5 changes: 2 additions & 3 deletions functions/example-lambda/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ import { httpErrorHandlerConfigured } from "../../shared/middleware/http-error-h
import { createFindManyOptions, makePaginationResult } from "../../shared/pagination-utils/pagination-utils";
import { Like } from "typeorm";

const connectToDb = dataSource.initialize();
const config = createConfig(process.env);
const userRepository = dataSource.getRepository(ExampleModel);

const lambdaHandler = async (event: ExampleLambdaPayload) => {
export const lambdaHandler = async (event: ExampleLambdaPayload) => {
const queryParams = event.queryStringParameters;
winstonLogger.info(`Hello from ${config.appName}. Example param is: ${queryParams.exampleParam}`);

await connectToDb;
await dataSource.initialize();

const findOptions = createFindManyOptions(userRepository, queryParams);

Expand Down
91 changes: 81 additions & 10 deletions functions/example-lambda/tests/handler.spec.ts
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 });
});
});
});
133 changes: 131 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dotenv": "^16.3.1",
"http-status-codes": "^2.3.0",
"pg": "^8.11.3",
"sinon": "^17.0.1",
"ts-pipe-compose": "^0.2.1",
"typeorm": "^0.3.17",
"winston": "^3.11.0",
Expand All @@ -57,6 +58,7 @@
"@types/aws-lambda": "^8.10.124",
"@types/mocha": "^10.0.2",
"@types/node": "^20.11.17",
"@types/sinon": "^17.0.3",
"@types/supertest": "^2.0.14",
"@typescript-eslint/eslint-plugin": "^6.7.4",
"@typescript-eslint/parser": "^6.7.4",
Expand Down

0 comments on commit 58456ff

Please sign in to comment.