Skip to content

Commit

Permalink
resolve tests
Browse files Browse the repository at this point in the history
Signed-off-by: Qxisylolo <[email protected]>
  • Loading branch information
Qxisylolo committed Nov 22, 2024
1 parent 327c62f commit 2f4dedd
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/core/types/saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export interface SavedObject<T = unknown> {
updated_at?: string;
error?: SavedObjectError;
/** {@inheritdoc SavedObjectAttributes} */
attributes: T | null;
attributes: T;
/** {@inheritdoc SavedObjectReference} */
references: SavedObjectReference[];
/** {@inheritdoc SavedObjectsMigrationVersion} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,24 @@ describe('WorkspaceSavedObjectsClientWrapper', () => {

describe('bulkGet', () => {
it('should throw forbidden error when user not permitted', async () => {
let error;
try {
await notPermittedSavedObjectedClient.bulkGet([
{ type: 'dashboard', id: 'inner-workspace-dashboard-1' },
]);
} catch (e) {
error = e;
}
expect(error).not.toBeUndefined();
expect(SavedObjectsErrorHelpers.isForbiddenError(error)).toBe(true);
const result = await notPermittedSavedObjectedClient.bulkGet([
{ type: 'dashboard', id: 'acl-controlled-dashboard-2' },
]);

error = undefined;
try {
await notPermittedSavedObjectedClient.bulkGet([
{ type: 'dashboard', id: 'acl-controlled-dashboard-2' },
]);
} catch (e) {
error = e;
}
expect(error).not.toBeUndefined();
expect(SavedObjectsErrorHelpers.isForbiddenError(error)).toBe(true);
expect(result.saved_objects).toEqual([
{
...result.saved_objects[0],
id: 'acl-controlled-dashboard-2',
type: 'dashboard',
attributes: {},
error: {
error: 'Invalid saved objects permission',
statusCode: 403,
message: 'Permission denied',
},
workspaces: [],
},
]);
});

it('should return consistent dashboard when user permitted', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -797,17 +797,17 @@ describe('WorkspaceSavedObjectsClientWrapper', () => {

expect(result.saved_objects).toEqual([
{
id: '',
id: 'not-permitted-dashboard',
type: 'dashboard',
attributes: null,
attributes: {},
error: {
error: 'Invalid saved objects permission',
error: 'Forbidden',
message: 'Invalid saved objects permission',
statusCode: 403,
message: 'Permission denied',
},
workspaces: [],
permissions: {},
references: [],
workspaces: [],
},
]);
});
Expand Down Expand Up @@ -854,36 +854,51 @@ describe('WorkspaceSavedObjectsClientWrapper', () => {
});
it('should validate data source or data connection workspace field', async () => {
const { wrapper } = generateWorkspaceSavedObjectsClientWrapper();
let errorCatched;
try {
await wrapper.bulkGet([
{
type: 'data-source',
id: 'workspace-1-data-source',
const result1 = await wrapper.bulkGet([
{
type: 'data-source',
id: 'workspace-1-data-source',
},
]);

expect(result1.saved_objects).toEqual([
{
id: 'workspace-1-data-source',
type: 'data-source',
attributes: {},
error: {
error: 'Forbidden',
message: 'Invalid data source permission, please associate it to current workspace',
statusCode: 403,
},
]);
} catch (e) {
errorCatched = e;
}
expect(errorCatched?.message).toEqual(
'Invalid data source permission, please associate it to current workspace'
);
workspaces: [],
references: [],
},
]);

try {
await wrapper.bulkGet([
{
type: 'data-connection',
id: 'workspace-1-data-connection',
const result2 = await wrapper.bulkGet([
{
type: 'data-connection',
id: 'workspace-1-data-connection',
},
]);

expect(result2.saved_objects).toEqual([
{
type: 'data-connection',
id: 'workspace-1-data-connection',
attributes: {},
error: {
error: 'Forbidden',
message: 'Invalid data source permission, please associate it to current workspace',
statusCode: 403,
},
]);
} catch (e) {
errorCatched = e;
}
expect(errorCatched?.message).toEqual(
'Invalid data source permission, please associate it to current workspace'
);
workspaces: [],
references: [],
},
]);

let result = await await wrapper.bulkGet([
let result = await wrapper.bulkGet([
{
type: 'data-source',
id: 'workspace-2-data-source',
Expand Down Expand Up @@ -971,46 +986,62 @@ describe('WorkspaceSavedObjectsClientWrapper', () => {
it('should throw permission error when tried to bulk get global data source or data connection', async () => {
const { wrapper, requestMock } = generateWorkspaceSavedObjectsClientWrapper();
updateWorkspaceState(requestMock, { requestWorkspaceId: undefined });
let errorCatched;
try {
await wrapper.bulkGet([{ type: 'data-source', id: 'global-data-source' }]);
} catch (e) {
errorCatched = e;
}
expect(errorCatched?.message).toEqual(
'Invalid data source permission, please associate it to current workspace'
);
try {
await wrapper.bulkGet([{ type: 'data-connection', id: 'global-data-connection' }]);
} catch (e) {
errorCatched = e;
}
expect(errorCatched?.message).toEqual(
'Invalid data source permission, please associate it to current workspace'
);

const result = await wrapper.bulkGet([{ type: 'data-source', id: 'global-data-source' }]);
expect(result.saved_objects).toEqual([
{
attributes: {},
id: 'global-data-source',
type: 'data-source',
references: [],
workspaces: [],
error: {
error: 'Forbidden',
message: 'Invalid data source permission, please associate it to current workspace',
statusCode: 403,
},
},
]);
});

it('should throw permission error when tried to bulk get a empty workspace global data source or data connection', async () => {
const { wrapper, requestMock } = generateWorkspaceSavedObjectsClientWrapper();
updateWorkspaceState(requestMock, { requestWorkspaceId: undefined });
let errorCatched;
try {
await wrapper.bulkGet([
{ type: 'data-source', id: 'global-data-source-empty-workspaces' },
]);
} catch (e) {
errorCatched = e;
}
expect(errorCatched?.message).toEqual(
'Invalid data source permission, please associate it to current workspace'
);
try {
await wrapper.bulkGet([
{ type: 'data-connection', id: 'global-data-connection-empty-workspaces' },
]);
} catch (e) {
errorCatched = e;
}
const result = await wrapper.bulkGet([
{ type: 'data-source', id: 'global-data-source-empty-workspaces' },
]);
expect(result.saved_objects).toEqual([
{
attributes: {},
id: 'global-data-source-empty-workspaces',
type: 'data-source',
references: [],
workspaces: [],
error: {
error: 'Forbidden',
message: 'Invalid data source permission, please associate it to current workspace',
statusCode: 403,
},
},
]);

const result1 = await wrapper.bulkGet([
{ type: 'data-source', id: 'global-data-source-empty-workspaces' },
]);
expect(result1.saved_objects).toEqual([
{
attributes: {},
id: 'global-data-source-empty-workspaces',
type: 'data-source',
references: [],
workspaces: [],
error: {
error: 'Forbidden',
message: 'Invalid data source permission, please associate it to current workspace',
statusCode: 403,
},
},
]);
});
});
describe('find', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ export class WorkspaceSavedObjectsClientWrapper {
wrapperOptions.request,
getWorkspacesFromSavedObjects(objectToBulkGet.saved_objects)
);

let flag = true;
const processedObjects = await Promise.all(
objectToBulkGet.saved_objects.map(async (object) => {
if (validateIsWorkspaceDataSourceAndConnectionObjectType(object.type)) {
Expand All @@ -511,8 +511,17 @@ export class WorkspaceSavedObjectsClientWrapper {
wrapperOptions.request
);
if (!hasPermission) {
flag = false;
ACLAuditor?.increment(ACLAuditorStateKey.VALIDATE_FAILURE, 1);
throw generateDataSourcePermissionError();
return {
...object,
workspaces: [],
attributes: {} as T,
error: {
...generateDataSourcePermissionError().output.payload,
statusCode: 403,
},
};
}
}

Expand All @@ -525,26 +534,28 @@ export class WorkspaceSavedObjectsClientWrapper {
false
))
) {
flag = false;
ACLAuditor?.increment(ACLAuditorStateKey.VALIDATE_FAILURE, 1);
return {
...object,
workspaces: [],
attributes: null,
attributes: {} as T,
error: {
error: 'Invalid saved objects permission',
...generateSavedObjectsPermissionError().output.payload,
statusCode: 403,
message: 'Permission denied',
},
};
}
return object;
})
);

ACLAuditor?.increment(
ACLAuditorStateKey.VALIDATE_SUCCESS,
objectToBulkGet.saved_objects.length
);
if (flag) {
ACLAuditor?.increment(
ACLAuditorStateKey.VALIDATE_SUCCESS,
objectToBulkGet.saved_objects.length
);
}
return { saved_objects: processedObjects };
};

Expand Down

0 comments on commit 2f4dedd

Please sign in to comment.