diff --git a/CHANGELOG.md b/CHANGELOG.md index c220e483..cbea3efc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,16 +19,17 @@ Another example [here](https://co-pilot.dev/changelog) - Add CHANGELOG.md ([#84](https://github.com/chingu-x/chingu-dashboard-be/pull/84)) - Add Role/Permission guard ([#97](https://github.com/chingu-x/chingu-dashboard-be/pull/97)) - Add e2e tests for auth controller ([#102](https://github.com/chingu-x/chingu-dashboard-be/pull/102)) -- Add e2e tests for techs controller (([#103](https://github.com/chingu-x/chingu-dashboard-be/pull/103)) -- Add check-in form database implementation and seed data (([#105](https://github.com/chingu-x/chingu-dashboard-be/pull/105)) +- Add e2e tests for techs controller ([#103](https://github.com/chingu-x/chingu-dashboard-be/pull/103)) +- Add check-in form database implementation and seed data ([#105](https://github.com/chingu-x/chingu-dashboard-be/pull/105)) +- Add e2e tests for forms controller ([#107](https://github.com/chingu-x/chingu-dashboard-be/pull/107)) ### Changed - Update docker compose and scripts in package.json to include a test database container and remove usage of .env.dev to avoid confusion ([#100](https://github.com/chingu-x/chingu-dashboard-be/pull/100)) - Restructure seed/index.ts to work with e2e tests, and add --runInBand to e2e scripts[#101](https://github.com/chingu-x/chingu-dashboard-be/pull/101) -- Update changelog (([#104](https://github.com/chingu-x/chingu-dashboard-be/pull/104)) +- Update changelog ([#104](https://github.com/chingu-x/chingu-dashboard-be/pull/104)) - Update test.yml to run e2e tests on pull requests to the main branch [#105](https://github.com/chingu-x/chingu-dashboard-be/pull/105) ### Fixed -- Fix failed tests in app and ideation due to the change from jwt token response to http cookies (([#98](https://github.com/chingu-x/chingu-dashboard-be/pull/98)) +- Fix failed tests in app and ideation due to the change from jwt token response to http cookies ([#98](https://github.com/chingu-x/chingu-dashboard-be/pull/98)) ### Removed diff --git a/test/forms.e2e-spec.ts b/test/forms.e2e-spec.ts new file mode 100644 index 00000000..ff0fc54c --- /dev/null +++ b/test/forms.e2e-spec.ts @@ -0,0 +1,114 @@ +import { INestApplication, ValidationPipe } from "@nestjs/common"; +import { Test, TestingModule } from "@nestjs/testing"; +import { AppModule } from "../src/app.module"; +import { seed } from "../prisma/seed/seed"; +import * as request from "supertest"; +import * as cookieParser from "cookie-parser"; +import { extractCookieByKey } from "./utils"; +import { PrismaClient } from "@prisma/client"; + +const loginUrl = "/auth/login"; + +const loginAndGetTokens = async ( + email: string, + password: string, + app: INestApplication, +) => { + const r = await request(app.getHttpServer()).post(loginUrl).send({ + email, + password, + }); + + const access_token = extractCookieByKey( + r.headers["set-cookie"], + "access_token", + ); + const refresh_token = extractCookieByKey( + r.headers["set-cookie"], + "refresh_token", + ); + + return { access_token, refresh_token }; +}; + +describe("FormController e2e Tests", () => { + let app: INestApplication; + + beforeAll(async () => { + const moduleFixture: TestingModule = await Test.createTestingModule({ + imports: [AppModule], + }).compile(); + + await seed(); + + app = moduleFixture.createNestApplication(); + app.useGlobalPipes(new ValidationPipe()); + app.use(cookieParser()); + await app.init(); + }); + + afterAll(async () => { + await app.close(); + }); + + describe("GET ALL /forms", () => { + it("should successfully retrieve all forms", async () => { + const { access_token, refresh_token } = await loginAndGetTokens( + "jessica.williamson@gmail.com", + "password", + app, + ); + const response = await request(app.getHttpServer()) + .get("/forms") + .set("Cookie", [access_token, refresh_token]) + .expect(200); + + const prisma = new PrismaClient(); + const forms = await prisma.form.findMany(); + const formsLength = forms.length; + expect(response.body.length).toEqual(formsLength); + await prisma.$disconnect(); + }); + }); + describe("GET /forms/:formId", () => { + it("should successfully retrieve a specific form by ID", async () => { + const { access_token, refresh_token } = await loginAndGetTokens( + "jessica.williamson@gmail.com", + "password", + app, + ); + const formId = 1; + const prisma = new PrismaClient(); + const expectedForm = await prisma.form.findUnique({ + where: { + id: formId, + }, + }); + const response = await request(app.getHttpServer()) + .get(`/forms/${formId}`) + .set("Cookie", [access_token, refresh_token]) + .expect(200); + + expect(response.body.id).toEqual(expectedForm.id); + + await prisma.$disconnect(); + }); + it("should return a 404 error for a non-existent form ID", async () => { + const { access_token, refresh_token } = await loginAndGetTokens( + "jessica.williamson@gmail.com", + "password", + app, + ); + const invalidFormId = 9999; + await request(app.getHttpServer()) + .get(`/forms/${invalidFormId}`) + .set("Cookie", [access_token, refresh_token]) + .expect(404) + .expect({ + message: `Invalid formId: Form (id:${invalidFormId}) does not exist.`, + error: "Not Found", + statusCode: 404, + }); + }); + }); +});