Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test forms #107

Merged
merged 16 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ 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))
siasktv marked this conversation as resolved.
Show resolved Hide resolved
- 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))

### 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
97 changes: 97 additions & 0 deletions test/forms.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
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 ALL /forms", () => {
it("should successfully retrieve 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);
});
});
describe("GET a specific form /forms/:formId and handle a form with invalid ID", () => {
siasktv marked this conversation as resolved.
Show resolved Hide resolved
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;
await request(app.getHttpServer())
.get(`/forms/${formId}`)
.set("Cookie", [access_token, refresh_token])
.expect(200);
});
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,
});
});
});
});
Loading