UNPKG

@senacor/azure-function-middleware

Version:

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

97 lines (96 loc) 4.83 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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 jest_mock_extended_1 = require("jest-mock-extended"); const JWTDecoder = __importStar(require("jwt-decode")); const error_1 = require("../error"); const jwtAuthorization_1 = __importDefault(require("./jwtAuthorization")); jest.mock('jwt-decode'); const jwtMock = JWTDecoder; describe('The authorization middleware should', () => { const requestMock = (0, jest_mock_extended_1.mockDeep)(); const initialMiddlewareResult = { $failed: false, $result: undefined }; beforeEach(() => { jest.restoreAllMocks(); jest.resetAllMocks(); }); test('successfully validate the passed authorization token', () => { requestMock.headers.get.mockImplementationOnce((name) => (name === 'authorization' ? 'Bearer token' : null)); jwtMock.jwtDecode.mockReturnValue('JWT-TEST'); const context = new functions_1.InvocationContext(); (0, jwtAuthorization_1.default)([ { jwtExtractor: (input) => { expect(input).toEqual('JWT-TEST'); return 'test'; }, parameterExtractor: () => 'test', }, ])(requestMock, context, initialMiddlewareResult); expect(context.extraInputs.get('jwt')).toEqual('JWT-TEST'); }); test('fail caused by missing authorization header', () => { requestMock.headers.get.mockReturnValue(null); const jwtExtractorMock = jest.fn(); const parameterExtractorMock = jest.fn(); expect(() => (0, jwtAuthorization_1.default)([{ jwtExtractor: jwtExtractorMock, parameterExtractor: parameterExtractorMock }])(requestMock, new functions_1.InvocationContext(), initialMiddlewareResult)).toThrow(new error_1.ApplicationError('Authorization error', 401)); expect(jwtMock.jwtDecode).not.toHaveBeenCalled(); }); test('fail caused by a incorrectly formatted authorization header', () => { requestMock.headers.get.mockImplementationOnce((name) => (name === 'authorization' ? 'Bearer' : null)); const jwtExtractorMock = jest.fn(); const parameterExtractorMock = jest.fn(); expect(() => (0, jwtAuthorization_1.default)([{ jwtExtractor: jwtExtractorMock, parameterExtractor: parameterExtractorMock }])(requestMock, new functions_1.InvocationContext(), initialMiddlewareResult)).toThrow(new error_1.ApplicationError('Authorization error', 401)); expect(jwtMock.jwtDecode).not.toHaveBeenCalled(); }); test('fail caused by second rule failing and therefore chaining failed', () => { requestMock.headers.get.mockImplementationOnce((name) => (name === 'authorization' ? 'Bearer' : null)); jwtMock.jwtDecode.mockReturnValue('JWT-TEST'); expect(() => (0, jwtAuthorization_1.default)([ { jwtExtractor: () => 'test', parameterExtractor: () => 'test', }, { jwtExtractor: () => 'failed', parameterExtractor: () => 'test', }, ])(requestMock, new functions_1.InvocationContext(), initialMiddlewareResult)).toThrow(new error_1.ApplicationError('Authorization error', 401, 'Unauthorized')); }); });