diff --git a/changelogs/fragments/6755.yml b/changelogs/fragments/6755.yml new file mode 100644 index 000000000000..8e06db889066 --- /dev/null +++ b/changelogs/fragments/6755.yml @@ -0,0 +1,2 @@ +fix: +- Add test for toast button and validation form ([#6755](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6755)) \ No newline at end of file diff --git a/src/plugins/data_source_management/public/components/toast_button/manage_data_source_button.test.tsx b/src/plugins/data_source_management/public/components/toast_button/manage_data_source_button.test.tsx new file mode 100644 index 000000000000..d61cd0dcabc7 --- /dev/null +++ b/src/plugins/data_source_management/public/components/toast_button/manage_data_source_button.test.tsx @@ -0,0 +1,38 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import React from 'react'; +import { shallow } from 'enzyme'; +import { getManageDataSourceButton } from './manage_data_source_button'; +import { coreMock } from '../../../../../core/public/mocks'; +import { DSM_APP_ID } from '../../plugin'; +import { render } from '@testing-library/react'; + +describe('ManageDataSourceButton', () => { + const applicationMock = coreMock.createStart().application; + + it('renders without crashing', () => { + const wrapper = render(getManageDataSourceButton()); + expect(wrapper).toBeTruthy(); + }); + + it('renders a button with correct label', () => { + const { getByTestId } = render(getManageDataSourceButton(applicationMock)); + const container = getByTestId('manageDataSourceButtonContainer'); + expect(container).toBeInTheDocument(); + expect(container).toHaveTextContent('Manage data sources'); + }); + + it('navigates to management app on button click', () => { + const { getByTestId } = render(getManageDataSourceButton(applicationMock)); + const button = getByTestId('manageDataSourceButton'); + button.click(); + expect(applicationMock.navigateToApp).toHaveBeenCalledTimes(1); + + expect(applicationMock.navigateToApp).toHaveBeenCalledWith('management', { + path: `opensearch-dashboards/${DSM_APP_ID}`, // Assuming DSM_APP_ID is replaced with a value + }); + }); +}); diff --git a/src/plugins/data_source_management/public/components/toast_button/manage_data_source_button.tsx b/src/plugins/data_source_management/public/components/toast_button/manage_data_source_button.tsx index 6222f74fdec4..63fcab2fdf70 100644 --- a/src/plugins/data_source_management/public/components/toast_button/manage_data_source_button.tsx +++ b/src/plugins/data_source_management/public/components/toast_button/manage_data_source_button.tsx @@ -11,9 +11,14 @@ import { DSM_APP_ID } from '../../plugin'; export const getManageDataSourceButton = (application?: ApplicationStart) => { return ( <> - + application?.navigateToApp('management', { diff --git a/src/plugins/data_source_management/public/components/toast_button/reload_button.test.tsx b/src/plugins/data_source_management/public/components/toast_button/reload_button.test.tsx new file mode 100644 index 000000000000..31223ae6943f --- /dev/null +++ b/src/plugins/data_source_management/public/components/toast_button/reload_button.test.tsx @@ -0,0 +1,26 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { render, fireEvent } from '@testing-library/react'; +import { getReloadButton } from './reload_button'; + +describe('getReloadButton', () => { + it('renders button with correct label', () => { + const { getByText } = render(getReloadButton()); + expect(getByText('Refresh the page')).toBeInTheDocument(); + }); + + it('calls window.location.reload() on button click', () => { + const reloadMock = jest.fn(); + Object.defineProperty(window, 'location', { + value: { reload: reloadMock }, + writable: true, + }); + + const { getByText } = render(getReloadButton()); + fireEvent.click(getByText('Refresh the page')); + expect(reloadMock).toHaveBeenCalled(); + }); +}); diff --git a/src/plugins/data_source_management/public/components/validation/datasource_form_validation.test.ts b/src/plugins/data_source_management/public/components/validation/datasource_form_validation.test.ts index 7b92a355e908..925185b98b6d 100644 --- a/src/plugins/data_source_management/public/components/validation/datasource_form_validation.test.ts +++ b/src/plugins/data_source_management/public/components/validation/datasource_form_validation.test.ts @@ -7,11 +7,14 @@ import { AuthType } from '../../types'; import { CreateDataSourceState } from '../create_data_source_wizard/components/create_form/create_data_source_form'; import { EditDataSourceState } from '../edit_data_source/components/edit_form/edit_data_source_form'; import { defaultValidation, performDataSourceFormValidation } from './datasource_form_validation'; -import { mockDataSourceAttributesWithAuth } from '../../mocks'; +import { + mockDataSourceAttributesWithAuth, + mockDataSourceAttributesWithSigV4Auth, +} from '../../mocks'; import { AuthenticationMethod, AuthenticationMethodRegistry } from '../../auth_registry'; describe('DataSourceManagement: Form Validation', () => { - describe('validate create/edit datasource', () => { + describe('validate create/edit datasource for Username and Password auth type', () => { let authenticationMethodRegistry = new AuthenticationMethodRegistry(); let form: CreateDataSourceState | EditDataSourceState = { formErrorsByField: { ...defaultValidation }, @@ -117,4 +120,113 @@ describe('DataSourceManagement: Form Validation', () => { expect(result).toBe(true); }); }); + + describe('validate create/edit datasource for SigV4 auth type', () => { + let authenticationMethodRegistry = new AuthenticationMethodRegistry(); + let form: CreateDataSourceState | EditDataSourceState = { + formErrorsByField: { ...defaultValidation }, + title: '', + description: '', + endpoint: '', + auth: { + type: AuthType.SigV4, + credentials: { + accesskey: 'test123', + secretKey: 'test123', + service: 'es', + region: 'us-east-1', + }, + }, + }; + test('should fail validation when title is empty', () => { + const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry); + expect(result).toBe(false); + }); + test('should fail validation on duplicate title', () => { + form.title = 'test'; + const result = performDataSourceFormValidation( + form, + ['oldTitle', 'test'], + 'oldTitle', + authenticationMethodRegistry + ); + expect(result).toBe(false); + }); + test('should fail validation when title is longer than 32 characters', () => { + form.title = 'test'.repeat(10); + const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry); + expect(result).toBe(false); + }); + test('should fail validation when endpoint is not valid', () => { + form.endpoint = mockDataSourceAttributesWithSigV4Auth.endpoint; + const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry); + expect(result).toBe(false); + }); + test('should fail validation when accesskey is empty', () => { + form.auth.credentials!.accessKey = 'test'; + const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry); + expect(result).toBe(false); + }); + test('should fail validation when secrectKey is empty', () => { + form.auth.credentials!.accessKey = 'test'; + form.auth.credentials!.secretKey = ''; + const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry); + expect(result).toBe(false); + }); + test('should NOT fail validation on empty accesskey/secretKey when No Auth is selected', () => { + form.auth.type = AuthType.NoAuth; + form.title = 'test'; + form.endpoint = mockDataSourceAttributesWithSigV4Auth.endpoint; + const result = performDataSourceFormValidation(form, [], '', authenticationMethodRegistry); + expect(result).toBe(true); + }); + test('should NOT fail validation on all fields', () => { + form = { ...form, ...mockDataSourceAttributesWithSigV4Auth }; + const result = performDataSourceFormValidation( + form, + [mockDataSourceAttributesWithSigV4Auth.title], + mockDataSourceAttributesWithSigV4Auth.title, + authenticationMethodRegistry + ); + expect(result).toBe(true); + }); + test('should NOT fail validation when registered auth type is selected and related credential field not empty', () => { + authenticationMethodRegistry = new AuthenticationMethodRegistry(); + const authMethodToBeTested = { + name: 'Some Auth Type', + credentialSourceOption: { + value: 'Some Auth Type', + inputDisplay: 'some input', + }, + credentialForm: jest.fn(), + credentialFormField: { + userNameRegistered: 'some filled in userName from registed auth credential form', + passWordRegistered: 'some filled in password from registed auth credential form', + }, + } as AuthenticationMethod; + + authenticationMethodRegistry.registerAuthenticationMethod(authMethodToBeTested); + + const formWithRegisteredAuth: CreateDataSourceState | EditDataSourceState = { + formErrorsByField: { ...defaultValidation }, + title: 'test registered auth type', + description: '', + endpoint: 'https://test.com', + auth: { + type: 'Some Auth Type', + credentials: { + userNameRegistered: 'some filled in userName from registed auth credential form', + passWordRegistered: 'some filled in password from registed auth credential form', + }, + }, + }; + const result = performDataSourceFormValidation( + formWithRegisteredAuth, + [], + '', + authenticationMethodRegistry + ); + expect(result).toBe(true); + }); + }); }); diff --git a/src/plugins/data_source_management/public/mocks.ts b/src/plugins/data_source_management/public/mocks.ts index 0e5ec60bc307..a33a55d6799c 100644 --- a/src/plugins/data_source_management/public/mocks.ts +++ b/src/plugins/data_source_management/public/mocks.ts @@ -278,6 +278,7 @@ export const mockDataSourceAttributesWithSigV4Auth = { accessKey: 'test123', secretKey: 'test123', region: 'us-east-1', + service: 'es', }, }, };