Skip to content

Commit

Permalink
wip placeholder + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolearagao committed Nov 20, 2024
1 parent 5e42a54 commit 7326ab3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 102 deletions.
2 changes: 1 addition & 1 deletion src/components/actionMenu/actionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { EllipsisVIcon } from '@patternfly/react-icons';
type Action<T> = {
label: string;
onClick: (item: T) => void;
disabled?: boolean;
disabled?: boolean;
ouiaId?: string; // Optional ouiaId for E2E testing
};

Expand Down
6 changes: 3 additions & 3 deletions src/hooks/__tests__/useCredentialApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,23 +230,23 @@ describe('useEditCredentialApi', () => {

it('should attempt an api call to edit credentials', () => {
const { apiCall } = hookResult;
const spyAxios = jest.spyOn(axios, 'put');
const spyAxios = jest.spyOn(axios, 'patch');

apiCall({ id: 123, name: 'Lorem' }).catch(Function.prototype);
expect(spyAxios.mock.calls).toMatchSnapshot('apiCall');
});

it('should handle success while attempting to edit a credential', async () => {
const { editCredentials } = hookResult;
jest.spyOn(axios, 'put').mockImplementation(() => Promise.resolve({}));
jest.spyOn(axios, 'patch').mockImplementation(() => Promise.resolve({}));

await editCredentials({ name: 'Lorem', id: '123' });
expect(mockOnAddAlert.mock.calls).toMatchSnapshot('editCredentials, success');
});

it('should handle errors while attempting to edit a credential', async () => {
const { editCredentials } = hookResult;
jest.spyOn(axios, 'put').mockImplementation(() => Promise.reject({ isAxiosError: true, message: 'Mock error' }));
jest.spyOn(axios, 'patch').mockImplementation(() => Promise.reject({ isAxiosError: true, message: 'Mock error' }));

await editCredentials({ name: 'Lorem', id: '123' });
expect(mockOnAddAlert.mock.calls).toMatchSnapshot('editCredentials, error');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@ exports[`AddCredentialModal should call onSubmit with the correct filtered data
[
[
{
"auth_token": "",
"authenticationType": "",
"become_method": "",
"become_password": "",
"become_user": "",
"cred_type": "network",
"name": "Test Credential",
"password": "",
"ssh_key": "",
"ssh_passphrase": "",
"username": "",
},
],
Expand Down Expand Up @@ -115,7 +110,6 @@ exports[`CredentialForm should render a basic component: basic 1`] = `
isRequired={true}
name="password"
onChange={[Function]}
onFocus={[Function]}
ouiaId="password"
placeholder="Enter password"
type="password"
Expand Down Expand Up @@ -184,7 +178,6 @@ exports[`CredentialForm should render ansible form appropriately: ansible, "User
isRequired={true}
name="password"
onChange={[Function]}
onFocus={[Function]}
ouiaId="password"
placeholder="Enter password"
type="password"
Expand Down Expand Up @@ -253,7 +246,6 @@ exports[`CredentialForm should render satellite form appropriately: satellite, "
isRequired={true}
name="password"
onChange={[Function]}
onFocus={[Function]}
ouiaId="password"
placeholder="Enter password"
type="password"
Expand Down Expand Up @@ -346,7 +338,6 @@ exports[`CredentialForm should render specific to authType for type network: net
isRequired={true}
name="password"
onChange={[Function]}
onFocus={[Function]}
ouiaId="password"
placeholder="Enter password"
type="password"
Expand Down Expand Up @@ -522,7 +513,6 @@ exports[`CredentialForm should render specific to authType for type network: net
isRequired={true}
name="password"
onChange={[Function]}
onFocus={[Function]}
ouiaId="password"
placeholder="Enter password"
type="password"
Expand Down Expand Up @@ -682,7 +672,6 @@ exports[`CredentialForm should render specific to authType for type openshift: o
isRequired={true}
name="auth_token"
onChange={[Function]}
onFocus={[Function]}
ouiaId="auth_token"
placeholder="Enter Token"
type="text"
Expand Down Expand Up @@ -758,7 +747,6 @@ exports[`CredentialForm should render specific to authType for type openshift: o
isRequired={true}
name="auth_token"
onChange={[Function]}
onFocus={[Function]}
ouiaId="auth_token"
placeholder="Enter Token"
type="text"
Expand Down Expand Up @@ -826,7 +814,6 @@ exports[`CredentialForm should render vcenter form appropriately: vcenter, "User
isRequired={true}
name="password"
onChange={[Function]}
onFocus={[Function]}
ouiaId="password"
placeholder="Enter password"
type="password"
Expand Down Expand Up @@ -855,7 +842,6 @@ exports[`getCleanedFormData should clean formData correctly for SSH Key authType
{
"auth_token": "test_token",
"name": "Test Credential",
"password": "",
"ssh_key": "test_ssh_key",
"ssh_passphrase": "test_passphrase",
"username": "test_user",
Expand All @@ -866,7 +852,6 @@ exports[`getCleanedFormData should clean formData correctly for Token authType:
{
"auth_token": "test_token",
"name": "Test Credential",
"password": "",
"ssh_key": "test_ssh_key",
"ssh_passphrase": "test_passphrase",
"username": "",
Expand All @@ -875,23 +860,18 @@ exports[`getCleanedFormData should clean formData correctly for Token authType:

exports[`getCleanedFormData should clean formData correctly for Username and Password authType: Username and Password auth cleanedData 1`] = `
{
"auth_token": "",
"name": "Test Credential",
"password": "test_password",
"ssh_key": "",
"ssh_passphrase": "",
"username": "test_user",
}
`;

exports[`useCredentialForm should allow editing a credential: formData, edit 1`] = `
{
"auth_token": "Ipsum",
"auth_token": "",
"become_method": "",
"become_password": "",
"become_user": "",
"cred_type": "openshift",
"id": 123,
"name": "lorem",
"password": "",
"ssh_key": "",
Expand Down Expand Up @@ -930,21 +910,3 @@ exports[`useCredentialForm should initialize formData correctly: formData 1`] =
"username": "",
}
`;

exports[`useCredentialForm should mask sensitive fields when credential has corresponding values: masked sensitive fields 1`] = `
{
"auth_token": "****",
"become_method": "",
"become_password": "",
"become_user": "",
"has_auth_token": true,
"has_password": true,
"has_ssh_key": true,
"has_ssh_passphrase": true,
"name": "",
"password": "****",
"ssh_key": "****",
"ssh_passphrase": "****",
"username": "",
}
`;
19 changes: 4 additions & 15 deletions src/views/credentials/__tests__/addCredentialModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,6 @@ describe('useCredentialForm', () => {
expect(filteredData.name).toBe(mockValue);
});

it('should mask sensitive fields when credential has corresponding values', () => {
const mockCredential = {
has_password: true,
has_ssh_key: true,
has_ssh_passphrase: true,
has_auth_token: true
};

const { result } = renderHook(() => useCredentialForm({ credential: mockCredential }));
expect(result.current.formData).toMatchSnapshot('masked sensitive fields');
});

it('should allow editing a credential', async () => {
const { result } = renderHook(() =>
useCredentialForm({
Expand Down Expand Up @@ -215,6 +203,7 @@ describe('CredentialForm', () => {
});

describe('getCleanedFormData', () => {
const maskedFields = ['password', 'ssh_key', 'ssh_passphrase', 'become_password', 'auth_token'];
it('should clean formData correctly for Token authType', () => {
const formData = {
name: 'Test Credential',
Expand All @@ -225,7 +214,7 @@ describe('getCleanedFormData', () => {
ssh_passphrase: 'test_passphrase'
};

const cleanedData = getCleanedFormData(formData, 'Token');
const cleanedData = getCleanedFormData(formData, 'Token', maskedFields);
expect(cleanedData).toMatchSnapshot('Token auth cleanedData');
});

Expand All @@ -239,7 +228,7 @@ describe('getCleanedFormData', () => {
ssh_passphrase: 'test_passphrase'
};

const cleanedData = getCleanedFormData(formData, 'Username and Password');
const cleanedData = getCleanedFormData(formData, 'Username and Password', maskedFields);
expect(cleanedData).toMatchSnapshot('Username and Password auth cleanedData');
});

Expand All @@ -253,7 +242,7 @@ describe('getCleanedFormData', () => {
ssh_passphrase: 'test_passphrase'
};

const cleanedData = getCleanedFormData(formData, 'SSH Key');
const cleanedData = getCleanedFormData(formData, 'SSH Key', maskedFields);
expect(cleanedData).toMatchSnapshot('SSH Key auth cleanedData');
});
});
Loading

0 comments on commit 7326ab3

Please sign in to comment.