Skip to content

Commit

Permalink
Add toast handling for purge cache action (opensearch-project#1824)
Browse files Browse the repository at this point in the history
  • Loading branch information
derek-ho authored Mar 13, 2024
1 parent c80b64a commit 942b2f7
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 32 deletions.
21 changes: 19 additions & 2 deletions public/apps/configuration/panels/get-started.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
EuiSteps,
EuiText,
EuiTitle,
EuiGlobalToastList,
} from '@elastic/eui';
import React from 'react';
import { FormattedMessage } from '@osd/i18n/react';
Expand All @@ -33,6 +34,8 @@ import { Action } from '../types';
import { ResourceType } from '../../../../common';
import { API_ENDPOINT_CACHE, DocLinks } from '../constants';
import { ExternalLink, ExternalLinkButton } from '../utils/display-utils';
import { httpDelete } from '../utils/request-utils';
import { createSuccessToast, createUnknownErrorToast, useToastState } from '../utils/toast-utils';

const addBackendStep = {
title: 'Add backends',
Expand Down Expand Up @@ -162,6 +165,7 @@ export function GetStarted(props: AppDependencies) {
} else {
steps = setOfSteps;
}
const [toasts, addToast, removeToast] = useToastState();

return (
<>
Expand Down Expand Up @@ -229,8 +233,20 @@ export function GetStarted(props: AppDependencies) {
<EuiButton
iconType="refresh"
fill
onClick={() => {
props.coreStart.http.delete(API_ENDPOINT_CACHE);
data-test-subj="purge-cache"
onClick={async () => {
try {
await httpDelete(props.coreStart.http, API_ENDPOINT_CACHE);
addToast(
createSuccessToast(
'cache-flush-success',
'Cache purge successful',
'Cache purge successful'
)
);
} catch (err) {
addToast(createUnknownErrorToast('cache-flush-failed', 'purge cache'));
}
}}
>
Purge cache
Expand Down Expand Up @@ -274,6 +290,7 @@ export function GetStarted(props: AppDependencies) {
</EuiText>
</EuiPanel>
</div>
<EuiGlobalToastList toasts={toasts} toastLifeTimeMs={10000} dismissToast={removeToast} />
</>
);
}
32 changes: 2 additions & 30 deletions public/apps/configuration/panels/tenant-list/configure_tab1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,66 +14,38 @@
*/

import {
EuiBadge,
EuiButton,
EuiButtonEmpty,
EuiFlexGroup,
EuiFlexItem,
EuiInMemoryTable,
EuiLink,
EuiPageBody,
EuiPageContent,
EuiPageContentHeader,
EuiPageContentHeaderSection,
EuiPageHeader,
EuiText,
EuiTitle,
EuiGlobalToastList,
EuiSwitch,
Query,
EuiHorizontalRule,
EuiFormRow,
EuiDescribedFormGroup,
EuiSpacer,
EuiCheckbox,
EuiModal,
EuiModalHeader,
EuiModalHeaderTitle,
EuiModalBody,
EuiModalFooter,
EuiCodeBlock,
EuiCallOut,
EuiBottomBar,
EuiComboBox,
EuiIcon,
EuiPanel,
} from '@elastic/eui';
import { ChangeEvent } from 'react';
import React, { ReactNode, useState, useCallback } from 'react';
import React, { ReactNode, useState } from 'react';
import { SaveChangesModalGenerator } from './save_changes_modal';
import { AppDependencies } from '../../../types';
import { displayBoolean } from '../../utils/display-utils';
import { updateAuditLogging } from '../../utils/audit-logging-utils';
import { AuditLoggingSettings } from '../audit-logging/types';
import { AuthInfo } from '../../../../types';
import { updateTenancyConfig } from '../../utils/tenancy-config_util';
import { TenancyConfigSettings } from '../tenancy-config/types';
import { getAuthInfo } from '../../../../utils/auth-info-utils';
import {
fetchTenants,
transformTenantData,
updateTenancyConfiguration,
updateTenant,
} from '../../utils/tenant-utils';
import { Action, Tenant } from '../../types';
import { showTableStatusMessage } from '../../utils/loading-spinner-utils';
import { useContextMenuState } from '../../utils/context-menu';
import { TenantEditModal } from './edit-modal';
import { Tenant } from '../../types';
import {
createTenancyErrorToast,
createTenancySuccessToast,
createUnknownErrorToast,
getSuccessToastMessage,
useToastState,
} from '../../utils/toast-utils';
import { getDashboardsInfo } from '../../../../utils/dashboards-info-utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ exports[`Get started (landing page) renders when backend configuration is disabl
By default, the security plugin caches authenticated users, along with their roles and permissions. This option will purge cached users, roles and permissions.
</p>
<EuiButton
data-test-subj="purge-cache"
fill={true}
iconType="refresh"
onClick={[Function]}
Expand Down Expand Up @@ -243,6 +244,12 @@ exports[`Get started (landing page) renders when backend configuration is disabl
</EuiText>
</EuiPanel>
</div>
<EuiGlobalToastList
dismissToast={[MockFunction]}
side="right"
toastLifeTimeMs={10000}
toasts={Array []}
/>
</Fragment>
`;

Expand Down Expand Up @@ -502,6 +509,7 @@ exports[`Get started (landing page) renders when backend configuration is enable
By default, the security plugin caches authenticated users, along with their roles and permissions. This option will purge cached users, roles and permissions.
</p>
<EuiButton
data-test-subj="purge-cache"
fill={true}
iconType="refresh"
onClick={[Function]}
Expand Down Expand Up @@ -556,5 +564,11 @@ exports[`Get started (landing page) renders when backend configuration is enable
</EuiText>
</EuiPanel>
</div>
<EuiGlobalToastList
dismissToast={[MockFunction]}
side="right"
toastLifeTimeMs={10000}
toasts={Array []}
/>
</Fragment>
`;
51 changes: 51 additions & 0 deletions public/apps/configuration/panels/test/get-started.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ import { Action } from '../../types';
import { ResourceType } from '../../../../../common';
import { buildHashUrl } from '../../utils/url-builder';
import { GetStarted } from '../get-started';
import * as ToastUtils from '../../utils/toast-utils'; // Import all functions from toast-utils
import * as RequestUtils from '../../utils/request-utils'; // Import all functions from request-utils

jest.mock('../../utils/toast-utils', () => ({
createSuccessToast: jest.fn(),
createUnknownErrorToast: jest.fn(),
useToastState: jest.fn().mockReturnValue([[], jest.fn(), jest.fn()]),
}));

jest.mock('../../utils/request-utils', () => ({
httpDelete: jest.fn(),
}));

describe('Get started (landing page)', () => {
const mockCoreStart = {
Expand Down Expand Up @@ -71,6 +83,7 @@ describe('Get started (landing page)', () => {
config={config as any}
/>
);
jest.clearAllMocks();
});

it('Review authentication and authorization button click', () => {
Expand Down Expand Up @@ -120,4 +133,42 @@ describe('Get started (landing page)', () => {
expect(window.location.hash).toBe(buildHashUrl(ResourceType.auditLogging));
});
});

describe('Tests purge cache button', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(
<GetStarted
coreStart={mockCoreStart as any}
navigation={{} as any}
params={{} as any}
config={config as any}
/>
);
jest.clearAllMocks();
});

it('Purge cache button fails', async () => {
const button = wrapper.find('[data-test-subj="purge-cache"]');
expect(button).toHaveLength(1);

// Failure case: Mock httpDelete to reject
jest
.spyOn(RequestUtils, 'httpDelete')
.mockRejectedValueOnce(new Error('Failed to purge cache'));

await button.props().onClick(); // Simulate button click
expect(ToastUtils.createUnknownErrorToast).toHaveBeenCalledTimes(1);
});

it('Purge cache button works', async () => {
const button = wrapper.find('[data-test-subj="purge-cache"]');
expect(button).toHaveLength(1);

// Success case: Mock httpDelete to resolve
jest.spyOn(RequestUtils, 'httpDelete').mockResolvedValueOnce('nice');
await button.props().onClick(); // Simulate button click
expect(ToastUtils.createSuccessToast).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit 942b2f7

Please sign in to comment.