diff --git a/public/apps/configuration/panels/permission-list/test/permission-list.test.tsx b/public/apps/configuration/panels/permission-list/test/permission-list.test.tsx index a2db15bda..9e1ec9afa 100644 --- a/public/apps/configuration/panels/permission-list/test/permission-list.test.tsx +++ b/public/apps/configuration/panels/permission-list/test/permission-list.test.tsx @@ -99,6 +99,12 @@ describe('Permission list page ', () => { }); describe('PermissionList', () => { + const mockCoreStart = { + http: 1, + }; + const dataSourceQuery = { + dataSourceId: 'test', + }; it('render empty', () => { const component = shallow( { jest.spyOn(React, 'useEffect').mockImplementationOnce((f) => f()); shallow( ); - expect(fetchActionGroups).toBeCalled(); + expect(fetchActionGroups).toBeCalledWith(mockCoreStart.http, dataSourceQuery); }); it('fetch data error', () => { @@ -152,7 +158,7 @@ describe('Permission list page ', () => { it('submit change', () => { const component = shallow( { const submitFunc = component.find(PermissionEditModal).prop('handleSave'); submitFunc('group1', []); - expect(updateActionGroup).toBeCalled(); + expect(updateActionGroup).toBeCalledWith( + mockCoreStart.http, + 'group1', + { allowed_actions: [] }, + dataSourceQuery + ); }); it('submit change error', () => { @@ -191,7 +202,7 @@ describe('Permission list page ', () => { it('delete action group', (done) => { shallow( { deleteFunc(); process.nextTick(() => { - expect(requestDeleteActionGroups).toBeCalled(); + expect(requestDeleteActionGroups).toBeCalledWith(mockCoreStart.http, [], dataSourceQuery); done(); }); }); diff --git a/test/helper/entity_operation.ts b/test/helper/entity_operation.ts index aabdfbd8a..aad5da56f 100644 --- a/test/helper/entity_operation.ts +++ b/test/helper/entity_operation.ts @@ -57,3 +57,24 @@ export async function getEntityAsAdminWithDataSource( .get(root, `/api/v1/configuration/${entityType}/${entityId}?dataSourceId=${dataSourceId}`) .set(AUTHORIZATION_HEADER_NAME, ADMIN_CREDENTIALS); } + +export async function getAllEntitiesAsAdminWithDataSource( + root: Root, + entityType: string, + dataSourceId: string +) { + return await osdTestServer.request + .get(root, `/api/v1/configuration/${entityType}?dataSourceId=${dataSourceId}`) + .set(AUTHORIZATION_HEADER_NAME, ADMIN_CREDENTIALS); +} + +export async function deleteEntityAsAdminWithDataSource( + root: Root, + entityType: string, + entityId: string, + dataSourceId: string +) { + return await osdTestServer.request + .delete(root, `/api/v1/configuration/${entityType}/${entityId}?dataSourceId=${dataSourceId}`) + .set(AUTHORIZATION_HEADER_NAME, ADMIN_CREDENTIALS); +} diff --git a/test/jest_integration/security_entity_api.test.ts b/test/jest_integration/security_entity_api.test.ts index 6e68a0574..0c9519164 100644 --- a/test/jest_integration/security_entity_api.test.ts +++ b/test/jest_integration/security_entity_api.test.ts @@ -29,6 +29,8 @@ import { extractAuthCookie, getAuthCookie } from '../helper/cookie'; import { createOrUpdateEntityAsAdmin, createOrUpdateEntityAsAdminWithDataSource, + deleteEntityAsAdminWithDataSource, + getAllEntitiesAsAdminWithDataSource, getEntityAsAdmin, getEntityAsAdminWithDataSource, } from '../helper/entity_operation'; @@ -478,6 +480,7 @@ describe('start OpenSearch Dashboards server multi datasources enabled', () => { }, }, }); + expect(createDataSource.status).toEqual(200); dataSourceId = createDataSource.body.id; }); @@ -541,10 +544,11 @@ describe('start OpenSearch Dashboards server multi datasources enabled', () => { it('create/get/update/list/delete internal user for external datasource', async () => { const testUsername = `test_user_${Date.now()}`; const testUserPassword = 'testUserPassword123'; + const entityType = 'internalusers'; const createUserResponse = await createOrUpdateEntityAsAdminWithDataSource( root, - 'internalusers', + entityType, testUsername, { description: 'test user description', @@ -557,7 +561,7 @@ describe('start OpenSearch Dashboards server multi datasources enabled', () => { const getUserResponse = await getEntityAsAdminWithDataSource( root, - 'internalusers', + entityType, testUsername, dataSourceId ); @@ -565,16 +569,18 @@ describe('start OpenSearch Dashboards server multi datasources enabled', () => { expect(getUserResponse.body.description).toEqual('test user description'); expect(getUserResponse.body.backend_roles).toContain('arbitrary_backend_role'); - const listUserResponse = await osdTestServer.request - .get(root, `/api/v1/configuration/internalusers?dataSourceId=${dataSourceId}`) - .set(AUTHORIZATION_HEADER_NAME, ADMIN_CREDENTIALS); + const listUserResponse = await getAllEntitiesAsAdminWithDataSource( + root, + entityType, + dataSourceId + ); expect(listUserResponse.status).toEqual(200); expect(listUserResponse.body.total).toBeGreaterThan(2); expect(listUserResponse.body.data[testUsername]).toBeTruthy(); const updateUserResponse = await createOrUpdateEntityAsAdminWithDataSource( root, - 'internalusers', + entityType, testUsername, { description: 'new description', @@ -587,27 +593,96 @@ describe('start OpenSearch Dashboards server multi datasources enabled', () => { const getUpdatedUserResponse = await getEntityAsAdminWithDataSource( root, - 'internalusers', + entityType, testUsername, dataSourceId ); expect(getUpdatedUserResponse.status).toEqual(200); expect(getUpdatedUserResponse.body.description).toEqual('new description'); - const deleteUserResponse = await osdTestServer.request - .delete( - root, - `/api/v1/configuration/internalusers/${testUsername}?dataSourceId=${dataSourceId}` - ) - .set(AUTHORIZATION_HEADER_NAME, ADMIN_CREDENTIALS); + const deleteUserResponse = await deleteEntityAsAdminWithDataSource( + root, + entityType, + testUsername, + dataSourceId + ); expect(deleteUserResponse.status).toEqual(200); const getDeletedUserResponse = await getEntityAsAdminWithDataSource( root, - 'internalusers', + entityType, testUsername, dataSourceId ); expect(getDeletedUserResponse.status).toEqual(404); }); + + it('CRUD Permissions for external datasource', async () => { + const entityType = 'actiongroups'; + const testActionGroupName = `test_action_group_${Date.now()}`; + + const createActionGroupResponse = await createOrUpdateEntityAsAdminWithDataSource( + root, + entityType, + testActionGroupName, + { + allowed_actions: ['some_allowed_action'], + }, + dataSourceId + ); + expect(createActionGroupResponse.status).toEqual(200); + + const getActionGroupsResponse = await getAllEntitiesAsAdminWithDataSource( + root, + entityType, + dataSourceId + ); + expect(getActionGroupsResponse.status).toEqual(200); + expect(getActionGroupsResponse.body.data?.hasOwnProperty(testActionGroupName)).toBe(true); + expect(getActionGroupsResponse.body.data[testActionGroupName].allowed_actions).toContain( + 'some_allowed_action' + ); + + const updatePermissionResponse = await createOrUpdateEntityAsAdminWithDataSource( + root, + entityType, + testActionGroupName, + { + allowed_actions: ['some_allowed_action', 'another_permission'], + }, + dataSourceId + ); + expect(updatePermissionResponse.status).toEqual(200); + + const getUpdatedActionGroupsResponse = await getAllEntitiesAsAdminWithDataSource( + root, + entityType, + dataSourceId + ); + expect(getUpdatedActionGroupsResponse.status).toEqual(200); + expect(getUpdatedActionGroupsResponse.body.data?.hasOwnProperty(testActionGroupName)).toBe( + true + ); + expect(getUpdatedActionGroupsResponse.body.data[testActionGroupName].allowed_actions).toContain( + 'another_permission' + ); + + const deleteActionGroupResponse = await deleteEntityAsAdminWithDataSource( + root, + entityType, + testActionGroupName, + dataSourceId + ); + expect(deleteActionGroupResponse.status).toEqual(200); + + const getDeletedActionGroupsResponse = await getAllEntitiesAsAdminWithDataSource( + root, + entityType, + dataSourceId + ); + expect(getDeletedActionGroupsResponse.status).toEqual(200); + expect(getDeletedActionGroupsResponse.body.data?.hasOwnProperty(testActionGroupName)).toBe( + false + ); + }); });