X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=test%2Flib%2Fauthenticator.js;h=067d0cae7a889a1eded72506527e29f08d1bc161;hb=eacffdc89c204c1764c6d5944e6a554c3be3c532;hp=3ab6fb34653d0189e5066c44ec4188c4544015bb;hpb=1e2d8a7bdb0df28d08258ee813ee6db77168d59e;p=squeep-authentication-module diff --git a/test/lib/authenticator.js b/test/lib/authenticator.js index 3ab6fb3..067d0ca 100644 --- a/test/lib/authenticator.js +++ b/test/lib/authenticator.js @@ -407,7 +407,7 @@ describe('Authenticator', function () { }); // sessionCheck describe('apiRequiredLocal', function () { - let req, res, ctx; + let req, res; beforeEach(function () { ctx = {}; req = { @@ -423,18 +423,39 @@ describe('Authenticator', function () { sinon.stub(authenticator, 'sessionCheck').resolves(false); sinon.stub(authenticator, 'isValidAuthorization').resolves(true); const result = await authenticator.apiRequiredLocal(req, res, ctx); - assert(authenticator.sessionCheck.called); + assert.strictEqual(result, true); assert(authenticator.isValidAuthorization.called); + assert(!authenticator.sessionCheck.called); + }); + it('covers invalid basic auth', async function () { + req.getHeader.returns('Basic Zm9vOmJhcg=='); + sinon.stub(authenticator, 'sessionCheck').resolves(false); + sinon.stub(authenticator, 'isValidAuthorization').resolves(false); + try { + await authenticator.apiRequiredLocal(req, res, ctx); + assert.fail(noExpectedException); + } catch (e) { + assert.strictEqual(e.statusCode, 401); + assert(!authenticator.sessionCheck.called); + assert(authenticator.isValidAuthorization.called); + } + }); + it('covers missing basic auth, valid session', async function () { + req.getHeader.returns(); + sinon.stub(authenticator, 'sessionCheck').resolves(true); + sinon.stub(authenticator, 'isValidAuthorization').resolves(false); + const result = await authenticator.apiRequiredLocal(req, res, ctx); assert.strictEqual(result, true); + assert(!authenticator.isValidAuthorization.called); + assert(authenticator.sessionCheck.called); }); - it('requests basic auth when missing, ignores session', async function () { + it('covers missing basic auth, ignores session', async function () { req.getHeader.returns(); sinon.stub(authenticator, 'isValidAuthorization').resolves(true); try { await authenticator.apiRequiredLocal(req, res, ctx, false); assert.fail(noExpectedException); } catch (e) { - console.log(e); assert.strictEqual(e.statusCode, 401); assert(!authenticator.sessionCheck.called); assert(!authenticator.isValidAuthorization.called);