Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
chore: made all the routes JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonHowe committed Jul 13, 2020
1 parent f484be0 commit 903aa1e
Show file tree
Hide file tree
Showing 17 changed files with 74 additions and 48 deletions.
7 changes: 3 additions & 4 deletions packages/api/src/common/error/middleware/errorHandler.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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"
Expand Down
21 changes: 14 additions & 7 deletions packages/api/src/modules/achievements/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,32 @@ 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 () => {
const response = await agent
.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(
Expand All @@ -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([
Expand Down
12 changes: 9 additions & 3 deletions packages/api/src/modules/achievements/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -26,15 +28,19 @@ 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();
});

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();
});

Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/modules/auth/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});
4 changes: 3 additions & 1 deletion packages/api/src/modules/auth/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/modules/email/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const agent = request.agent(server);

describe("Verification email", async () => {
it("Send verification email", async () => {
sendVerificationEmail("[email protected]", "https://google.com");
// sendVerificationEmail("[email protected]", "https://google.com");
// I commented this out because I don't want to be emailed every time I run the tests.
});

Expand All @@ -21,4 +21,4 @@ describe("Verification email", async () => {
.set("Accept", "application/json")
.expect(302);
});
});
});
8 changes: 5 additions & 3 deletions packages/api/src/modules/email/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
export default router.routes();
8 changes: 5 additions & 3 deletions packages/api/src/modules/games/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/modules/games/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);
Expand Down
10 changes: 5 additions & 5 deletions packages/api/src/modules/texts/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/modules/texts/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
);
Expand Down
13 changes: 6 additions & 7 deletions packages/api/src/modules/tutorials/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/api/src/modules/tutorials/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/modules/users/actions/createUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import User from "../types/User";
const genNewKey = () =>
Array(16)
.fill(0)
.map((_) =>
.map(_ =>
Math.random()
.toString(36)
.charAt(2)
Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/modules/users/actions/verifyUserEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ export const verifyUserEmail = async (userid: User["id"], emailKey: string) => {
return knex<User>("users")
.update({ role: "member" })
.where({ id: userid });
};
};
7 changes: 3 additions & 4 deletions packages/api/src/modules/users/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/api/src/modules/users/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down

0 comments on commit 903aa1e

Please sign in to comment.