X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;f=test%2Flib%2Fauthenticator.js;fp=test%2Flib%2Fauthenticator.js;h=067d0cae7a889a1eded72506527e29f08d1bc161;hb=c13db4d55a8cf25c109dfcbb88a2d7828b791147;hp=3ab6fb34653d0189e5066c44ec4188c4544015bb;hpb=ecf9c52f00dae3b9eee8316a1cffbc496f0fbd67;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);