2017-09-30 05:06:42 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const uplinkStorage = require('../../src/lib/storage/up-storage');
|
|
|
|
const assert = require('assert');
|
|
|
|
|
|
|
|
function createUplink(config) {
|
|
|
|
const defaultConfig = {
|
|
|
|
url: 'https://registry.npmjs.org/'
|
|
|
|
};
|
|
|
|
let mergeConfig = Object.assign({}, defaultConfig, config);
|
|
|
|
return new uplinkStorage(mergeConfig, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setHeaders(config, headers) {
|
|
|
|
config = config || {};
|
|
|
|
headers = headers || {};
|
|
|
|
const uplink = createUplink(config);
|
|
|
|
return uplink._setHeaders({
|
|
|
|
headers
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function () {
|
|
|
|
|
|
|
|
describe('uplink auth test', function () {
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('if set headers empty should return default headers', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
const headers = setHeaders();
|
2017-10-01 02:29:13 +02:00
|
|
|
const keys = Object.keys(headers);
|
|
|
|
const keysExpected = ['Accept', 'Accept-Encoding', 'User-Agent'];
|
2017-09-30 05:06:42 +02:00
|
|
|
|
2017-10-01 02:29:13 +02:00
|
|
|
assert.deepEqual(keys, keysExpected);
|
|
|
|
assert.equal(keys.length, 3);
|
2017-09-30 05:06:42 +02:00
|
|
|
});
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('if assigns value invalid to attribute auth', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
const fnError = function () {
|
|
|
|
setHeaders({
|
|
|
|
auth: ''
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.throws(fnError, 'Auth invalid');
|
|
|
|
});
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('if assigns the header authorization', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
const headers = setHeaders({}, {
|
|
|
|
'authorization': 'basic Zm9vX2Jhcg=='
|
|
|
|
});
|
2017-10-01 02:07:45 +02:00
|
|
|
|
2017-09-30 05:06:42 +02:00
|
|
|
assert.equal(Object.keys(headers).length, 4);
|
|
|
|
assert.equal(headers['authorization'], 'basic Zm9vX2Jhcg==');
|
|
|
|
});
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('if assigns headers authorization and token the header precedes', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'bearer',
|
|
|
|
token: 'tokenBearer'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'authorization': 'basic tokenBasic'
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(headers['authorization'], 'basic tokenBasic');
|
|
|
|
});
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('set type auth basic', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'basic',
|
|
|
|
token: 'Zm9vX2Jhcg=='
|
|
|
|
}
|
|
|
|
});
|
2017-10-01 02:07:45 +02:00
|
|
|
|
2017-09-30 05:06:42 +02:00
|
|
|
assert.equal(Object.keys(headers).length, 4);
|
|
|
|
assert.equal(headers['authorization'], 'Basic Zm9vX2Jhcg==');
|
|
|
|
});
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('set type auth bearer', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'bearer',
|
|
|
|
token: 'Zm9vX2Jhcf==='
|
|
|
|
}
|
|
|
|
});
|
2017-10-01 02:07:45 +02:00
|
|
|
|
2017-09-30 05:06:42 +02:00
|
|
|
assert.equal(Object.keys(headers).length, 4);
|
|
|
|
assert.equal(headers['authorization'], 'Bearer Zm9vX2Jhcf===');
|
|
|
|
});
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('set auth type invalid', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
const fnError = function() {
|
|
|
|
setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'null',
|
|
|
|
token: 'Zm9vX2Jhcf==='
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.throws(fnError, `Auth type 'null' not allowed`);
|
|
|
|
});
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('set auth with NPM_TOKEN', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
process.env.NPM_TOKEN = 'myToken';
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'bearer'
|
|
|
|
}
|
|
|
|
});
|
2017-10-01 02:07:45 +02:00
|
|
|
|
2017-09-30 05:06:42 +02:00
|
|
|
assert.equal(headers['authorization'], 'Bearer myToken');
|
|
|
|
delete process.env.NPM_TOKEN;
|
|
|
|
});
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('set auth with token name and assigns in env', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
process.env.NPM_TOKEN_TEST = 'myTokenTest';
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'basic',
|
|
|
|
token_env: 'NPM_TOKEN_TEST'
|
|
|
|
}
|
|
|
|
});
|
2017-10-01 02:07:45 +02:00
|
|
|
|
2017-09-30 05:06:42 +02:00
|
|
|
assert.equal(headers['authorization'], 'Basic myTokenTest');
|
|
|
|
delete process.env.NPM_TOKEN_TEST;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2017-10-01 02:07:45 +02:00
|
|
|
it('if token not set', function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
const fnError = function() {
|
|
|
|
setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'basic'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.throws(fnError, 'Token is required');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|