Skip to content

Commit

Permalink
feat: optimize test cases
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 Mar 5, 2024
1 parent f16f1cc commit 60b3b02
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
30 changes: 23 additions & 7 deletions src/plugins/workspace/server/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { httpServerMock, httpServiceMock } from '../../../core/server/mocks';
import { generateRandomId, getPrincipalsFromRequest } from './utils';

describe('workspace utils', () => {
const mockAuth = httpServiceMock.createAuth();
it('should generate id with the specified size', () => {
expect(generateRandomId(6)).toHaveLength(6);
});
Expand All @@ -23,13 +24,19 @@ describe('workspace utils', () => {

it('should return empty map when request do not have authentication', () => {
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest();
const result = getPrincipalsFromRequest(mockRequest);
mockAuth.get.mockReturnValueOnce({
status: AuthStatus.unknown,
state: {
user_name: 'bar',
backend_roles: ['foo'],
},
});
const result = getPrincipalsFromRequest(mockRequest, mockAuth);
expect(result).toEqual({});
});

it('should return normally when request has authentication', () => {
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest();
const mockAuth = httpServiceMock.createAuth();
mockAuth.get.mockReturnValueOnce({
status: AuthStatus.authenticated,
state: {
Expand All @@ -42,15 +49,24 @@ describe('workspace utils', () => {
expect(result.groups).toEqual(['foo']);
});

it('should return a fake user when there is auth field but no backend_roles or user name', () => {
it('should throw error when request is not authenticated', () => {
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest();
const mockAuth = httpServiceMock.createAuth();
mockAuth.get.mockReturnValueOnce({
status: AuthStatus.unauthenticated,
state: {},
});
const result = getPrincipalsFromRequest(mockRequest, mockAuth);
expect(result.users?.[0].startsWith('_user_fake_')).toEqual(true);
expect(result.groups).toEqual(undefined);
expect(() => getPrincipalsFromRequest(mockRequest, mockAuth)).toThrow('NOT_AUTHORIZED');
});

it('should throw error when authentication status is not expected', () => {
const mockRequest = httpServerMock.createOpenSearchDashboardsRequest();
mockAuth.get.mockReturnValueOnce({
// @ts-ignore
status: 'foo',
state: {},
});
expect(() => getPrincipalsFromRequest(mockRequest, mockAuth)).toThrow(
'UNEXPECTED_AUTHORIZATION_STATUS'
);
});
});
25 changes: 13 additions & 12 deletions src/plugins/workspace/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,20 @@ export const getPrincipalsFromRequest = (
return payload;
}

if (authInfoResp?.status === AuthStatus.unauthenticated) {
/**
* use a fake user that won't be granted permission explicitly when authenticated error.
*/
payload[PrincipalType.Users] = [`_user_fake_${Date.now()}_`];
if (authInfoResp?.status === AuthStatus.authenticated) {
const authInfo = authInfoResp?.state as AuthInfo | null;
if (authInfo?.backend_roles) {
payload[PrincipalType.Groups] = authInfo.backend_roles;
}
if (authInfo?.user_name) {
payload[PrincipalType.Users] = [authInfo.user_name];
}
return payload;
}
const authInfo = authInfoResp?.state as AuthInfo | null;
if (authInfo?.backend_roles) {
payload[PrincipalType.Groups] = authInfo.backend_roles;
}
if (authInfo?.user_name) {
payload[PrincipalType.Users] = [authInfo.user_name];

if (authInfoResp?.status === AuthStatus.unauthenticated) {
throw new Error('NOT_AUTHORIZED');
}
return payload;

throw new Error('UNEXPECTED_AUTHORIZATION_STATUS');
};

0 comments on commit 60b3b02

Please sign in to comment.