Skip to content

Commit

Permalink
Remove POST / from user router
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez committed Oct 22, 2023
1 parent d4fafe5 commit f2b64bf
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 118 deletions.
64 changes: 1 addition & 63 deletions src/services/user/user-router.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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: `[email protected]`,
Expand All @@ -41,12 +26,6 @@ const OTHER_USER_AUTH = {
roles: AUTH_ROLE_TO_ROLES[Role.ATTENDEE],
} satisfies AuthInfo;

const NEW_USER = {
userId: "new-user",
email: `[email protected]`,
name: "New User",
} satisfies UserInfo;

// Before each test, initialize database with tester & other users
beforeEach(async () => {
Models.initialize();
Expand Down Expand Up @@ -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);
});
});
55 changes: 0 additions & 55 deletions src/services/user/user-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit f2b64bf

Please sign in to comment.