Skip to content

Commit

Permalink
[Workspace] Fix workspace name duplication check (opensearch-project#…
Browse files Browse the repository at this point in the history
…6776)

* fix workspace name duplication check

Signed-off-by: Hailong Cui <ihailong@amazon.com>

* Changeset file for PR opensearch-project#6776 created/updated

---------

Signed-off-by: Hailong Cui <ihailong@amazon.com>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and LDrago27 committed Jun 3, 2024
1 parent 5a5d8ad commit 430a990
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6776.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Fix workspace name duplication check ([#6776](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6776))
87 changes: 87 additions & 0 deletions src/plugins/workspace/server/integration_tests/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,43 @@ describe('workspace service api integration test', () => {
expect(result.body.success).toEqual(true);
expect(typeof result.body.result.id).toBe('string');
});

it('create workspace failed when name duplicate', async () => {
let result: any = await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: omitId(testWorkspace),
})
.expect(200);

expect(result.body.success).toEqual(true);

await opensearchServer.opensearch.getClient().indices.refresh({ index: '.kibana' });

// same name
result = await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: omitId(testWorkspace),
})
.expect(200);

expect(result.body.success).toEqual(false);
expect(result.body.error).toEqual(
'workspace name has already been used, try with a different name'
);

// to verify workspace name is not interpreted as we have enclosed the name with double quotes
result = await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: { ...omitId(testWorkspace), name: 'test test_workspace' },
})
.expect(200);

expect(result.body.success).toEqual(true);
});

it('get', async () => {
const result = await osdTestServer.request
.post(root, `/api/workspaces`)
Expand Down Expand Up @@ -123,6 +160,56 @@ describe('workspace service api integration test', () => {
expect(getResult.body.success).toEqual(true);
expect(getResult.body.result.name).toEqual('updated');
});

it('update workspace failed when new name is duplicate', async () => {
const result: any = await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: { ...omitId(testWorkspace), name: 'foo' },
})
.expect(200);

await osdTestServer.request
.post(root, `/api/workspaces`)
.send({
attributes: { ...omitId(testWorkspace), name: 'bar baz' },
})
.expect(200);

const updateResult = await osdTestServer.request
.put(root, `/api/workspaces/${result.body.result.id}`)
.send({
attributes: {
...omitId(testWorkspace),
name: 'bar baz',
},
})
.expect(200);

expect(updateResult.body.success).toEqual(false);
expect(updateResult.body.error).toEqual(
'workspace name has already been used, try with a different name'
);

await osdTestServer.request
.put(root, `/api/workspaces/${result.body.result.id}`)
.send({
attributes: {
...omitId(testWorkspace),
name: 'bar',
},
})
.expect(200);

const getResult = await osdTestServer.request.get(
root,
`/api/workspaces/${result.body.result.id}`
);

expect(getResult.body.success).toEqual(true);
expect(getResult.body.result.name).toEqual('bar');
});

it('delete', async () => {
const result: any = await osdTestServer.request
.post(root, `/api/workspaces`)
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
const existingWorkspaceRes = await this.getScopedClientWithoutPermission(requestDetail)?.find(
{
type: WORKSPACE_TYPE,
search: attributes.name,
search: `"${attributes.name}"`,
searchFields: ['name'],
}
);
Expand Down Expand Up @@ -184,7 +184,7 @@ export class WorkspaceClient implements IWorkspaceClientImpl {
requestDetail
)?.find({
type: WORKSPACE_TYPE,
search: attributes.name,
search: `"${attributes.name}"`,
searchFields: ['name'],
fields: ['_id'],
});
Expand Down

0 comments on commit 430a990

Please sign in to comment.