UNPKG

@senacor/azure-function-middleware

Version:

Middleware for azure functions to handle authentication, authorization, error handling and logging

63 lines (62 loc) 3.75 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const functions_1 = require("@azure/functions"); const jest_mock_extended_1 = require("jest-mock-extended"); const joi_1 = __importDefault(require("joi")); const error_1 = require("../error"); const responseValidation_1 = require("./responseValidation"); describe('The responseValidation should', () => { const initialMiddlewareResult = { $failed: false, $result: undefined }; const exampleSchema = joi_1.default.object({ status: joi_1.default.number().required(), jsonBody: joi_1.default.object({ example: joi_1.default.string().required(), }), }); const requestMock = (0, jest_mock_extended_1.mockDeep)(); beforeEach(() => { jest.restoreAllMocks(); }); test('do nothing, if the response is valid', () => { initialMiddlewareResult.$result = { status: 201, jsonBody: { example: 'test-body' } }; const result = (0, responseValidation_1.responseValidation)(exampleSchema)(requestMock, new functions_1.InvocationContext(), initialMiddlewareResult); expect(result).toBeUndefined(); }); test('do nothing, if the object, extracted through the passed function, is valid', () => { initialMiddlewareResult.$result = { jsonBody: { example: 'test-body' } }; const extractValidationContentFromRequest = () => ({ status: 201, jsonBody: { example: 'not-test-body' }, }); const result = (0, responseValidation_1.responseValidation)(exampleSchema, { extractValidationContentFromRequest, })(requestMock, new functions_1.InvocationContext(), initialMiddlewareResult); expect(result).toBeUndefined(); }); test('successfully validate response with joi validation options', () => { initialMiddlewareResult.$result = { status: 201, jsonBody: { example: 'test-body', unknownValue: 'yes' } }; const result = (0, responseValidation_1.responseValidation)(exampleSchema, { joiValidationOptions: { allowUnknown: true } })(requestMock, new functions_1.InvocationContext(), initialMiddlewareResult); expect(result).toBeUndefined(); }); test('throw, if the response is invalid', () => { initialMiddlewareResult.$result = { status: 201, jsonBody: { fail: 'this-fails' } }; expect(() => (0, responseValidation_1.responseValidation)(exampleSchema)(requestMock, new functions_1.InvocationContext(), initialMiddlewareResult)).toThrow(new error_1.ApplicationError('Internal server error', 500)); }); test('do not throw an error, even when the validation was not successful, if throwing is disabled', () => { initialMiddlewareResult.$result = { status: 201, jsonBody: { fail: 'this-fails' } }; const res = (0, responseValidation_1.responseValidation)(exampleSchema, { shouldThrowOnValidationError: false })(requestMock, new functions_1.InvocationContext(), initialMiddlewareResult); expect(res).toEqual(undefined); }); test('fail when the validation was not successful with transformed error message', () => { initialMiddlewareResult.$result = { status: 201, jsonBody: { fail: 'this-fails' } }; expect(() => (0, responseValidation_1.responseValidation)(exampleSchema, { transformErrorMessage: (message) => ({ type: 'Validation Error', message, }), })(requestMock, new functions_1.InvocationContext(), initialMiddlewareResult)).toThrow(new error_1.ApplicationError('Internal server error', 500)); }); });