From c16283e959d13ccc355d54c5f993f86843f2c819 Mon Sep 17 00:00:00 2001 From: nickofthyme Date: Fri, 13 Dec 2024 11:59:57 -0600 Subject: [PATCH 1/3] fix: user input change with password manager --- .../authentication/login/components/login_form/login_form.tsx | 4 +++- .../security/public/management/users/edit_user/user_form.tsx | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/x-pack/plugins/security/public/authentication/login/components/login_form/login_form.tsx b/x-pack/plugins/security/public/authentication/login/components/login_form/login_form.tsx index e8b2e684819c0..921ff64e56c9b 100644 --- a/x-pack/plugins/security/public/authentication/login/components/login_form/login_form.tsx +++ b/x-pack/plugins/security/public/authentication/login/components/login_form/login_form.tsx @@ -229,7 +229,9 @@ export class LoginForm extends Component { name="username" data-test-subj="loginUsername" value={this.state.username} - onChange={this.onUsernameChange} + onChange={ + !this.isLoadingState(LoadingStateType.None) ? undefined : this.onUsernameChange + } disabled={!this.isLoadingState(LoadingStateType.None)} isInvalid={false} aria-required={true} diff --git a/x-pack/plugins/security/public/management/users/edit_user/user_form.tsx b/x-pack/plugins/security/public/management/users/edit_user/user_form.tsx index d5cfc09819157..3fdbf2ab1bc90 100644 --- a/x-pack/plugins/security/public/management/users/edit_user/user_form.tsx +++ b/x-pack/plugins/security/public/management/users/edit_user/user_form.tsx @@ -272,7 +272,7 @@ export const UserForm: FunctionComponent = ({ isLoading={form.isValidating} isInvalid={form.touched.username && !!form.errors.username} disabled={disabled || !isNewUser} - onChange={eventHandlers.onChange} + onChange={disabled || !isNewUser ? undefined : eventHandlers.onChange} onBlur={eventHandlers.onBlur} /> From dff109ccaed82f6101adec121528a378c969daa3 Mon Sep 17 00:00:00 2001 From: Nick Partridge Date: Sun, 15 Dec 2024 09:21:03 -0600 Subject: [PATCH 2/3] chore: remove `login_form.tsx` changes --- .../authentication/login/components/login_form/login_form.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/x-pack/plugins/security/public/authentication/login/components/login_form/login_form.tsx b/x-pack/plugins/security/public/authentication/login/components/login_form/login_form.tsx index 921ff64e56c9b..e8b2e684819c0 100644 --- a/x-pack/plugins/security/public/authentication/login/components/login_form/login_form.tsx +++ b/x-pack/plugins/security/public/authentication/login/components/login_form/login_form.tsx @@ -229,9 +229,7 @@ export class LoginForm extends Component { name="username" data-test-subj="loginUsername" value={this.state.username} - onChange={ - !this.isLoadingState(LoadingStateType.None) ? undefined : this.onUsernameChange - } + onChange={this.onUsernameChange} disabled={!this.isLoadingState(LoadingStateType.None)} isInvalid={false} aria-required={true} From f2ab08d79819974c9c217fd1d00387226fcf5643 Mon Sep 17 00:00:00 2001 From: nickofthyme Date: Thu, 9 Jan 2025 13:33:45 -0600 Subject: [PATCH 3/3] test: add tests for username change while disabled --- .../users/edit_user/user_form.test.tsx | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 x-pack/platform/plugins/shared/security/public/management/users/edit_user/user_form.test.tsx diff --git a/x-pack/platform/plugins/shared/security/public/management/users/edit_user/user_form.test.tsx b/x-pack/platform/plugins/shared/security/public/management/users/edit_user/user_form.test.tsx new file mode 100644 index 0000000000000..2bdae6fd56311 --- /dev/null +++ b/x-pack/platform/plugins/shared/security/public/management/users/edit_user/user_form.test.tsx @@ -0,0 +1,64 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { fireEvent, render, screen } from '@testing-library/react'; +import { createMemoryHistory } from 'history'; +import React from 'react'; + +import { coreMock } from '@kbn/core/public/mocks'; + +import type { UserFormProps, UserFormValues } from './user_form'; +import { UserForm } from './user_form'; +import { securityMock } from '../../../mocks'; +import { Providers } from '../users_management_app'; + +const userMock: UserFormValues = { + username: 'jdoe', + full_name: '', + email: '', + roles: ['superuser'], +}; + +describe('UserForm', () => { + const coreStart = coreMock.createStart(); + const authc = securityMock.createSetup().authc; + const history = createMemoryHistory({ initialEntries: ['/edit/jdoe'] }); + + const onCancelMock = jest.fn(); + const onSuccessMock = jest.fn(); + + let defaultProps: UserFormProps; + + beforeEach(() => { + defaultProps = { + isNewUser: true, + isReservedUser: false, + isCurrentUser: false, + defaultValues: userMock, + onCancel: onCancelMock, + onSuccess: onSuccessMock, + disabled: false, + }; + }); + + const renderUserForm = (props: Partial = {}) => { + return render( + + + + ); + }; + + it('prevents editing username when disabled', async () => { + // See https://github.com/elastic/kibana/issues/204268 + + renderUserForm({ disabled: true }); + const usernameInput = screen.getByTestId('userFormUserNameInput'); + fireEvent.change(usernameInput, { target: { value: 'foo' } }); + expect(usernameInput.value).toBe('jdoe'); + }); +});