-
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.
test: Add API tests for new endpoint
- Loading branch information
1 parent
3bb35f0
commit 5117d60
Showing
3 changed files
with
69 additions
and
8 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.