mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-02-21 07:29:37 +01:00
refactor: small changes in the code
- describe message tests; - remove condition duplicate; - add logger in exceptions;
This commit is contained in:
parent
f7ad05ec86
commit
231a4d7227
@ -239,17 +239,12 @@ class ProxyStorage {
|
|||||||
*/
|
*/
|
||||||
_setAuth(headers) {
|
_setAuth(headers) {
|
||||||
|
|
||||||
if (_.isUndefined(this.config.auth)) {
|
if (_.isNil(this.config.auth) || headers['authorization']) {
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
|
|
||||||
// if header Authorization assigns this has precedence
|
|
||||||
if (headers['authorization']) {
|
|
||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!_.isObject(this.config.auth)) {
|
if (!_.isObject(this.config.auth)) {
|
||||||
throw new Error('Auth invalid');
|
this._throwErrorAuth('Auth invalid');
|
||||||
}
|
}
|
||||||
|
|
||||||
// get NPM_TOKEN http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
|
// get NPM_TOKEN http://blog.npmjs.org/post/118393368555/deploying-with-npm-private-modules
|
||||||
@ -261,8 +256,8 @@ class ProxyStorage {
|
|||||||
token = process.env[this.config.auth.token_env];
|
token = process.env[this.config.auth.token_env];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_.isUndefined(token) || _.isNil(token)) {
|
if (_.isNil(token)) {
|
||||||
throw new Error('Token is required');
|
this._throwErrorAuth('Token is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
// define type Auth allow basic and bearer
|
// define type Auth allow basic and bearer
|
||||||
@ -271,6 +266,16 @@ class ProxyStorage {
|
|||||||
return headers;
|
return headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} message
|
||||||
|
* @throws {Error}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
_throwErrorAuth(message) {
|
||||||
|
this.logger.error(message);
|
||||||
|
throw new Error(message);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Assign Header authorization with type authentication
|
* Assign Header authorization with type authentication
|
||||||
* @param {Object} headers
|
* @param {Object} headers
|
||||||
@ -280,7 +285,7 @@ class ProxyStorage {
|
|||||||
*/
|
*/
|
||||||
_setHeaderAuthorization(headers, type, token) {
|
_setHeaderAuthorization(headers, type, token) {
|
||||||
if (type !== 'bearer' && type !== 'basic') {
|
if (type !== 'bearer' && type !== 'basic') {
|
||||||
throw new Error(`Auth type '${type}' not allowed`);
|
this._throwErrorAuth(`Auth type '${type}' not allowed`);
|
||||||
}
|
}
|
||||||
|
|
||||||
type = _.upperFirst(type);
|
type = _.upperFirst(type);
|
||||||
|
@ -24,13 +24,13 @@ module.exports = function () {
|
|||||||
|
|
||||||
describe('uplink auth test', function () {
|
describe('uplink auth test', function () {
|
||||||
|
|
||||||
it('if set headers empty', function () {
|
it('if set headers empty should return default headers', function () {
|
||||||
const headers = setHeaders();
|
const headers = setHeaders();
|
||||||
|
|
||||||
assert.equal(Object.keys(headers).length, 3);
|
assert.equal(Object.keys(headers).length, 3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('invalid auth', function () {
|
it('if assigns value invalid to attribute auth', function () {
|
||||||
const fnError = function () {
|
const fnError = function () {
|
||||||
setHeaders({
|
setHeaders({
|
||||||
auth: ''
|
auth: ''
|
||||||
@ -40,15 +40,16 @@ module.exports = function () {
|
|||||||
assert.throws(fnError, 'Auth invalid');
|
assert.throws(fnError, 'Auth invalid');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('if set headers authorization', function () {
|
it('if assigns the header authorization', function () {
|
||||||
const headers = setHeaders({}, {
|
const headers = setHeaders({}, {
|
||||||
'authorization': 'basic Zm9vX2Jhcg=='
|
'authorization': 'basic Zm9vX2Jhcg=='
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(Object.keys(headers).length, 4);
|
assert.equal(Object.keys(headers).length, 4);
|
||||||
assert.equal(headers['authorization'], 'basic Zm9vX2Jhcg==');
|
assert.equal(headers['authorization'], 'basic Zm9vX2Jhcg==');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('if set headers authorization precendence token', function () {
|
it('if assigns headers authorization and token the header precedes', function () {
|
||||||
const headers = setHeaders({
|
const headers = setHeaders({
|
||||||
auth: {
|
auth: {
|
||||||
type: 'bearer',
|
type: 'bearer',
|
||||||
@ -61,29 +62,31 @@ module.exports = function () {
|
|||||||
assert.equal(headers['authorization'], 'basic tokenBasic');
|
assert.equal(headers['authorization'], 'basic tokenBasic');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('basic auth test', function () {
|
it('set type auth basic', function () {
|
||||||
const headers = setHeaders({
|
const headers = setHeaders({
|
||||||
auth: {
|
auth: {
|
||||||
type: 'basic',
|
type: 'basic',
|
||||||
token: 'Zm9vX2Jhcg=='
|
token: 'Zm9vX2Jhcg=='
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(Object.keys(headers).length, 4);
|
assert.equal(Object.keys(headers).length, 4);
|
||||||
assert.equal(headers['authorization'], 'Basic Zm9vX2Jhcg==');
|
assert.equal(headers['authorization'], 'Basic Zm9vX2Jhcg==');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('basic auth test', function () {
|
it('set type auth bearer', function () {
|
||||||
const headers = setHeaders({
|
const headers = setHeaders({
|
||||||
auth: {
|
auth: {
|
||||||
type: 'bearer',
|
type: 'bearer',
|
||||||
token: 'Zm9vX2Jhcf==='
|
token: 'Zm9vX2Jhcf==='
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(Object.keys(headers).length, 4);
|
assert.equal(Object.keys(headers).length, 4);
|
||||||
assert.equal(headers['authorization'], 'Bearer Zm9vX2Jhcf===');
|
assert.equal(headers['authorization'], 'Bearer Zm9vX2Jhcf===');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('invalid auth type test', function () {
|
it('set auth type invalid', function () {
|
||||||
const fnError = function() {
|
const fnError = function() {
|
||||||
setHeaders({
|
setHeaders({
|
||||||
auth: {
|
auth: {
|
||||||
@ -96,18 +99,19 @@ module.exports = function () {
|
|||||||
assert.throws(fnError, `Auth type 'null' not allowed`);
|
assert.throws(fnError, `Auth type 'null' not allowed`);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('get NPM_TOKEN process test', function () {
|
it('set auth with NPM_TOKEN', function () {
|
||||||
process.env.NPM_TOKEN = 'myToken';
|
process.env.NPM_TOKEN = 'myToken';
|
||||||
const headers = setHeaders({
|
const headers = setHeaders({
|
||||||
auth: {
|
auth: {
|
||||||
type: 'bearer'
|
type: 'bearer'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(headers['authorization'], 'Bearer myToken');
|
assert.equal(headers['authorization'], 'Bearer myToken');
|
||||||
delete process.env.NPM_TOKEN;
|
delete process.env.NPM_TOKEN;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('get name variable assigns process.env test', function () {
|
it('set auth with token name and assigns in env', function () {
|
||||||
process.env.NPM_TOKEN_TEST = 'myTokenTest';
|
process.env.NPM_TOKEN_TEST = 'myTokenTest';
|
||||||
const headers = setHeaders({
|
const headers = setHeaders({
|
||||||
auth: {
|
auth: {
|
||||||
@ -115,12 +119,13 @@ module.exports = function () {
|
|||||||
token_env: 'NPM_TOKEN_TEST'
|
token_env: 'NPM_TOKEN_TEST'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
assert.equal(headers['authorization'], 'Basic myTokenTest');
|
assert.equal(headers['authorization'], 'Basic myTokenTest');
|
||||||
delete process.env.NPM_TOKEN_TEST;
|
delete process.env.NPM_TOKEN_TEST;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
it('token is required', function () {
|
it('if token not set', function () {
|
||||||
const fnError = function() {
|
const fnError = function() {
|
||||||
setHeaders({
|
setHeaders({
|
||||||
auth: {
|
auth: {
|
||||||
|
Loading…
Reference in New Issue
Block a user