From 1ab0c447d1c5b903932dbe878a23a569b978a405 Mon Sep 17 00:00:00 2001 From: Jonas Date: Sat, 11 Dec 2021 16:08:10 +0100 Subject: [PATCH 1/4] Update pipeline.yml --- .github/workflows/pipeline.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 1d980f3..a09be7b 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -39,7 +39,7 @@ jobs: # start: npm run start-prod # wait-on: http://localhost:5000 - name: Deploy - if: ${{ github.base_ref == 'main' }} + if: ${{ github.ref == 'main' }} uses: akhileshns/heroku-deploy@v3.12.12 # This is the action with: heroku_api_key: ${{secrets.HEROKU_API_KEY}} @@ -47,11 +47,9 @@ jobs: heroku_email: "youneslamkhanter02@gmail.com" # healthcheck: "law-firm-management-sys.herokuapp.com/version" # checkString: "1.1.0" - - name: Bump version and push tag - if: ${{ github.base_ref == 'main' }} + if: ${{ github.ref == 'main' }} uses: anothrNick/github-tag-action@1.33.0 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} WITH_V: true - From 1e869e4f59447dbf45fd220f2e84957cd2f021d7 Mon Sep 17 00:00:00 2001 From: Jonas Date: Sat, 11 Dec 2021 16:08:34 +0100 Subject: [PATCH 2/4] Add health check test --- server/routes/api.js | 2 +- server/tests/cases/case_api.test.js | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/server/routes/api.js b/server/routes/api.js index e4cee3c..e873ff7 100644 --- a/server/routes/api.js +++ b/server/routes/api.js @@ -9,7 +9,7 @@ api.use("/api/cases", middleware.userAuthentication, caseRouter); api.use("/api/users", userRouter); api.use("/api/login", loginRouter); api.get("/health", (req, res) => { - res.send("v0.1.0"); + res.status(200).send("v0.1.0"); }); module.exports = api; diff --git a/server/tests/cases/case_api.test.js b/server/tests/cases/case_api.test.js index 75f2784..271b0ea 100644 --- a/server/tests/cases/case_api.test.js +++ b/server/tests/cases/case_api.test.js @@ -17,6 +17,12 @@ beforeAll(async () => { await Case.create({ ...helper.cases }); }); +describe("Health check", () => { + test("should return 200 OK", async () => { + await request(app).get("/v1/health").expect(200); + }); +}); + describe("GET /v1/api/cases", () => { test("should return all cases", async () => { const logedInUser = await request(app) From 415091204dd1a72da274368a2eb2467c6b0abe59 Mon Sep 17 00:00:00 2001 From: Jonas Date: Sat, 11 Dec 2021 16:23:13 +0100 Subject: [PATCH 3/4] Update tests --- server/tests/cases/case_api.test.js | 309 ++++++++++++++-------------- 1 file changed, 155 insertions(+), 154 deletions(-) diff --git a/server/tests/cases/case_api.test.js b/server/tests/cases/case_api.test.js index 271b0ea..4294d15 100644 --- a/server/tests/cases/case_api.test.js +++ b/server/tests/cases/case_api.test.js @@ -33,6 +33,7 @@ describe("GET /v1/api/cases", () => { }) .set("Content-Type", "application/json"); + console.log(logedInUser.body); const response = await request(app) .get("/v1/api/cases") .set({ Authorization: "bearer " + logedInUser.body.token }) @@ -44,160 +45,160 @@ describe("GET /v1/api/cases", () => { }); }); -describe("POST /v1/api/cases", () => { - const insertedCase = { - title: "Case example", - description: "Case description", - judge: "Jonas Tesla", - enemy: "Vladimir Lennin", - place: " Dalas", - result: "Fucked up", - status: "opened", - priority: "high", - }; - const insertedCaseFalse = { - description: "Case description", - judge: "Jonas Tesla", - enemy: "Vladimir Lennin", - place: " Dalas", - result: "Fucked up", - status: "opened", - priority: "high", - }; - - test("should return the last inserted case", async () => { - const logedInUser = await request(app).post("/v1/api/login").send({ - email: "jonas@email.com", - password: "Jonas@123", - }); - - const response = await request(app) - .post("/v1/api/cases") - .set({ Authorization: "bearer " + logedInUser.body.token }) - .send({ - ...insertedCase, - clientId: 1, - userId: 2, - }) - .set("Content-Type", "application/json") - .expect(201); - expect(response.body).toEqual(insertedCase); - }); - - test("should throw an error when inserting false data", async () => { - const logedInUser = await request(app).post("/v1/api/login").send({ - email: "jonas@email.com", - password: "Jonas@123", - }); - - const response = await request(app) - .post("/v1/api/cases") - .set({ Authorization: "bearer " + logedInUser.body.token }) - .send({ - ...insertedCaseFalse, - clientId: 1, - }) - .set("Content-Type", "application/json") - .expect(400); - expect(response.body).toEqual({ - error: "notNull Violation: The case should contain a title field", - }); - }); - test("should return error if not authorized", async () => { - const logedInUser = await request(app).post("/v1/api/login").send({ - email: "jonas@email.com", - password: "s@123", - }); - await request(app) - .post("/v1/api/cases") - .set({ Authorization: "bearer " + logedInUser.body.token }) - .send({ - ...insertedCase, - clientId: 1, - }) - .set("Content-Type", "application/json") - .expect(401); - }); -}); - -describe("GET /v1/api/cases/:id", () => { - test("should return a valid case", async () => { - const logedInUser = await request(app).post("/v1/api/login").send({ - email: "jonas@email.com", - password: "Jonas@123", - }); - const response = await request(app) - .get("/v1/api/cases/1") - .set({ Authorization: "bearer " + logedInUser.body.token }) - .expect(200); - - expect(Array(response.body).length).toBe(1); - }); - test("should return an error when accessing unkonwn Case", async () => { - const logedInUser = await request(app).post("/v1/api/login").send({ - email: "jonas@email.com", - password: "Jonas@123", - }); - const response = await request(app) - .get("/v1/api/cases/10") - .set({ Authorization: "bearer " + logedInUser.body.token }) - .expect(400); - expect(response.body).toEqual({ error: "Case not found" }); - }); -}); - -describe("DELETE /v1/api/cases/:id", () => { - const deletedCase = { - title: "Case example", - description: "Case description", - judge: "Jonas Tesla", - enemy: "Vladimir Lennin", - place: " Dalas", - result: "Fucked up", - status: "opened", - priority: "high", - }; - test("should succesfully delete a case", async () => { - const logedInUser = await request(app).post("/v1/api/login").send({ - email: "jonas@email.com", - password: "Jonas@123", - }); - await request(app) - .post("/v1/api/cases") - .send({ - ...deletedCase, - clientId: 1, - userId: 1, - }) - .set({ Authorization: "bearer " + logedInUser.body.token }) - .set("Content-Type", "application/json") - .expect(201); - - const lastCase = await helper.getLastInsertedCaseId(); - - const response = await request(app) - .delete(`/v1/api/cases/${lastCase.id}`) - .set({ Authorization: "bearer " + logedInUser.body.token }) - .expect(200); - - expect(response.body).toEqual({ message: "Case deleted succesfully" }); - }); - test("should return error when deleting unkonwn Case", async () => { - const logedInUser = await request(app).post("/v1/api/login").send({ - email: "jonas@email.com", - password: "Jonas@123", - }); - const lastCase = await helper.getLastInsertedCaseId(); - const id = lastCase.id + 10; - - const response = await request(app) - .delete(`/v1/api/cases/${id}`) - .set({ Authorization: "bearer " + logedInUser.body.token }) - .expect(400); - - expect(response.body).toEqual({ error: "Cannot find the requested data" }); - }); -}); +// describe("POST /v1/api/cases", () => { +// const insertedCase = { +// title: "Case example", +// description: "Case description", +// judge: "Jonas Tesla", +// enemy: "Vladimir Lennin", +// place: " Dalas", +// result: "Fucked up", +// status: "opened", +// priority: "high", +// }; +// const insertedCaseFalse = { +// description: "Case description", +// judge: "Jonas Tesla", +// enemy: "Vladimir Lennin", +// place: " Dalas", +// result: "Fucked up", +// status: "opened", +// priority: "high", +// }; + +// test("should return the last inserted case", async () => { +// const logedInUser = await request(app).post("/v1/api/login").send({ +// email: "jonas@email.com", +// password: "Jonas@123", +// }); + +// const response = await request(app) +// .post("/v1/api/cases") +// .set({ Authorization: "bearer " + logedInUser.body.token }) +// .send({ +// ...insertedCase, +// clientId: 1, +// userId: 2, +// }) +// .set("Content-Type", "application/json") +// .expect(201); +// expect(response.body).toEqual(insertedCase); +// }); + +// test("should throw an error when inserting false data", async () => { +// const logedInUser = await request(app).post("/v1/api/login").send({ +// email: "jonas@email.com", +// password: "Jonas@123", +// }); + +// const response = await request(app) +// .post("/v1/api/cases") +// .set({ Authorization: "bearer " + logedInUser.body.token }) +// .send({ +// ...insertedCaseFalse, +// clientId: 1, +// }) +// .set("Content-Type", "application/json") +// .expect(400); +// expect(response.body).toEqual({ +// error: "notNull Violation: The case should contain a title field", +// }); +// }); +// test("should return error if not authorized", async () => { +// const logedInUser = await request(app).post("/v1/api/login").send({ +// email: "jonas@email.com", +// password: "s@123", +// }); +// await request(app) +// .post("/v1/api/cases") +// .set({ Authorization: "bearer " + logedInUser.body.token }) +// .send({ +// ...insertedCase, +// clientId: 1, +// }) +// .set("Content-Type", "application/json") +// .expect(401); +// }); +// }); + +// describe("GET /v1/api/cases/:id", () => { +// test("should return a valid case", async () => { +// const logedInUser = await request(app).post("/v1/api/login").send({ +// email: "jonas@email.com", +// password: "Jonas@123", +// }); +// const response = await request(app) +// .get("/v1/api/cases/1") +// .set({ Authorization: "bearer " + logedInUser.body.token }) +// .expect(200); + +// expect(Array(response.body).length).toBe(1); +// }); +// test("should return an error when accessing unkonwn Case", async () => { +// const logedInUser = await request(app).post("/v1/api/login").send({ +// email: "jonas@email.com", +// password: "Jonas@123", +// }); +// const response = await request(app) +// .get("/v1/api/cases/10") +// .set({ Authorization: "bearer " + logedInUser.body.token }) +// .expect(400); +// expect(response.body).toEqual({ error: "Case not found" }); +// }); +// }); + +// describe("DELETE /v1/api/cases/:id", () => { +// const deletedCase = { +// title: "Case example", +// description: "Case description", +// judge: "Jonas Tesla", +// enemy: "Vladimir Lennin", +// place: " Dalas", +// result: "Fucked up", +// status: "opened", +// priority: "high", +// }; +// test("should succesfully delete a case", async () => { +// const logedInUser = await request(app).post("/v1/api/login").send({ +// email: "jonas@email.com", +// password: "Jonas@123", +// }); +// await request(app) +// .post("/v1/api/cases") +// .send({ +// ...deletedCase, +// clientId: 1, +// userId: 1, +// }) +// .set({ Authorization: "bearer " + logedInUser.body.token }) +// .set("Content-Type", "application/json") +// .expect(201); + +// const lastCase = await helper.getLastInsertedCaseId(); + +// const response = await request(app) +// .delete(`/v1/api/cases/${lastCase.id}`) +// .set({ Authorization: "bearer " + logedInUser.body.token }) +// .expect(200); + +// expect(response.body).toEqual({ message: "Case deleted succesfully" }); +// }); +// test("should return error when deleting unkonwn Case", async () => { +// const logedInUser = await request(app).post("/v1/api/login").send({ +// email: "jonas@email.com", +// password: "Jonas@123", +// }); +// const lastCase = await helper.getLastInsertedCaseId(); +// const id = lastCase.id + 10; + +// const response = await request(app) +// .delete(`/v1/api/cases/${id}`) +// .set({ Authorization: "bearer " + logedInUser.body.token }) +// .expect(400); + +// expect(response.body).toEqual({ error: "Cannot find the requested data" }); +// }); +// }); afterAll(async () => { await sequelize.close(); From d615a387061a09b388225a9ba6524c1f377764b0 Mon Sep 17 00:00:00 2001 From: Jonas Date: Sat, 11 Dec 2021 16:33:41 +0100 Subject: [PATCH 4/4] Update pipeline --- .github/workflows/pipeline.yml | 1 + server/tests/cases/case_api.test.js | 309 ++++++++++++++-------------- 2 files changed, 155 insertions(+), 155 deletions(-) diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index a09be7b..08d61fd 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -31,6 +31,7 @@ jobs: DB_USERNAME: ${{ secrets.DB_USERNAME }} DB_PASSWORD: ${{ secrets.DB_PASSWORD }} DB_HOSTNAME: ${{ secrets.DB_HOSTNAME }} + SECRET: 0 # ****End to end Testing***** # # - name: e2e tests # uses: cypress-io/github-action@v2 diff --git a/server/tests/cases/case_api.test.js b/server/tests/cases/case_api.test.js index 4294d15..271b0ea 100644 --- a/server/tests/cases/case_api.test.js +++ b/server/tests/cases/case_api.test.js @@ -33,7 +33,6 @@ describe("GET /v1/api/cases", () => { }) .set("Content-Type", "application/json"); - console.log(logedInUser.body); const response = await request(app) .get("/v1/api/cases") .set({ Authorization: "bearer " + logedInUser.body.token }) @@ -45,160 +44,160 @@ describe("GET /v1/api/cases", () => { }); }); -// describe("POST /v1/api/cases", () => { -// const insertedCase = { -// title: "Case example", -// description: "Case description", -// judge: "Jonas Tesla", -// enemy: "Vladimir Lennin", -// place: " Dalas", -// result: "Fucked up", -// status: "opened", -// priority: "high", -// }; -// const insertedCaseFalse = { -// description: "Case description", -// judge: "Jonas Tesla", -// enemy: "Vladimir Lennin", -// place: " Dalas", -// result: "Fucked up", -// status: "opened", -// priority: "high", -// }; - -// test("should return the last inserted case", async () => { -// const logedInUser = await request(app).post("/v1/api/login").send({ -// email: "jonas@email.com", -// password: "Jonas@123", -// }); - -// const response = await request(app) -// .post("/v1/api/cases") -// .set({ Authorization: "bearer " + logedInUser.body.token }) -// .send({ -// ...insertedCase, -// clientId: 1, -// userId: 2, -// }) -// .set("Content-Type", "application/json") -// .expect(201); -// expect(response.body).toEqual(insertedCase); -// }); - -// test("should throw an error when inserting false data", async () => { -// const logedInUser = await request(app).post("/v1/api/login").send({ -// email: "jonas@email.com", -// password: "Jonas@123", -// }); - -// const response = await request(app) -// .post("/v1/api/cases") -// .set({ Authorization: "bearer " + logedInUser.body.token }) -// .send({ -// ...insertedCaseFalse, -// clientId: 1, -// }) -// .set("Content-Type", "application/json") -// .expect(400); -// expect(response.body).toEqual({ -// error: "notNull Violation: The case should contain a title field", -// }); -// }); -// test("should return error if not authorized", async () => { -// const logedInUser = await request(app).post("/v1/api/login").send({ -// email: "jonas@email.com", -// password: "s@123", -// }); -// await request(app) -// .post("/v1/api/cases") -// .set({ Authorization: "bearer " + logedInUser.body.token }) -// .send({ -// ...insertedCase, -// clientId: 1, -// }) -// .set("Content-Type", "application/json") -// .expect(401); -// }); -// }); - -// describe("GET /v1/api/cases/:id", () => { -// test("should return a valid case", async () => { -// const logedInUser = await request(app).post("/v1/api/login").send({ -// email: "jonas@email.com", -// password: "Jonas@123", -// }); -// const response = await request(app) -// .get("/v1/api/cases/1") -// .set({ Authorization: "bearer " + logedInUser.body.token }) -// .expect(200); - -// expect(Array(response.body).length).toBe(1); -// }); -// test("should return an error when accessing unkonwn Case", async () => { -// const logedInUser = await request(app).post("/v1/api/login").send({ -// email: "jonas@email.com", -// password: "Jonas@123", -// }); -// const response = await request(app) -// .get("/v1/api/cases/10") -// .set({ Authorization: "bearer " + logedInUser.body.token }) -// .expect(400); -// expect(response.body).toEqual({ error: "Case not found" }); -// }); -// }); - -// describe("DELETE /v1/api/cases/:id", () => { -// const deletedCase = { -// title: "Case example", -// description: "Case description", -// judge: "Jonas Tesla", -// enemy: "Vladimir Lennin", -// place: " Dalas", -// result: "Fucked up", -// status: "opened", -// priority: "high", -// }; -// test("should succesfully delete a case", async () => { -// const logedInUser = await request(app).post("/v1/api/login").send({ -// email: "jonas@email.com", -// password: "Jonas@123", -// }); -// await request(app) -// .post("/v1/api/cases") -// .send({ -// ...deletedCase, -// clientId: 1, -// userId: 1, -// }) -// .set({ Authorization: "bearer " + logedInUser.body.token }) -// .set("Content-Type", "application/json") -// .expect(201); - -// const lastCase = await helper.getLastInsertedCaseId(); - -// const response = await request(app) -// .delete(`/v1/api/cases/${lastCase.id}`) -// .set({ Authorization: "bearer " + logedInUser.body.token }) -// .expect(200); - -// expect(response.body).toEqual({ message: "Case deleted succesfully" }); -// }); -// test("should return error when deleting unkonwn Case", async () => { -// const logedInUser = await request(app).post("/v1/api/login").send({ -// email: "jonas@email.com", -// password: "Jonas@123", -// }); -// const lastCase = await helper.getLastInsertedCaseId(); -// const id = lastCase.id + 10; - -// const response = await request(app) -// .delete(`/v1/api/cases/${id}`) -// .set({ Authorization: "bearer " + logedInUser.body.token }) -// .expect(400); - -// expect(response.body).toEqual({ error: "Cannot find the requested data" }); -// }); -// }); +describe("POST /v1/api/cases", () => { + const insertedCase = { + title: "Case example", + description: "Case description", + judge: "Jonas Tesla", + enemy: "Vladimir Lennin", + place: " Dalas", + result: "Fucked up", + status: "opened", + priority: "high", + }; + const insertedCaseFalse = { + description: "Case description", + judge: "Jonas Tesla", + enemy: "Vladimir Lennin", + place: " Dalas", + result: "Fucked up", + status: "opened", + priority: "high", + }; + + test("should return the last inserted case", async () => { + const logedInUser = await request(app).post("/v1/api/login").send({ + email: "jonas@email.com", + password: "Jonas@123", + }); + + const response = await request(app) + .post("/v1/api/cases") + .set({ Authorization: "bearer " + logedInUser.body.token }) + .send({ + ...insertedCase, + clientId: 1, + userId: 2, + }) + .set("Content-Type", "application/json") + .expect(201); + expect(response.body).toEqual(insertedCase); + }); + + test("should throw an error when inserting false data", async () => { + const logedInUser = await request(app).post("/v1/api/login").send({ + email: "jonas@email.com", + password: "Jonas@123", + }); + + const response = await request(app) + .post("/v1/api/cases") + .set({ Authorization: "bearer " + logedInUser.body.token }) + .send({ + ...insertedCaseFalse, + clientId: 1, + }) + .set("Content-Type", "application/json") + .expect(400); + expect(response.body).toEqual({ + error: "notNull Violation: The case should contain a title field", + }); + }); + test("should return error if not authorized", async () => { + const logedInUser = await request(app).post("/v1/api/login").send({ + email: "jonas@email.com", + password: "s@123", + }); + await request(app) + .post("/v1/api/cases") + .set({ Authorization: "bearer " + logedInUser.body.token }) + .send({ + ...insertedCase, + clientId: 1, + }) + .set("Content-Type", "application/json") + .expect(401); + }); +}); + +describe("GET /v1/api/cases/:id", () => { + test("should return a valid case", async () => { + const logedInUser = await request(app).post("/v1/api/login").send({ + email: "jonas@email.com", + password: "Jonas@123", + }); + const response = await request(app) + .get("/v1/api/cases/1") + .set({ Authorization: "bearer " + logedInUser.body.token }) + .expect(200); + + expect(Array(response.body).length).toBe(1); + }); + test("should return an error when accessing unkonwn Case", async () => { + const logedInUser = await request(app).post("/v1/api/login").send({ + email: "jonas@email.com", + password: "Jonas@123", + }); + const response = await request(app) + .get("/v1/api/cases/10") + .set({ Authorization: "bearer " + logedInUser.body.token }) + .expect(400); + expect(response.body).toEqual({ error: "Case not found" }); + }); +}); + +describe("DELETE /v1/api/cases/:id", () => { + const deletedCase = { + title: "Case example", + description: "Case description", + judge: "Jonas Tesla", + enemy: "Vladimir Lennin", + place: " Dalas", + result: "Fucked up", + status: "opened", + priority: "high", + }; + test("should succesfully delete a case", async () => { + const logedInUser = await request(app).post("/v1/api/login").send({ + email: "jonas@email.com", + password: "Jonas@123", + }); + await request(app) + .post("/v1/api/cases") + .send({ + ...deletedCase, + clientId: 1, + userId: 1, + }) + .set({ Authorization: "bearer " + logedInUser.body.token }) + .set("Content-Type", "application/json") + .expect(201); + + const lastCase = await helper.getLastInsertedCaseId(); + + const response = await request(app) + .delete(`/v1/api/cases/${lastCase.id}`) + .set({ Authorization: "bearer " + logedInUser.body.token }) + .expect(200); + + expect(response.body).toEqual({ message: "Case deleted succesfully" }); + }); + test("should return error when deleting unkonwn Case", async () => { + const logedInUser = await request(app).post("/v1/api/login").send({ + email: "jonas@email.com", + password: "Jonas@123", + }); + const lastCase = await helper.getLastInsertedCaseId(); + const id = lastCase.id + 10; + + const response = await request(app) + .delete(`/v1/api/cases/${id}`) + .set({ Authorization: "bearer " + logedInUser.body.token }) + .expect(400); + + expect(response.body).toEqual({ error: "Cannot find the requested data" }); + }); +}); afterAll(async () => { await sequelize.close();