-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lin Wang <[email protected]>
- Loading branch information
Showing
6 changed files
with
153 additions
and
1 deletion.
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
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-analytics, 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-analytics, 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
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(), | ||
}; |