forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Workspace]Validate features parameter in workspace create and update…
… API (opensearch-project#7884) * Add validate for features field in workspace create and update API Signed-off-by: Lin Wang <[email protected]> * Changeset file for PR opensearch-project#7884 created/updated * Changeset file for PR opensearch-project#7884 created/updated * Move router outside align with other plugins Signed-off-by: Lin Wang <[email protected]> * Add ut and fix integration tests Signed-off-by: Lin Wang <[email protected]> * Import use case id from default nav groups Signed-off-by: Lin Wang <[email protected]> * Fix workspace routes UT Signed-off-by: Lin Wang <[email protected]> * Share feature config generator between publicn and server Signed-off-by: Lin Wang <[email protected]> * Fix osd server crashed due to import from public Signed-off-by: Lin Wang <[email protected]> --------- Signed-off-by: Lin Wang <[email protected]> Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f7a168d
commit b8ac23e
Showing
16 changed files
with
200 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
feat: | ||
- [Workspace]Validate features parameter in workspace create and update API ([#7884](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7884)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-essentials, 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-essentials, 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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; |