1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/test/functional/uplink.auth.spec.js

145 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

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 () {
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
});
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');
});
it('if assigns the header authorization', function () {
2017-09-30 05:06:42 +02:00
const headers = setHeaders({}, {
'authorization': 'basic Zm9vX2Jhcg=='
});
2017-09-30 05:06:42 +02:00
assert.equal(Object.keys(headers).length, 4);
assert.equal(headers['authorization'], 'basic Zm9vX2Jhcg==');
});
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');
});
it('set type auth basic', function () {
2017-09-30 05:06:42 +02:00
const headers = setHeaders({
auth: {
type: 'basic',
token: 'Zm9vX2Jhcg=='
}
});
2017-09-30 05:06:42 +02:00
assert.equal(Object.keys(headers).length, 4);
assert.equal(headers['authorization'], 'Basic Zm9vX2Jhcg==');
});
it('set type auth bearer', function () {
2017-09-30 05:06:42 +02:00
const headers = setHeaders({
auth: {
type: 'bearer',
token: 'Zm9vX2Jhcf==='
}
});
2017-09-30 05:06:42 +02:00
assert.equal(Object.keys(headers).length, 4);
assert.equal(headers['authorization'], 'Bearer Zm9vX2Jhcf===');
});
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`);
});
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-09-30 05:06:42 +02:00
assert.equal(headers['authorization'], 'Bearer myToken');
delete process.env.NPM_TOKEN;
});
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-09-30 05:06:42 +02:00
assert.equal(headers['authorization'], 'Basic myTokenTest');
delete process.env.NPM_TOKEN_TEST;
});
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');
});
});
};