UNPKG

navy

Version:

Quick and powerful development environments using Docker and Docker Compose

66 lines (64 loc) 2.26 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _chai = require("chai"); var _nock = _interopRequireDefault(require("nock")); var _getToken = _interopRequireDefault(require("../get-token")); /* eslint-env mocha */ describe('get-token', function () { afterEach(function () { _nock.default.cleanAll(); }); it('should call the realm with service+scope and return the token from the JSON body', async function () { const scope = (0, _nock.default)('https://auth.docker.io').get('/token').query({ service: 'registry.docker.io', scope: 'repository:library/node:pull' }).matchHeader('authorization', 'Basic dXNlcjpwYXNz').reply(200, { token: 'abc123' }); const token = await (0, _getToken.default)({ realm: 'https://auth.docker.io/token', service: 'registry.docker.io', scope: 'repository:library/node:pull' }, { headers: { Authorization: 'Basic dXNlcjpwYXNz' } }); (0, _chai.expect)(token).to.equal('abc123'); scope.done(); }); it('should throw "Invalid authentication" when the realm responds 401', async function () { (0, _nock.default)('https://auth.docker.io').get('/token').query(true).reply(401, {}); let caught; try { await (0, _getToken.default)({ realm: 'https://auth.docker.io/token', service: 'reg', scope: 'sc' }, { headers: {} }); } catch (err) { caught = err; } (0, _chai.expect)(caught).to.be.an('error'); (0, _chai.expect)(caught.message).to.equal('Invalid authentication'); }); it('should pick only the Authorization header from the supplied headers (no other headers leak)', async function () { (0, _nock.default)('https://auth.example.com').get('/token').query(true).matchHeader('authorization', 'Basic xyz').reply(200, { token: 't' }); const token = await (0, _getToken.default)({ realm: 'https://auth.example.com/token', service: 'svc', scope: 'sc' }, { headers: { Authorization: 'Basic xyz', Cookie: 'should-not-be-sent', 'X-Custom': 'should-not-be-sent' } }); (0, _chai.expect)(token).to.equal('t'); }); });