Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tidy: remove redundant add-team-member route #3611

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 0 additions & 99 deletions api.planx.uk/modules/team/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,105 +14,6 @@ vi.mock("./service", () => ({

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

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

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

it("validates that userId is required", async () => {
await supertest(app)
.put("/team/council/add-member")
.set(auth)
.send({
role: "teamViewer",
})
.expect(400)
.then((res) => {
expect(res.body).toHaveProperty("issues");
expect(res.body).toHaveProperty("name", "ZodError");
});
});

it("validates that role is required", async () => {
await supertest(app)
.put("/team/council/add-member")
.set(auth)
.send({
userEmail: "[email protected]",
})
.expect(400)
.then((res) => {
expect(res.body).toHaveProperty("issues");
expect(res.body).toHaveProperty("name", "ZodError");
});
});

it("validates that role must be an accepted value", async () => {
await supertest(app)
.put("/team/council/add-member")
.set(auth)
.send({
userEmail: "[email protected]",
role: "pirate",
})
.expect(400)
.then((res) => {
expect(res.body).toHaveProperty("issues");
expect(res.body).toHaveProperty("name", "ZodError");
});
});

it("handles an error thrown in the service", async () => {
mockAddMember.mockRejectedValueOnce("Something went wrong in the service");

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

it("can successfully add a team member", async () => {
await supertest(app)
.put("/team/council/add-member")
.set(auth)
.send({
userEmail: "[email protected]",
role: "teamEditor",
})
.expect(200)
.then((res) => {
expect(mockAddMember).toHaveBeenCalled();
expect(res.body).toHaveProperty("message");
expect(res.body.message).toMatch(/Successfully added user to team/);
});
});
});
describe("Removing a user from a team", () => {
it("requires authentication", async () => {
await supertest(app)
Expand Down
21 changes: 2 additions & 19 deletions api.planx.uk/modules/team/controller.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Service from "./service.js";
import { z } from "zod";
import { ValidatedRequestHandler } from "../../shared/middleware/validate.js";
import { ServerError } from "../../errors/index.js";
import { ValidatedRequestHandler } from "../../shared/middleware/validate.js";
import * as Service from "./service.js";

interface TeamMemberResponse {
message: string;
Expand Down Expand Up @@ -36,23 +36,6 @@ export type RemoveMember = ValidatedRequestHandler<
TeamMemberResponse
>;

export const addMember: UpsertMember = async (_req, res, next) => {
const { teamSlug } = res.locals.parsedReq.params;
const { userEmail, role } = res.locals.parsedReq.body;

try {
await Service.addMember({ userEmail, teamSlug, role });
return res.send({ message: "Successfully added user to team" });
} catch (error) {
return next(
new ServerError({
message: `Failed to add member ${userEmail} to team ${teamSlug}. Error: ${error}`,
cause: error,
}),
);
}
};

export const changeMemberRole: UpsertMember = async (_req, res, next) => {
const { teamSlug } = res.locals.parsedReq.params;
const { userEmail, role } = res.locals.parsedReq.body;
Expand Down
20 changes: 0 additions & 20 deletions api.planx.uk/modules/team/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,6 @@ components:
type: string
enum: ["teamViewer", "teamEditor"]
paths:
/team/{teamSlug}/add-member:
put:
summary: Add user to team, and assign them a role
description: "Requires authentication via a Cloudflare WARP client
\n\n
Please login at [https://api.editor.planx.uk/team](https://api.editor.planx.uk/team)"
tags: ["team"]
parameters:
- $ref: "#/components/parameters/teamSlug"
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/UpsertMember"
responses:
"200":
$ref: "#/components/responses/SuccessMessage"
"500":
$ref: "#/components/responses/ErrorMessage"
/team/{teamSlug}/change-member-role:
patch:
summary: Change role of an existing team member
Expand Down
5 changes: 0 additions & 5 deletions api.planx.uk/modules/team/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import { validate } from "../../shared/middleware/validate.js";
const router = Router();

router.use("/team/", AuthMiddleware.usePlatformAdminAuth);
router.put(
"/team/:teamSlug/add-member",
validate(Controller.upsertMemberSchema),
Controller.addMember,
);
router.patch(
"/team/:teamSlug/change-member-role",
validate(Controller.upsertMemberSchema),
Expand Down
Loading