Skip to content

Commit

Permalink
new status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
aletya committed Oct 26, 2023
1 parent b0f6441 commit 7c9e27c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
24 changes: 12 additions & 12 deletions src/services/rsvp/rsvp-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -68,15 +68,15 @@ 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");
});
});

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");
});
Expand All @@ -85,27 +85,27 @@ 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);
});

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");
});
Expand Down
20 changes: 10 additions & 10 deletions src/services/rsvp/rsvp-router.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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());
});

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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);
});

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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" });
}
});

Expand Down

0 comments on commit 7c9e27c

Please sign in to comment.