Skip to content

Commit

Permalink
Added registration tests (#167)
Browse files Browse the repository at this point in the history
* Added registration tests

* formatting

* remove log

---------

Co-authored-by: Lasya <neti.lasya@gmail.com>
  • Loading branch information
AydanPirani and lasyaneti authored Jan 22, 2024
1 parent e598670 commit 4465937
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/database/registration-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
90 changes: 90 additions & 0 deletions src/services/registration/registration-router.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
1 change: 1 addition & 0 deletions src/services/registration/registration-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ registrationRouter.post("/submit/", strongJwtVerification, async (_: Request, re
templateId: RegistrationTemplates.REGISTRATION_SUBMISSION,
recipients: [registrationInfo.emailAddress],
};

return sendMailWrapper(res, next, mailInfo);
});

Expand Down

0 comments on commit 4465937

Please sign in to comment.