Skip to content

Commit

Permalink
Add test coverage for front end and backend
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <[email protected]>
  • Loading branch information
derek-ho committed Mar 25, 2024
1 parent 39b12b1 commit 1cadc38
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function requestDeleteUsers(http: HttpStart, users: string[], query
}
}

async function getUserListRaw(
export async function getUserListRaw(
http: HttpStart,
userType: string,
query?: HttpFetchQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
* permissions and limitations under the License.
*/

import { transformUserData } from '../internal-user-list-utils';
import { fetchUserNameList, getUserList, transformUserData } from '../internal-user-list-utils';
import { httpGet } from '../request-utils';
import * as InternalUserListUtils from '../internal-user-list-utils';

jest.mock('../../utils/request-utils', () => ({
httpGet: jest.fn().mockResolvedValue({ data: {} }),
}));

describe('Internal user list utils', () => {
const userList = {
Expand All @@ -32,4 +38,102 @@ describe('Internal user list utils', () => {
];
expect(result).toEqual(expectedUserList);
});

it('getUserList calls httpGet with the correct parameters for internal users', async () => {
const httpMock = {}; // Mock HttpStart object
const userType = 'internalaccounts';
const query = { dataSourceId: 'test' };

// Mock the response data from httpGet
const mockRawData = {
data: {
// your mocked data here
},
};

// Mock the return value of getUserListRaw
jest.spyOn(InternalUserListUtils, 'getUserListRaw').mockResolvedValue(mockRawData);

// Call the function you want to test
const test = await getUserList(httpMock, userType, query);

// Assert that httpGet was called with the correct parameters
expect(httpGet).toHaveBeenCalledWith({
http: httpMock,
url: '/api/v1/configuration/internalaccounts',
query,
});
expect(test).toEqual([]);
});

it('getUserList calls httpGet with the correct parameters for service accounts', async () => {
const httpMock = {}; // Mock HttpStart object
const userType = 'serviceAccounts';
const query = { dataSourceId: 'test' };

// Mock the response data from httpGet
const mockRawData = {
data: {},
};

// Mock the return value of getUserListRaw
jest.spyOn(InternalUserListUtils, 'getUserListRaw').mockResolvedValue(mockRawData);

// Call the function you want to test
const test = await getUserList(httpMock, userType, query);

// Assert that httpGet was called with the correct parameters
expect(httpGet).toHaveBeenCalledWith({
http: httpMock,
url: '/api/v1/configuration/serviceaccounts',
query,
});
expect(test).toEqual([]);
});

it('fetchUserNameList calls httpGet with the correct parameters for service accounts', async () => {
const httpMock = {}; // Mock HttpStart object
const userType = 'serviceAccounts';

// Mock the response data from httpGet
const mockRawData = {
data: {},
};

// Mock the return value of getUserListRaw
jest.spyOn(InternalUserListUtils, 'getUserListRaw').mockResolvedValue(mockRawData);

// Call the function you want to test
const test = await fetchUserNameList(httpMock, userType);

// Assert that httpGet was called with the correct parameters
expect(httpGet).toHaveBeenCalledWith({
http: httpMock,
url: '/api/v1/configuration/serviceaccounts',
});
expect(test).toEqual([]);
});

it('fetchUserNameList calls httpGet with the correct parameters for internal users', async () => {
const httpMock = {}; // Mock HttpStart object
const userType = 'internalaccounts';

// Mock the response data from httpGet
const mockRawData = {
data: {},
};

// Mock the return value of getUserListRaw
jest.spyOn(InternalUserListUtils, 'getUserListRaw').mockResolvedValue(mockRawData);

// Call the function you want to test
const test = await fetchUserNameList(httpMock, userType);

// Assert that httpGet was called with the correct parameters
expect(httpGet).toHaveBeenCalledWith({
http: httpMock,
url: '/api/v1/configuration/internalaccounts',
});
expect(test).toEqual([]);
});
});
24 changes: 24 additions & 0 deletions test/helper/entity_operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ export async function getEntityAsAdmin(root: Root, entityType: string, entityId:
.get(root, `/api/v1/configuration/${entityType}/${entityId}`)
.set(AUTHORIZATION_HEADER_NAME, ADMIN_CREDENTIALS);
}

export async function createOrUpdateEntityAsAdminWithDataSource(
root: Root,
entityType: string,
entityId: string,
body: any,
dataSourceId: string
) {
return await osdTestServer.request
.post(root, `/api/v1/configuration/${entityType}/${entityId}?dataSourceId=${dataSourceId}`)
.set(AUTHORIZATION_HEADER_NAME, ADMIN_CREDENTIALS)
.send(body);
}

export async function getEntityAsAdminWithDataSource(
root: Root,
entityType: string,
entityId: string,
dataSourceId: string
) {
return await osdTestServer.request
.get(root, `/api/v1/configuration/${entityType}/${entityId}?dataSourceId=${dataSourceId}`)
.set(AUTHORIZATION_HEADER_NAME, ADMIN_CREDENTIALS);
}
80 changes: 79 additions & 1 deletion test/jest_integration/security_entity_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ import {
AUTHORIZATION_HEADER_NAME,
} from '../constant';
import { extractAuthCookie, getAuthCookie } from '../helper/cookie';
import { createOrUpdateEntityAsAdmin, getEntityAsAdmin } from '../helper/entity_operation';
import {
createOrUpdateEntityAsAdmin,
createOrUpdateEntityAsAdminWithDataSource,
getEntityAsAdmin,
getEntityAsAdminWithDataSource,
} from '../helper/entity_operation';

describe('start OpenSearch Dashboards server', () => {
let root: Root;
Expand Down Expand Up @@ -532,4 +537,77 @@ describe('start OpenSearch Dashboards server multi datasources enabled', () => {
// Getting auth info on an empty datasource calls local cluster
expect(getAuthResponseRemoteDataSource.status).toEqual(200);
});

it('create/get/update/list/delete internal user for external datasource', async () => {
const testUsername = `test_user_${Date.now()}`;
const testUserPassword = 'testUserPassword123';

const createUserResponse = await createOrUpdateEntityAsAdminWithDataSource(
root,
'internalusers',
testUsername,
{
description: 'test user description',
password: testUserPassword,
backend_roles: ['arbitrary_backend_role'],
},
dataSourceId
);
expect(createUserResponse.status).toEqual(200);

const getUserResponse = await getEntityAsAdminWithDataSource(
root,
'internalusers',
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);
expect(listUserResponse.status).toEqual(200);
expect(listUserResponse.body.total).toBeGreaterThan(2);
expect(listUserResponse.body.data[testUsername]).toBeTruthy();

const updateUserResponse = await createOrUpdateEntityAsAdminWithDataSource(
root,
'internalusers',
testUsername,
{
description: 'new description',
password: testUserPassword,
backend_roles: ['arbitrary_backend_role'],
},
dataSourceId
);
expect(updateUserResponse.status).toEqual(200);

const getUpdatedUserResponse = await getEntityAsAdminWithDataSource(
root,
'internalusers',
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);
expect(deleteUserResponse.status).toEqual(200);

const getDeletedUserResponse = await getEntityAsAdminWithDataSource(
root,
'internalusers',
testUsername,
dataSourceId
);
expect(getDeletedUserResponse.status).toEqual(404);
});
});

0 comments on commit 1cadc38

Please sign in to comment.