Skip to content

Commit

Permalink
feat: add test case and add restrict on create method
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Apr 17, 2024
1 parent cd77dba commit 1522637
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,38 @@ describe('workspace_id_consumer integration test', () => {
});
});

it('create disallowed types within workspace', async () => {
const createDataSourceResult = await osdTestServer.request
.post(root, `/w/${createdFooWorkspace.id}/api/saved_objects/${dataSource.type}`)
.send({
attributes: dataSource.attributes,
})
.expect(400);

expect(createDataSourceResult.body).toMatchInlineSnapshot(`
Object {
"error": "Bad Request",
"message": "Unsupport type in workspace: 'data-source' is not allowed to create in workspace.",
"statusCode": 400,
}
`);

const createConfigResult = await osdTestServer.request
.post(root, `/w/${createdFooWorkspace.id}/api/saved_objects/config`)
.send({
attributes: dataSource.attributes,
})
.expect(400);

expect(createConfigResult.body).toMatchInlineSnapshot(`
Object {
"error": "Bad Request",
"message": "Unsupport type in workspace: 'config' is not allowed to create in workspace.",
"statusCode": 400,
}
`);
});

it('bulk create', async () => {
await clearFooAndBar();
const createResultFoo = await osdTestServer.request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ describe('WorkspaceIdConsumerWrapper', () => {

expect(mockedClient.create.mock.calls[0][2]?.hasOwnProperty('workspaces')).toEqual(false);
});

it(`Should throw error when trying to create disallowed type in workspace`, async () => {
expect(() =>
wrapperClient.create(
DATA_SOURCE_SAVED_OBJECT_TYPE,
{
name: 'foo',
},

{
workspaces: ['foo'],
}
)
).toThrowErrorMatchingInlineSnapshot(
`"Unsupport type in workspace: 'data-source' is not allowed to create in workspace."`
);

expect(() =>
wrapperClient.create(
'config',
{
name: 'foo',
},

{
workspaces: ['foo'],
}
)
).toThrowErrorMatchingInlineSnapshot(
`"Unsupport type in workspace: 'config' is not allowed to create in workspace."`
);
});
});

describe('bulkCreate', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,23 @@ export class WorkspaceIdConsumerWrapper {
public wrapperFactory: SavedObjectsClientWrapperFactory = (wrapperOptions) => {
return {
...wrapperOptions.client,
create: <T>(type: string, attributes: T, options: SavedObjectsCreateOptions = {}) =>
wrapperOptions.client.create(
create: <T>(type: string, attributes: T, options: SavedObjectsCreateOptions = {}) => {
const { workspaces } = this.formatWorkspaceIdParams(wrapperOptions.request, options);
if (workspaces?.length && (this.isDataSourceType(type) || this.isConfigType(type))) {
// For 2.14, data source can only be created without workspace info
// config can not be created inside a workspace
throw SavedObjectsErrorHelpers.decorateBadRequestError(
new Error(`'${type}' is not allowed to create in workspace.`),
'Unsupport type in workspace'
);
}

return wrapperOptions.client.create(
type,
attributes,
this.formatWorkspaceIdParams(wrapperOptions.request, options)
),
);
},
bulkCreate: async <T = unknown>(
objects: Array<SavedObjectsBulkCreateObject<T>>,
options: SavedObjectsCreateOptions = {}
Expand Down

0 comments on commit 1522637

Please sign in to comment.