UNPKG

koa-userinfo

Version:

openid style userinfo as middleware, for koa

80 lines (73 loc) 2.19 kB
"use strict"; var _ = _interopRequireDefault(require("../")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } jest.mock('../request-userinfo'); const newCtx = () => ({ headers: {}, throw: jest.fn() }); describe('the middleware', () => { describe('creating it', () => { it('fails without site', () => { expect(() => (0, _.default)({})).toThrow(); }); }); describe('using it', () => { it('throws without authorization header', () => { const middleware = (0, _.default)({ site: '123' }); const ctx = newCtx(); middleware(ctx); expect(ctx.throw.mock.calls.length).toBe(1); }); it('throws with invalid authorization header', () => { const middleware = (0, _.default)({ site: '123' }); const ctx = newCtx(); ctx.headers.authorization = 'this is not okay'; middleware(ctx); expect(ctx.throw.mock.calls.length).toBe(1); }); it('does not throw if throwing disabled', async () => { const middleware = (0, _.default)({ site: '123', allowMissingOrInvalidToken: true }); const ctx = newCtx(); return await new Promise((resolve, reject) => { middleware(ctx, () => resolve()); }); }); it('adds "userinfo" to the context', async () => { const middleware = (0, _.default)({ site: 'some-site' }); const ctx = newCtx(); ctx.headers.authorization = 'Bearer 123'; return await new Promise(resolve => { middleware(ctx, () => { expect(ctx.userinfo).toBeTruthy(); resolve(); }); }); }); it('throws the original error, in case of a downstream error', async () => { const middleware = (0, _.default)({ site: 'some-site' }); const ctx = newCtx(); ctx.headers.authorization = 'Bearer 123'; const next = async () => { throw new Error('oops'); }; try { await middleware(ctx, next); throw new Error('FAIL: should have thrown!'); } catch (e) { expect(e.message).toMatch(/oops/); } }); }); });