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] Fix workspace name duplication check #6776

Merged
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/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}"`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this search for a workspace including this exact phrase or one that is an exact match?

Copy link
Collaborator Author

@Hailong-am Hailong-am May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on the field type. For my case, since workspace.name filed type is keyword, it will convert to TermQuery that's exact match. To make the PR description more clear, it add an example query with profile enabled.

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
Loading