Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
johnstcn merged 7 commits intomainfromcj/gh-3286/update-usernames
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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()
const btn = await screen.findByRole("button", { name: /Update settings/i })
expect(btn).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()
const btn = await screen.findByRole("button", { name: /Update settings/i })
expect(btn).toBeDisabled()
})
})
})
View file
Open in desktop
Original file line numberDiff line numberDiff 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 {
exportinterface 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 DownExpand Up@@ -62,14 +64,22 @@ export const AccountForm: FC<React.PropsWithChildren<AccountFormProps>> = ({
<TextField
{...getFieldHelpers("username")}
onChange={onChangeTrimmed(form)}
aria-disabled={!editable}
autoComplete="username"
disabled={!editable}
fullWidth
label={Language.usernameLabel}
variant="outlined"
/>

<div>
<LoadingButton loading={isLoading} type="submit" variant="contained">
<LoadingButton
loading={isLoading}
aria-disabled={!editable}
disabled={!editable}
type="submit"
variant="contained"
>
{isLoading ? "" : Language.updateSettings}
</LoadingButton>
</div>
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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={Boolean(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 deletionssite/src/testHelpers/entities.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp