UNPKG

@senacor/azure-function-middleware

Version:

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

99 lines (98 loc) 6.28 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 error_1 = require("../error"); const headerAuthentication_1 = __importDefault(require("./headerAuthentication")); describe('The header authentication middleware should', () => { const context = new functions_1.InvocationContext(); const initialMiddlewareResult = { $failed: false, $result: undefined }; beforeEach(() => { jest.restoreAllMocks(); jest.resetAllMocks(); }); test('successfully resolves when the default "x-ms-client-principal" header is present', () => __awaiter(void 0, void 0, void 0, function* () { const res = yield (0, headerAuthentication_1.default)()(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'GET', headers: { 'x-ms-client-principal-id': 'Test Principal' }, }), context, initialMiddlewareResult); expect(res).toBeUndefined(); })); test('successfully resolves when the passed header validation function returns true', () => __awaiter(void 0, void 0, void 0, function* () { const res = yield (0, headerAuthentication_1.default)({ validateUsingHeaderFn: () => true })(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'GET', }), context, initialMiddlewareResult); expect(res).toBeUndefined(); })); test('successfully resolves when the passed async header validation function returns true', () => __awaiter(void 0, void 0, void 0, function* () { const res = yield (0, headerAuthentication_1.default)({ validateUsingHeaderFn: () => __awaiter(void 0, void 0, void 0, function* () { return true; }) })(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'GET', }), context, initialMiddlewareResult); expect(res).toBeUndefined(); })); test('fail caused by missing default "x-ms-client-principal" header', () => __awaiter(void 0, void 0, void 0, function* () { yield expect((0, headerAuthentication_1.default)()(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'GET', }), context, initialMiddlewareResult)).rejects.toThrow(new error_1.ApplicationError('Authentication error', 403, 'No sophisticated credentials provided')); })); test('fail caused by missing default "x-ms-client-principal" header and using the provided error body', () => __awaiter(void 0, void 0, void 0, function* () { yield expect((0, headerAuthentication_1.default)({ errorResponseBody: { error: 'Please authenticate properly' } })(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'GET', }), context, initialMiddlewareResult)).rejects.toThrow(new error_1.ApplicationError('Authentication error', 403, { error: 'Please authenticate properly' })); })); test('fail caused by passed header validation function returns false', () => __awaiter(void 0, void 0, void 0, function* () { yield expect((0, headerAuthentication_1.default)({ validateUsingHeaderFn: () => false })(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'GET', }), context, initialMiddlewareResult)).rejects.toThrow(new error_1.ApplicationError('Authentication error', 403, 'No sophisticated credentials provided')); })); test('fail caused by passed header validation function returns false and use the provided error body', () => __awaiter(void 0, void 0, void 0, function* () { yield expect(() => (0, headerAuthentication_1.default)({ validateUsingHeaderFn: () => false, errorResponseBody: { error: 'Please authenticate properly' } })(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'GET', }), context, initialMiddlewareResult)).rejects.toThrow(new error_1.ApplicationError('Authentication error', 403, { error: 'Please authenticate properly' })); })); test('do nothing if the passed result indicates an error in a prev function and skipIfResultIsFaulty is true', () => __awaiter(void 0, void 0, void 0, function* () { const res = yield (0, headerAuthentication_1.default)({ validateUsingHeaderFn: () => false, errorResponseBody: { error: 'Please authenticate properly' }, skipIfResultIsFaulty: true, })(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'GET', }), context, { $failed: true, $error: Error(), }); expect(res).toBeUndefined(); })); test('execute the function even if the passed result indicates an error in a prev function, but skipIfResultIsFaulty is false', () => __awaiter(void 0, void 0, void 0, function* () { yield expect((0, headerAuthentication_1.default)({ validateUsingHeaderFn: () => false, errorResponseBody: { error: 'Please authenticate properly' }, skipIfResultIsFaulty: false, })(new functions_1.HttpRequest({ url: 'http://localhost:8080', method: 'GET', }), context, { $failed: true, $error: Error(), })).rejects.toThrow(new error_1.ApplicationError('Authentication error', 403, { error: 'Please authenticate properly' })); })); });