Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
EVG-21009: Make distro settings page readonly if user lacks edit perm…
Browse files Browse the repository at this point in the history
…issions (#2086)
  • Loading branch information
sophstad authored Oct 5, 2023
1 parent 9b09e06 commit f05d428
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
83 changes: 83 additions & 0 deletions cypress/integration/distroSettings/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
describe("with various permission levels", () => {
it("hides the new distro button when a user cannot create distros", () => {
const userData = {
data: {
user: {
userId: "admin",
permissions: {
canCreateDistro: false,
distroPermissions: {
admin: true,
edit: true,
},
},
},
},
};
cy.overwriteGQL("UserDistroSettingsPermissions", userData);
cy.visit("/distro/rhel71-power8-large/settings/general");
cy.dataCy("new-distro-button").should("not.exist");
cy.dataCy("delete-distro-button").should(
"not.have.attr",
"aria-disabled",
"true"
);
cy.get("textarea").should("not.be.disabled");
});

it("disables the delete button when user lacks admin permissions", () => {
const userData = {
data: {
user: {
userId: "admin",
permissions: {
canCreateDistro: false,
distroPermissions: {
admin: false,
edit: true,
},
},
},
},
};
cy.overwriteGQL("UserDistroSettingsPermissions", userData);
cy.visit("/distro/rhel71-power8-large/settings/general");
cy.dataCy("new-distro-button").should("not.exist");
cy.dataCy("delete-distro-button").should(
"have.attr",
"aria-disabled",
"true"
);
cy.get("textarea").should("not.be.disabled");
});

it("disables fields when user lacks edit permissions", () => {
const userData = {
data: {
user: {
userId: "admin",
permissions: {
canCreateDistro: false,
distroPermissions: {
admin: false,
edit: false,
},
},
},
},
};
cy.overwriteGQL("UserDistroSettingsPermissions", userData);
cy.visit("/distro/rhel71-power8-large/settings/general");
cy.dataCy("new-distro-button").should("not.exist");
cy.dataCy("delete-distro-button").should(
"have.attr",
"aria-disabled",
"true"
);
cy.dataCy("distro-settings-page").within(() => {
cy.get("input").should("be.disabled");
cy.get("textarea").should("be.disabled");
cy.get("button").should("have.attr", "aria-disabled", "true");
});
});
});
6 changes: 5 additions & 1 deletion src/gql/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8471,7 +8471,11 @@ export type UserDistroSettingsPermissionsQuery = {
permissions: {
__typename?: "Permissions";
canCreateDistro: boolean;
distroPermissions: { __typename?: "DistroPermissions"; admin: boolean };
distroPermissions: {
__typename?: "DistroPermissions";
admin: boolean;
edit: boolean;
};
};
};
};
Expand Down
1 change: 1 addition & 0 deletions src/gql/queries/user-distro-settings-permissions.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ query UserDistroSettingsPermissions($distroId: String!) {
canCreateDistro
distroPermissions(options: { distroId: $distroId }) {
admin
edit
}
}
userId
Expand Down
2 changes: 2 additions & 0 deletions src/pages/distroSettings/DeleteDistro/DeleteDistro.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ const isAdminMock: ApolloMock<
distroPermissions: {
__typename: "DistroPermissions",
admin: true,
edit: true,
},
},
},
Expand Down Expand Up @@ -159,6 +160,7 @@ const notAdminMock: ApolloMock<
distroPermissions: {
__typename: "DistroPermissions",
admin: false,
edit: false,
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions src/pages/distroSettings/NewDistro/NewDistroButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe("new distro button", () => {
distroPermissions: {
__typename: "DistroPermissions",
admin: false,
edit: false,
},
},
},
Expand Down Expand Up @@ -131,6 +132,7 @@ const hasPermissionsMock: ApolloMock<
distroPermissions: {
__typename: "DistroPermissions",
admin: true,
edit: true,
},
},
},
Expand Down
18 changes: 17 additions & 1 deletion src/pages/distroSettings/tabs/BaseTab.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { useQuery } from "@apollo/client";
import { useParams } from "react-router-dom";
import { Form } from "components/Settings/Form";
import { GetFormSchema, ValidateProps } from "components/SpruceForm";
import {
UserDistroSettingsPermissionsQuery,
UserDistroSettingsPermissionsQueryVariables,
} from "gql/generated/types";
import { USER_DISTRO_SETTINGS_PERMISSIONS } from "gql/queries";
import { usePopulateForm, useDistroSettingsContext } from "../Context";
import { FormStateMap, WritableDistroSettingsType } from "./types";

Expand All @@ -15,12 +21,22 @@ export const BaseTab = <T extends WritableDistroSettingsType>({
initialFormState,
...rest
}: BaseTabProps<T>) => {
const { tab } = useParams<{ tab: T }>();
const { distroId, tab } = useParams<{ distroId: string; tab: T }>();
const state = useDistroSettingsContext();
usePopulateForm(initialFormState, tab);

const { data } = useQuery<
UserDistroSettingsPermissionsQuery,
UserDistroSettingsPermissionsQueryVariables
>(USER_DISTRO_SETTINGS_PERMISSIONS, {
variables: { distroId },
});
const canEditDistro =
data?.user?.permissions?.distroPermissions?.edit ?? false;

return (
<Form<WritableDistroSettingsType, FormStateMap>
disabled={!canEditDistro}
{...rest}
state={state}
tab={tab}
Expand Down

0 comments on commit f05d428

Please sign in to comment.