diff --git a/src/web/pages/credentials/__tests__/dialog.jsx b/src/web/pages/credentials/__tests__/dialog.jsx index c545f22759..5fccda3a67 100644 --- a/src/web/pages/credentials/__tests__/dialog.jsx +++ b/src/web/pages/credentials/__tests__/dialog.jsx @@ -16,7 +16,7 @@ import { getSelectElement, getSelectItemElementsForSelect, } from 'web/components/testing'; -import {rendererWith, fireEvent} from 'web/utils/testing'; +import {rendererWith, fireEvent, screen, render} from 'web/utils/testing'; import CredentialsDialog from '../dialog'; @@ -30,7 +30,7 @@ beforeEach(() => { handleErrorClose = testing.fn(); }); -const credential = Credential.fromElement({ +const credentialMock = Credential.fromElement({ _id: 'foo', allow_insecure: 1, creation_time: '2020-12-16T15:23:59Z', @@ -49,10 +49,6 @@ const credential = Credential.fromElement({ describe('CredentialsDialog component tests', () => { test('should render', () => { - const {render} = rendererWith({ - capabilities: true, - }); - const {getByName} = render( { const {getByName, getAllByName} = render( { }); test('should allow to change text field', () => { - const {render} = rendererWith({ - capabilities: true, - }); - const {getByName} = render( { auth_algorithm: 'sha1', autogenerate: 0, change_community: undefined, - change_passphrase: undefined, change_password: undefined, + change_passphrase: undefined, change_privacy_password: undefined, comment: 'bar', community: '', @@ -174,10 +166,6 @@ describe('CredentialsDialog component tests', () => { }); test('should allow changing select values', async () => { - const {render} = rendererWith({ - capabilities: true, - }); - render( { }); test('should allow to close the dialog', () => { - const {render} = rendererWith({ - capabilities: true, - }); - render( { }); test('should render form fields for Username + SSH', () => { - const {render} = rendererWith({ - capabilities: true, - }); - const {getByName} = render( { }); test('should render form fields for SNMP', () => { - const {render} = rendererWith({ - capabilities: true, - }); - const {getByName, getAllByName} = render( { }); test('should render form fields for S/MIME Certificate', () => { - const {render} = rendererWith({ - capabilities: true, - }); - const {getByName} = render( { }); test('should render form fields for PGP Encryption Key', () => { - const {render} = rendererWith({ - capabilities: true, - }); - const {getByName} = render( { }); test('should render form fields for Password Only', () => { - const {render} = rendererWith({ - capabilities: true, - }); - - const {getByName} = render( + render( { const select = getSelectElement(); expect(select).toHaveValue('Password only'); - const password = getByName('password'); + const password = screen.getByTestId('password-input'); expect(password).toHaveValue(''); expect(password).toHaveAttribute('type', 'password'); }); + + test('should render CredentialsDialog and handle replace password interactions correctly', () => { + const credentialEntryMock = Credential.fromElement({ + _id: '9b0', + allow_insecure: 1, + creation_time: '2025-01-08T15:50:23.000Z', + comment: 'MockComment', + formats: {format: 'exe'}, + full_type: 'username + password', + in_use: 0, + login: 'user42', + modification_time: '2025-01-09T08:58:33.000Z', + name: 'Unnamed', + owner: {name: 'admin'}, + permissions: {permission: {name: 'Everything'}}, + type: 'up', + writable: 1, + }); + + render( + , + ); + + const title = screen.getByText('Edit Credential'); + expect(title).toBeVisible(); + + const passwordField = screen.getByTestId('password-input'); + expect(passwordField).toBeDisabled(); + + const checkbox = screen.getByLabelText('Replace existing password with'); + + expect(checkbox).not.toBeChecked(); + + fireEvent.click(checkbox); + + expect(checkbox).toBeChecked(); + + expect(passwordField).not.toBeDisabled(); + }); }); diff --git a/src/web/pages/credentials/dialog.jsx b/src/web/pages/credentials/dialog.jsx index e6f3e75a04..acea1d8fcf 100644 --- a/src/web/pages/credentials/dialog.jsx +++ b/src/web/pages/credentials/dialog.jsx @@ -70,8 +70,8 @@ const CredentialsDialog = props => { const isEdit = isDefined(credential); useEffect(() => { - const {autogenerate: pAutogen, credential_type} = props; - setCredentialTypeAndAutoGenerate(credential_type, pAutogen); + const {autogenerate: pAutogenerate, credential_type} = props; + setCredentialTypeAndAutoGenerate(credential_type, pAutogenerate); }, [props]); const setCredentialTypeAndAutoGenerate = (type, autogenerate) => { @@ -270,7 +270,7 @@ const CredentialsDialog = props => { autoComplete="new-password" disabled={ state.autogenerate === YES_VALUE || - state.change_password === NO_VALUE + (isEdit && state.change_password !== YES_VALUE) } grow="1" name="password" @@ -296,7 +296,7 @@ const CredentialsDialog = props => { autoComplete="new-password" disabled={ state.autogenerate === YES_VALUE || - state.change_passphrase === NO_VALUE + (isEdit && state.change_passphrase !== YES_VALUE) } grow="1" name="passphrase" @@ -320,7 +320,10 @@ const CredentialsDialog = props => { )}