diff --git a/editor.planx.uk/src/components/Header.tsx b/editor.planx.uk/src/components/Header.tsx index c2a668a2e5..d8ea18c2c9 100644 --- a/editor.planx.uk/src/components/Header.tsx +++ b/editor.planx.uk/src/components/Header.tsx @@ -482,9 +482,7 @@ const EditorToolbar: React.FC<{ {user.isPlatformAdmin ? `All teams` - : user.teams - .map((team) => team.team.name) - .join(", ")} + : user.teams.map((team) => team.team.name).join(", ")} )} diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/user.test.ts b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/user.test.ts new file mode 100644 index 0000000000..5c0f9e24c7 --- /dev/null +++ b/editor.planx.uk/src/pages/FlowEditor/lib/__tests__/user.test.ts @@ -0,0 +1,101 @@ +import { User } from "@opensystemslab/planx-core/types"; + +import { FullStore, vanillaStore } from "../store"; + +const { getState, setState } = vanillaStore; +const { canUserEditTeam } = getState(); + +const redUser: User = { + id: 1, + isPlatformAdmin: false, + firstName: "Red", + lastName: "Reddison", + email: "red@red-team.com", + teams: [ + { + role: "teamEditor", + team: { + name: "Red Team", + slug: "red-team", + id: 1, + }, + }, + { + role: "teamViewer", + team: { + name: "Blue Team", + slug: "blue-team", + id: 1, + }, + }, + ], +}; + +const blueUser: User = { + id: 2, + isPlatformAdmin: false, + firstName: "Blue", + lastName: "Bluey", + email: "blue@blue-team.com", + teams: [ + { + role: "teamEditor", + team: { + name: "Blue Team", + slug: "blue-team", + id: 1, + }, + }, + ], +}; + +const readOnlyUser: User = { + id: 3, + isPlatformAdmin: false, + firstName: "Read", + lastName: "Only", + email: "readonly@no-team.com", + teams: [], +}; + +const adminUser: User = { + id: 4, + isPlatformAdmin: true, + firstName: "Platform", + lastName: "Admin", + email: "admin@opensystemslab.io", + teams: [], +}; + +let initialState: FullStore; + +beforeEach(() => { + initialState = getState(); +}); + +afterEach(() => setState(initialState)); + +describe("canUserEditTeam helper function", () => { + it("returns true when a user has teamEditor permission for a team", () => { + setState({ user: redUser }); + expect(canUserEditTeam("red-team")).toBe(true); + expect(canUserEditTeam("blue-team")).toBe(false); + }); + + it("returns false when a user does not have permission for a team", () => { + setState({ user: blueUser }); + expect(canUserEditTeam("red-team")).toBe(false); + }); + + it("returns false when a user does not have any permissions", () => { + setState({ user: readOnlyUser }); + expect(canUserEditTeam("red-team")).toBe(false); + expect(canUserEditTeam("blue-team")).toBe(false); + }); + + it("returns true when a user is has the platformAdmin role", () => { + setState({ user: adminUser }); + expect(canUserEditTeam("red-team")).toBe(true); + expect(canUserEditTeam("blue-team")).toBe(true); + }); +}); diff --git a/editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts b/editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts index 8ca692315c..861ad8c764 100644 --- a/editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts +++ b/editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts @@ -22,15 +22,13 @@ export const userStore: StateCreator = ( getUser: () => get().user, canUserEditTeam(teamSlug) { - const user = this.getUser(); + const user = get().getUser(); if (!user) return false; - - const hasTeamEditorRole = (team: UserTeams) => team.role === "teamEditor" && team.team.slug === teamSlug; - return ( - user.isPlatformAdmin || - user.teams.some(hasTeamEditorRole) - ); + const hasTeamEditorRole = (team: UserTeams) => + team.role === "teamEditor" && team.team.slug === teamSlug; + + return user.isPlatformAdmin || user.teams.some(hasTeamEditorRole); }, async initUserStore(jwt: string) {