Skip to content

Commit

Permalink
feat: Update team member routes to take slug and email
Browse files Browse the repository at this point in the history
 - Standardise error handling
 - Update tests
  • Loading branch information
DafyddLlyr committed Sep 26, 2023
1 parent bc23067 commit a07c55c
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 127 deletions.
2 changes: 1 addition & 1 deletion api.planx.uk/docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const responses = {
schema: {
type: "object",
properties: {
message: {
error: {
type: "string",
example: "Error!",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,39 @@ const mockAddMember = jest.fn();
const mockRemoveMember = jest.fn();
const mockChangeMemberRole = jest.fn();

jest.mock("@opensystemslab/planx-core", () => {
return {
CoreDomainClient: jest.fn().mockImplementation(() => ({
team: {
addMember: () => mockAddMember(),
removeMember: () => mockRemoveMember(),
changeMemberRole: () => mockChangeMemberRole(),
},
})),
};
});
jest.mock("./service", () => ({
addMember: () => mockAddMember(),
changeMemberRole: () => mockChangeMemberRole(),
removeMember: () => mockRemoveMember(),
}));

const auth = authHeader({ role: "platformAdmin" });

describe("Adding a user to a team", () => {
it("requires authentication", async () => {
await supertest(app)
.put("/team/123/add-member")
.put("/team/council/add-member")
.send({
userId: 123,
userEmail: "[email protected]",
role: "teamViewer",
})
.expect(401);
});

it("requires the 'platformAdmin' role", async () => {
await supertest(app)
.put("/team/123/add-member")
.put("/team/council/add-member")
.set(authHeader({ role: "teamEditor" }))
.send({
userId: 123,
userEmail: "[email protected]",
role: "teamViewer",
})
.expect(403);
});

it("validates that userId is required", async () => {
await supertest(app)
.put("/team/123/add-member")
.put("/team/council/add-member")
.set(auth)
.send({
role: "teamViewer",
Expand All @@ -58,10 +52,10 @@ describe("Adding a user to a team", () => {

it("validates that role is required", async () => {
await supertest(app)
.put("/team/123/add-member")
.put("/team/council/add-member")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
})
.expect(400)
.then((res) => {
Expand All @@ -72,10 +66,10 @@ describe("Adding a user to a team", () => {

it("validates that role must be an accepted value", async () => {
await supertest(app)
.put("/team/123/add-member")
.put("/team/council/add-member")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
role: "pirate",
})
.expect(400)
Expand All @@ -85,32 +79,30 @@ describe("Adding a user to a team", () => {
});
});

it("handles Hasura / DB errors", async () => {
mockAddMember.mockResolvedValue(false);
it("handles an error thrown in the service", async () => {
mockAddMember.mockRejectedValueOnce("Something went wrong in the service");

await supertest(app)
.put("/team/123/add-member")
.put("/team/council/add-member")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
role: "teamEditor",
})
.expect(500)
.then((res) => {
expect(mockAddMember).toHaveBeenCalled();
expect(res.body).toHaveProperty("message");
expect(res.body.message).toMatch(/Failed to add member to team/);
expect(res.body).toHaveProperty("error");
expect(res.body.error).toMatch(/Failed to add member to team/);
});
});

it("can successfully add a team member", async () => {
mockAddMember.mockResolvedValue(true);

await supertest(app)
.put("/team/123/add-member")
.put("/team/council/add-member")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
role: "teamEditor",
})
.expect(200)
Expand All @@ -121,20 +113,19 @@ describe("Adding a user to a team", () => {
});
});
});

describe("Removing a user from a team", () => {
it("requires authentication", async () => {
await supertest(app)
.delete("/team/123/remove-member")
.delete("/team/council/remove-member")
.send({
userId: 123,
userEmail: "[email protected]",
})
.expect(401);
});

it("validates that userId is required", async () => {
await supertest(app)
.delete("/team/123/remove-member")
.delete("/team/council/remove-member")
.set(auth)
.send({})
.expect(400)
Expand All @@ -144,31 +135,31 @@ describe("Removing a user from a team", () => {
});
});

it("handles Hasura / DB errors", async () => {
mockRemoveMember.mockResolvedValue(false);
it("handles an error thrown in the service", async () => {
mockRemoveMember.mockRejectedValueOnce(
"Something went wrong in the service",
);

await supertest(app)
.delete("/team/123/remove-member")
.delete("/team/council/remove-member")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
})
.expect(500)
.then((res) => {
expect(mockRemoveMember).toHaveBeenCalled();
expect(res.body).toHaveProperty("message");
expect(res.body.message).toMatch(/Failed to remove member from team/);
expect(res.body).toHaveProperty("error");
expect(res.body.error).toMatch(/Failed to remove member from team/);
});
});

it("can successfully remove a team member", async () => {
mockRemoveMember.mockResolvedValue(true);

await supertest(app)
.delete("/team/123/remove-member")
.delete("/team/council/remove-member")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
role: "teamEditor",
})
.expect(200)
Expand All @@ -183,17 +174,17 @@ describe("Removing a user from a team", () => {
describe("Changing a user's role", () => {
it("requires authentication", async () => {
await supertest(app)
.patch("/team/123/change-member-role")
.patch("/team/council/change-member-role")
.send({
userId: 123,
userEmail: "[email protected]",
role: "teamEditor",
})
.expect(401);
});

it("validates that userId is required", async () => {
await supertest(app)
.patch("/team/123/change-member-role")
.patch("/team/council/change-member-role")
.set(auth)
.send({
role: "teamEditor",
Expand All @@ -207,10 +198,10 @@ describe("Changing a user's role", () => {

it("validates that role is required", async () => {
await supertest(app)
.patch("/team/123/change-member-role")
.patch("/team/council/change-member-role")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
})
.expect(400)
.then((res) => {
Expand All @@ -221,10 +212,10 @@ describe("Changing a user's role", () => {

it("validates that role is an accepted value", async () => {
await supertest(app)
.patch("/team/123/change-member-role")
.patch("/team/council/change-member-role")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
role: "professor",
})
.expect(400)
Expand All @@ -234,32 +225,32 @@ describe("Changing a user's role", () => {
});
});

it("handles Hasura / DB errors", async () => {
mockChangeMemberRole.mockResolvedValue(false);
it("handles an error thrown in the service", async () => {
mockChangeMemberRole.mockRejectedValueOnce(
"Something went wrong in the service",
);

await supertest(app)
.patch("/team/123/change-member-role")
.patch("/team/council/change-member-role")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
role: "teamEditor",
})
.expect(500)
.then((res) => {
expect(mockChangeMemberRole).toHaveBeenCalled();
expect(res.body).toHaveProperty("message");
expect(res.body.message).toMatch(/Failed to change role/);
expect(res.body).toHaveProperty("error");
expect(res.body.error).toMatch(/Failed to change role/);
});
});

it("can successfully change a user's role", async () => {
mockChangeMemberRole.mockResolvedValue(true);

await supertest(app)
.patch("/team/123/change-member-role")
.patch("/team/council/change-member-role")
.set(auth)
.send({
userId: 123,
userEmail: "[email protected]",
role: "teamEditor",
})
.expect(200)
Expand Down
Loading

0 comments on commit a07c55c

Please sign in to comment.