@senacor/azure-function-middleware
Version:
Middleware for azure functions to handle authentication, authorization, error handling and logging
77 lines (76 loc) • 4.32 kB
JavaScript
;
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 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);
try {
yield validator(createRequest({ id: 42 }), context, { $failed: false, $result: undefined });
}
catch (err) {
if (err instanceof error_1.ApplicationError) {
expect(err.message).toEqual('Request body validation error');
expect(err.status).toEqual(400);
expect(err.body).toEqual({
message: '"name" is required',
});
}
}
expect.assertions(3);
}));
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`,
});
try {
yield validator(createRequest({ id: 42 }), context, { $failed: false, $result: undefined });
}
catch (err) {
if (err instanceof error_1.ApplicationError) {
expect(err.message).toEqual('Request body validation error');
expect(err.status).toEqual(400);
expect(err.body).toEqual('Custom error message');
}
}
expect.assertions(3);
}));
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) },
});
}