diff --git a/src/services/rsvp/rsvp-router.test.ts b/src/services/rsvp/rsvp-router.test.ts index bb0a30d8..4ab22ee8 100644 --- a/src/services/rsvp/rsvp-router.test.ts +++ b/src/services/rsvp/rsvp-router.test.ts @@ -2,7 +2,7 @@ import { describe, expect, it, beforeEach } from "@jest/globals"; import { TESTER, getAsAttendee, getAsStaff, putAsApplicant, getAsAdmin } from "../../testTools.js"; import { DecisionStatus, DecisionResponse } from "../../database/decision-db.js"; import Models from "../../database/models.js"; -import Constants from "../../constants.js"; +import { StatusCode } from "status-code-enum"; const TESTER_DECISION_INFO = { userId: TESTER.id, @@ -23,13 +23,13 @@ describe("GET /rsvp", () => { userId: TESTER.id, }); - const response = await getAsAttendee("/rsvp/").expect(Constants.BAD_REQUEST); + const response = await getAsAttendee("/rsvp/").expect(StatusCode.ClientErrorBadRequest); expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound"); }); it("works for an attendee user and returns filtered data", async () => { - const response = await getAsAttendee("/rsvp/").expect(Constants.SUCCESS); + const response = await getAsAttendee("/rsvp/").expect(StatusCode.SuccessOK); expect(JSON.parse(response.text)).toMatchObject({ userId: TESTER.id, @@ -39,7 +39,7 @@ describe("GET /rsvp", () => { }); it("works for a staff user and returns unfiltered data", async () => { - const response = await getAsStaff("/rsvp/").expect(Constants.SUCCESS); + const response = await getAsStaff("/rsvp/").expect(StatusCode.SuccessOK); expect(JSON.parse(response.text)).toMatchObject(TESTER_DECISION_INFO); }); @@ -53,13 +53,13 @@ describe("GET /rsvp/:USERID", () => { }); it("gets if caller has elevated perms (Staff)", async () => { - const response = await getAsStaff(`/rsvp/${TESTER.id}`).expect(Constants.SUCCESS); + const response = await getAsStaff(`/rsvp/${TESTER.id}`).expect(StatusCode.SuccessOK); expect(JSON.parse(response.text)).toMatchObject(TESTER_DECISION_INFO); }); it("gets if caller has elevated perms (Admin)", async () => { - const response = await getAsAdmin("/rsvp/" + TESTER.id).expect(Constants.SUCCESS); + const response = await getAsAdmin("/rsvp/" + TESTER.id).expect(StatusCode.SuccessOK); expect(JSON.parse(response.text)).toMatchObject(TESTER_DECISION_INFO); }); @@ -68,7 +68,7 @@ describe("GET /rsvp/:USERID", () => { await Models.DecisionInfo.deleteOne({ userId: TESTER.id, }); - const response = await getAsStaff("/rsvp/idontexist").expect(Constants.BAD_REQUEST); + const response = await getAsStaff("/rsvp/idontexist").expect(StatusCode.ClientErrorBadRequest); expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound"); }); @@ -76,7 +76,7 @@ describe("GET /rsvp/:USERID", () => { describe("PUT /rsvp", () => { it("error checking for empty query works", async () => { - const response = await putAsApplicant("/rsvp/").send({}).expect(Constants.BAD_REQUEST); + const response = await putAsApplicant("/rsvp/").send({}).expect(StatusCode.ClientErrorBadRequest); expect(JSON.parse(response.text)).toHaveProperty("error", "InvalidParams"); }); @@ -85,19 +85,19 @@ describe("PUT /rsvp", () => { await Models.DecisionInfo.deleteOne({ userId: TESTER.id, }); - const response = await putAsApplicant("/rsvp/").send({ isAttending: true }).expect(Constants.BAD_REQUEST); + const response = await putAsApplicant("/rsvp/").send({ isAttending: true }).expect(StatusCode.ClientErrorBadRequest); expect(JSON.parse(response.text)).toHaveProperty("error", "UserNotFound"); }); it("lets applicant accept accepted decision", async () => { - const response = await putAsApplicant("/rsvp/").send({ isAttending: true }).expect(Constants.SUCCESS); + const response = await putAsApplicant("/rsvp/").send({ isAttending: true }).expect(StatusCode.SuccessOK); expect(JSON.parse(response.text)).toHaveProperty("response", DecisionResponse.ACCEPTED); }); it("lets applicant reject accepted decision", async () => { - const response = await putAsApplicant("/rsvp/").send({ isAttending: false }).expect(Constants.SUCCESS); + const response = await putAsApplicant("/rsvp/").send({ isAttending: false }).expect(StatusCode.SuccessOK); expect(JSON.parse(response.text)).toHaveProperty("response", DecisionResponse.DECLINED); }); @@ -105,7 +105,7 @@ describe("PUT /rsvp", () => { it("doesn't let applicant accept rejected decision", async () => { await Models.DecisionInfo.findOneAndUpdate({ userId: TESTER.id }, { status: DecisionStatus.REJECTED }); - const response = await putAsApplicant("/rsvp/").send({ isAttending: false }).expect(Constants.FORBIDDEN); + const response = await putAsApplicant("/rsvp/").send({ isAttending: false }).expect(StatusCode.ClientErrorForbidden); expect(JSON.parse(response.text)).toHaveProperty("error", "Forbidden"); }); diff --git a/src/services/rsvp/rsvp-router.ts b/src/services/rsvp/rsvp-router.ts index a1d777b9..dd0237cd 100644 --- a/src/services/rsvp/rsvp-router.ts +++ b/src/services/rsvp/rsvp-router.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import Constants from "../../constants.js"; +import { StatusCode } from "status-code-enum"; import { strongJwtVerification } from "../../middleware/verify-jwt.js"; import { JwtPayload } from "../auth/auth-models.js"; import { hasElevatedPerms } from "../auth/auth-lib.js"; @@ -46,10 +46,10 @@ rsvpRouter.get("/:USERID", strongJwtVerification, async (req: Request, res: Resp //Returns error if query is empty if (!queryResult) { - return res.status(Constants.BAD_REQUEST).send({ error: "UserNotFound" }); + return res.status(StatusCode.ClientErrorBadRequest).send({ error: "UserNotFound" }); } - return res.status(Constants.SUCCESS).send(queryResult.toObject()); + return res.status(StatusCode.SuccessOK).send(queryResult.toObject()); }); /** @@ -93,7 +93,7 @@ rsvpRouter.get("/", strongJwtVerification, async (_: Request, res: Response) => //Returns error if query is empty if (!queryResult) { - return res.status(Constants.BAD_REQUEST).send({ error: "UserNotFound" }); + return res.status(StatusCode.ClientErrorBadRequest).send({ error: "UserNotFound" }); } const toReturn = queryResult.toObject(); @@ -104,7 +104,7 @@ rsvpRouter.get("/", strongJwtVerification, async (_: Request, res: Response) => delete toReturn.emailSent; } - return res.status(Constants.SUCCESS).send(toReturn); + return res.status(StatusCode.SuccessOK).send(toReturn); }); /** @@ -142,7 +142,7 @@ rsvpRouter.put("/", strongJwtVerification, async (req: Request, res: Response) = //Returns error if request body has no isAttending parameter if (rsvp === undefined) { - return res.status(Constants.BAD_REQUEST).send({ error: "InvalidParams" }); + return res.status(StatusCode.ClientErrorBadRequest).send({ error: "InvalidParams" }); } const payload: JwtPayload = res.locals.payload as JwtPayload; @@ -153,12 +153,12 @@ rsvpRouter.put("/", strongJwtVerification, async (req: Request, res: Response) = //Returns error if query is empty if (!queryResult) { - return res.status(Constants.BAD_REQUEST).send({ error: "UserNotFound" }); + return res.status(StatusCode.ClientErrorBadRequest).send({ error: "UserNotFound" }); } //If the current user has not been accepted, send an error if (queryResult.status != DecisionStatus.ACCEPTED) { - return res.status(Constants.FORBIDDEN).send({ error: "Forbidden" }); + return res.status(StatusCode.ClientErrorForbidden).send({ error: "Forbidden" }); } //If current user has been accepted, update their RSVP decision to "ACCEPTED"/"DECLINED" acoordingly @@ -172,9 +172,9 @@ rsvpRouter.put("/", strongJwtVerification, async (req: Request, res: Response) = ); if (updatedDecision) { - return res.status(Constants.SUCCESS).send(updatedDecision.toObject()); + return res.status(StatusCode.SuccessOK).send(updatedDecision.toObject()); } else { - return res.status(Constants.INTERNAL_ERROR).send({ error: "InternalError" }); + return res.status(StatusCode.ServerErrorInternal).send({ error: "InternalError" }); } });