UNPKG

nralcm

Version:

This is a framework based on NodeJs to manage rest api request lifecycle

93 lines (92 loc) 3.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("mocha"); const chai_1 = require("chai"); const sinon = require("sinon"); const __1 = require(".."); const product_controller_1 = require("../../controllers/product.controller"); const authentication_filter_1 = require("../../filters/authentication.filter"); const exceptions_1 = require("../../exceptions"); describe("AuthHandler", () => { it("handle, must return true when no AuthenticationFilter registered.", () => { let request = {}; let response = {}; let httpContext = new __1.HttpContext(request, response); httpContext.controller = product_controller_1.ProductController; let restApiConfiguration = {}; let authHandler = new __1.AuthHandler(restApiConfiguration); let result = authHandler.handle(httpContext); chai_1.expect(result).to.equal(true); }); it("handle, must throw UnAuthenticateException when no Authorization header send in request", () => { let request = { header: sinon.stub() }; let response = { type: sinon.stub(), status: sinon.stub(), send: sinon.stub() }; request.header.callsFake(() => { return undefined; }); response.type.callsFake(() => { return response; }); response.status.callsFake((status) => { response.statusCode = status; return response; }); response.send.callsFake(() => { return response; }); let httpContext = new __1.HttpContext(request, response); httpContext.controller = product_controller_1.ProductController; let httpResponseHandler = { sendResponse: sinon.stub() }; let restApiConfiguration = { AuthenticationFilter: new authentication_filter_1.AuthenticationFilter(), HttpResponseHandler: httpResponseHandler }; let authHandler = new __1.AuthHandler(restApiConfiguration); try { authHandler.handle(httpContext); throw new Error("Result not as expected"); } catch (e) { chai_1.expect(e).to.instanceof(exceptions_1.UnAuthenticateException); } }); it("handle, must throw true when Authorization header send in request", () => { let request = { header: sinon.stub() }; let response = { type: sinon.stub(), status: sinon.stub(), send: sinon.stub() }; request.header.callsFake(() => { return "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE1MjE3Mjg0NDgsImV4cCI6MTU1MzI2NDQ0OCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJFbWFpbCI6Impyb2NrZXRAZXhhbXBsZS5jb20iLCJSb2xlIjpbIk1hbmFnZXIiLCJQcm9qZWN0IEFkbWluaXN0cmF0b3IiXX0.-nv6BWfQrXD3UI7l6OM0AYnCNIVfxG816UAkg0jqXas"; }); response.type.callsFake(() => { return response; }); response.status.callsFake((status) => { response.statusCode = status; return response; }); response.send.callsFake(() => { return response; }); let httpContext = new __1.HttpContext(request, response); httpContext.controller = product_controller_1.ProductController; let restApiConfiguration = { AuthenticationFilter: new authentication_filter_1.AuthenticationFilter() }; let authHandler = new __1.AuthHandler(restApiConfiguration); let result = authHandler.handle(httpContext); chai_1.expect(result).to.equal(true); }); });