UNPKG

@senacor/azure-function-middleware

Version:

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

61 lines (60 loc) 4.18 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; 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 joi_1 = __importDefault(require("joi")); const error_1 = require("../error"); const requestValidation_1 = require("./requestValidation"); describe('The requestValidation should', () => { const exampleSchema = joi_1.default.object({ example: joi_1.default.string().required() }); const initialMiddlewareResult = { $failed: false, $result: undefined }; const createRequest = (jsonBody = { example: 'test-body' }) => new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'POST', body: { string: JSON.stringify(jsonBody) }, }); test('successfully validate the passed object', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, requestValidation_1.requestValidation)(exampleSchema)(createRequest(), new functions_1.InvocationContext(), initialMiddlewareResult); expect(result).toBeUndefined(); })); test('successfully validate request with joi validation options', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, requestValidation_1.requestValidation)(exampleSchema, { joiValidationOptions: { allowUnknown: true }, })(createRequest({ example: 'test-body', unknownValue: 'yes' }), new functions_1.InvocationContext(), initialMiddlewareResult); expect(result).toBeUndefined(); })); test('successfully validate the object extracted through the passed function', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, requestValidation_1.requestValidation)(exampleSchema, { extractValidationContentFromRequest: () => ({ example: 'test-extracted-content', }), })(createRequest(), new functions_1.InvocationContext(), initialMiddlewareResult); expect(result).toBeUndefined(); })); test('fail when the validation was not successful', () => __awaiter(void 0, void 0, void 0, function* () { yield expect((0, requestValidation_1.requestValidation)(exampleSchema)(createRequest({ not: 'valid' }), new functions_1.InvocationContext(), initialMiddlewareResult)).rejects.toThrowError(new error_1.ApplicationError('Validation Error', 400)); })); test('do not throw an error, even when the validation was not successful, if throwing is disabled', () => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, requestValidation_1.requestValidation)(exampleSchema, { shouldThrowOnValidationError: false })(createRequest({ example: 'test-body' }), new functions_1.InvocationContext(), initialMiddlewareResult); expect(result).toBeUndefined(); })); test('fail when the validation was not successful with transformed error message', () => __awaiter(void 0, void 0, void 0, function* () { yield expect((0, requestValidation_1.requestValidation)(exampleSchema, { transformErrorMessage: (message) => ({ type: 'Validation Error', message, }), })(createRequest({ example: { fail: 'test-body' } }), new functions_1.InvocationContext(), initialMiddlewareResult)).rejects.toThrow(new error_1.ApplicationError('Validation Error', 400)); })); });