From f0201efc953fc3ec5b18a24687352c3c07d246b1 Mon Sep 17 00:00:00 2001 From: Xinrui Bai Date: Fri, 23 Feb 2024 19:39:21 +0000 Subject: [PATCH] [UT] Add more unit tests Signed-off-by: Xinrui Bai --- .../create_data_source_form.test.tsx | 128 ++++++++++++++++++ .../create_form/create_data_source_form.tsx | 13 +- .../public/components/utils.test.ts | 55 +++++++- .../public/components/utils.ts | 24 +++- 4 files changed, 207 insertions(+), 13 deletions(-) diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx index 272ff1ba88eb..36d415712178 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.test.tsx @@ -17,6 +17,7 @@ import { sigV4AuthMethod, usernamePasswordAuthMethod, } from '../../../../types'; +import { AuthenticationMethodRegistery } from 'src/plugins/data_source_management/public/auth_registry'; const titleIdentifier = '[data-test-subj="createDataSourceFormTitleField"]'; const descriptionIdentifier = `[data-test-subj="createDataSourceFormDescriptionField"]`; @@ -26,6 +27,7 @@ const usernameIdentifier = '[data-test-subj="createDataSourceFormUsernameField"] const passwordIdentifier = '[data-test-subj="createDataSourceFormPasswordField"]'; const createButtonIdentifier = '[data-test-subj="createDataSourceButton"]'; const testConnectionButtonIdentifier = '[data-test-subj="createDataSourceTestConnectionButton"]'; +const authOptionSelectorIdentifier = '[data-test-subj="createDataSourceFormAuthTypeSelect"]'; describe('Datasource Management: Create Datasource form', () => { const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); @@ -235,3 +237,129 @@ describe('Datasource Management: Create Datasource form', () => { expect(component.find(passwordIdentifier).first().props().isInvalid).toBe(false); }); }); + +describe('Datasource Management: Create Datasource form with different authType configurations', () => { + let component: ReactWrapper, React.Component<{}, {}, any>>; + const mockSubmitHandler = jest.fn(); + const mockTestConnectionHandler = jest.fn(); + const mockCancelHandler = jest.fn(); + + /* Scenario 1: Should render the page normally with all authMethod combinations */ + test('should render normally with all authMethod combinations', () => { + const authMethodCombinationsToBeTested = [ + [noAuthCredentialAuthMethod], + [usernamePasswordAuthMethod], + [sigV4AuthMethod], + [noAuthCredentialAuthMethod, usernamePasswordAuthMethod], + [noAuthCredentialAuthMethod, sigV4AuthMethod], + [usernamePasswordAuthMethod, sigV4AuthMethod], + [noAuthCredentialAuthMethod, usernamePasswordAuthMethod, sigV4AuthMethod], + ]; + + authMethodCombinationsToBeTested.forEach((authMethodCombination) => { + const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); + mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery(); + + authMethodCombination.forEach((authMethod) => { + mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethod); + }); + + component = mount( + wrapWithIntl( + + ), + { + wrappingComponent: OpenSearchDashboardsContextProvider, + wrappingComponentProps: { + services: mockedContext, + }, + } + ); + + const testConnBtn = component.find(testConnectionButtonIdentifier).last(); + expect(testConnBtn.prop('disabled')).toBe(true); + }); + }); + + /* Scenario 2: options selector should be disabled when only one authMethod supported */ + test('options selector should be disabled when only one authMethod supported', () => { + const authMethodCombinationsToBeTested = [ + [noAuthCredentialAuthMethod], + [usernamePasswordAuthMethod], + [sigV4AuthMethod], + ]; + + authMethodCombinationsToBeTested.forEach((authMethodCombination) => { + const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); + mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery(); + + authMethodCombination.forEach((authMethod) => { + mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethod); + }); + + component = mount( + wrapWithIntl( + + ), + { + wrappingComponent: OpenSearchDashboardsContextProvider, + wrappingComponentProps: { + services: mockedContext, + }, + } + ); + + const authOptionSelector = component.find(authOptionSelectorIdentifier).last(); + expect(authOptionSelector.prop('disabled')).toBe(true); + }); + }); + + /* Scenario 3: options selector should not be disabled when more than one authMethod supported */ + test('options selector should not be disabled when more than one authMethod supported', () => { + const authMethodCombinationsToBeTested = [ + [sigV4AuthMethod, usernamePasswordAuthMethod], + [noAuthCredentialAuthMethod, sigV4AuthMethod], + [noAuthCredentialAuthMethod, usernamePasswordAuthMethod], + [noAuthCredentialAuthMethod, sigV4AuthMethod, usernamePasswordAuthMethod], + ]; + + authMethodCombinationsToBeTested.forEach((authMethodCombination) => { + const mockedContext = mockManagementPlugin.createDataSourceManagementContext(); + mockedContext.authenticationMethodRegistery = new AuthenticationMethodRegistery(); + + authMethodCombination.forEach((authMethod) => { + mockedContext.authenticationMethodRegistery.registerAuthenticationMethod(authMethod); + }); + + component = mount( + wrapWithIntl( + + ), + { + wrappingComponent: OpenSearchDashboardsContextProvider, + wrappingComponentProps: { + services: mockedContext, + }, + } + ); + + const authOptionSelector = component.find(authOptionSelectorIdentifier).last(); + expect(authOptionSelector.prop('disabled')).toBe(false); + }); + }); +}); diff --git a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx index e98e2d7ff960..2fb89bb64554 100644 --- a/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx +++ b/src/plugins/data_source_management/public/components/create_data_source_wizard/components/create_form/create_data_source_form.tsx @@ -28,8 +28,6 @@ import { DataSourceAttributes, DataSourceManagementContextValue, UsernamePasswordTypedContent, - defaultAuthType, - noAuthCredentialAuthMethod, sigV4ServiceOptions, } from '../../../../types'; import { Header } from '../header'; @@ -40,7 +38,7 @@ import { isTitleValid, performDataSourceFormValidation, } from '../../../validation'; -import { isValidUrl } from '../../../utils'; +import { getDefaultAuthMethod, isValidUrl } from '../../../utils'; export interface CreateDataSourceProps { existingDatasourceNamesList: string[]; @@ -75,19 +73,12 @@ export class CreateDataSourceForm extends React.Component< const authenticationMethodRegistery = context.services.authenticationMethodRegistery; const registeredAuthMethods = authenticationMethodRegistery.getAllAuthenticationMethods(); + const initialSelectedAuthMethod = getDefaultAuthMethod(authenticationMethodRegistery); this.authOptions = registeredAuthMethods.map((authMethod) => { return authMethod.credentialSourceOption; }); - const defaultAuthMethod = - registeredAuthMethods.length > 0 - ? authenticationMethodRegistery.getAuthenticationMethod(registeredAuthMethods[0].name) - : noAuthCredentialAuthMethod; - - const initialSelectedAuthMethod = - authenticationMethodRegistery.getAuthenticationMethod(defaultAuthType) ?? defaultAuthMethod; - this.state = { formErrorsByField: { ...defaultValidation }, title: '', diff --git a/src/plugins/data_source_management/public/components/utils.test.ts b/src/plugins/data_source_management/public/components/utils.test.ts index a94d5b2260e6..1b78b20e19d1 100644 --- a/src/plugins/data_source_management/public/components/utils.test.ts +++ b/src/plugins/data_source_management/public/components/utils.test.ts @@ -9,6 +9,7 @@ import { deleteMultipleDataSources, getDataSourceById, getDataSources, + getDefaultAuthMethod, isValidUrl, testConnection, updateDataSourceById, @@ -23,8 +24,14 @@ import { mockErrorResponseForSavedObjectsCalls, mockResponseForSavedObjectsCalls, } from '../mocks'; -import { AuthType } from '../types'; +import { + AuthType, + noAuthCredentialAuthMethod, + sigV4AuthMethod, + usernamePasswordAuthMethod, +} from '../types'; import { HttpStart } from 'opensearch-dashboards/public'; +import { AuthenticationMethodRegistery } from '../auth_registry'; const { savedObjects } = coreMock.createStart(); @@ -219,4 +226,50 @@ describe('DataSourceManagement: Utils.ts', () => { /* True cases: port number scenario*/ expect(isValidUrl('http://192.168.1.1:1234/')).toBeTruthy(); }); + + describe('Check default auth method', () => { + test('default auth method is Username & Password when Username & Password is enabled', () => { + const authMethodCombinationsToBeTested = [ + [usernamePasswordAuthMethod], + [sigV4AuthMethod, usernamePasswordAuthMethod], + [noAuthCredentialAuthMethod, usernamePasswordAuthMethod], + [noAuthCredentialAuthMethod, sigV4AuthMethod, usernamePasswordAuthMethod], + ]; + + authMethodCombinationsToBeTested.forEach((authOptions) => { + const authenticationMethodRegistery = new AuthenticationMethodRegistery(); + + authOptions.forEach((authMethod) => { + authenticationMethodRegistery.registerAuthenticationMethod(authMethod); + }); + + expect(getDefaultAuthMethod(authenticationMethodRegistery)?.name).toBe( + AuthType.UsernamePasswordType + ); + }); + }); + + test('default auth method is first one in AuthList when Username & Password is not enabled', () => { + const authMethodCombinationsToBeTested = [ + [sigV4AuthMethod], + [noAuthCredentialAuthMethod], + [sigV4AuthMethod, noAuthCredentialAuthMethod], + ]; + + authMethodCombinationsToBeTested.forEach((authOptions) => { + const authenticationMethodRegistery = new AuthenticationMethodRegistery(); + + authOptions.forEach((authMethod) => { + authenticationMethodRegistery.registerAuthenticationMethod(authMethod); + }); + + expect(getDefaultAuthMethod(authenticationMethodRegistery)?.name).toBe(authOptions[0].name); + }); + }); + + test('default auth method is NoAuth when no auth options configured', () => { + const authenticationMethodRegistery = new AuthenticationMethodRegistery(); + expect(getDefaultAuthMethod(authenticationMethodRegistery)?.name).toBe(AuthType.NoAuth); + }); + }); }); diff --git a/src/plugins/data_source_management/public/components/utils.ts b/src/plugins/data_source_management/public/components/utils.ts index 5f2cfb2337ad..726fcc527f30 100644 --- a/src/plugins/data_source_management/public/components/utils.ts +++ b/src/plugins/data_source_management/public/components/utils.ts @@ -4,7 +4,13 @@ */ import { HttpStart, SavedObjectsClientContract } from 'src/core/public'; -import { DataSourceAttributes, DataSourceTableItem } from '../types'; +import { + DataSourceAttributes, + DataSourceTableItem, + defaultAuthType, + noAuthCredentialAuthMethod, +} from '../types'; +import { AuthenticationMethodRegistery } from '../auth_registry'; export async function getDataSources(savedObjectsClient: SavedObjectsClientContract) { return savedObjectsClient @@ -108,3 +114,19 @@ export const isValidUrl = (endpoint: string) => { return false; } }; + +export const getDefaultAuthMethod = ( + authenticationMethodRegistery: AuthenticationMethodRegistery +) => { + const registeredAuthMethods = authenticationMethodRegistery.getAllAuthenticationMethods(); + + const defaultAuthMethod = + registeredAuthMethods.length > 0 + ? authenticationMethodRegistery.getAuthenticationMethod(registeredAuthMethods[0].name) + : noAuthCredentialAuthMethod; + + const initialSelectedAuthMethod = + authenticationMethodRegistery.getAuthenticationMethod(defaultAuthType) ?? defaultAuthMethod; + + return initialSelectedAuthMethod; +};