Skip to content

Commit

Permalink
BC-6453 - extend unit tests for AuthorizationClientAdapter to inclu…
Browse files Browse the repository at this point in the history
…de loggables
  • Loading branch information
bergatco committed Jun 13, 2024
1 parent e15c9bb commit ec9dddd
Showing 1 changed file with 243 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,32 +107,137 @@ describe(AuthorizationClientAdapter.name, () => {

await expect(service.checkPermissionsByReference(params)).rejects.toThrowError(expectedError);
});

it('should have correct log message', async () => {
const { params } = setup({ isAuthorized: false });

const expectedLogMessage = {
type: 'FORBIDDEN_EXCEPTION',
stack: expect.any(String),
data: {
action: params.context.action,
referenceId: params.referenceId,
referenceType: params.referenceType,
requiredPermissions: params.context.requiredPermissions.join(','),
},
};

try {
await service.checkPermissionsByReference(params);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
const logMessage = (e as AuthorizationForbiddenLoggableException).getLogMessage();

expect(logMessage).toEqual(expectedLogMessage);
}
});
});
});

describe('when authorizationReferenceControllerAuthorizeByReference returns error', () => {
const setup = () => {
const error = new Error('testError');
authorizationApi.authorizationReferenceControllerAuthorizeByReference.mockRejectedValueOnce(error);
describe('when error is instance of Error', () => {
const setup = () => {
const error = new Error('testError');
authorizationApi.authorizationReferenceControllerAuthorizeByReference.mockRejectedValueOnce(error);

const params = {
context: {
action: Action.READ,
requiredPermissions,
},
referenceType: AuthorizationBodyParamsReferenceType.COURSES,
referenceId: 'someReferenceId',
};

const params = {
context: {
action: Action.READ,
requiredPermissions,
},
referenceType: AuthorizationBodyParamsReferenceType.COURSES,
referenceId: 'someReferenceId',
return { params, error };
};

return { params, error };
};
it('should throw AuthorizationErrorLoggableException', async () => {
const { params, error } = setup();

it('should throw AuthorizationErrorLoggableException', async () => {
const { params, error } = setup();
const expectedError = new AuthorizationErrorLoggableException(error, params);

const expectedError = new AuthorizationErrorLoggableException(error, params);
await expect(service.checkPermissionsByReference(params)).rejects.toThrowError(expectedError);
});

await expect(service.checkPermissionsByReference(params)).rejects.toThrowError(expectedError);
it('should have correct log message', async () => {
const { params, error } = setup();

const expectedLogMessage = {
type: 'INTERNAL_SERVER_ERROR',
error,
stack: expect.any(String),
data: {
action: params.context.action,
referenceId: params.referenceId,
referenceType: params.referenceType,
requiredPermissions: params.context.requiredPermissions.join(','),
},
};

try {
await service.hasPermissionsByReference(params);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
const logMessage = (e as AuthorizationErrorLoggableException).getLogMessage();

expect(logMessage).toEqual(expectedLogMessage);
}
});
});

describe('when error is NOT instance of Error', () => {
const setup = () => {
const params = {
context: {
action: Action.READ,
requiredPermissions,
},
referenceType: AuthorizationBodyParamsReferenceType.COURSES,
referenceId: 'someReferenceId',
};

const error = { code: '123', message: 'testError' };
authorizationApi.authorizationReferenceControllerAuthorizeByReference.mockRejectedValueOnce(error);

return { params, error };
};

it('should throw AuthorizationErrorLoggableException', async () => {
const { params, error } = setup();

const expectedError = new AuthorizationErrorLoggableException(error, params);

await expect(service.hasPermissionsByReference(params)).rejects.toThrowError(expectedError);
});

it('should have correct log message', async () => {
const { params, error } = setup();

const expectedLogMessage = {
type: 'INTERNAL_SERVER_ERROR',
error: new Error(JSON.stringify(error)),
stack: expect.any(String),
data: {
action: params.context.action,
referenceId: params.referenceId,
referenceType: params.referenceType,
requiredPermissions: params.context.requiredPermissions.join(','),
},
};

try {
await service.hasPermissionsByReference(params);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
const logMessage = (e as AuthorizationErrorLoggableException).getLogMessage();

expect(logMessage).toEqual(expectedLogMessage);
}
});
});
});
});
Expand Down Expand Up @@ -296,31 +401,137 @@ describe(AuthorizationClientAdapter.name, () => {

await expect(adapter.hasPermissionsByReference(params)).rejects.toThrowError(expectedError);
});

it('should have correct log message', async () => {
const { params, adapter, error } = setup();

const expectedLogMessage = {
type: 'INTERNAL_SERVER_ERROR',
error,
stack: expect.any(String),
data: {
action: params.context.action,
referenceId: params.referenceId,
referenceType: params.referenceType,
requiredPermissions: params.context.requiredPermissions.join(','),
},
};

try {
await adapter.hasPermissionsByReference(params);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
const logMessage = (e as AuthorizationErrorLoggableException).getLogMessage();

expect(logMessage).toEqual(expectedLogMessage);
}
});
});

describe('when authorizationReferenceControllerAuthorizeByReference returns error', () => {
const setup = () => {
const params = {
context: {
action: Action.READ,
requiredPermissions,
},
referenceType: AuthorizationBodyParamsReferenceType.COURSES,
referenceId: 'someReferenceId',
describe('when error is instance of Error', () => {
const setup = () => {
const params = {
context: {
action: Action.READ,
requiredPermissions,
},
referenceType: AuthorizationBodyParamsReferenceType.COURSES,
referenceId: 'someReferenceId',
};

const error = new Error('testError');
authorizationApi.authorizationReferenceControllerAuthorizeByReference.mockRejectedValueOnce(error);

return { params, error };
};

const error = new Error('testError');
authorizationApi.authorizationReferenceControllerAuthorizeByReference.mockRejectedValueOnce(error);
it('should throw AuthorizationErrorLoggableException', async () => {
const { params, error } = setup();

return { params, error };
};
const expectedError = new AuthorizationErrorLoggableException(error, params);

it('should throw AuthorizationErrorLoggableException', async () => {
const { params, error } = setup();
await expect(service.hasPermissionsByReference(params)).rejects.toThrowError(expectedError);
});

const expectedError = new AuthorizationErrorLoggableException(error, params);
it('should have correct log message', async () => {
const { params, error } = setup();

const expectedLogMessage = {
type: 'INTERNAL_SERVER_ERROR',
error,
stack: expect.any(String),
data: {
action: params.context.action,
referenceId: params.referenceId,
referenceType: params.referenceType,
requiredPermissions: params.context.requiredPermissions.join(','),
},
};

try {
await service.hasPermissionsByReference(params);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
const logMessage = (e as AuthorizationErrorLoggableException).getLogMessage();

expect(logMessage).toEqual(expectedLogMessage);
}
});
});

describe('when error is NOT instance of Error', () => {
const setup = () => {
const params = {
context: {
action: Action.READ,
requiredPermissions,
},
referenceType: AuthorizationBodyParamsReferenceType.COURSES,
referenceId: 'someReferenceId',
};

const error = { code: '123', message: 'testError' };
authorizationApi.authorizationReferenceControllerAuthorizeByReference.mockRejectedValueOnce(error);

await expect(service.hasPermissionsByReference(params)).rejects.toThrowError(expectedError);
return { params, error };
};

it('should throw AuthorizationErrorLoggableException', async () => {
const { params, error } = setup();

const expectedError = new AuthorizationErrorLoggableException(error, params);

await expect(service.hasPermissionsByReference(params)).rejects.toThrowError(expectedError);
});

it('should have correct log message', async () => {
const { params, error } = setup();

const expectedLogMessage = {
type: 'INTERNAL_SERVER_ERROR',
error: new Error(JSON.stringify(error)),
stack: expect.any(String),
data: {
action: params.context.action,
referenceId: params.referenceId,
referenceType: params.referenceType,
requiredPermissions: params.context.requiredPermissions.join(','),
},
};

try {
await service.hasPermissionsByReference(params);
// Fail test if above expression doesn't throw anything.
expect(true).toBe(false);
} catch (e) {
const logMessage = (e as AuthorizationErrorLoggableException).getLogMessage();

expect(logMessage).toEqual(expectedLogMessage);
}
});
});
});
});
Expand Down

0 comments on commit ec9dddd

Please sign in to comment.