Skip to content

Commit

Permalink
fix Datasource testing connection don't validate endpoints with path o…
Browse files Browse the repository at this point in the history
…pensearch-project#5656

Signed-off-by: Xinrui Bai <[email protected]>
  • Loading branch information
xinruiba committed Jan 4, 2024
1 parent 6710ec3 commit 9b21554
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ describe('DataSourceManagement: Utils.ts', () => {
expect(isValidUrl('')).toBeFalsy();
expect(isValidUrl('test')).toBeFalsy();

/* False cases: path name scenario*/
expect(isValidUrl('https://test.com/_somepath')).toBeFalsy();

/* True cases */
expect(isValidUrl('https://test.com')).toBeTruthy();
expect(isValidUrl('http://test.com')).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ export async function testConnection(
export const isValidUrl = (endpoint: string) => {
try {
const url = new URL(endpoint);
return Boolean(url) && (url.protocol === 'http:' || url.protocol === 'https:');
return (
Boolean(url) &&
(url.protocol === 'http:' || url.protocol === 'https:') &&
(!url.pathname || url.pathname.length === 0 || url.pathname === '/')
);
} catch (e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ 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,
mockDataSourceAttributesWithValidEndpoint,
mockDataSourceAttributesWithInvalidEndpoint,
} from '../../mocks';

describe('DataSourceManagement: Form Validation', () => {
describe('validate create/edit datasource', () => {
Expand Down Expand Up @@ -49,6 +53,16 @@ describe('DataSourceManagement: Form Validation', () => {
const result = performDataSourceFormValidation(form, [], '');
expect(result).toBe(false);
});
test('should fail validation when endpoint path is not empty', () => {
form.endpoint = mockDataSourceAttributesWithInvalidEndpoint.endpoint;
const result = performDataSourceFormValidation(form, [], '');
expect(result).toBe(false);
});
test('should NOT fail validation when endpoint path is empty', () => {
form.endpoint = mockDataSourceAttributesWithValidEndpoint.endpoint;
const result = performDataSourceFormValidation(form, [], '');
expect(result).toBe(false);
});
test('should NOT fail validation on empty username/password when No Auth is selected', () => {
form.auth.type = AuthType.NoAuth;
form.title = 'test';
Expand Down
28 changes: 28 additions & 0 deletions src/plugins/data_source_management/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,34 @@ export const getMappedDataSources = [
},
];

export const mockDataSourceAttributesWithValidEndpoint = {
id: 'test',
title: 'create-test-ds',
description: 'jest testing',
endpoint: 'https://test.com',
auth: {
type: AuthType.UsernamePasswordType,
credentials: {
username: 'test123',
password: 'test123',
},
},
};

export const mockDataSourceAttributesWithInvalidEndpoint = {
id: 'test',
title: 'create-test-ds',
description: 'jest testing',
endpoint: 'https://test.com/_somepath',
auth: {
type: AuthType.UsernamePasswordType,
credentials: {
username: 'test123',
password: 'test123',
},
},
};

export const mockDataSourceAttributesWithAuth = {
id: 'test',
title: 'create-test-ds',
Expand Down

0 comments on commit 9b21554

Please sign in to comment.