-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Remove user endpoint (#2249)
* feat: Grant platformAdmin user update permissions * feat: Delete user endpoint * test: Add API tests for new endpoint
- Loading branch information
1 parent
4c84b41
commit a059f60
Showing
8 changed files
with
136 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,22 @@ paths: | |
$ref: "#/components/responses/SuccessMessage" | ||
"500": | ||
$ref: "#/components/responses/ErrorMessage" | ||
/user/{email}: | ||
delete: | ||
summary: Delete a user | ||
description: "Requires authentication via a Cloudflare WARP client | ||
\n\n | ||
Please login at [https://api.editor.planx.uk/user](https://api.editor.planx.uk/user)" | ||
tags: ["user"] | ||
parameters: | ||
- in: path | ||
name: email | ||
type: string | ||
format: email | ||
example: [email protected] | ||
description: Email address of the user to be deleted | ||
responses: | ||
"200": | ||
$ref: "#/components/responses/SuccessMessage" | ||
"500": | ||
$ref: "#/components/responses/ErrorMessage" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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[email protected]", | ||
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/[email protected]").expect(401); | ||
}); | ||
|
||
it("requires the 'platformAdmin' role", async () => { | ||
await supertest(app) | ||
.delete("/user/[email protected]") | ||
.set(authHeader({ role: "teamEditor" })) | ||
.expect(403); | ||
}); | ||
|
||
it("handles an invalid email", async () => { | ||
mockGetByEmail.mockResolvedValueOnce(null); | ||
|
||
await supertest(app) | ||
.delete("/user/[email protected]") | ||
.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/[email protected]") | ||
.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/[email protected]") | ||
.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/); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import { Router } from "express"; | ||
import { usePlatformAdminAuth } from "../auth/middleware"; | ||
import { validate } from "../../shared/middleware/validate"; | ||
import { createUserSchema, createUser } from "./controller"; | ||
import { | ||
createUserSchema, | ||
createUser, | ||
deleteUserSchema, | ||
deleteUser, | ||
} from "./controller"; | ||
|
||
const router = Router(); | ||
|
||
router.use(usePlatformAdminAuth); | ||
router.put("/", validate(createUserSchema), createUser); | ||
router.delete("/:email", validate(deleteUserSchema), deleteUser); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters