Skip to content

Commit

Permalink
Merge branch 'main' into multi_tenacy_check
Browse files Browse the repository at this point in the history
  • Loading branch information
Hailong-am authored Mar 12, 2024
2 parents 55f6344 + 4b89ad0 commit d877522
Show file tree
Hide file tree
Showing 6 changed files with 554 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Multiple Datasource] Test connection schema validation for registered auth types ([#6109](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6109))
- [Workspace] Consume workspace id in saved object client ([#6014](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6014))
- [Multiple Datasource] Export DataSourcePluginRequestContext at top level for plugins to use ([#6108](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6108))

- [Workspace] Add delete saved objects by workspace functionality([#6013](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6013))
- [Workspace] Add a workspace client in workspace plugin ([#6094](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6094))

### 🐛 Bug Fixes

Expand Down
18 changes: 17 additions & 1 deletion src/plugins/workspace/public/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { coreMock } from '../../../core/public/mocks';
import { workspaceClientMock, WorkspaceClientMock } from './workspace_client.mock';
import { chromeServiceMock, coreMock } from '../../../core/public/mocks';
import { WorkspacePlugin } from './plugin';

describe('Workspace plugin', () => {
const getSetupMock = () => ({
...coreMock.createSetup(),
chrome: chromeServiceMock.createSetupContract(),
});
beforeEach(() => {
WorkspaceClientMock.mockClear();
Object.values(workspaceClientMock).forEach((item) => item.mockClear());
});
it('#setup', async () => {
const setupMock = getSetupMock();
const workspacePlugin = new WorkspacePlugin();
await workspacePlugin.setup(setupMock);
expect(WorkspaceClientMock).toBeCalledTimes(1);
});

it('#call savedObjectsClient.setCurrentWorkspace when current workspace id changed', () => {
const workspacePlugin = new WorkspacePlugin();
const coreStart = coreMock.createStart();
Expand Down
7 changes: 5 additions & 2 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

import type { Subscription } from 'rxjs';
import { Plugin, CoreStart } from '../../../core/public';
import { Plugin, CoreStart, CoreSetup } from '../../../core/public';
import { WorkspaceClient } from './workspace_client';

export class WorkspacePlugin implements Plugin<{}, {}, {}> {
private coreStart?: CoreStart;
Expand All @@ -18,7 +19,9 @@ export class WorkspacePlugin implements Plugin<{}, {}, {}> {
});
}
}
public async setup() {
public async setup(core: CoreSetup) {
const workspaceClient = new WorkspaceClient(core.http, core.workspaces);
await workspaceClient.init();
return {};
}

Expand Down
25 changes: 25 additions & 0 deletions src/plugins/workspace/public/workspace_client.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export const workspaceClientMock = {
init: jest.fn(),
enterWorkspace: jest.fn(),
getCurrentWorkspaceId: jest.fn(),
getCurrentWorkspace: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
list: jest.fn(),
get: jest.fn(),
update: jest.fn(),
stop: jest.fn(),
};

export const WorkspaceClientMock = jest.fn(function () {
return workspaceClientMock;
});

jest.doMock('./workspace_client', () => ({
WorkspaceClient: WorkspaceClientMock,
}));
212 changes: 212 additions & 0 deletions src/plugins/workspace/public/workspace_client.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { httpServiceMock, workspacesServiceMock } from '../../../core/public/mocks';
import { WorkspaceClient } from './workspace_client';

const getWorkspaceClient = () => {
const httpSetupMock = httpServiceMock.createSetupContract();
const workspaceMock = workspacesServiceMock.createSetupContract();
return {
httpSetupMock,
workspaceMock,
workspaceClient: new WorkspaceClient(httpSetupMock, workspaceMock),
};
};

describe('#WorkspaceClient', () => {
it('#init', async () => {
const { workspaceClient, httpSetupMock, workspaceMock } = getWorkspaceClient();
await workspaceClient.init();
expect(workspaceMock.initialized$.getValue()).toEqual(true);
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#enterWorkspace', async () => {
const { workspaceClient, httpSetupMock, workspaceMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: false,
});
const result = await workspaceClient.enterWorkspace('foo');
expect(result.success).toEqual(false);
httpSetupMock.fetch.mockResolvedValue({
success: true,
});
const successResult = await workspaceClient.enterWorkspace('foo');
expect(workspaceMock.currentWorkspaceId$.getValue()).toEqual('foo');
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/foo', {
method: 'GET',
});
expect(successResult.success).toEqual(true);
});

it('#getCurrentWorkspaceId', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
});
await workspaceClient.enterWorkspace('foo');
expect(workspaceClient.getCurrentWorkspaceId()).toEqual({
success: true,
result: 'foo',
});
});

it('#getCurrentWorkspace', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
name: 'foo',
},
});
await workspaceClient.enterWorkspace('foo');
expect(await workspaceClient.getCurrentWorkspace()).toEqual({
success: true,
result: {
name: 'foo',
},
});
});

it('#create', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
name: 'foo',
workspaces: [],
},
});
await workspaceClient.create({
name: 'foo',
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces', {
method: 'POST',
body: JSON.stringify({
attributes: {
name: 'foo',
},
}),
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#delete', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
name: 'foo',
workspaces: [],
},
});
await workspaceClient.delete('foo');
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/foo', {
method: 'DELETE',
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#list', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
workspaces: [],
},
});
await workspaceClient.list({
perPage: 999,
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#get', async () => {
const { workspaceClient, httpSetupMock } = getWorkspaceClient();
await workspaceClient.get('foo');
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/foo', {
method: 'GET',
});
});

it('#update', async () => {
const { workspaceClient, httpSetupMock, workspaceMock } = getWorkspaceClient();
httpSetupMock.fetch.mockResolvedValue({
success: true,
result: {
workspaces: [
{
id: 'foo',
},
],
},
});
await workspaceClient.update('foo', {
name: 'foo',
});
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/foo', {
method: 'PUT',
body: JSON.stringify({
attributes: {
name: 'foo',
},
}),
});
expect(workspaceMock.workspaceList$.getValue()).toEqual([
{
id: 'foo',
},
]);
expect(httpSetupMock.fetch).toBeCalledWith('/api/workspaces/_list', {
method: 'POST',
body: JSON.stringify({
perPage: 999,
}),
});
});

it('#update with list gives error', async () => {
const { workspaceClient, httpSetupMock, workspaceMock } = getWorkspaceClient();
let callTimes = 0;
httpSetupMock.fetch.mockImplementation(async () => {
callTimes++;
if (callTimes > 1) {
return {
success: false,
error: 'Something went wrong',
};
}

return {
success: true,
};
});
await workspaceClient.update('foo', {
name: 'foo',
});
expect(workspaceMock.workspaceList$.getValue()).toEqual([]);
});
});
Loading

0 comments on commit d877522

Please sign in to comment.