From 5117d600fcff074af6895ced3e6cc46f49519f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 29 Sep 2023 17:09:54 +0100 Subject: [PATCH] test: Add API tests for new endpoint --- api.planx.uk/modules/user/index.test.ts | 67 +++++++++++++++++++++++-- api.planx.uk/package.json | 2 +- api.planx.uk/pnpm-lock.yaml | 8 +-- 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/api.planx.uk/modules/user/index.test.ts b/api.planx.uk/modules/user/index.test.ts index 4290fb65ec..5ee527b459 100644 --- a/api.planx.uk/modules/user/index.test.ts +++ b/api.planx.uk/modules/user/index.test.ts @@ -2,20 +2,24 @@ import supertest from "supertest"; import app from "../../server"; import { authHeader } from "../../tests/mockJWT"; -const mockCreateUser = jest.fn(); - const mockUser = { firstName: "Bilbo", lastName: "Baggins", - email: "bilbo@bagend.sh", + email: "bilbo.baggins@example.com", isPlatformAdmin: false, }; +const mockCreateUser = jest.fn(); +const mockDeleteUser = jest.fn(); +const mockGetByEmail = jest.fn().mockResolvedValue(mockUser); + jest.mock("@opensystemslab/planx-core", () => { return { CoreDomainClient: jest.fn().mockImplementation(() => ({ user: { create: () => mockCreateUser(), + delete: () => mockDeleteUser(), + getByEmail: () => mockGetByEmail(), }, })), }; @@ -66,3 +70,60 @@ describe("Create user endpoint", () => { }); }); }); + +describe("Delete user endpoint", () => { + it("requires authentication", async () => { + await supertest(app).delete("/user/bilbo.baggins@example.com").expect(401); + }); + + it("requires the 'platformAdmin' role", async () => { + await supertest(app) + .delete("/user/bilbo.baggins@example.com") + .set(authHeader({ role: "teamEditor" })) + .expect(403); + }); + + it("handles an invalid email", async () => { + mockGetByEmail.mockResolvedValueOnce(null); + + await supertest(app) + .delete("/user/bilbo.baggins@example.com") + .set(auth) + .expect(500) + .then((res) => { + expect(mockGetByEmail).toHaveBeenCalled(); + expect(res.body).toHaveProperty("error"); + expect(res.body.error).toMatch(/Failed to delete user/); + }); + }); + + it("handles a failure to delete the user", async () => { + mockDeleteUser.mockResolvedValueOnce(false); + + await supertest(app) + .delete("/user/bilbo.baggins@example.com") + .set(auth) + .expect(500) + .then((res) => { + expect(mockGetByEmail).toHaveBeenCalled(); + expect(mockDeleteUser).toHaveBeenCalled(); + expect(res.body).toHaveProperty("error"); + expect(res.body.error).toMatch(/Failed to delete user/); + }); + }); + + it("can successfully delete a user", async () => { + mockDeleteUser.mockResolvedValue(true); + + await supertest(app) + .delete("/user/bilbo.baggins@example.com") + .set(auth) + .expect(200) + .then((res) => { + expect(mockGetByEmail).toHaveBeenCalled(); + expect(mockDeleteUser).toHaveBeenCalled(); + expect(res.body).toHaveProperty("message"); + expect(res.body.message).toMatch(/Successfully deleted user/); + }); + }); +}); diff --git a/api.planx.uk/package.json b/api.planx.uk/package.json index 67bb14eb41..8b374e44d5 100644 --- a/api.planx.uk/package.json +++ b/api.planx.uk/package.json @@ -4,7 +4,7 @@ "private": true, "dependencies": { "@airbrake/node": "^2.1.8", - "@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#44e9ebe", + "@opensystemslab/planx-core": "git+https://github.com/theopensystemslab/planx-core#3d395fa", "@types/isomorphic-fetch": "^0.0.36", "adm-zip": "^0.5.10", "aws-sdk": "^2.1467.0", diff --git a/api.planx.uk/pnpm-lock.yaml b/api.planx.uk/pnpm-lock.yaml index 1043cbbbaf..aad282e96d 100644 --- a/api.planx.uk/pnpm-lock.yaml +++ b/api.planx.uk/pnpm-lock.yaml @@ -9,8 +9,8 @@ dependencies: specifier: ^2.1.8 version: 2.1.8 '@opensystemslab/planx-core': - specifier: git+https://github.com/theopensystemslab/planx-core#44e9ebe - version: git/github.com+theopensystemslab/planx-core/44e9ebe + specifier: git+https://github.com/theopensystemslab/planx-core#3d395fa + version: github.com/theopensystemslab/planx-core/3d395fa '@types/isomorphic-fetch': specifier: ^0.0.36 version: 0.0.36 @@ -8073,8 +8073,8 @@ packages: resolution: {integrity: sha512-wvWkphh5WQsJbVk1tbx1l1Ly4yg+XecD+Mq280uBGt9wa5BKSWf4Mhp6GmrkPixhMxmabYY7RbzlwVP32pbGCg==} dev: false - git/github.com+theopensystemslab/planx-core/44e9ebe: - resolution: {commit: 44e9ebe, repo: git@github.com:theopensystemslab/planx-core.git, type: git} + github.com/theopensystemslab/planx-core/3d395fa: + resolution: {tarball: https://codeload.github.com/theopensystemslab/planx-core/tar.gz/3d395fa} name: '@opensystemslab/planx-core' version: 1.0.0 prepare: true