Skip to content

Commit

Permalink
feat: add function test for workspace CRUD routes
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Sep 20, 2023
1 parent fb2b21e commit 0039087
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 23 deletions.
10 changes: 1 addition & 9 deletions src/plugins/workspace/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,7 @@ export function registerRoutes({
return res.ok({ body: result });
}
return res.ok({
body: {
...result,
result: {
...result.result,
workspaces: result.result.workspaces.map((workspace) => ({
...workspace,
})),
},
},
body: result,
});
})
);
Expand Down
1 change: 1 addition & 0 deletions test/api_integration/apis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ export default function ({ loadTestFile }) {
loadTestFile(require.resolve('./stats'));
loadTestFile(require.resolve('./ui_metric'));
loadTestFile(require.resolve('./telemetry'));
loadTestFile(require.resolve('./workspace'));
});
}
121 changes: 107 additions & 14 deletions test/api_integration/apis/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import expect from '@osd/expect';
import { WorkspaceAttribute } from 'opensearch-dashboards/server';
import { omit } from 'lodash';
import { FtrProviderContext } from '../../ftr_provider_context';

const testWorkspace: WorkspaceAttribute = {
Expand All @@ -15,25 +16,117 @@ const testWorkspace: WorkspaceAttribute = {

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');
const opensearch = getService('legacyOpenSearch');

const MILLISECOND_IN_WEEK = 1000 * 60 * 60 * 24 * 7;

describe('Workspace CRUD apis', () => {
it('basic CRUD', async () => {
const resp = await supertest
.post(`/api/workspaces`)
afterEach(async () => {
const listResult = await supertest
.post(`/api/workspaces/_list`)
.send({
page: 1,
})
.set('osd-xsrf', 'opensearch-dashboards')
.send(
JSON.stringify({
attributes: testWorkspace,
})
.expect(200);
await Promise.all(
listResult.body.result.workspaces.map((item: WorkspaceAttribute) =>
supertest
.delete(`/api/workspaces/${item.id}`)
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200)
)
);
});
it('create', async () => {
await supertest
.post(`/api/workspaces`)
.send({
attributes: testWorkspace,
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(400);

const result: any = await supertest
.post(`/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);

expect(result.body.success).equal(true);
expect(result.body.result.id).to.be.a('string');
});
it('get', async () => {
const result = await supertest
.post(`/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);

const getResult = await supertest.get(`/api/workspaces/${result.body.result.id}`);
expect(getResult.body.result.name).equal(testWorkspace.name);
});
it('update', async () => {
const result: any = await supertest
.post(`/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);

expect(resp.body).to.be.an('array');
expect(resp.body.length).to.be.above(0);
expect(resp.body[0].status).to.be('not_installed');
await supertest
.put(`/api/workspaces/${result.body.result.id}`)
.send({
attributes: {
...omit(testWorkspace, 'id'),
name: 'updated',
},
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);

const getResult = await supertest.get(`/api/workspaces/${result.body.result.id}`);

expect(getResult.body.success).equal(true);
expect(getResult.body.result.name).equal('updated');
});
it('delete', async () => {
const result: any = await supertest
.post(`/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);

await supertest
.delete(`/api/workspaces/${result.body.result.id}`)
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);

const getResult = await supertest.get(`/api/workspaces/${result.body.result.id}`);

expect(getResult.body.success).equal(false);
});
it('list', async () => {
await supertest
.post(`/api/workspaces`)
.send({
attributes: omit(testWorkspace, 'id'),
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);

const listResult = await supertest
.post(`/api/workspaces/_list`)
.send({
page: 1,
})
.set('osd-xsrf', 'opensearch-dashboards')
.expect(200);
expect(listResult.body.result.total).equal(1);
});
});
}).tags('is:workspace');
}

0 comments on commit 0039087

Please sign in to comment.