X-Git-Url: http://git.squeep.com/?p=squeep-authentication-module;a=blobdiff_plain;f=test%2Flib%2Fauthenticator.js;h=3ab6fb34653d0189e5066c44ec4188c4544015bb;hp=cc75652443568a1a320b4b418de665d55693971a;hb=1e2d8a7bdb0df28d08258ee813ee6db77168d59e;hpb=dd173e6b450cbba8100883514610c9fde83d050a diff --git a/test/lib/authenticator.js b/test/lib/authenticator.js index cc75652..3ab6fb3 100644 --- a/test/lib/authenticator.js +++ b/test/lib/authenticator.js @@ -406,4 +406,41 @@ describe('Authenticator', function () { }); // convenience wrappers }); // sessionCheck + describe('apiRequiredLocal', function () { + let req, res, ctx; + beforeEach(function () { + ctx = {}; + req = { + getHeader: sinon.stub(), + }; + res = { + end: sinon.stub(), + setHeader: sinon.stub(), + }; + }); + it('covers valid basic auth', async function () { + req.getHeader.returns('Basic Zm9vOmJhcg=='); + 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(authenticator.isValidAuthorization.called); + assert.strictEqual(result, true); + }); + it('requests basic auth when missing, 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); + assert(res.setHeader.called); + } + }); + }); // apiRequiredLocal + }); // Authenticator