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

[Workspace] Workspace assets page crashed after library_write permission revoked #8648

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/8648.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace] Workspace assets page crashed after library_write permission revoked ([#8648](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8648))
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,13 @@ export class DuplicateResultFlyout extends React.Component<DuplicateResultFlyout
<EuiFlyout ownFocus onClose={onClose} size="s">
<EuiFlyoutHeader hasBorder>
<EuiTitle size="s">
<FormattedMessage
id="savedObjectsManagement.copyResult.title"
defaultMessage="Copy saved objects to {workspaceName}"
values={{ workspaceName }}
/>
<h2>
<FormattedMessage
id="savedObjectsManagement.copyResult.title"
defaultMessage="Copy saved objects to {workspaceName}"
values={{ workspaceName }}
/>
</h2>
</EuiTitle>
</EuiFlyoutHeader>
<EuiFlyoutBody>{this.copyResult({ failedCopies, successfulCopies })}</EuiFlyoutBody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,38 @@ describe('SavedObjectsTable', () => {
});
});

it('should catch error when duplicating selected object is fail', async () => {
it('should catch error when duplicating selected object is failed', async () => {
const component = shallowRender({ applications, workspaces });
component.setState({ isShowingDuplicateModal: true });

const mockCopy = jest.fn().mockResolvedValue({ error: 'error' });
workspaces.client$.next({ copy: mockCopy });
const client = workspaces.client$.getValue();

// Ensure all promises resolve
await new Promise((resolve) => process.nextTick(resolve));
// Ensure the state changes are reflected
component.update();

await component.instance().onDuplicate(mockSelectedSavedObjects, false, 'workspace2', 'bar');

expect(client?.copy).toHaveBeenCalledWith(
[
{ id: '1', type: 'dashboard' },
{ id: '2', type: 'dashboard' },
],
'workspace2',
false
);
component.update();

expect(notifications.toasts.addDanger).toHaveBeenCalledWith({
title: 'Unable to copy 2 saved objects.',
text: 'error',
});
});

it('should show error toast when copy is fail', async () => {
const component = shallowRender({ applications, workspaces });
component.setState({ isShowingDuplicateModal: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -761,13 +761,14 @@
const { notifications, workspaces, useUpdatedUX } = this.props;
const workspaceClient = workspaces.client$.getValue();

const showErrorNotification = () => {
const showErrorNotification = (text?: string) => {

Check warning on line 764 in src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx#L764

Added line #L764 was not covered by tests
notifications.toasts.addDanger({
title: i18n.translate('savedObjectsManagement.objectsTable.duplicate.dangerNotification', {
defaultMessage:
'Unable to copy {useUpdatedUX, select, true {{errorCount, plural, one {# asset} other {# assets}}} other {{errorCount, plural, one {# saved object} other {# saved objects}}}}.',
values: { errorCount: savedObjects.length, useUpdatedUX },
}),
...(text && { text }),
});
};
if (!workspaceClient) {
Expand All @@ -781,18 +782,22 @@
targetWorkspace,
includeReferencesDeep
);

this.setState({
isShowingDuplicateResultFlyout: true,
failedCopies: result.success ? [] : result.errors,
successfulCopies: result.successCount > 0 ? result.successResults : [],
targetWorkspaceName,
});
if (result?.error) {
showErrorNotification(result.error);

Check warning on line 786 in src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx#L786

Added line #L786 was not covered by tests
} else {
this.setState({

Check warning on line 788 in src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx#L788

Added line #L788 was not covered by tests
isShowingDuplicateResultFlyout: true,
failedCopies: result?.errors || [],
successfulCopies: result?.successCount > 0 ? result.successResults : [],
targetWorkspaceName,
});
}
} catch (e) {
showErrorNotification();
} finally {
this.hideDuplicateModal();
await this.refreshObjects();

Check warning on line 799 in src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/saved_objects_management/public/management_section/objects_table/saved_objects_table.tsx#L798-L799

Added lines #L798 - L799 were not covered by tests
}
this.hideDuplicateModal();
await this.refreshObjects();
};

renderDuplicateModal() {
Expand Down
Loading