From 22c1da411227d727fac96c161b975ad307342c4f Mon Sep 17 00:00:00 2001 From: Pawel Pograniczny Date: Fri, 19 Apr 2024 12:58:28 +0200 Subject: [PATCH] test: add get me lambda tests --- functions/me/handler.ts | 2 +- functions/me/tests/handler.spec.ts | 48 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 functions/me/tests/handler.spec.ts diff --git a/functions/me/handler.ts b/functions/me/handler.ts index f07f676..beb7f65 100644 --- a/functions/me/handler.ts +++ b/functions/me/handler.ts @@ -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; diff --git a/functions/me/tests/handler.spec.ts b/functions/me/tests/handler.spec.ts new file mode 100644 index 0000000..00f23a4 --- /dev/null +++ b/functions/me/tests/handler.spec.ts @@ -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 = "john@doe.com"; + + 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`); + } + }); + }); +});