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

Added registration tests #167

Merged
merged 3 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading