Skip to content

Commit

Permalink
[UT] Add more unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Xinrui Bai <[email protected]>
  • Loading branch information
xinruiba committed Feb 23, 2024
1 parent 2bf8abe commit f0201ef
Show file tree
Hide file tree
Showing 4 changed files with 207 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"]`;
Expand All @@ -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();
Expand Down Expand Up @@ -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<any, Readonly<{}>, 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(
<CreateDataSourceForm
handleTestConnection={mockTestConnectionHandler}
handleSubmit={mockSubmitHandler}
handleCancel={mockCancelHandler}
existingDatasourceNamesList={['dup20']}
/>
),
{
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(
<CreateDataSourceForm
handleTestConnection={mockTestConnectionHandler}
handleSubmit={mockSubmitHandler}
handleCancel={mockCancelHandler}
existingDatasourceNamesList={['dup20']}
/>
),
{
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(
<CreateDataSourceForm
handleTestConnection={mockTestConnectionHandler}
handleSubmit={mockSubmitHandler}
handleCancel={mockCancelHandler}
existingDatasourceNamesList={['dup20']}
/>
),
{
wrappingComponent: OpenSearchDashboardsContextProvider,
wrappingComponentProps: {
services: mockedContext,
},
}
);

const authOptionSelector = component.find(authOptionSelectorIdentifier).last();
expect(authOptionSelector.prop('disabled')).toBe(false);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import {
DataSourceAttributes,
DataSourceManagementContextValue,
UsernamePasswordTypedContent,
defaultAuthType,
noAuthCredentialAuthMethod,
sigV4ServiceOptions,
} from '../../../../types';
import { Header } from '../header';
Expand All @@ -40,7 +38,7 @@ import {
isTitleValid,
performDataSourceFormValidation,
} from '../../../validation';
import { isValidUrl } from '../../../utils';
import { getDefaultAuthMethod, isValidUrl } from '../../../utils';

export interface CreateDataSourceProps {
existingDatasourceNamesList: string[];
Expand Down Expand Up @@ -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: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
deleteMultipleDataSources,
getDataSourceById,
getDataSources,
getDefaultAuthMethod,
isValidUrl,
testConnection,
updateDataSourceById,
Expand All @@ -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();

Expand Down Expand Up @@ -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);
});
});
});
24 changes: 23 additions & 1 deletion src/plugins/data_source_management/public/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
};

0 comments on commit f0201ef

Please sign in to comment.