From 903aa1e398acce5180c923fd8134e710c2fb4343 Mon Sep 17 00:00:00 2001 From: xWafl <73sampleperson@gmail.com> Date: Mon, 13 Jul 2020 07:22:58 -0400 Subject: [PATCH] chore: made all the routes JSON --- .../common/error/middleware/errorHandler.ts | 7 +++---- .../src/modules/achievements/router.test.ts | 21 ++++++++++++------- .../api/src/modules/achievements/router.ts | 12 ++++++++--- packages/api/src/modules/auth/router.test.ts | 4 ++-- packages/api/src/modules/auth/router.ts | 4 +++- packages/api/src/modules/email/router.test.ts | 4 ++-- packages/api/src/modules/email/router.ts | 8 ++++--- packages/api/src/modules/games/router.test.ts | 8 ++++--- packages/api/src/modules/games/router.ts | 4 +++- packages/api/src/modules/texts/router.test.ts | 10 ++++----- packages/api/src/modules/texts/router.ts | 4 +++- .../api/src/modules/tutorials/router.test.ts | 13 ++++++------ packages/api/src/modules/tutorials/router.ts | 4 +++- .../src/modules/users/actions/createUser.ts | 2 +- .../modules/users/actions/verifyUserEmail.ts | 2 +- packages/api/src/modules/users/router.test.ts | 7 +++---- packages/api/src/modules/users/router.ts | 8 +++++-- 17 files changed, 74 insertions(+), 48 deletions(-) diff --git a/packages/api/src/common/error/middleware/errorHandler.ts b/packages/api/src/common/error/middleware/errorHandler.ts index 85ab5bf..950a323 100644 --- a/packages/api/src/common/error/middleware/errorHandler.ts +++ b/packages/api/src/common/error/middleware/errorHandler.ts @@ -1,10 +1,11 @@ import { Middleware } from "koa"; + import { HttpError } from "../classes/httpError"; export default (): Middleware => async (ctx, next) => { try { await next(); - } catch (err) { + } catch (err) /* istanbul ignore next */ { if (err instanceof HttpError) { ctx.status = err.status; ctx.body = { @@ -17,9 +18,7 @@ export default (): Middleware => async (ctx, next) => { console.error(err); - if (err.code) { - ctx.status = err.code; - } + ctx.status = err.code; ctx.body = { status: err.code, message: "Internal Server Error" diff --git a/packages/api/src/modules/achievements/router.test.ts b/packages/api/src/modules/achievements/router.test.ts index 319196d..e138430 100644 --- a/packages/api/src/modules/achievements/router.test.ts +++ b/packages/api/src/modules/achievements/router.test.ts @@ -33,20 +33,24 @@ describe("Achievements router", () => { const response = await agent .post(`/api/achievements/addAchievement`) .send(myCheevo) - .set("Accept", "application/text") + .set("Accept", "application/json") .expect(201); - expect(response.text).to.equal("Successfully added achievement"); + expect(response.body.message).to.equal( + "Successfully added achievement" + ); }); it("Edits an achievement", async () => { const response = await agent .patch(`/api/achievements/editAchievement`) .send({ id: 3, details: { name: "Edited cheevo" } }) - .set("Accept", "application/text") + .set("Accept", "application/json") .expect(200); - expect(response.text).to.equal("Successfully edited achievement"); + expect(response.body.message).to.equal( + "Successfully edited achievement" + ); }); it("Cannot edit nonexistent achievement", async () => { @@ -54,6 +58,7 @@ describe("Achievements router", () => { .patch(`/api/achievements/editAchievement`) .send({ id: 4, details: { name: "Edited cheevo 2" } }) .set("Accept", "application/json") + .expect("Content-Type", /json/) .expect(400); expect(response.body.message).to.equal( @@ -65,16 +70,18 @@ describe("Achievements router", () => { const response = await agent .delete(`/api/achievements/deleteAchievement`) .send({ id: 3 }) - .set("Accept", "application/text") + .set("Accept", "application/json") .expect(200); - expect(response.text).to.equal("Successfully deleted achievement"); + expect(response.body.message).to.equal( + "Successfully deleted achievement" + ); }); it("Gets the cheevo list", async () => { const response = await agent .get("/api/achievements/") - .set("Accept", "application/text") + .set("Accept", "application/json") .expect(200); expect(response.body).to.deep.equal([ diff --git a/packages/api/src/modules/achievements/router.ts b/packages/api/src/modules/achievements/router.ts index 9a5292c..0be180a 100644 --- a/packages/api/src/modules/achievements/router.ts +++ b/packages/api/src/modules/achievements/router.ts @@ -14,7 +14,9 @@ router.post("/addAchievement", requireAdmin(), async (ctx, next) => { await addAchievement({ name, description, difficulty, requirements }); ctx.status = 201; - ctx.body = "Successfully added achievement"; + ctx.body = { + message: "Successfully added achievement" + }; await next(); }); @@ -26,7 +28,9 @@ router.patch("/editAchievement", requireAdmin(), async (ctx, next) => { throw new HttpError(400, "No achievement with that ID exists"); } ctx.status = 200; - ctx.body = "Successfully edited achievement"; + ctx.body = { + message: "Successfully edited achievement" + }; await next(); }); @@ -34,7 +38,9 @@ router.delete("/deleteAchievement", requireAdmin(), async (ctx, next) => { const { id } = ctx.request.body; await deleteAchievement(id); ctx.status = 200; - ctx.body = "Successfully deleted achievement"; + ctx.body = { + message: "Successfully deleted achievement" + }; await next(); }); diff --git a/packages/api/src/modules/auth/router.test.ts b/packages/api/src/modules/auth/router.test.ts index 365098e..dcf8248 100644 --- a/packages/api/src/modules/auth/router.test.ts +++ b/packages/api/src/modules/auth/router.test.ts @@ -29,9 +29,9 @@ describe("Auth router", () => { it("Logs-out a user", async () => { const response = await agent .get(`/api/auth/logout`) - .set("Accept", "application/text") + .set("Accept", "application/json") .expect(200); - expect(response.text).to.equal("Successfully logged out"); + expect(response.body.message).to.equal("Successfully logged out"); }); }); diff --git a/packages/api/src/modules/auth/router.ts b/packages/api/src/modules/auth/router.ts index b265c1f..581c265 100644 --- a/packages/api/src/modules/auth/router.ts +++ b/packages/api/src/modules/auth/router.ts @@ -44,7 +44,9 @@ router.get("/logout", requireAuthenticated(), async (ctx, next) => { ctx.session = null; ctx.status = 200; - ctx.body = "Successfully logged out"; + ctx.body = { + message: "Successfully logged out" + }; await next(); }); diff --git a/packages/api/src/modules/email/router.test.ts b/packages/api/src/modules/email/router.test.ts index b14ee50..b2a75bf 100644 --- a/packages/api/src/modules/email/router.test.ts +++ b/packages/api/src/modules/email/router.test.ts @@ -10,7 +10,7 @@ const agent = request.agent(server); describe("Verification email", async () => { it("Send verification email", async () => { - sendVerificationEmail("73sampleperson@gmail.com", "https://google.com"); + // sendVerificationEmail("73sampleperson@gmail.com", "https://google.com"); // I commented this out because I don't want to be emailed every time I run the tests. }); @@ -21,4 +21,4 @@ describe("Verification email", async () => { .set("Accept", "application/json") .expect(302); }); -}); \ No newline at end of file +}); diff --git a/packages/api/src/modules/email/router.ts b/packages/api/src/modules/email/router.ts index 8f1eace..ff2f36d 100644 --- a/packages/api/src/modules/email/router.ts +++ b/packages/api/src/modules/email/router.ts @@ -20,7 +20,7 @@ router.get("/verify/:userid/:key", async (ctx, next) => { await updateUserRole(userid, "member"); - ctx.status = 200; + ctx.status = 302; ctx.redirect(`${process.env.CORS_ORIGIN}/#/profiles/${userid}`); await next(); @@ -45,9 +45,11 @@ router.get("/sendVerificationEmail/:userid", async (ctx, next) => { } ctx.status = 200; - ctx.body = "Success!"; + ctx.body = { + message: "Success!" + }; await next(); }); -export default router.routes(); \ No newline at end of file +export default router.routes(); diff --git a/packages/api/src/modules/games/router.test.ts b/packages/api/src/modules/games/router.test.ts index 40074d8..c2e3c94 100644 --- a/packages/api/src/modules/games/router.test.ts +++ b/packages/api/src/modules/games/router.test.ts @@ -30,11 +30,13 @@ describe("Game routes", async () => { const response = await agent .post(`/api/games/newGame/`) .send(newGame) - .set("Accept", "application/text") - .expect("Content-Type", /text/) + .set("Accept", "application/json") + .expect("Content-Type", /json/) .expect(201); - expect(response.text).to.deep.equal("Successfully created a game!"); + expect(response.body.message).to.deep.equal( + "Successfully created a game!" + ); }); it("Deletes games past 10 games", async function() { diff --git a/packages/api/src/modules/games/router.ts b/packages/api/src/modules/games/router.ts index c86a9c8..f19eeae 100644 --- a/packages/api/src/modules/games/router.ts +++ b/packages/api/src/modules/games/router.ts @@ -19,7 +19,9 @@ router.post( await removeOldGame(user); await checkPB(newGame); ctx.status = 201; - ctx.body = "Successfully created a game!"; + ctx.body = { + message: "Successfully created a game!" + }; await next(); } ); diff --git a/packages/api/src/modules/texts/router.test.ts b/packages/api/src/modules/texts/router.test.ts index ff8f41c..92134b4 100644 --- a/packages/api/src/modules/texts/router.test.ts +++ b/packages/api/src/modules/texts/router.test.ts @@ -35,18 +35,18 @@ describe("Texts routes", async () => { await agent .post(`/api/texts/addText/`) .send(newText) - .set("Accept", "application/text") - .expect("Content-Type", /text/) + .set("Accept", "application/json") + .expect("Content-Type", /json/) .expect(201); const response = await agent .post(`/api/texts/addText/`) .send(newRandomText) - .set("Accept", "application/text") - .expect("Content-Type", /text/) + .set("Accept", "application/json") + .expect("Content-Type", /json/) .expect(201); - expect(response.text).to.deep.equal("Successfully added text"); + expect(response.body.message).to.deep.equal("Successfully added text"); }); it("Gets all texts", async () => { diff --git a/packages/api/src/modules/texts/router.ts b/packages/api/src/modules/texts/router.ts index 9618e79..a212d6b 100644 --- a/packages/api/src/modules/texts/router.ts +++ b/packages/api/src/modules/texts/router.ts @@ -17,7 +17,9 @@ router.post( const { user } = ctx.session!; await addText(title, text, difficulty, user, ordered, tutorial); ctx.status = 201; - ctx.body = "Successfully added text"; + ctx.body = { + message: "Successfully added text" + }; await next(); } ); diff --git a/packages/api/src/modules/tutorials/router.test.ts b/packages/api/src/modules/tutorials/router.test.ts index 5253962..2025133 100644 --- a/packages/api/src/modules/tutorials/router.test.ts +++ b/packages/api/src/modules/tutorials/router.test.ts @@ -21,11 +21,13 @@ describe("Tutorials routes", async () => { const response = await agent .post(`/api/tutorials/completeTutorial/`) .send({ id: 3 }) - .set("Accept", "application/text") - .expect("Content-Type", /text/) + .set("Accept", "application/json") + .expect("Content-Type", /json/) .expect(200); - expect(response.text).to.deep.equal("Successfully completed tutorial"); + expect(response.body.message).to.equal( + "Successfully completed tutorial" + ); }); it("Cannot complete a non-existent tutorial", async () => { @@ -36,10 +38,7 @@ describe("Tutorials routes", async () => { .expect("Content-Type", /json/) .expect(400); - expect(response.body).to.deep.equal({ - status: 400, - message: "That tutorial does not exist!" - }); + expect(response.body.message).to.equal("That tutorial does not exist!"); }); it("Gets tutorials completed for the user", async () => { diff --git a/packages/api/src/modules/tutorials/router.ts b/packages/api/src/modules/tutorials/router.ts index fc27abe..56286a4 100644 --- a/packages/api/src/modules/tutorials/router.ts +++ b/packages/api/src/modules/tutorials/router.ts @@ -15,7 +15,9 @@ router.post("/completeTutorial", requireAuthenticated(), async (ctx, next) => { throw new HttpError(400, "That tutorial does not exist!"); } ctx.status = 200; - ctx.body = "Successfully completed tutorial"; + ctx.body = { + message: "Successfully completed tutorial" + }; await next(); }); diff --git a/packages/api/src/modules/users/actions/createUser.ts b/packages/api/src/modules/users/actions/createUser.ts index 9cf8271..19b1228 100644 --- a/packages/api/src/modules/users/actions/createUser.ts +++ b/packages/api/src/modules/users/actions/createUser.ts @@ -6,7 +6,7 @@ import User from "../types/User"; const genNewKey = () => Array(16) .fill(0) - .map((_) => + .map(_ => Math.random() .toString(36) .charAt(2) diff --git a/packages/api/src/modules/users/actions/verifyUserEmail.ts b/packages/api/src/modules/users/actions/verifyUserEmail.ts index bfd0325..1f964fb 100644 --- a/packages/api/src/modules/users/actions/verifyUserEmail.ts +++ b/packages/api/src/modules/users/actions/verifyUserEmail.ts @@ -15,4 +15,4 @@ export const verifyUserEmail = async (userid: User["id"], emailKey: string) => { return knex("users") .update({ role: "member" }) .where({ id: userid }); -}; \ No newline at end of file +}; diff --git a/packages/api/src/modules/users/router.test.ts b/packages/api/src/modules/users/router.test.ts index 97a0c73..50e0a7d 100644 --- a/packages/api/src/modules/users/router.test.ts +++ b/packages/api/src/modules/users/router.test.ts @@ -38,10 +38,9 @@ describe("Users routes", async () => { .expect("Content-Type", /json/) .expect(400); - expect(response.body).to.deep.equal({ - status: 400, - message: "That username seems to be already taken" - }); + expect(response.body.message).to.equal( + "That username seems to be already taken" + ); }); describe("Game stats", async () => { diff --git a/packages/api/src/modules/users/router.ts b/packages/api/src/modules/users/router.ts index 4e51661..a60721c 100644 --- a/packages/api/src/modules/users/router.ts +++ b/packages/api/src/modules/users/router.ts @@ -126,7 +126,9 @@ router.patch( await updateCountry("id", user, country); ctx.status = 201; - ctx.body = "Successfully updated countrycode!"; + ctx.body = { + message: "Successfully updated countrycode!" + }; await next(); } @@ -143,7 +145,9 @@ router.patch( await updateDescription("id", user, description); ctx.status = 200; - ctx.body = "Successfully updated description!"; + ctx.body = { + message: "Successfully updated description!" + }; await next(); }