Skip to content

Commit

Permalink
Cleanup & add spec to admission router
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez committed Oct 23, 2024
1 parent 90e60e9 commit 2672994
Show file tree
Hide file tree
Showing 8 changed files with 383 additions and 432 deletions.
34 changes: 0 additions & 34 deletions src/database/admission-db.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/database/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getModelForClass } from "@typegoose/typegoose";

import { AuthInfo } from "../services/auth/auth-schemas";
import { AttendeeFollowing, AttendeeProfile } from "./attendee-db";
import { AdmissionDecision } from "./admission-db";
import { AdmissionDecision } from "../services/admission/admission-schemas";
import { MentorOfficeHours } from "./mentor-db";
import { Event, EventAttendance, EventFollowers } from "../services/event/event-schemas";
import { NewsletterSubscription } from "../services/newsletter/newsletter-schemas";
Expand Down
2 changes: 1 addition & 1 deletion src/services/admission/admission-formats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArrayOfType, isBoolean, isEnumOfType, isNumber, isString } from "../../common/formatTools";
import { AdmissionDecision, DecisionResponse, DecisionStatus } from "../../database/admission-db";
import { AdmissionDecision, DecisionResponse, DecisionStatus } from "./admission-schemas";

export function isValidApplicantFormat(obj: AdmissionDecision[]): boolean {
return isArrayOfType(obj, isValidApplicantDecision);
Expand Down
7 changes: 0 additions & 7 deletions src/services/admission/admission-lib.ts

This file was deleted.

40 changes: 28 additions & 12 deletions src/services/admission/admission-router.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";
import Models from "../../database/models";
import { DecisionStatus, DecisionResponse, AdmissionDecision } from "../../database/admission-db";
import { DecisionStatus, DecisionResponse, AdmissionDecision } from "./admission-schemas";
import { RegistrationFormat } from "../registration/registration-formats";
import { RegistrationTemplates } from "../../common/config";
import { Gender, Degree, Race, HackInterest, HackOutreach } from "../registration/registration-models";
Expand All @@ -15,13 +15,17 @@ const TESTER_DECISION = {
status: DecisionStatus.ACCEPTED,
response: DecisionResponse.PENDING,
emailSent: false,
admittedPro: false,
reimbursementValue: 0,
} satisfies AdmissionDecision;

const OTHER_DECISION = {
userId: "other-user",
status: DecisionStatus.REJECTED,
response: DecisionResponse.DECLINED,
emailSent: true,
admittedPro: false,
reimbursementValue: 0,
} satisfies AdmissionDecision;

const TESTER_APPLICATION = {
Expand Down Expand Up @@ -52,12 +56,16 @@ const updateRequest = [
status: DecisionStatus.WAITLISTED,
response: DecisionResponse.PENDING,
emailSent: false,
admittedPro: false,
reimbursementValue: 0,
},
{
userId: OTHER_DECISION.userId,
status: DecisionStatus.ACCEPTED,
response: DecisionResponse.PENDING,
emailSent: false,
admittedPro: false,
reimbursementValue: 0,
},
] satisfies AdmissionDecision[];

Expand Down Expand Up @@ -99,7 +107,7 @@ describe("PUT /admission/update/", () => {

it("should update application status of applicants", async () => {
const response = await putAsStaff("/admission/update/").send(updateRequest).expect(StatusCode.SuccessOK);
expect(JSON.parse(response.text)).toHaveProperty("message", "StatusSuccess");
expect(JSON.parse(response.text)).toEqual({ success: true });

const ops = updateRequest.map((entry) => Models.AdmissionDecision.findOne({ userId: entry.userId }));
const retrievedEntries = await Promise.all(ops);
Expand All @@ -118,14 +126,14 @@ describe("PUT /admission/update/", () => {
});

describe("GET /admission/rsvp/", () => {
it("gives a UserNotFound error for an non-existent user", async () => {
it("gives a DecisionNotFound error for an non-existent user", async () => {
await Models.AdmissionDecision.deleteOne({
userId: TESTER.id,
});

const response = await getAsAttendee("/admission/rsvp/").expect(StatusCode.ClientErrorNotFound);

expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound");
expect(JSON.parse(response.text)).toHaveProperty("error", "DecisionNotFound");
});

it("works for an attendee user and returns filtered data", async () => {
Expand All @@ -137,9 +145,17 @@ describe("GET /admission/rsvp/", () => {
response: TESTER_DECISION.response,
});
});
});
describe("GET /admission/rsvp/staff/", () => {
it("gives forbidden error for user without elevated perms", async () => {
const responseUser = await getAsUser("/admission/rsvp/staff/")
.send(updateRequest)
.expect(StatusCode.ClientErrorForbidden);
expect(JSON.parse(responseUser.text)).toHaveProperty("error", "Forbidden");
});

it("works for a staff user and returns unfiltered data", async () => {
const response = await getAsStaff("/admission/rsvp/").expect(StatusCode.SuccessOK);
const response = await getAsStaff("/admission/rsvp/staff/").expect(StatusCode.SuccessOK);

// expect(JSON.parse(response.text)).toMatchObject(TESTER_DECISION);
expect(JSON.parse(response.text)).toEqual(
Expand All @@ -158,7 +174,7 @@ describe("GET /admission/rsvp/", () => {
});
});

describe("GET /admission/rsvp/:USERID", () => {
describe("GET /admission/rsvp/:id", () => {
it("returns forbidden error if caller doesn't have elevated perms", async () => {
const response = await getAsAttendee(`/admission/rsvp/${TESTER.id}`).expect(StatusCode.ClientErrorForbidden);

Expand All @@ -171,10 +187,10 @@ describe("GET /admission/rsvp/:USERID", () => {
expect(JSON.parse(response.text)).toMatchObject(TESTER_DECISION);
});

it("returns UserNotFound error if user doesn't exist", async () => {
it("returns DecisionNotFound error if user doesn't exist", async () => {
const response = await getAsStaff("/admission/rsvp/idontexist").expect(StatusCode.ClientErrorNotFound);

expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound");
expect(JSON.parse(response.text)).toHaveProperty("error", "DecisionNotFound");
});
});

Expand All @@ -187,14 +203,14 @@ describe("PUT /admission/rsvp/accept", () => {
sendMail.mockImplementation(async (_) => ({}) as AxiosResponse);
});

it("returns UserNotFound for nonexistent user", async () => {
it("returns DecisionNotFound for nonexistent user", async () => {
await Models.AdmissionDecision.deleteOne({
userId: TESTER.id,
});

const response = await putAsApplicant("/admission/rsvp/accept/").expect(StatusCode.ClientErrorNotFound);

expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound");
expect(JSON.parse(response.text)).toHaveProperty("error", "DecisionNotFound");
});

it("lets applicant accept accepted decision", async () => {
Expand Down Expand Up @@ -240,14 +256,14 @@ describe("PUT /admission/rsvp/decline/", () => {
sendMail.mockImplementation(async (_) => ({}) as AxiosResponse);
});

it("returns UserNotFound for nonexistent user", async () => {
it("returns DecisionNotFound for nonexistent user", async () => {
await Models.AdmissionDecision.deleteOne({
userId: TESTER.id,
});

const response = await putAsApplicant("/admission/rsvp/decline/").expect(StatusCode.ClientErrorNotFound);

expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound");
expect(JSON.parse(response.text)).toHaveProperty("error", "DecisionNotFound");
});

it("lets applicant decline accepted decision", async () => {
Expand Down
Loading

0 comments on commit 2672994

Please sign in to comment.