Skip to content

Commit

Permalink
test: Add API tests for new endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 2, 2023
1 parent 3bb35f0 commit 5117d60
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
67 changes: 64 additions & 3 deletions api.planx.uk/modules/user/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
})),
};
Expand Down Expand Up @@ -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/);
});
});
});
2 changes: 1 addition & 1 deletion api.planx.uk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions api.planx.uk/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5117d60

Please sign in to comment.