From b8401c2251ecc340e73e1b1935bd8704193c635e Mon Sep 17 00:00:00 2001 From: Aydan Pirani Date: Sun, 21 Jan 2024 18:31:42 -0600 Subject: [PATCH 1/3] Added registration tests --- src/database/registration-db.ts | 8 +- src/services/mail/mail-lib.ts | 1 + .../registration/registration-router.test.ts | 89 +++++++++++++++++++ .../registration/registration-router.ts | 1 + 4 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/mail/mail-lib.ts b/src/services/mail/mail-lib.ts index f30e030a..e80f3b50 100644 --- a/src/services/mail/mail-lib.ts +++ b/src/services/mail/mail-lib.ts @@ -20,6 +20,7 @@ export async function sendMailWrapper(res: Response, next: NextFunction, mailInf } function sendMail(templateId: string, emails: string[], scheduleTime?: string): Promise { + console.log("in here 2!"); const options = scheduleTime ? { start_time: scheduleTime } : {}; const recipients = emails.map((emailAddress: string) => ({ address: `${emailAddress}` })); diff --git a/src/services/registration/registration-router.test.ts b/src/services/registration/registration-router.test.ts new file mode 100644 index 00000000..66154273 --- /dev/null +++ b/src/services/registration/registration-router.test.ts @@ -0,0 +1,89 @@ +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"); + }); +}); \ No newline at end of file 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); }); From e7f93d523b47f2c87c4d22ff0f85459953c860ed Mon Sep 17 00:00:00 2001 From: Aydan Pirani Date: Sun, 21 Jan 2024 18:32:41 -0600 Subject: [PATCH 2/3] formatting --- .../registration/registration-router.test.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/services/registration/registration-router.test.ts b/src/services/registration/registration-router.test.ts index 66154273..51f9e02b 100644 --- a/src/services/registration/registration-router.test.ts +++ b/src/services/registration/registration-router.test.ts @@ -24,15 +24,14 @@ const GENERAL_APPLICATION = { dietaryRestrictions: [], race: [], hackInterest: [], - hackOutreach: [] + hackOutreach: [], } satisfies RegistrationFormat; -const UNSUBMITTED_GENERAL_REGISTRATION_DATA = {hasSubmitted: false, ...GENERAL_APPLICATION}; -const SUBMITTED_GENERAL_REGISTRATION_DATA = {hasSubmitted: true, ...GENERAL_APPLICATION}; - +const UNSUBMITTED_GENERAL_REGISTRATION_DATA = { hasSubmitted: false, ...GENERAL_APPLICATION }; +const SUBMITTED_GENERAL_REGISTRATION_DATA = { hasSubmitted: true, ...GENERAL_APPLICATION }; describe("GET /registration/ Endpoint", () => { - beforeEach(async () => { + beforeEach(async () => { await Models.RegistrationApplication.create(UNSUBMITTED_GENERAL_REGISTRATION_DATA); }); @@ -49,7 +48,7 @@ describe("GET /registration/ Endpoint", () => { }); describe("GET /registration/userid/:USERID Endpoint", () => { - beforeEach(async () => { + beforeEach(async () => { await Models.RegistrationApplication.create(UNSUBMITTED_GENERAL_REGISTRATION_DATA); }); @@ -83,7 +82,9 @@ describe("POST /registration/ Endpoint", () => { 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); + const response = await postAsUser("/registration/") + .send(GENERAL_APPLICATION) + .expect(StatusCode.ClientErrorUnprocessableEntity); expect(JSON.parse(response.text)).toHaveProperty("error", "AlreadySubmitted"); }); -}); \ No newline at end of file +}); From 4cfc843ea5ff1c2ac8240f417d098448310a0336 Mon Sep 17 00:00:00 2001 From: Lasya Date: Sun, 21 Jan 2024 23:25:08 -0600 Subject: [PATCH 3/3] remove log --- src/services/mail/mail-lib.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/services/mail/mail-lib.ts b/src/services/mail/mail-lib.ts index e80f3b50..f30e030a 100644 --- a/src/services/mail/mail-lib.ts +++ b/src/services/mail/mail-lib.ts @@ -20,7 +20,6 @@ export async function sendMailWrapper(res: Response, next: NextFunction, mailInf } function sendMail(templateId: string, emails: string[], scheduleTime?: string): Promise { - console.log("in here 2!"); const options = scheduleTime ? { start_time: scheduleTime } : {}; const recipients = emails.map((emailAddress: string) => ({ address: `${emailAddress}` }));