From 446593727c05951afc3a4f54baa474724cf7cc49 Mon Sep 17 00:00:00 2001 From: Aydan Pirani Date: Sun, 21 Jan 2024 23:27:08 -0600 Subject: [PATCH] Added registration tests (#167) * Added registration tests * formatting * remove log --------- Co-authored-by: Lasya --- src/database/registration-db.ts | 8 +- .../registration/registration-router.test.ts | 90 +++++++++++++++++++ .../registration/registration-router.ts | 1 + 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 src/services/registration/registration-router.test.ts diff --git a/src/database/registration-db.ts b/src/database/registration-db.ts index 06c658e4..13b69f7e 100644 --- a/src/database/registration-db.ts +++ b/src/database/registration-db.ts @@ -4,7 +4,7 @@ export class RegistrationApplication { @prop({ required: true }) public userId: string; - @prop({ required: true }) + @prop({ default: false }) public hasSubmitted: boolean; @prop({ required: true }) @@ -44,7 +44,7 @@ export class RegistrationApplication { public major: string; @prop({ required: false }) - public minor: string; + public minor?: string; @prop({ required: true }) public university: string; @@ -79,9 +79,9 @@ export class RegistrationApplication { @prop({ required: true }) public optionalEssay?: string; - @prop({ required: true }) + @prop({ required: false }) proEssay?: string; - @prop({ required: true }) + @prop({ required: false }) considerForGeneral?: boolean; } diff --git a/src/services/registration/registration-router.test.ts b/src/services/registration/registration-router.test.ts new file mode 100644 index 00000000..51f9e02b --- /dev/null +++ b/src/services/registration/registration-router.test.ts @@ -0,0 +1,90 @@ +import { beforeEach, describe, expect, it } from "@jest/globals"; +import { StatusCode } from "status-code-enum"; +import Models from "../../database/models.js"; +import { TESTER, getAsUser, getAsAdmin, postAsUser } from "../../testTools.js"; +import { RegistrationFormat } from "./registration-formats.js"; +import { Degree, Gender } from "./registration-models.js"; + +const GENERAL_APPLICATION = { + isProApplicant: false, + userId: TESTER.id, + preferredName: "ap", + legalName: "ap4", + emailAddress: "apirani2@illinois.edu", + university: "ap", + hackEssay1: "ap", + hackEssay2: "ap", + optionalEssay: "ap", + location: "ap", + gender: Gender.OTHER, + degree: Degree.BACHELORS, + major: "CS", + gradYear: 0, + requestedTravelReimbursement: false, + dietaryRestrictions: [], + race: [], + hackInterest: [], + hackOutreach: [], +} satisfies RegistrationFormat; + +const UNSUBMITTED_GENERAL_REGISTRATION_DATA = { hasSubmitted: false, ...GENERAL_APPLICATION }; +const SUBMITTED_GENERAL_REGISTRATION_DATA = { hasSubmitted: true, ...GENERAL_APPLICATION }; + +describe("GET /registration/ Endpoint", () => { + beforeEach(async () => { + await Models.RegistrationApplication.create(UNSUBMITTED_GENERAL_REGISTRATION_DATA); + }); + + it("should retrieve user's registration data when it exists", async () => { + const response = await getAsUser("/registration/").expect(StatusCode.SuccessOK); + expect(JSON.parse(response.text)).toMatchObject(UNSUBMITTED_GENERAL_REGISTRATION_DATA); + }); + + it("should return 404 error when user's registration data does not exist", async () => { + await Models.RegistrationApplication.deleteOne({ userId: TESTER.id }); + const response = await getAsUser("/registration/").expect(StatusCode.ClientErrorNotFound); + expect(JSON.parse(response.text)).toHaveProperty("error", "NotFound"); + }); +}); + +describe("GET /registration/userid/:USERID Endpoint", () => { + beforeEach(async () => { + await Models.RegistrationApplication.create(UNSUBMITTED_GENERAL_REGISTRATION_DATA); + }); + + it("should retrieve user's registration data with elevated permissions", async () => { + const response = await getAsAdmin(`/registration/userid/${TESTER.id}`).expect(StatusCode.SuccessOK); + expect(JSON.parse(response.text)).toMatchObject(UNSUBMITTED_GENERAL_REGISTRATION_DATA); + }); + + it("should return 403 error when user does not have elevated permissions", async () => { + const response = await getAsUser(`/registration/userid/${TESTER.id}`).expect(StatusCode.ClientErrorForbidden); + expect(JSON.parse(response.text)).toHaveProperty("error", "Forbidden"); + }); + + it("should return 404 error when specified user ID does not have registration data", async () => { + const response = await getAsAdmin("/registration/userid/nonexistentuser").expect(StatusCode.ClientErrorNotFound); + expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound"); + }); +}); + +describe("POST /registration/ Endpoint", () => { + it("should create or update user's registration data with valid data", async () => { + const response = await postAsUser("/registration/").send(GENERAL_APPLICATION).expect(StatusCode.SuccessOK); + expect(JSON.parse(response.text)).toMatchObject(UNSUBMITTED_GENERAL_REGISTRATION_DATA); + }); + + it("should return 400 error when registration data is invalid", async () => { + const response = await postAsUser("/registration/").send({}).expect(StatusCode.ClientErrorBadRequest); + expect(JSON.parse(response.text)).toHaveProperty("error", "BadRequest"); + }); + + it("should return 422 error when user has already submitted registration data", async () => { + await Models.RegistrationApplication.create(SUBMITTED_GENERAL_REGISTRATION_DATA); + + const response = await postAsUser("/registration/") + .send(GENERAL_APPLICATION) + .expect(StatusCode.ClientErrorUnprocessableEntity); + expect(JSON.parse(response.text)).toHaveProperty("error", "AlreadySubmitted"); + }); +}); diff --git a/src/services/registration/registration-router.ts b/src/services/registration/registration-router.ts index 7b66f032..91702987 100644 --- a/src/services/registration/registration-router.ts +++ b/src/services/registration/registration-router.ts @@ -318,6 +318,7 @@ registrationRouter.post("/submit/", strongJwtVerification, async (_: Request, re templateId: RegistrationTemplates.REGISTRATION_SUBMISSION, recipients: [registrationInfo.emailAddress], }; + return sendMailWrapper(res, next, mailInfo); });