From f2b64bfd9a698e66f07fb8b2f925ad3c4464b7e6 Mon Sep 17 00:00:00 2001 From: Timothy-Gonzalez <105177619+Timothy-Gonzalez@users.noreply.github.com> Date: Sun, 22 Oct 2023 12:05:47 -0500 Subject: [PATCH] Remove POST / from user router --- src/services/user/user-router.test.ts | 64 +-------------------------- src/services/user/user-router.ts | 55 ----------------------- 2 files changed, 1 insertion(+), 118 deletions(-) diff --git a/src/services/user/user-router.test.ts b/src/services/user/user-router.test.ts index 805c83ca..f9c8f52f 100644 --- a/src/services/user/user-router.test.ts +++ b/src/services/user/user-router.test.ts @@ -1,14 +1,5 @@ import { describe, expect, it, beforeEach, jest } from "@jest/globals"; -import { - AUTH_ROLE_TO_ROLES, - TESTER, - get, - getAsAdmin, - getAsAttendee, - getAsStaff, - postAsAttendee, - postAsStaff, -} from "../../testTools.js"; +import { AUTH_ROLE_TO_ROLES, TESTER, get, getAsAdmin, getAsAttendee, getAsStaff } from "../../testTools.js"; import Models from "../../database/models.js"; import { UserInfo } from "../../database/user-db.js"; import { AuthInfo } from "../../database/auth-db.js"; @@ -23,12 +14,6 @@ const TESTER_USER = { email: TESTER.email, } satisfies UserInfo; -const TESTER_USER_WITH_NEW_EMAIL = { - userId: TESTER.id, - email: `${TESTER.email}-with-new-email.com`, - name: TESTER.name, -} satisfies UserInfo; - const OTHER_USER = { userId: "other-user", email: `other-user@hackillinois.org`, @@ -41,12 +26,6 @@ const OTHER_USER_AUTH = { roles: AUTH_ROLE_TO_ROLES[Role.ATTENDEE], } satisfies AuthInfo; -const NEW_USER = { - userId: "new-user", - email: `new-user@hackillinois.org`, - name: "New User", -} satisfies UserInfo; - // Before each test, initialize database with tester & other users beforeEach(async () => { Models.initialize(); @@ -205,44 +184,3 @@ describe("GET /:USERID/", () => { expect(JSON.parse(response.text)).toMatchObject(OTHER_USER); }); }); - -describe("POST /", () => { - it("gives an unauthorized error for an non-staff user", async () => { - const response = await postAsAttendee("/user/").send(JSON.stringify(TESTER_USER_WITH_NEW_EMAIL)).expect(403); - - expect(JSON.parse(response.text)).toHaveProperty("error", "InvalidToken"); - }); - it("gives an bad format error for bad data", async () => { - const response = await postAsStaff("/user/") - .send( - JSON.stringify({ - nonsense: 123, - }), - ) - .expect(400); - - expect(JSON.parse(response.text)).toHaveProperty("error", "InvalidParams"); - }); - it("creates a user for an staff user", async () => { - const response = await postAsStaff("/user/").send(JSON.stringify(NEW_USER)).expect(200); - - expect(JSON.parse(response.text)).toMatchObject(NEW_USER); - - const stored = await Models.UserInfo.findOne({ - userId: NEW_USER.userId, - }); - - expect(stored).toMatchObject(NEW_USER); - }); - it("updates a user for an staff user", async () => { - const response = await postAsStaff("/user/").send(JSON.stringify(TESTER_USER_WITH_NEW_EMAIL)).expect(200); - - expect(JSON.parse(response.text)).toMatchObject(TESTER_USER_WITH_NEW_EMAIL); - - const stored = await Models.UserInfo.findOne({ - userId: TESTER_USER_WITH_NEW_EMAIL.userId, - }); - - expect(stored).toMatchObject(TESTER_USER_WITH_NEW_EMAIL); - }); -}); diff --git a/src/services/user/user-router.ts b/src/services/user/user-router.ts index ad76eb14..acf7a921 100644 --- a/src/services/user/user-router.ts +++ b/src/services/user/user-router.ts @@ -167,59 +167,4 @@ userRouter.get("/", strongJwtVerification, async (_: Request, res: Response) => } }); -/** - * @api {post} /user/ POST /user/ - * @apiGroup User - * @apiDescription Update a given user - * - * @apiBody {String} userId UserID - * @apiBody {String} name User's name. - * @apiBody {String} email Email address (staff gmail or Github email). - * @apiParamExample {json} Example Request: - * { - "userId": "provider00001", - "name": "john doe", - "email": "johndoe@provider.com" - * } - * - * @apiSuccess (200: Success) {String} userId UserID - * @apiSuccess (200: Success) {String} name User's name. - * @apiSuccess (200: Success) {String} email Email address (staff gmail or Github email). - - * @apiSuccessExample Example Success Response: - * HTTP/1.1 200 OK - * { - "userId": "provider00001", - "name": "john", - "email": "johndoe@provider.com" - * } - * @apiUse strongVerifyErrors - */ -userRouter.post("/", strongJwtVerification, async (req: Request, res: Response) => { - const token: JwtPayload = res.locals.payload as JwtPayload; - - if (!hasElevatedPerms(token)) { - return res.status(Constants.FORBIDDEN).send({ error: "InvalidToken" }); - } - - // Get userData from the request, and print to output - const userData: UserFormat = req.body as UserFormat; - - if (!isValidUserFormat(userData)) { - return res.status(Constants.BAD_REQUEST).send({ error: "InvalidParams" }); - } - - // Update the given user - const updatedUser: UserInfo | null = await Models.UserInfo.findOneAndUpdate({ userId: userData.userId }, userData, { - upsert: true, - new: true, - }); - - if (updatedUser) { - return res.status(Constants.SUCCESS).send(updatedUser); - } else { - return res.status(Constants.INTERNAL_ERROR).send({ error: "InternalError" }); - } -}); - export default userRouter;