Skip to content

Commit

Permalink
Add ut and fix integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Aug 28, 2024
1 parent c2aa002 commit 1d9bf60
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const testWorkspace: WorkspaceAttribute = {
id: 'fake_id',
name: 'test_workspace',
description: 'test_workspace_description',
features: ['use-case-all'],
};

describe('workspace service api integration test', () => {
Expand Down
129 changes: 129 additions & 0 deletions src/plugins/workspace/server/routes/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import supertest from 'supertest';
import { UnwrapPromise } from '@osd/utility-types';

import { setupServer } from '../../../../core/server/test_utils';
import { loggingSystemMock } from '../../../../core/server/mocks';
// eslint-disable-next-line @osd/eslint/no-restricted-paths
import { dynamicConfigServiceMock } from '../../../../core/server/config';

import { workspaceClientMock } from '../workspace_client.mock';

import { registerRoutes, WORKSPACES_API_BASE_URL } from './index';

type SetupServerReturn = UnwrapPromise<ReturnType<typeof setupServer>>;
const mockDynamicConfigService = dynamicConfigServiceMock.createInternalStartContract();

describe(`Workspace routes`, () => {
let server: SetupServerReturn['server'];
let httpSetup: SetupServerReturn['httpSetup'];

beforeEach(async () => {
({ server, httpSetup } = await setupServer());

const router = httpSetup.createRouter('');

registerRoutes({
router,
client: workspaceClientMock,
logger: loggingSystemMock.create().get(),
maxImportExportSize: Number.MAX_SAFE_INTEGER,
isPermissionControlEnabled: false,
});

await server.start({ dynamicConfigService: mockDynamicConfigService });
});

afterEach(async () => {
await server.stop();
});

it('creates a workspace successfully', async () => {
const result = await supertest(httpSetup.server.listener)
.post(WORKSPACES_API_BASE_URL)
.send({
attributes: {
name: 'Observability',
features: ['use-case-observability'],
},
})
.expect(200);
expect(result.body).toEqual({ id: expect.any(String) });
expect(workspaceClientMock.create).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
name: 'Observability',
features: ['use-case-observability'],
})
);
});

describe('feature validation', () => {
it('returns 400 when no features is provided during workspace creation', async () => {
await supertest(httpSetup.server.listener)
.post(WORKSPACES_API_BASE_URL)
.send({
attributes: {
name: 'Observability',
},
})
.expect(400);
});
it('returns 400 when no valid use case is provided during workspace creation', async () => {
const result = await supertest(httpSetup.server.listener)
.post(WORKSPACES_API_BASE_URL)
.send({
attributes: {
name: 'Observability',
features: ['use-case-valid'],
},
})
.expect(400);
expect(result.body.message).toEqual(
'[request body.attributes.features]: At least one use case is required. Valid options: use-case-all, use-case-observability, use-case-security-analytics, use-case-analytics, use-case-search'
);
});
it('returns 400 when multiple use cases are provided during workspace creation', async () => {
const result = await supertest(httpSetup.server.listener)
.post(WORKSPACES_API_BASE_URL)
.send({
attributes: {
name: 'Observability',
features: ['use-case-observability', 'use-case-all'],
},
})
.expect(400);
expect(result.body.message).toEqual(
'[request body.attributes.features]: Only one use case is allowed per workspace.'
);
});
it('returns 400 when no valid use case is provided during workspace update', async () => {
const result = await supertest(httpSetup.server.listener)
.put(`${WORKSPACES_API_BASE_URL}/mock-workspace-id`)
.send({
attributes: {
name: 'Observability',
features: ['feature1', 'feature2'],
},
})
.expect(400);
expect(result.body.message).toEqual(
'[request body.attributes.features]: At least one use case is required. Valid options: use-case-all, use-case-observability, use-case-security-analytics, use-case-analytics, use-case-search'
);
});
it('updates workspace name successfully without modifying features', async () => {
await supertest(httpSetup.server.listener)
.put(`${WORKSPACES_API_BASE_URL}/mock-workspace-id`)
.send({
attributes: {
name: 'Observability',
},
})
.expect(200);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const advancedSettings: Omit<SavedObject, 'id'> = {
interface WorkspaceAttributes {
id: string;
name?: string;
features?: string[];
}

describe('saved_objects_wrapper_for_check_workspace_conflict integration test', () => {
Expand Down Expand Up @@ -76,9 +77,11 @@ describe('saved_objects_wrapper_for_check_workspace_conflict integration test',

createdFooWorkspace = await createWorkspace({
name: 'foo',
features: ['use-case-all'],
}).then((resp) => resp.body.result);
createdBarWorkspace = await createWorkspace({
name: 'bar',
features: ['use-case-all'],
}).then((resp) => resp.body.result);
}, 30000);
afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const dashboard: Omit<SavedObject, 'id'> = {
interface WorkspaceAttributes {
id: string;
name?: string;
features?: string[];
}

describe('workspace_id_consumer integration test', () => {
Expand Down Expand Up @@ -56,11 +57,13 @@ describe('workspace_id_consumer integration test', () => {

createdFooWorkspace = await createWorkspace({
name: 'foo',
features: ['use-case-all'],
}).then((resp) => {
return resp.body.result;
});
createdBarWorkspace = await createWorkspace({
name: 'bar',
features: ['use-case-all'],
}).then((resp) => resp.body.result);
}, 30000);
afterAll(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('workspace ui settings saved object client wrapper', () => {
globalUiSettingsClient = osd.coreStart.uiSettings.asScopedToClient(savedObjectsClient);

const res = await osdTestServer.request.post(osd.root, '/api/workspaces').send({
attributes: { name: 'test workspace' },
attributes: { name: 'test workspace', features: ['use-case-all'] },
});
testWorkspace = res.body.result;
}, 30000);
Expand Down
16 changes: 16 additions & 0 deletions src/plugins/workspace/server/workspace_client.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export const workspaceClientMock = {
setup: jest.fn(),
setSavedObjects: jest.fn(),
setUiSettings: jest.fn(),
create: jest.fn().mockResolvedValue({ id: 'mock-workspace-id' }),
list: jest.fn(),
get: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
destroy: jest.fn(),
};

0 comments on commit 1d9bf60

Please sign in to comment.