UNPKG

navy

Version:

Quick and powerful development environments using Docker and Docker Compose

124 lines (122 loc) 4.39 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _chai = require("chai"); var _sinon = _interopRequireDefault(require("sinon")); var _nock = _interopRequireDefault(require("nock")); var _proxyquire = _interopRequireDefault(require("proxyquire")); /* eslint-env mocha */ function loadModule({ getCredentials, getToken } = {}) { return _proxyquire.default.noCallThru()('../get-endpoint', { './get-credentials': getCredentials || _sinon.default.stub().resolves({ username: 'u', password: 'p' }), './get-token': getToken || _sinon.default.stub().resolves('the-token') }); } describe('get-endpoint', function () { afterEach(function () { _nock.default.cleanAll(); }); it('should return the response when the registry replies 200', async function () { const body = { schemaVersion: 2 }; (0, _nock.default)('https://reg.example.com').get('/v2/library/node/manifests/latest').reply(200, body); const getEndpoint = loadModule(); const response = await getEndpoint({ registry: 'reg.example.com', endpoint: 'library/node/manifests/latest' }); (0, _chai.expect)(response.status).to.equal(200); (0, _chai.expect)(await response.json()).to.eql(body); }); it('should throw "Operation not found" when the registry returns 404', async function () { (0, _nock.default)('https://reg.example.com').get('/v2/missing').reply(404, { errors: [] }); const getEndpoint = loadModule(); let caught; try { await getEndpoint({ registry: 'reg.example.com', endpoint: 'missing' }); } catch (err) { caught = err; } (0, _chai.expect)(caught).to.be.an('error'); (0, _chai.expect)(caught.message).to.equal('Operation not found'); }); it('should retry with a Bearer token when the response is 401 with a Bearer challenge', async function () { (0, _nock.default)('https://reg.example.com').get('/v2/secret').reply(401, '', { 'WWW-Authenticate': 'Bearer realm="https://auth.example.com/token",service="reg.example.com",scope="repository:secret:pull"' }).get('/v2/secret').matchHeader('authorization', 'Bearer the-token').reply(200, { ok: true }); const getToken = _sinon.default.stub().resolves('the-token'); const getEndpoint = loadModule({ getToken }); const response = await getEndpoint({ registry: 'reg.example.com', endpoint: 'secret' }); (0, _chai.expect)(response.status).to.equal(200); (0, _chai.expect)(await response.json()).to.eql({ ok: true }); (0, _chai.expect)(getToken.calledOnce).to.equal(true); }); it('should throw "Access denied" when the bearer-token retry still returns 401', async function () { (0, _nock.default)('https://reg.example.com').get('/v2/secret').reply(401, '', { 'WWW-Authenticate': 'Bearer realm="https://auth.example.com/token",service="reg.example.com",scope="repository:secret:pull"' }).get('/v2/secret').reply(401, ''); const getEndpoint = loadModule(); let caught; try { await getEndpoint({ registry: 'reg.example.com', endpoint: 'secret' }); } catch (err) { caught = err; } (0, _chai.expect)(caught.message).to.equal('Access denied'); }); it('should throw "Invalid authentication" for a Basic challenge', async function () { (0, _nock.default)('https://reg.example.com').get('/v2/needs-basic').reply(401, '', { 'WWW-Authenticate': 'Basic realm="reg"' }); const getEndpoint = loadModule(); let caught; try { await getEndpoint({ registry: 'reg.example.com', endpoint: 'needs-basic' }); } catch (err) { caught = err; } (0, _chai.expect)(caught.message).to.equal('Invalid authentication'); }); it('should throw "Invalid authentication" for an unknown auth scheme', async function () { (0, _nock.default)('https://reg.example.com').get('/v2/weird').reply(401, '', { 'WWW-Authenticate': 'Digest realm="reg"' }); const getEndpoint = loadModule(); let caught; try { await getEndpoint({ registry: 'reg.example.com', endpoint: 'weird' }); } catch (err) { caught = err; } (0, _chai.expect)(caught.message).to.equal('Invalid authentication'); }); });