From 0c6111452ba6b32399c45b6faf05bfd0f4657610 Mon Sep 17 00:00:00 2001 From: Elihu Rodriguez Date: Tue, 27 Jun 2023 11:42:32 -0700 Subject: [PATCH 1/2] Added test suite for Players API endpoints. --- test/routes/players.test.js | 93 +++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 test/routes/players.test.js diff --git a/test/routes/players.test.js b/test/routes/players.test.js new file mode 100644 index 0000000..995a8e6 --- /dev/null +++ b/test/routes/players.test.js @@ -0,0 +1,93 @@ +import "dotenv/config"; +import express from "express"; +import mongoose from "mongoose"; +import request from "supertest"; +import router from "../../src/routes/players"; +import Model from "../../src/models/Players"; + +const app = express(); + +app.use(express.json()); +app.use("/players", router); + +// before all the route tests, connect to the database +beforeAll(async () => { + await mongoose.connect(process.env.DB_CONNECTION, { + useNewUrlParser: true, + useUnifiedTopology: true, + }); +}); + +// after all the test, close database connection +afterAll(async () => { + await mongoose.connection.close(); +}); + +// test player to be used for endpoints that need to reference a document in the database +let testPlayer; +const testFields = { + username: "this user should not exist", + level: "0", + score: "00", + email: "test@email.com", + groupID: "0", +}; + +// before each test, create a temp test player +beforeEach(async () => { + testPlayer = await Model.create(testFields); +}); + +// after each test, delete the temp test player +afterEach(async () => { + await Model.deleteOne({ _id: testPlayer._id }); +}); + +// run tests againts the players endpoints by receiving the expected responses +describe("Test the players endpoint(s)", () => { + // test for successful response to a GET request to /players + test("GET /players", async () => { + const response = await request(app).get("/players"); + expect(response.statusCode).toBe(200); + }); + + // test for successful response to a POST request to /players + test("POST /players", async () => { + // appends integer to username to avoid unique validators, if any + testFields.username += Date.now().toString(); + + // send request to create a temp document + const response = await request(app).post("/players").send(testFields); + expect(response.statusCode).toBe(201); + + // delete the temp document that was created for this test + await Model.deleteOne({ _id: response.body._id }); + }); + + // test for successful response to a GET request to /players/id + test("GET /players/id", async () => { + // send request to get the test player that was just created + const response = await request(app).get(`/players/${testPlayer._id}`); + expect(response.statusCode).toBe(200); + }); + + // test for successful response to a PATCH request to /players/id + test("PATCH /players/id", async () => { + const updatePlayer = { username: "this updated user should not exist" }; + + // send request to update username of the test player + const response = await request(app) + .patch(`/players/${testPlayer.id}`) + .send(updatePlayer); + expect(response.statusCode).toBe(200); + }); + + // test for successful response to a DELETE request to /players/id + test("DELETE /players/id", async () => { + // send request to delete the test player + const response = await request(app).delete( + `/players/${testPlayer._id}` + ); + expect(response.statusCode).toBe(200); + }); +}); From a58f09ba8f3472215caa94f201c149e51f88e6c4 Mon Sep 17 00:00:00 2001 From: Elihu Rodriguez Date: Tue, 27 Jun 2023 11:43:20 -0700 Subject: [PATCH 2/2] Added more defined expectations for various endpoints. --- test/routes/players.test.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/test/routes/players.test.js b/test/routes/players.test.js index 995a8e6..59914f4 100644 --- a/test/routes/players.test.js +++ b/test/routes/players.test.js @@ -12,7 +12,7 @@ app.use("/players", router); // before all the route tests, connect to the database beforeAll(async () => { - await mongoose.connect(process.env.DB_CONNECTION, { + await mongoose.connect(process.env.TEST_DB_CONNECTION, { useNewUrlParser: true, useUnifiedTopology: true, }); @@ -49,6 +49,7 @@ describe("Test the players endpoint(s)", () => { test("GET /players", async () => { const response = await request(app).get("/players"); expect(response.statusCode).toBe(200); + expect(response.body.length >= 0).toBe(true); }); // test for successful response to a POST request to /players @@ -60,15 +61,27 @@ describe("Test the players endpoint(s)", () => { const response = await request(app).post("/players").send(testFields); expect(response.statusCode).toBe(201); + // confirm a player document was created by checking id + expect(mongoose.Types.ObjectId.isValid(response.body._id)).toBe(true); + + // confirm the player created used the test fields + expect(response.body.username).toBe(testFields.username); + // delete the temp document that was created for this test await Model.deleteOne({ _id: response.body._id }); }); // test for successful response to a GET request to /players/id test("GET /players/id", async () => { + const requestedID = testPlayer._id.toString(); + // send request to get the test player that was just created - const response = await request(app).get(`/players/${testPlayer._id}`); + const response = await request(app).get(`/players/${requestedID}`); expect(response.statusCode).toBe(200); + + const receivedID = response.body._id.toString(); + // confirm the player document received in response is the document requested + expect(receivedID).toBe(requestedID); }); // test for successful response to a PATCH request to /players/id @@ -80,14 +93,22 @@ describe("Test the players endpoint(s)", () => { .patch(`/players/${testPlayer.id}`) .send(updatePlayer); expect(response.statusCode).toBe(200); + + // confirm the request to update one document was executed + expect(response.body.acknowledged).toBe(true); + expect(response.body.modifiedCount).toBe(1); }); // test for successful response to a DELETE request to /players/id test("DELETE /players/id", async () => { + const requestedID = testPlayer._id.toString(); + // send request to delete the test player - const response = await request(app).delete( - `/players/${testPlayer._id}` - ); + const response = await request(app).delete(`/players/${requestedID}`); expect(response.statusCode).toBe(200); + + // confirm the request to delete one document was executed + expect(response.body.acknowledged).toBe(true); + expect(response.body.deletedCount).toBe(1); }); });