Skip to content

Commit

Permalink
Adds integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
  • Loading branch information
DarshitChanpura committed Mar 28, 2024
1 parent b706840 commit 250e71a
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ describe('Permission list page ', () => {
});

describe('PermissionList', () => {
const mockCoreStart = {
http: 1,
};
const dataSourceQuery = {
dataSourceId: 'test',
};
it('render empty', () => {
const component = shallow(
<PermissionList
Expand All @@ -116,14 +122,14 @@ describe('Permission list page ', () => {
jest.spyOn(React, 'useEffect').mockImplementationOnce((f) => f());
shallow(
<PermissionList
coreStart={{} as any}
coreStart={mockCoreStart as any}
navigation={{} as any}
params={{} as any}
config={{} as any}
/>
);

expect(fetchActionGroups).toBeCalled();
expect(fetchActionGroups).toBeCalledWith(mockCoreStart.http, dataSourceQuery);
});

it('fetch data error', () => {
Expand Down Expand Up @@ -152,7 +158,7 @@ describe('Permission list page ', () => {
it('submit change', () => {
const component = shallow(
<PermissionList
coreStart={{} as any}
coreStart={mockCoreStart as any}
navigation={{} as any}
params={{} as any}
config={{} as any}
Expand All @@ -162,7 +168,12 @@ describe('Permission list page ', () => {
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', () => {
Expand Down Expand Up @@ -191,7 +202,7 @@ describe('Permission list page ', () => {
it('delete action group', (done) => {
shallow(
<PermissionList
coreStart={{} as any}
coreStart={mockCoreStart as any}
navigation={{} as any}
params={{} as any}
config={{} as any}
Expand All @@ -202,7 +213,7 @@ describe('Permission list page ', () => {
deleteFunc();

process.nextTick(() => {
expect(requestDeleteActionGroups).toBeCalled();
expect(requestDeleteActionGroups).toBeCalledWith(mockCoreStart.http, [], dataSourceQuery);
done();
});
});
Expand Down
21 changes: 21 additions & 0 deletions test/helper/entity_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
103 changes: 89 additions & 14 deletions test/jest_integration/security_entity_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { extractAuthCookie, getAuthCookie } from '../helper/cookie';
import {
createOrUpdateEntityAsAdmin,
createOrUpdateEntityAsAdminWithDataSource,
deleteEntityAsAdminWithDataSource,
getAllEntitiesAsAdminWithDataSource,
getEntityAsAdmin,
getEntityAsAdminWithDataSource,
} from '../helper/entity_operation';
Expand Down Expand Up @@ -478,6 +480,7 @@ describe('start OpenSearch Dashboards server multi datasources enabled', () => {
},
},
});
expect(createDataSource.status).toEqual(200);
dataSourceId = createDataSource.body.id;
});

Expand Down Expand Up @@ -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',
Expand All @@ -557,24 +561,26 @@ describe('start OpenSearch Dashboards server multi datasources enabled', () => {

const getUserResponse = await getEntityAsAdminWithDataSource(
root,
'internalusers',
entityType,
testUsername,
dataSourceId
);
expect(getUserResponse.status).toEqual(200);
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',
Expand All @@ -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
);
});
});

0 comments on commit 250e71a

Please sign in to comment.