Skip to content

Commit

Permalink
feat(credential): ds-438 edit credential (#507)
Browse files Browse the repository at this point in the history
Relates to DISCOVERY: JIRA-438
  • Loading branch information
nicolearagao authored Nov 22, 2024
1 parent 3825c7c commit 8412af8
Show file tree
Hide file tree
Showing 12 changed files with 759 additions and 744 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ REACT_APP_CONFIG_SERVICE_LOCALES=/locales/locales.json
REACT_APP_CONFIG_SERVICE_LOCALES_PATH=/locales/{{lng}}.json
REACT_APP_CONFIG_SERVICE_LOCALES_EXPIRE=604800000

REACT_APP_CREDENTIALS_SERVICE=/api/v1/credentials/
REACT_APP_CREDENTIALS_SERVICE_BULK_DELETE=/api/v1/credentials/bulk_delete/
REACT_APP_CREDENTIALS_SERVICE=/api/v2/credentials/
REACT_APP_CREDENTIALS_SERVICE_BULK_DELETE=/api/v2/credentials/bulk_delete/
REACT_APP_FACTS_SERVICE=/api/v1/facts/

REACT_APP_REPORTS_SERVICE=/api/v1/reports/
Expand Down
10 changes: 9 additions & 1 deletion src/components/actionMenu/actionMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ import React, { useState } from 'react';
import { Dropdown, DropdownItem, DropdownList, MenuToggle, type MenuToggleElement } from '@patternfly/react-core';
import { EllipsisVIcon } from '@patternfly/react-icons';

type Action<T> = {
label: string;
onClick: (item: T) => void;
disabled?: boolean;
ouiaId?: string; // Optional ouiaId for E2E testing
};

interface ActionMenuProps<T = unknown> {
item: T;
actions: { label: string; onClick: (item: T) => void; disabled?: boolean }[];
actions: Action<T>[];
}

const ActionMenu = <T,>({ item, actions }: ActionMenuProps<T>) => {
Expand Down Expand Up @@ -40,6 +47,7 @@ const ActionMenu = <T,>({ item, actions }: ActionMenuProps<T>) => {
<DropdownItem
value={a.label}
key={a.label}
ouiaId={a.ouiaId}
onClick={() => {
a.onClick(item);
}}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ enum authType {
* @param {CredentialType} credential - The CredentialType object representing authentication information.
* @returns {string} - A string indicating the authentication type, e.g., "Username and Password".
*/
const getAuthType = ({ auth_type }: CredentialType): authType => {
const getAuthType = ({ auth_type }: Partial<CredentialType>): authType => {
switch (auth_type) {
case 'password':
return authType.UsernameAndPassword;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`useAddCredentialApi should attempt an api call to add credentials: apiCall 1`] = `
[
[
"/api/v1/credentials/",
"/api/v2/credentials/",
{
"name": "Lorem",
},
Expand Down Expand Up @@ -100,7 +100,7 @@ exports[`useAddCredentialApi should process an API success response: callbackSuc
exports[`useDeleteCredentialApi should attempt an api call to delete credentials: apiCall 1`] = `
[
[
"/api/v1/credentials/bulk_delete/",
"/api/v2/credentials/bulk_delete/",
{
"ids": [
456,
Expand Down Expand Up @@ -284,7 +284,7 @@ exports[`useDeleteCredentialApi should process an API success response: callback
exports[`useEditCredentialApi should attempt an api call to edit credentials: apiCall 1`] = `
[
[
"/api/v1/credentials/123/",
"/api/v2/credentials/123/",
{
"id": 123,
"name": "Lorem",
Expand Down Expand Up @@ -382,7 +382,7 @@ exports[`useEditCredentialApi should process an API success response: callbackSu
exports[`useGetCredentialsApi should attempt an api call to retrieve credentials: apiCall 1`] = `
[
[
"/api/v1/credentials/",
"/api/v2/credentials/",
{},
],
]
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 @@ -232,23 +232,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 expect(editCredentials({ name: 'Lorem', id: '123' })).rejects.toMatchSnapshot('editCredentials, error');
expect(mockOnAddAlert.mock.calls).toMatchSnapshot('editCredentials, alert error');
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useCredentialApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ const useEditCredentialApi = (onAddAlert: (alert: AlertProps) => void) => {

const apiCall = useCallback(
(payload: CredentialType): Promise<AxiosResponse<CredentialType>> =>
axios.put(`${process.env.REACT_APP_CREDENTIALS_SERVICE}${payload.id}/`, payload),
axios.patch(`${process.env.REACT_APP_CREDENTIALS_SERVICE}${payload.id}/`, payload),
[]
);

Expand Down
5 changes: 5 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export type CredentialType = {
become_password: string;
sources: SourceType[];
auth_type: string;
has_password?: boolean;
has_ssh_key?: boolean;
has_ssh_passphrase?: boolean;
has_become_password?: boolean;
has_auth_token?: boolean;
};

export type SourceConnectionType = {
Expand Down
Loading

0 comments on commit 8412af8

Please sign in to comment.