From 01e2b186b4c6fd5d45c0a904763ad25d9f0fc73c Mon Sep 17 00:00:00 2001 From: siasktv Date: Tue, 20 Feb 2024 11:13:51 -0500 Subject: [PATCH 01/11] Your commit message here --- test/forms.e2e-spec.ts | 66 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/forms.e2e-spec.ts diff --git a/test/forms.e2e-spec.ts b/test/forms.e2e-spec.ts new file mode 100644 index 00000000..b515fc8e --- /dev/null +++ b/test/forms.e2e-spec.ts @@ -0,0 +1,66 @@ +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"; + +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 Forms", () => { + it("Get all forms", async () => { + const { access_token, refresh_token } = await loginAndGetTokens( + "jessica.williamson@gmail.com", + "password", + app, + ); + await request(app.getHttpServer()) + .get("/forms") + .set("Cookie", [access_token, refresh_token]) + .expect(200); + }); + }); +}); From 50ff61c980271154cd190c98a9a7c9a87056da9c Mon Sep 17 00:00:00 2001 From: siasktv Date: Tue, 20 Feb 2024 11:15:15 -0500 Subject: [PATCH 02/11] test/getAllForms --- test/forms.e2e-spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/forms.e2e-spec.ts b/test/forms.e2e-spec.ts index b515fc8e..3fab1b84 100644 --- a/test/forms.e2e-spec.ts +++ b/test/forms.e2e-spec.ts @@ -5,7 +5,7 @@ import { seed } from "../prisma/seed/seed"; import * as request from "supertest"; import * as cookieParser from "cookie-parser"; import { extractCookieByKey } from "./utils"; - +// const loginUrl = "/auth/login"; const loginAndGetTokens = async ( From 1f48e7ce3a3a2eb525106bbde9cee0b2c8e78e7c Mon Sep 17 00:00:00 2001 From: siasktv Date: Thu, 22 Feb 2024 19:57:24 -0500 Subject: [PATCH 03/11] tests/e22/forms --- test/forms.e2e-spec.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/forms.e2e-spec.ts b/test/forms.e2e-spec.ts index 3fab1b84..92798958 100644 --- a/test/forms.e2e-spec.ts +++ b/test/forms.e2e-spec.ts @@ -62,5 +62,17 @@ describe("FormController e2e Tests", () => { .set("Cookie", [access_token, refresh_token]) .expect(200); }); + it("Get a specific form", async () => { + const { access_token, refresh_token } = await loginAndGetTokens( + "jessica.williamson@gmail.com", + "password", + app, + ); + const formId = 1; + await request(app.getHttpServer()) + .get(`/forms/${formId}`) + .set("Cookie", [access_token, refresh_token]) + .expect(200); + }); }); }); From f69db3a7e893220eaf11a395744e3897080f94b0 Mon Sep 17 00:00:00 2001 From: siasktv Date: Mon, 26 Feb 2024 20:34:54 -0500 Subject: [PATCH 04/11] forms/tests --- test/forms.e2e-spec.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/forms.e2e-spec.ts b/test/forms.e2e-spec.ts index 92798958..a889daa6 100644 --- a/test/forms.e2e-spec.ts +++ b/test/forms.e2e-spec.ts @@ -5,7 +5,7 @@ import { seed } from "../prisma/seed/seed"; import * as request from "supertest"; import * as cookieParser from "cookie-parser"; import { extractCookieByKey } from "./utils"; -// + const loginUrl = "/auth/login"; const loginAndGetTokens = async ( @@ -74,5 +74,22 @@ describe("FormController e2e Tests", () => { .set("Cookie", [access_token, refresh_token]) .expect(200); }); + it("Get a form with invalid 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) // Not Found + .expect({ + message: `Invalid formId: Form (id:${invalidFormId}) does not exist.`, + error: "Not Found", + statusCode: 404, + }); + }); }); }); From a6c1c10f046b9a523b35406f118943ad6702344f Mon Sep 17 00:00:00 2001 From: siasktv Date: Mon, 26 Feb 2024 20:50:57 -0500 Subject: [PATCH 05/11] forms/tests/description --- test/forms.e2e-spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/forms.e2e-spec.ts b/test/forms.e2e-spec.ts index a889daa6..10c136fa 100644 --- a/test/forms.e2e-spec.ts +++ b/test/forms.e2e-spec.ts @@ -50,8 +50,8 @@ describe("FormController e2e Tests", () => { await app.close(); }); - describe("Get Forms", () => { - it("Get all forms", async () => { + describe("Get Forms Endpoint Testing", () => { + it("should successfully retrieve all forms", async () => { const { access_token, refresh_token } = await loginAndGetTokens( "jessica.williamson@gmail.com", "password", @@ -62,7 +62,7 @@ describe("FormController e2e Tests", () => { .set("Cookie", [access_token, refresh_token]) .expect(200); }); - it("Get a specific form", async () => { + it("should successfully retrieve a specific form by ID", async () => { const { access_token, refresh_token } = await loginAndGetTokens( "jessica.williamson@gmail.com", "password", @@ -74,7 +74,7 @@ describe("FormController e2e Tests", () => { .set("Cookie", [access_token, refresh_token]) .expect(200); }); - it("Get a form with invalid ID", async () => { + 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", From eca3613b79fa8d6696c61e33d14aea28d22d7086 Mon Sep 17 00:00:00 2001 From: siasktv Date: Mon, 26 Feb 2024 21:17:50 -0500 Subject: [PATCH 06/11] forms/tests/changelog --- CHANGELOG.md | 7 ++++--- test/forms.e2e-spec.ts | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6798d9a..efa9f97c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,14 +19,15 @@ 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 e2e tests for techs controller ([#103](https://github.com/chingu-x/chingu-dashboard-be/pull/103)) +- Add end-to-end tests for form endpoints ([#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)) ### 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 index 10c136fa..48cba42f 100644 --- a/test/forms.e2e-spec.ts +++ b/test/forms.e2e-spec.ts @@ -84,7 +84,7 @@ describe("FormController e2e Tests", () => { await request(app.getHttpServer()) .get(`/forms/${invalidFormId}`) .set("Cookie", [access_token, refresh_token]) - .expect(404) // Not Found + .expect(404) .expect({ message: `Invalid formId: Form (id:${invalidFormId}) does not exist.`, error: "Not Found", From a42d40a3226ccf51495fb661ca9491e588e1c9b8 Mon Sep 17 00:00:00 2001 From: siasktv Date: Tue, 27 Feb 2024 14:40:33 -0500 Subject: [PATCH 07/11] Add e2e tests for form endpoints and update form controller tests --- CHANGELOG.md | 2 +- test/forms.e2e-spec.ts | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efa9f97c..3a248f5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,7 +20,7 @@ Another example [here](https://co-pilot.dev/changelog) - 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 end-to-end tests for form endpoints ([#107](https://github.com/chingu-x/chingu-dashboard-be/pull/107)) +- Add e2e tests for form endpoints ([#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)) diff --git a/test/forms.e2e-spec.ts b/test/forms.e2e-spec.ts index 48cba42f..db8f7a8c 100644 --- a/test/forms.e2e-spec.ts +++ b/test/forms.e2e-spec.ts @@ -50,7 +50,7 @@ describe("FormController e2e Tests", () => { await app.close(); }); - describe("Get Forms Endpoint Testing", () => { + describe("GET ALL /forms", () => { it("should successfully retrieve all forms", async () => { const { access_token, refresh_token } = await loginAndGetTokens( "jessica.williamson@gmail.com", @@ -62,6 +62,8 @@ describe("FormController e2e Tests", () => { .set("Cookie", [access_token, refresh_token]) .expect(200); }); + }); + describe("GET a specific form /forms/:formId and handle a form with invalid ID", () => { it("should successfully retrieve a specific form by ID", async () => { const { access_token, refresh_token } = await loginAndGetTokens( "jessica.williamson@gmail.com", From 160bd1be7156c1d99bcb28a400f0a76376751b1f Mon Sep 17 00:00:00 2001 From: siasktv Date: Tue, 27 Feb 2024 16:18:38 -0500 Subject: [PATCH 08/11] CHANGELOG updated --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a248f5a..7f8cadc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ Another example [here](https://co-pilot.dev/changelog) - 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 form endpoints ([#107](https://github.com/chingu-x/chingu-dashboard-be/pull/107)) ### Changed From a7279509a9fb933b549215c000c499d14a6df421 Mon Sep 17 00:00:00 2001 From: siasktv Date: Tue, 27 Feb 2024 16:22:15 -0500 Subject: [PATCH 09/11] CHANGELOG updated --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f8cadc0..fd4b7a7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,7 @@ Another example [here](https://co-pilot.dev/changelog) - 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 form endpoints ([#107](https://github.com/chingu-x/chingu-dashboard-be/pull/107)) +- 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)) From c5c997a057c0670e9a9210b5925d629b610ad763 Mon Sep 17 00:00:00 2001 From: siasktv Date: Fri, 1 Mar 2024 07:36:33 -0500 Subject: [PATCH 10/11] Adds test assertions for number of forms retrieved and ensures correct form retrieval by ID in FormController. --- test/forms.e2e-spec.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/test/forms.e2e-spec.ts b/test/forms.e2e-spec.ts index db8f7a8c..d398a07e 100644 --- a/test/forms.e2e-spec.ts +++ b/test/forms.e2e-spec.ts @@ -5,6 +5,7 @@ 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"; @@ -57,13 +58,19 @@ describe("FormController e2e Tests", () => { "password", app, ); - await request(app.getHttpServer()) + 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 a specific form /forms/:formId and handle a form with invalid ID", () => { + 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", @@ -71,10 +78,20 @@ describe("FormController e2e Tests", () => { app, ); const formId = 1; - await request(app.getHttpServer()) + 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).toEqual(expectedForm); + + await prisma.$disconnect(); }); it("should return a 404 error for a non-existent form ID", async () => { const { access_token, refresh_token } = await loginAndGetTokens( From 47134087db1257c08b97307072ac4660c9ec1685 Mon Sep 17 00:00:00 2001 From: siasktv Date: Fri, 1 Mar 2024 08:48:33 -0500 Subject: [PATCH 11/11] Fix assertion in forms.e2e-spec.ts --- test/forms.e2e-spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/forms.e2e-spec.ts b/test/forms.e2e-spec.ts index d398a07e..ff0fc54c 100644 --- a/test/forms.e2e-spec.ts +++ b/test/forms.e2e-spec.ts @@ -89,7 +89,7 @@ describe("FormController e2e Tests", () => { .set("Cookie", [access_token, refresh_token]) .expect(200); - expect(response.body).toEqual(expectedForm); + expect(response.body.id).toEqual(expectedForm.id); await prisma.$disconnect(); });