UNPKG

@senacor/azure-function-middleware

Version:

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

97 lines (96 loc) 5.19 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 responseBodyValidation_1 = require("./responseBodyValidation"); describe('responseBodyValidation should', () => { const context = new functions_1.InvocationContext(); const responseSchemas = { 200: joi_1.default.object({ name: joi_1.default.string().required() }), 404: joi_1.default.object({ details: joi_1.default.string().required() }), }; test('accept valid response body for status 200', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, responseBodyValidation_1.responseBodyValidation)(responseSchemas, { shouldThrowOnValidationError: true }); yield expect(validator(createRequest(), context, { $failed: false, $result: { status: 200, jsonBody: { name: 'John Doe' } }, })).resolves.toBeUndefined(); })); test('accept valid response body for status 404', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, responseBodyValidation_1.responseBodyValidation)(responseSchemas, { shouldThrowOnValidationError: true }); yield expect(validator(createRequest(), context, { $failed: false, $result: { status: 404, jsonBody: { details: 'Not found' } }, })).resolves.toBeUndefined(); })); test('validate response body with joi validation options', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, responseBodyValidation_1.responseBodyValidation)(responseSchemas, { shouldThrowOnValidationError: true, joiValidationOptions: { allowUnknown: true }, }); yield expect(validator(createRequest(), context, { $failed: false, $result: { status: 200, jsonBody: { name: 'John Doe', unknownValue: 'yes' } }, })).resolves.toBeUndefined(); })); test('throw error if the response body does not match the given schema and shouldThrowOnValidationError = true', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, responseBodyValidation_1.responseBodyValidation)(responseSchemas, { shouldThrowOnValidationError: true }); try { yield validator(createRequest(), context, { $failed: false, $result: { status: 200, jsonBody: { id: 42 } }, }); } catch (err) { if (err instanceof error_1.ApplicationError) { expect(err.message).toEqual('Response body validation error'); expect(err.status).toEqual(500); expect(err.body).toBeUndefined(); } } expect.assertions(3); })); test('throw error if there is no schema for the response status and shouldThrowOnValidationError = true', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, responseBodyValidation_1.responseBodyValidation)(responseSchemas, { shouldThrowOnValidationError: true }); try { yield validator(createRequest(), context, { $failed: false, $result: { status: 201, jsonBody: {} }, }); } catch (err) { if (err instanceof error_1.ApplicationError) { expect(err.message).toEqual('Response body validation error as there is no schema for status 201'); expect(err.status).toEqual(500); expect(err.body).toBeUndefined(); } } expect.assertions(3); })); test('throw no error if the response body does not match the given schema and shouldThrowOnValidationError = false', () => __awaiter(void 0, void 0, void 0, function* () { const validator = (0, responseBodyValidation_1.responseBodyValidation)(responseSchemas, { shouldThrowOnValidationError: false }); yield expect(validator(createRequest(), context, { $failed: false, $result: { status: 200, jsonBody: { id: 42 } }, })).resolves.toBeUndefined(); })); }); function createRequest() { return new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'POST', }); }