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

fix: hide "Add a new editor" button from non-admin users #3713

Merged
merged 2 commits into from
Sep 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,20 @@ export const MembersTable = ({
</StyledTableRow>
))}
{showAddMemberButton && (
<TableRow>
<TableCell colSpan={3}>
<AddButton
onClick={() => {
setInitialValues(undefined);
setShowAddModal(true);
}}
>
Add a new editor
</AddButton>
</TableCell>
</TableRow>
<Permission.IsPlatformAdmin>
<TableRow>
<TableCell colSpan={3}>
<AddButton
onClick={() => {
setInitialValues(undefined);
setShowAddModal(true);
}}
>
Add a new editor
</AddButton>
</TableCell>
</TableRow>
</Permission.IsPlatformAdmin>
)}
</TableBody>
</Table>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { vi } from "vitest";
import { setupTeamMembersScreen } from "./helpers/setupTeamMembersScreen";
import { userTriesToAddNewEditor } from "./helpers/userTriesToAddNewEditor";
import { mockTeamMembersData } from "./mocks/mockTeamMembersData";
import { alreadyExistingUser } from "./mocks/mockUsers";
import { alreadyExistingUser, mockPlatformAdminUser } from "./mocks/mockUsers";

let initialState: FullStore;
vi.mock(
Expand All @@ -22,6 +22,7 @@ describe("when a user fills in the 'add a new editor' form correctly but there i
beforeEach(async () => {
useStore.setState({
teamMembers: [...mockTeamMembersData, alreadyExistingUser],
user: mockPlatformAdminUser,
});

const { user } = await setupTeamMembersScreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { vi } from "vitest";
import { setupTeamMembersScreen } from "./helpers/setupTeamMembersScreen";
import { userTriesToAddNewEditor } from "./helpers/userTriesToAddNewEditor";
import { mockTeamMembersData } from "./mocks/mockTeamMembersData";
import { alreadyExistingUser } from "./mocks/mockUsers";
import { alreadyExistingUser, mockPlatformAdminUser } from "./mocks/mockUsers";

vi.mock(
"pages/FlowEditor/components/Team/queries/createAndAddUserToTeam.tsx",
Expand All @@ -23,6 +23,7 @@ describe("when a user fills in the 'add a new editor' form correctly but the use
beforeEach(async () => {
useStore.setState({
teamMembers: [...mockTeamMembersData, alreadyExistingUser],
user: mockPlatformAdminUser,
});

const { user } = await setupTeamMembersScreen();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useStore } from "pages/FlowEditor/lib/store";

import { TeamMember } from "../types";
import { setupTeamMembersScreen } from "./helpers/setupTeamMembersScreen";
import { mockPlatformAdminUser } from "./mocks/mockUsers";

const mockTeamMembersDataWithNoTeamEditors: TeamMember[] = [
{
Expand All @@ -16,7 +17,10 @@ const mockTeamMembersDataWithNoTeamEditors: TeamMember[] = [

describe("when a user views the 'Team members' screen but there are no existing team editors listed", () => {
beforeEach(async () => {
useStore.setState({ teamMembers: mockTeamMembersDataWithNoTeamEditors });
useStore.setState({
teamMembers: mockTeamMembersDataWithNoTeamEditors,
user: mockPlatformAdminUser,
});
const { getByText } = await setupTeamMembersScreen();
getByText("No members found");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import { EditorUpsertModal } from "../components/EditorUpsertModal";
import { setupTeamMembersScreen } from "./helpers/setupTeamMembersScreen";
import { userTriesToAddNewEditor } from "./helpers/userTriesToAddNewEditor";
import { mockTeamMembersData } from "./mocks/mockTeamMembersData";
import { emptyTeamMemberObj } from "./mocks/mockUsers";
import {
emptyTeamMemberObj,
mockPlainUser,
mockPlatformAdminUser,
} from "./mocks/mockUsers";

vi.mock(
"pages/FlowEditor/components/Team/queries/createAndAddUserToTeam.tsx",
Expand All @@ -27,7 +31,11 @@ let initialState: FullStore;

describe("when a user presses 'add a new editor'", () => {
beforeEach(async () => {
useStore.setState({ teamMembers: mockTeamMembersData, teamSlug: "planx" });
useStore.setState({
teamMembers: mockTeamMembersData,
user: mockPlatformAdminUser,
teamSlug: "planx",
});
const { user } = await setupTeamMembersScreen();

const teamEditorsTable = screen.getByTestId("team-editors");
Expand All @@ -45,8 +53,13 @@ describe("when a user presses 'add a new editor'", () => {

describe("when a user fills in the 'add a new editor' form correctly", () => {
afterAll(() => useStore.setState(initialState));

beforeEach(async () => {
useStore.setState({ teamMembers: mockTeamMembersData, teamSlug: "planx" });
useStore.setState({
teamMembers: mockTeamMembersData,
user: mockPlatformAdminUser,
teamSlug: "planx",
});
const { user } = await setupTeamMembersScreen();
await userTriesToAddNewEditor(user);
});
Expand Down Expand Up @@ -97,6 +110,7 @@ describe("'add a new editor' button is hidden from Templates team", () => {
beforeEach(async () => {
useStore.setState({
teamMembers: mockTeamMembersData,
user: mockPlatformAdminUser,
teamSlug: "templates",
});
});
Expand All @@ -109,3 +123,21 @@ describe("'add a new editor' button is hidden from Templates team", () => {
expect(addEditorButton).not.toBeInTheDocument();
});
});

describe("when a user is not a platform admin", () => {
beforeEach(async () => {
useStore.setState({
teamMembers: mockTeamMembersData,
user: mockPlainUser,
teamSlug: "templates",
});
});

it("hides the button from non-admin users", async () => {
const { user: _user } = await setupTeamMembersScreen();
const teamEditorsTable = screen.getByTestId("team-editors");
const addEditorButton =
within(teamEditorsTable).queryByText("Add a new editor");
expect(addEditorButton).not.toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe("when a user is not a platform admin", () => {

await setupTeamMembersScreen();
});

it("does not show an edit button", async () => {
const teamEditorsTable = screen.getByTestId("team-editors");
const addEditorButton =
Expand Down
Loading