UNPKG

@qelos/auth

Version:

Express Passport authentication service

231 lines (230 loc) 9.29 kB
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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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()); }); }; function getAsyncVerifyUser() { return __awaiter(this, void 0, void 0, function* () { const module = yield Promise.resolve().then(() => __importStar(require('../verify-user'))); return module.default; }); } describe('Auth Check middlewares - verifyUser', () => { let verifyUser; let tokens; beforeEach(() => __awaiter(this, void 0, void 0, function* () { jest.mock('../../services/tokens'); tokens = yield Promise.resolve().then(() => __importStar(require('../../services/tokens'))); verifyUser = yield getAsyncVerifyUser(); })); afterEach(() => { jest.resetAllMocks(); jest.clearAllMocks(); jest.resetModules(); }); describe('When get request without any authorization', () => { test('should trigger next middleware', () => __awaiter(this, void 0, void 0, function* () { const req = { cookies: {}, signedCookies: {}, headers: { tenant: 'tenant-1', }, }; const res = {}; const next = jest.fn(); yield verifyUser(req, res, next); expect(next).toBeCalled(); expect(next.mock.calls.length).toBe(1); // next function should be called with not arguments expect(next.mock.calls[0][0]).toBeUndefined(); })); }); describe('When get request with authorization header', () => { test('should check token and tenant using verifyToken', () => __awaiter(this, void 0, void 0, function* () { const req = { cookies: {}, signedCookies: {}, headers: { tenant: 'tenant-1', authorization: 'Bearer token-value', } }; const res = {}; const next = jest.fn(); expect(tokens.verifyToken).not.toBeCalled(); yield verifyUser(req, res, next); expect(tokens.verifyToken).toBeCalled(); })); test('should trigger next middleware', () => __awaiter(this, void 0, void 0, function* () { const req = { cookies: {}, signedCookies: {}, headers: { tenant: 'tenant-1', authorization: 'Bearer token-value', } }; const res = {}; const next = jest.fn(); expect(next).not.toBeCalled(); tokens.verifyToken.mockImplementation(() => __awaiter(this, void 0, void 0, function* () { return null; })); yield verifyUser(req, res, next); expect(next).toBeCalled(); })); test('should set payload on request when token is verified', () => __awaiter(this, void 0, void 0, function* () { const req = { userPayload: null, cookies: {}, signedCookies: {}, headers: { tenant: 'tenant-1', authorization: 'Bearer valid-token', } }; const res = {}; const userPayload = { user: 'demo', roles: ['admin'], workspace: 'workspace-1', }; const next = jest.fn(); expect(next).not.toBeCalled(); tokens.verifyToken.mockImplementation(() => __awaiter(this, void 0, void 0, function* () { return userPayload; })); yield verifyUser(req, res, next); expect(req.userPayload).toEqual(userPayload); })); test('should set isPrivileged as TRUE on request when token is verified', () => __awaiter(this, void 0, void 0, function* () { const req = { userPayload: null, isPrivileged: null, cookies: {}, signedCookies: {}, headers: { tenant: 'tenant-1', authorization: 'Bearer valid-token', } }; const res = {}; const userPayload = { user: 'demo', roles: ['admin'], workspace: 'workspace-1', }; const next = jest.fn(); expect(next).not.toBeCalled(); tokens.verifyToken.mockImplementation(() => __awaiter(this, void 0, void 0, function* () { return userPayload; })); yield verifyUser(req, res, next); expect(req.userPayload.isPrivileged).toEqual(true); })); test('should set isPrivileged as FALSE on request when token is verified', () => __awaiter(this, void 0, void 0, function* () { const req = { userPayload: null, isPrivileged: null, cookies: {}, signedCookies: {}, headers: { tenant: 'tenant-1', authorization: 'Bearer valid-token', } }; const res = {}; const userPayload = { user: 'demo', roles: ['user'], workspace: 'workspace-1', }; const next = jest.fn(); expect(next).not.toBeCalled(); tokens.verifyToken.mockImplementation(() => __awaiter(this, void 0, void 0, function* () { return userPayload; })); yield verifyUser(req, res, next); expect(req.userPayload.isPrivileged).toEqual(false); })); test('should set activeWorkspace on request when token is verified', () => __awaiter(this, void 0, void 0, function* () { const req = { userPayload: null, activeWorkspace: null, cookies: {}, signedCookies: {}, headers: { tenant: 'tenant-1', authorization: 'Bearer valid-token', } }; const res = {}; const userPayload = { user: 'demo', roles: ['admin'], workspace: 'workspace-1', }; const next = jest.fn(); expect(next).not.toBeCalled(); tokens.verifyToken.mockImplementation(() => __awaiter(this, void 0, void 0, function* () { return userPayload; })); yield verifyUser(req, res, next); expect(req.activeWorkspace).toEqual('workspace-1'); })); }); describe('When get request with cookie', () => { test('should trigger next middleware', () => __awaiter(this, void 0, void 0, function* () { const req = { cookies: { token: 'cookie-token' }, signedCookies: {}, headers: { tenant: 'tenant-1', } }; const res = {}; const next = jest.fn(); expect(next).not.toBeCalled(); tokens.verifyToken.mockImplementation(() => __awaiter(this, void 0, void 0, function* () { return null; })); try { yield verifyUser(req, res, next); } catch (_a) { // ignore } expect(next).toBeCalled(); })); }); });