Skip to content

Commit

Permalink
test: add get me lambda tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelTshDev committed Apr 19, 2024
1 parent 4cf441f commit 22c1da4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion functions/me/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { validateAccessToken } from "../../shared/middleware/auth0-authorization
const connectToDb = dataSource.initialize();
const userRepository = dataSource.getRepository(ExampleModel);

const lambdaHandler = async (event: MeLambdaPayload) => {
export const lambdaHandler = async (event: MeLambdaPayload) => {
await connectToDb;

const { email } = event.queryStringParameters;
Expand Down
48 changes: 48 additions & 0 deletions functions/me/tests/handler.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import sinon from "sinon";
import { DataSource, Repository } from "typeorm";
import { StatusCodes, getReasonPhrase } from "http-status-codes";
import { lambdaHandler } from "../handler";
import { expect } from "chai";

describe("GET /me", () => {
let sandbox: sinon.SinonSandbox;
let userRepositoryStub: sinon.SinonStub;
const email = "[email protected]";

const profileModel = {
id: "50ece88a-61ac-4435-8457-051693716276",
firstName: "John",
lastName: "Doe",
email,
};

const params = { email };

beforeEach(() => {
sandbox = sinon.createSandbox();
userRepositoryStub = sandbox.stub(Repository.prototype, "findOne");
});

afterEach(() => {
sandbox.restore();
});

describe("GET dev/users", () => {
it("should return logged profile data", async () => {
userRepositoryStub.resolves(profileModel);
const response = await lambdaHandler({ queryStringParameters: params });

const parsedResponse = JSON.parse(response.body);
expect(response.statusCode).to.equal(StatusCodes.OK);
expect(parsedResponse.data).to.deep.equal(profileModel);
});

it("should return error when profile wasn't found in database", async () => {
try {
await lambdaHandler({ queryStringParameters: params });
} catch(error: any) {
expect(JSON.parse(error.message).message).to.be.equal(`User with email "${email}" does not exist`);
}
});
});
});

0 comments on commit 22c1da4

Please sign in to comment.