UNPKG

@senacor/azure-function-middleware

Version:

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

74 lines (73 loc) 4.51 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 requestBodyValidation_1 = require("./requestBodyValidation"); describe('requestBodyValidation should', () => { const context = new functions_1.InvocationContext(); const exampleSchema = joi_1.default.object({ name: joi_1.default.string().required() }); test('accept valid request body', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, requestBodyValidation_1.requestBodyValidation)(exampleSchema); yield expect(validator(createRequest(), context, { $failed: false, $result: undefined })).resolves.toBeUndefined(); })); test('successfully validate request body with joi validation options', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, requestBodyValidation_1.requestBodyValidation)(exampleSchema, { joiValidationOptions: { allowUnknown: true } }); yield expect(validator(createRequest({ name: 'John Doe', unknownValue: 'yes' }), context, { $failed: false, $result: undefined, })).resolves.toBeUndefined(); })); test('throw error if the request body does not match the given schema', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, requestBodyValidation_1.requestBodyValidation)(exampleSchema); yield expect(validator(createRequest({ id: 42 }), context, { $failed: false, $result: undefined })).rejects.toMatchObject({ message: 'Request body validation error', status: 400, body: { message: '"name" is required' }, }); })); test('throw error if the request body does not match the given schema and transform error message correctly', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, requestBodyValidation_1.requestBodyValidation)(exampleSchema, { transformErrorMessage: () => `Custom error message`, }); yield expect(validator(createRequest({ id: 42 }), context, { $failed: false, $result: undefined })).rejects.toMatchObject({ message: 'Request body validation error', status: 400, body: 'Custom error message', }); })); test('throw error if request body contains invalid json', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, requestBodyValidation_1.requestBodyValidation)(exampleSchema); yield expect(validator(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'POST', body: { string: 'invalid json' }, }), context, { $failed: false, $result: undefined })).rejects.toMatchObject({ message: 'Request body contains invalid json', status: 400, body: { message: 'Request body contains invalid json' }, }); })); test('throw no error if the request body does not match the given schema and shouldThrowOnValidationError = false', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, requestBodyValidation_1.requestBodyValidation)(exampleSchema, { shouldThrowOnValidationError: false }); yield expect(validator(createRequest({ id: 42 }), context, { $failed: false, $result: undefined })).resolves.toBeUndefined(); })); }); function createRequest(jsonBody = { name: 'John Doe' }) { return new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'POST', body: { string: JSON.stringify(jsonBody) }, }); }