Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ilm navigation #81664

Merged
merged 19 commits into from Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 43 additions & 6 deletions src/plugins/es_ui_shared/public/url/attempt_to_uri_decode.test.ts
Expand Up @@ -19,14 +19,51 @@

import { attemptToURIDecode } from './attempt_to_uri_decode';

// this function doesn't work for % with other special chars or sequence %25
// known issue https://github.com/elastic/kibana/issues/82440
test('decodes an encoded string', () => {
const encodedString = 'test%3F';
expect(attemptToURIDecode(encodedString)).toBe('test?');
const originalName = 'test;,/?:@&=+$#';
const encodedName = encodeURIComponent(originalName);
// react router v5 automatically decodes route match params
const reactRouterDecoded = decodeURI(encodedName);

expect(attemptToURIDecode(encodedName)).toBe(originalName);
expect(attemptToURIDecode(reactRouterDecoded)).toBe(originalName);
});

// react router partially decodes %25 sequence to % in match params
// https://github.com/elastic/kibana/pull/81664
test('ignores the error if a string is already decoded', () => {
const decodedString = 'test%';
expect(attemptToURIDecode(decodedString)).toBe(decodedString);
const originalName = 'test%';

const encodedName = encodeURIComponent(originalName);
// react router v5 automatically decodes route match params
const reactRouterDecoded = decodeURI(encodedName);

expect(attemptToURIDecode(encodedName)).toBe(originalName);
expect(attemptToURIDecode(reactRouterDecoded)).toBe(originalName);
});

test('returns wrong decoded value for %25 sequence', () => {
const originalName = 'test%25';

const encodedName = encodeURIComponent(originalName);
// react router v5 automatically decodes route match params
const reactRouterDecoded = decodeURI(encodedName);

expect(attemptToURIDecode(encodedName)).toBe(originalName);
expect(attemptToURIDecode(reactRouterDecoded)).not.toBe(originalName);
});

test('returns wrong decoded value for % with other escaped characters', () => {
const originalName = 'test%?#';

const encodedName = encodeURIComponent(originalName);
// react router v5 automatically decodes route match params
const reactRouterDecoded = decodeURI(encodedName);

expect(attemptToURIDecode(encodedName)).toBe(originalName);
expect(attemptToURIDecode(reactRouterDecoded)).not.toBe(originalName);
});

test("doesn't convert undefined to a string", () => {
expect(attemptToURIDecode(undefined)).toBeUndefined();
});
8 changes: 5 additions & 3 deletions src/plugins/es_ui_shared/public/url/attempt_to_uri_decode.ts
Expand Up @@ -19,12 +19,14 @@

/*
* Use this function with any match params coming from react router to safely decode values.
* https://github.com/elastic/kibana/pull/81664
* After an update to react router v6, this functions should be deprecated.
* Known issue for navigation with special characters in paths
* https://github.com/elastic/kibana/issues/82440
*/
export const attemptToURIDecode = (value: string) => {
export const attemptToURIDecode = (value?: string): string | undefined => {
let result = value;
try {
result = decodeURIComponent(value);
result = value ? decodeURIComponent(value) : value;
} catch (e) {
// do nothing
}
Expand Down
@@ -0,0 +1,74 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import React from 'react';
import { act } from 'react-dom/test-utils';
import { registerTestBed, TestBed, TestBedConfig } from '../../../../../test_utils';
import { App } from '../../../public/application/app';
import { TestSubjects } from '../helpers';
import { createBreadcrumbsMock } from '../../../public/application/services/breadcrumbs.mock';
import { KibanaContextProvider } from '../../../../../../src/plugins/kibana_react/public/context';

const breadcrumbService = createBreadcrumbsMock();

const AppWithContext = (props: any) => {
return (
<KibanaContextProvider services={{ breadcrumbService }}>
<App {...props} />
</KibanaContextProvider>
);
};

const getTestBedConfig = (initialEntries: string[]): TestBedConfig => ({
memoryRouter: {
initialEntries,
},
defaultProps: {
getUrlForApp: () => {},
navigateToApp: () => {},
},
});

const initTestBed = (initialEntries: string[]) =>
registerTestBed(AppWithContext, getTestBedConfig(initialEntries))();

export interface AppTestBed extends TestBed<TestSubjects> {
actions: {
clickPolicyNameLink: () => void;
clickCreatePolicyButton: () => void;
};
}

export const setup = async (initialEntries: string[]): Promise<AppTestBed> => {
const testBed = await initTestBed(initialEntries);

const clickPolicyNameLink = async () => {
const { component, find } = testBed;
await act(async () => {
find('policyTablePolicyNameLink').simulate('click', { button: 0 });
});
component.update();
};

const clickCreatePolicyButton = async () => {
const { component, find } = testBed;
await act(async () => {
find('createPolicyButton').simulate('click', { button: 0 });
});
component.update();
};

return {
...testBed,
actions: { clickPolicyNameLink, clickCreatePolicyButton },
};
};

export const getEncodedPolicyEditPath = (policyName: string): string =>
`/policies/edit/${encodeURIComponent(policyName)}`;

export const getDoubleEncodedPolicyEditPath = (policyName: string): string =>
encodeURI(getEncodedPolicyEditPath(policyName));