From aa57253db46f190238277bf1b3adc3342e64f015 Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Mon, 13 May 2024 11:40:45 +0800 Subject: [PATCH 1/2] fix workspace name duplication check Signed-off-by: Hailong Cui --- .../server/integration_tests/routes.test.ts | 87 +++++++++++++++++++ .../workspace/server/workspace_client.ts | 4 +- 2 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/plugins/workspace/server/integration_tests/routes.test.ts b/src/plugins/workspace/server/integration_tests/routes.test.ts index cc1ef5f756b3..1e0530217f21 100644 --- a/src/plugins/workspace/server/integration_tests/routes.test.ts +++ b/src/plugins/workspace/server/integration_tests/routes.test.ts @@ -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`) @@ -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`) diff --git a/src/plugins/workspace/server/workspace_client.ts b/src/plugins/workspace/server/workspace_client.ts index 72609519d2f7..a906e3548cab 100644 --- a/src/plugins/workspace/server/workspace_client.ts +++ b/src/plugins/workspace/server/workspace_client.ts @@ -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'], } ); @@ -184,7 +184,7 @@ export class WorkspaceClient implements IWorkspaceClientImpl { requestDetail )?.find({ type: WORKSPACE_TYPE, - search: attributes.name, + search: `"${attributes.name}"`, searchFields: ['name'], fields: ['_id'], }); From 485084bc59b876d83e459430b9ad5b6ad48605a0 Mon Sep 17 00:00:00 2001 From: "opensearch-changeset-bot[bot]" <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> Date: Mon, 13 May 2024 03:49:42 +0000 Subject: [PATCH 2/2] Changeset file for PR #6776 created/updated --- changelogs/fragments/6776.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/6776.yml diff --git a/changelogs/fragments/6776.yml b/changelogs/fragments/6776.yml new file mode 100644 index 000000000000..eb976800efa4 --- /dev/null +++ b/changelogs/fragments/6776.yml @@ -0,0 +1,2 @@ +fix: +- Fix workspace name duplication check ([#6776](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/6776)) \ No newline at end of file