-
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.
chore: Remove deprecated
/user
endpoints (#3737)
- Loading branch information
1 parent
1fec387
commit 8669951
Showing
4 changed files
with
2 additions
and
254 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 |
---|---|---|
|
@@ -5,61 +5,7 @@ info: | |
tags: | ||
name: user | ||
description: User management | ||
components: | ||
schemas: | ||
CreateUserSchema: | ||
type: object | ||
properties: | ||
firstName: | ||
type: string | ||
example: Bilbo | ||
lastName: | ||
type: string | ||
example: Baggins | ||
email: | ||
type: email | ||
example: [email protected] | ||
isPlatformAdmin: | ||
type: bool | ||
example: false | ||
paths: | ||
/user: | ||
put: | ||
summary: Create a new 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"] | ||
requestBody: | ||
required: true | ||
content: | ||
application/json: | ||
schema: | ||
$ref: "#/components/schemas/CreateUserSchema" | ||
responses: | ||
"200": | ||
$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" | ||
/user/me: | ||
get: | ||
summary: Get information about currently logged in 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 |
---|---|---|
|
@@ -5,16 +5,6 @@ import { userContext } from "../auth/middleware.js"; | |
|
||
const getStoreMock = vi.spyOn(userContext, "getStore"); | ||
|
||
const mockUser = { | ||
firstName: "Bilbo", | ||
lastName: "Baggins", | ||
email: "[email protected]", | ||
isPlatformAdmin: false, | ||
}; | ||
|
||
const mockCreateUser = vi.fn(); | ||
const mockDeleteUser = vi.fn(); | ||
const mockGetByEmail = vi.fn().mockResolvedValue(mockUser); | ||
const mockGetById = vi.fn().mockResolvedValue({ | ||
id: 123, | ||
firstName: "Albert", | ||
|
@@ -37,118 +27,12 @@ vi.mock("@opensystemslab/planx-core", () => { | |
return { | ||
CoreDomainClient: vi.fn().mockImplementation(() => ({ | ||
user: { | ||
create: () => mockCreateUser(), | ||
delete: () => mockDeleteUser(), | ||
getByEmail: () => mockGetByEmail(), | ||
getById: () => mockGetById(), | ||
}, | ||
})), | ||
}; | ||
}); | ||
|
||
const auth = authHeader({ role: "platformAdmin" }); | ||
|
||
describe("Create user endpoint", () => { | ||
it("requires authentication", async () => { | ||
await supertest(app).put("/user").send(mockUser).expect(401); | ||
}); | ||
|
||
it("requires the 'platformAdmin' role", async () => { | ||
await supertest(app) | ||
.put("/user") | ||
.set(authHeader({ role: "teamEditor" })) | ||
.send(mockUser) | ||
.expect(403); | ||
}); | ||
|
||
it("handles Hasura / DB errors", async () => { | ||
mockCreateUser.mockRejectedValue(new Error("Something went wrong")); | ||
|
||
await supertest(app) | ||
.put("/user") | ||
.set(auth) | ||
.send(mockUser) | ||
.expect(500) | ||
.then((res) => { | ||
expect(mockCreateUser).toHaveBeenCalled(); | ||
expect(res.body).toHaveProperty("error"); | ||
expect(res.body.error).toMatch(/Failed to create user/); | ||
}); | ||
}); | ||
|
||
it("can successfully create a user", async () => { | ||
mockCreateUser.mockResolvedValue(123); | ||
|
||
await supertest(app) | ||
.put("/user") | ||
.set(auth) | ||
.send(mockUser) | ||
.expect(200) | ||
.then((res) => { | ||
expect(mockCreateUser).toHaveBeenCalled(); | ||
expect(res.body).toHaveProperty("message"); | ||
expect(res.body.message).toMatch(/Successfully created user/); | ||
}); | ||
}); | ||
}); | ||
|
||
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/); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("/me endpoint", () => { | ||
beforeEach(() => { | ||
getStoreMock.mockReturnValue({ | ||
|
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,28 +1,9 @@ | ||
import { Router } from "express"; | ||
import { useLoginAuth, usePlatformAdminAuth } from "../auth/middleware.js"; | ||
import { validate } from "../../shared/middleware/validate.js"; | ||
import { | ||
createUserSchema, | ||
createUser, | ||
deleteUserSchema, | ||
deleteUser, | ||
getLoggedInUserDetails, | ||
} from "./controller.js"; | ||
import { useLoginAuth } from "../auth/middleware.js"; | ||
import { getLoggedInUserDetails } from "./controller.js"; | ||
|
||
const router = Router(); | ||
|
||
router.put( | ||
"/user", | ||
usePlatformAdminAuth, | ||
validate(createUserSchema), | ||
createUser, | ||
); | ||
router.delete( | ||
"/user/:email", | ||
usePlatformAdminAuth, | ||
validate(deleteUserSchema), | ||
deleteUser, | ||
); | ||
router.get("/user/me", useLoginAuth, getLoggedInUserDetails); | ||
|
||
export default router; |