Skip to content

Commit

Permalink
Use forbidden instead of customError.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcenizal committed Apr 15, 2021
1 parent 3add225 commit d54f85c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 7 additions & 6 deletions x-pack/plugins/license_api_guard/server/license.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ describe('License API guard', () => {

const route = jest.fn();
const guardedRoute = license.guardApiRoute(route);
const customError = jest.fn();
const forbidden = jest.fn();
const responseMock = httpServerMock.createResponseFactory();
responseMock.customError = customError;
responseMock.forbidden = forbidden;
guardedRoute({} as RequestHandlerContext, {} as KibanaRequest, responseMock);

return {
errorResponse:
customError.mock.calls.length > 0
? customError.mock.calls[customError.mock.calls.length - 1][0]
forbidden.mock.calls.length > 0
? forbidden.mock.calls[forbidden.mock.calls.length - 1][0]
: undefined,
logMesssage:
logger.warn.mock.calls.length > 0
Expand Down Expand Up @@ -86,14 +86,15 @@ describe('License API guard', () => {
},
].forEach(({ licenseState, expectedMessage }) => {
describe(`${licenseState} license`, () => {
it('replies with 403 and message and logs the message', () => {
it('replies with and logs the error message', () => {
const { errorResponse, logMesssage, route } = testRoute({ licenseState });

// We depend on the call to `response.forbidden()` to generate the 403 status code,
// so we can't assert for it here.
expect(errorResponse).toEqual({
body: {
message: expectedMessage,
},
statusCode: 403,
});

expect(logMesssage).toBe(expectedMessage);
Expand Down
4 changes: 1 addition & 3 deletions x-pack/plugins/license_api_guard/server/license.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,17 @@ export class License {
const licenseErrorMessage = this.getLicenseErrorMessage(this.licenseCheckState);
this.logger?.warn(licenseErrorMessage);

return response.customError({
return response.forbidden({
body: {
message: licenseErrorMessage,
},
statusCode: 403,
});
}

return handler(ctx, request, response);
};
}

// eslint-disable-next-line @typescript-eslint/explicit-member-accessibility
public get isEsSecurityEnabled() {
return this._isEsSecurityEnabled;
}
Expand Down

0 comments on commit d54f85c

Please sign in to comment.