Skip to content

Commit

Permalink
Added integration tests for players (#7)
Browse files Browse the repository at this point in the history
* Added integration tests for players

* Some updates to see if we can fix the integration tests

* Update to the global setup

* Fix the verify integration test action

* A couple of edits

* A small adjustment to the Dockerfile
  • Loading branch information
richarddubay authored Aug 9, 2024
1 parent fba05ce commit 9c674ca
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/verify-server-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ defaults:
working-directory: ./server

env:
DATABASE_URL: postgresql://postgres:password@localhost:5432/f1_fantasy_league_db?schema=public
DATABASE_URL: postgresql://postgres:password@localhost:5433/f1_fantasy_league_db?schema=public

jobs:
execute:
Expand Down
18 changes: 18 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM node:18-alpine

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY . .

RUN npm install

ARG API_DB_URL
ARG SESSION_KEY
ENV DATABASE_URL=$API_DB_URL
ENV SESSION_KEY=$SESSION_KEY

EXPOSE 3000

CMD ["npm", "run", "run-server"]
8 changes: 1 addition & 7 deletions server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,7 @@ app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.use("/auth", authRouter);

/* Auth Middleware */
// app.use(authMiddleware);
app.use((req, res, next) => {
if (req.path.startsWith("/auth")) {
return next(); // Skip authMiddleware for /auth routes
}
authMiddleware(req, res, next);
});
app.use(authMiddleware);

/* Choices */
app.use("/choice", choicesRouter);
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/players.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const deletePlayer = async (req: Request, res: Response) => {
});
if (player) {
const deletedPlayer = await playersModel.deletePlayer(numericId);
res.json({
res.status(204).json({
message: "Deleted player",
player: {
id: deletedPlayer.id,
Expand Down
4 changes: 4 additions & 0 deletions server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export async function authMiddleware(
return next();
}

if (req.path.startsWith("/auth")) {
return next(); // Skip authMiddleware for /auth routes
}

const authToken = req.headers.authorization?.split(" ")[1];

if (authToken) {
Expand Down
89 changes: 89 additions & 0 deletions server/routers/__tests__/players.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import app from "../../app";
import request from "supertest";

describe("/player", () => {
describe("POST /player", () => {
it("should respond with a 201", async () => {
await request(app)
.post("/player")
.send({
first_name: "Magic",
last_name: "Johnson",
identifier: "[email protected]",
password: "b@sketB@11",
})
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(201);
});

it("should respond with a 201 again", async () => {
await request(app)
.post("/player")
.send({
first_name: "Larry",
last_name: "Bird",
identifier: "[email protected]",
password: "Thr$$p0IntKing",
})
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(201);
});
});

describe("DELETE /player/1", () => {
it("should respond with a 204", async () => {
await request(app)
.delete("/player/1")
.set("Accept", "application/json")
.expect(204);
});
});

describe("GET /player", () => {
it("should respond with a 200", async () => {
await request(app)
.get("/player")
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200);
});
});

describe("GET /player/1", () => {
it("should respond with a 404", async () => {
await request(app)
.get("/player/1")
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(404);
});
});

describe("GET /player/2", () => {
it("should respond with a 200", async () => {
await request(app)
.get("/player/2")
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200);
});
});

describe("PUT /player/2", () => {
it("should respond with a 200", async () => {
await request(app)
.put("/player/2")
.send({
first_name: "Bruce",
last_name: "Wayne",
identifier: "[email protected]",
password: "Imn0tb@tman",
})
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200);
});
});
});

0 comments on commit 9c674ca

Please sign in to comment.