Skip to content

fix: disable AccountForm when user is not allowed edit users #3649

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

Merged
merged 7 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
@@ -0,0 +1,61 @@
import { screen } from "@testing-library/react"
import { MockUser2 } from "../../testHelpers/entities"
import { render } from "../../testHelpers/renderHelpers"
import { AccountForm, AccountFormValues } from "./SettingsAccountForm"

// NOTE: it does not matter what the role props of MockUser are set to,
// only that editable is set to true or false. This is passed from
// the call to /authorization done by authXService
describe("AccountForm", () => {
describe("when editable is set to true", () => {
it("allows updating username", async () => {
// Given
const mockInitialValues: AccountFormValues = {
username: MockUser2.username,
}

// When
render(
<AccountForm
editable
email={MockUser2.email}
initialValues={mockInitialValues}
isLoading={false}
onSubmit={() => {
return
}}
/>,
)

// Then
const el = await screen.findByLabelText("Username")
expect(el).toBeEnabled()
})
})

describe("when editable is set to false", () => {
it("does not allow updating username", async () => {
// Given
const mockInitialValues: AccountFormValues = {
username: MockUser2.username,
}

// When
render(
<AccountForm
editable={false}
email={MockUser2.email}
initialValues={mockInitialValues}
isLoading={false}
onSubmit={() => {
return
}}
/>,
)

// Then
const el = await screen.findByLabelText("Username")
expect(el).toBeDisabled()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getFormHelpersWithError, nameValidator, onChangeTrimmed } from "../../u
import { LoadingButton } from "../LoadingButton/LoadingButton"
import { Stack } from "../Stack/Stack"

interface AccountFormValues {
export interface AccountFormValues {
username: string
}

Expand All @@ -22,6 +22,7 @@ const validationSchema = Yup.object({
})

export interface AccountFormProps {
editable: boolean
email: string
isLoading: boolean
initialValues: AccountFormValues
Expand All @@ -32,6 +33,7 @@ export interface AccountFormProps {
}

export const AccountForm: FC<React.PropsWithChildren<AccountFormProps>> = ({
editable,
email,
isLoading,
onSubmit,
Expand Down Expand Up @@ -63,6 +65,7 @@ export const AccountForm: FC<React.PropsWithChildren<AccountFormProps>> = ({
{...getFieldHelpers("username")}
onChange={onChangeTrimmed(form)}
autoComplete="username"
disabled={!editable}
fullWidth
label={Language.usernameLabel}
variant="outlined"
Expand Down
8 changes: 6 additions & 2 deletions site/src/pages/UserSettingsPage/AccountPage/AccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const Language = {
export const AccountPage: React.FC = () => {
const xServices = useContext(XServiceContext)
const [authState, authSend] = useActor(xServices.authXService)
const { me, updateProfileError } = authState.context
const { me, permissions, updateProfileError } = authState.context
const canEditUsers = permissions && permissions.updateUsers

if (!me) {
throw new Error("No current user found")
Expand All @@ -20,10 +21,13 @@ export const AccountPage: React.FC = () => {
return (
<Section title={Language.title}>
<AccountForm
editable={!!canEditUsers}
email={me.email}
updateProfileError={updateProfileError}
isLoading={authState.matches("signedIn.profile.updatingProfile")}
initialValues={{ username: me.username }}
initialValues={{
username: me.username,
}}
onSubmit={(data) => {
authSend({
type: "UPDATE_PROFILE",
Expand Down
10 changes: 10 additions & 0 deletions site/src/testHelpers/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export const MockUser: TypesGen.User = {
roles: [MockOwnerRole],
}

export const MockUserAdmin: TypesGen.User = {
id: "test-user",
username: "TestUser",
email: "test@coder.com",
created_at: "",
status: "active",
organization_ids: ["fc0774ce-cc9e-48d4-80ae-88f7a4d4a8b0"],
roles: [MockUserAdminRole],
}

export const MockUser2: TypesGen.User = {
id: "test-user-2",
username: "TestUser2",
Expand Down