Skip to content

Commit

Permalink
squash! - try 13 - revert of test case for cross-check
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Somogyvari <peter.somogyvari@accenture.com>
  • Loading branch information
petermetz committed Feb 6, 2024
1 parent f5b3c00 commit d4cccf8
Showing 1 changed file with 54 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,98 +10,74 @@ import {
} from "../../../main/typescript/public-api"; // replace with the correct path to your module

import { LoggerProvider } from "@hyperledger/cactus-common";
import {
identifierByCodes,
INTERNAL_SERVER_ERROR,
} from "http-errors-enhanced-cjs";

// Since we are testing error handling, lots of false positive error logs appear
// that are hard to untangle/confusing when reading the test log output.
// The SILENT level suppresses these.
const log = LoggerProvider.getOrCreate({
label: "handle-rest-endpoint-exception.test.ts",
level: "SILENT",
level: "DEBUG",
});

describe("handleRestEndpointException", () => {
afterEach(() => {
jest.clearAllMocks();
});

it("should handle HttpError with statusCode gte 500", async () => {
it("should handle HttpError with statusCode >= 500", async () => {
const mockResponse = createResponse();

const rootCauseErrorMsg = "Root Cause Exception that should cause gte 500";
const rootError = new Error(rootCauseErrorMsg);

const reThrowErrorMsg =
"Message of the Re-thrown Exception that should have some context for debugging on top of the information already available in the rootCauseErrorMsg.";

const ctx: IHandleRestEndpointExceptionOptions = {
errorMsg: reThrowErrorMsg,
const mockOptions: IHandleRestEndpointExceptionOptions = {
errorMsg: "Test error message",
log: jest.mocked(log), // Provide a mock logger if needed
error: rootError, // Provide appropriate error objects for testing
error: new Error("Test error"), // Provide appropriate error objects for testing
res: mockResponse,
};

const spyLog = jest.spyOn(ctx.log, "error");
const mockHttpError = createHttpError(500, "Test HTTP error", {
expose: true,
});

const errorAsSanitizedJson = safeStringifyException(mockHttpError);
const spyLogDebug = jest.spyOn(mockOptions.log, "debug");
const spyStatus = jest.spyOn(mockResponse, "status");
const spyJson = jest.spyOn(mockResponse, "json");

await handleRestEndpointException(ctx);
await handleRestEndpointException({ ...mockOptions, error: mockHttpError });

expect(spyStatus).toHaveBeenCalledWith(500);

expect(spyLog).toHaveBeenCalledWith(
ctx.errorMsg,
safeStringifyException(rootError),
);

expect(spyJson).toHaveBeenCalledWith(
expect.objectContaining({
message: expect.stringMatching(
identifierByCodes[INTERNAL_SERVER_ERROR],
),
error: expect.stringContaining(reThrowErrorMsg),
}),
expect(spyLogDebug).toHaveBeenCalledWith(
mockOptions.errorMsg,
errorAsSanitizedJson,
);

expect(spyJson).toHaveBeenCalledWith(
expect.objectContaining({
message: expect.stringMatching(
identifierByCodes[INTERNAL_SERVER_ERROR],
),
error: expect.stringContaining(rootCauseErrorMsg),
}),
);
expect(spyJson).toHaveBeenCalledWith({
message: "InternalServerError",
error: errorAsSanitizedJson,
});
});

it("should handle HttpError with statusCode < 500", async () => {
const mockResponse = createResponse();

const rootErrorStr = "Test HTTP 404 error";
const mockHttpError = createHttpError(404, rootErrorStr, {
expose: true,
});

const ctx: IHandleRestEndpointExceptionOptions = {
const mockOptions: IHandleRestEndpointExceptionOptions = {
errorMsg: "Test error message",
log: jest.mocked(log), // Provide a mock logger if needed
error: mockHttpError, // Provide appropriate error objects for testing
error: new Error("Test error"), // Provide appropriate error objects for testing
res: mockResponse,
};

const mockHttpError = createHttpError(404, "Test HTTP error", {
expose: true,
});

const errorAsSanitizedJson = safeStringifyException(mockHttpError);
const spyLogError = jest.spyOn(ctx.log, "error");
const spyLogError = jest.spyOn(mockOptions.log, "error");
const spyStatus = jest.spyOn(mockResponse, "status");
const spyJson = jest.spyOn(mockResponse, "json");

await handleRestEndpointException(ctx);
await handleRestEndpointException({ ...mockOptions, error: mockHttpError });

expect(spyStatus).toHaveBeenCalledWith(404);

expect(spyLogError).toHaveBeenCalledWith(
ctx.errorMsg,
mockOptions.errorMsg,
errorAsSanitizedJson,
);

Expand All @@ -113,24 +89,30 @@ describe("handleRestEndpointException", () => {

it("should handle non-HttpError", async () => {
const mockResponse = createResponse();

const mockError = new Error("An unexpected exception. Ha!");

const ctx: IHandleRestEndpointExceptionOptions = {
const mockOptions: IHandleRestEndpointExceptionOptions = {
errorMsg: "Test error message",
log: jest.mocked(log), // Provide a mock logger if needed
error: mockError,
res: mockResponse,
};

const mockErrorJson = safeStringifyException(mockError);
const spyLoggerFn = jest.spyOn(ctx.log, "error");
const spyLoggerFn = jest.spyOn(mockOptions.log, "error");
const spyStatus = jest.spyOn(mockResponse, "status");
const spyJson = jest.spyOn(mockResponse, "json");

await handleRestEndpointException(ctx);
await handleRestEndpointException({ ...mockOptions, error: mockError });

expect(spyStatus).toHaveBeenCalledWith(500);
expect(spyLoggerFn).toHaveBeenCalledWith(ctx.errorMsg, mockErrorJson);

expect(spyLoggerFn).toHaveBeenCalledWith(
mockOptions.errorMsg,
mockErrorJson,
);

expect(spyJson).toHaveBeenCalledOnce();

const mostRecentCall = spyJson.mock.lastCall;
Expand All @@ -149,23 +131,26 @@ describe("handleRestEndpointException", () => {
const dummyXssPayload = `<script>stealAndUploadPrivateKeys();</script>`;
const mockError = new Error(dummyXssPayload);

const ctx: IHandleRestEndpointExceptionOptions = {
const mockOptions: IHandleRestEndpointExceptionOptions = {
errorMsg: "Test error message",
log: jest.mocked(log), // Provide a mock logger if needed
error: mockError,
res: mockResponse,
};

const mockErrorJson = safeStringifyException(mockError);
const spyLoggerFn = jest.spyOn(ctx.log, "error");
const spyLoggerFn = jest.spyOn(mockOptions.log, "error");
const spyStatus = jest.spyOn(mockResponse, "status");
const spyJson = jest.spyOn(mockResponse, "json");

await handleRestEndpointException(ctx);
await handleRestEndpointException({ ...mockOptions, error: mockError });

expect(spyStatus).toHaveBeenCalledWith(500);

expect(spyLoggerFn).toHaveBeenCalledWith(ctx.errorMsg, mockErrorJson);
expect(spyLoggerFn).toHaveBeenCalledWith(
mockOptions.errorMsg,
mockErrorJson,
);

expect(spyJson).toHaveBeenCalledOnce();

Expand All @@ -188,22 +173,27 @@ describe("handleRestEndpointException", () => {
const dummyXssPayload = `<script>stealAndUploadPrivateKeys();</script>`;
const mockError = dummyXssPayload;

const ctx: IHandleRestEndpointExceptionOptions = {
const mockOptions: IHandleRestEndpointExceptionOptions = {
errorMsg: "Test error message",
log: jest.mocked(log), // Provide a mock logger if needed
error: mockError,
res: mockResponse,
};

const mockErrorJson = safeStringifyException(mockError);
const spyLoggerFn = jest.spyOn(ctx.log, "error");
const spyLoggerFn = jest.spyOn(mockOptions.log, "error");
const spyStatus = jest.spyOn(mockResponse, "status");
const spyJson = jest.spyOn(mockResponse, "json");

await handleRestEndpointException(ctx);
await handleRestEndpointException({ ...mockOptions, error: mockError });

expect(spyStatus).toHaveBeenCalledWith(500);
expect(spyLoggerFn).toHaveBeenCalledWith(ctx.errorMsg, mockErrorJson);

expect(spyLoggerFn).toHaveBeenCalledWith(
mockOptions.errorMsg,
mockErrorJson,
);

expect(spyJson).toHaveBeenCalledOnce();

const mostRecentCall = spyJson.mock.lastCall;
Expand Down

0 comments on commit d4cccf8

Please sign in to comment.