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
Juan Picado @jotadeveloper cee2564ac2
feat: add node 10 support for test and docker
update Docker to Node 10
we use yarn 1.6.0 as default for build
2018-05-20 21:46:54 +02:00

149 lines
3.5 KiB
JavaScript

import assert from 'assert';
import ProxyStorage, {DEFAULT_REGISTRY} from '../../src/lib/up-storage';
function createUplink(config) {
const defaultConfig = {
url: DEFAULT_REGISTRY
};
let mergeConfig = Object.assign({}, defaultConfig, config);
return new ProxyStorage(mergeConfig, {});
}
function setHeaders(config, headers) {
config = config || {};
headers = headers || {};
const uplink = createUplink(config);
return uplink._setHeaders({
headers
});
}
export default function () {
describe('uplink auth test', () => {
test('if set headers empty should return default headers', () => {
const headers = setHeaders();
const keys = Object.keys(headers);
const keysExpected = ['Accept', 'Accept-Encoding', 'User-Agent'];
assert.deepEqual(keys, keysExpected);
assert.equal(keys.length, 3);
});
test('if assigns value invalid to attribute auth', () => {
const fnError = function () {
setHeaders({
auth: ''
});
};
expect(function ( ) {
fnError();
}).toThrow(Error('Auth invalid'));
});
test('if assigns the header authorization', () => {
const headers = setHeaders({}, {
'authorization': 'basic Zm9vX2Jhcg=='
});
assert.equal(Object.keys(headers).length, 4);
assert.equal(headers['authorization'], 'basic Zm9vX2Jhcg==');
});
test(
'if assigns headers authorization and token the header precedes',
() => {
const headers = setHeaders({
auth: {
type: 'bearer',
token: 'tokenBearer'
}
}, {
'authorization': 'basic tokenBasic'
});
assert.equal(headers['authorization'], 'basic tokenBasic');
}
);
test('set type auth basic', () => {
const headers = setHeaders({
auth: {
type: 'basic',
token: 'Zm9vX2Jhcg=='
}
});
assert.equal(Object.keys(headers).length, 4);
assert.equal(headers['authorization'], 'Basic Zm9vX2Jhcg==');
});
test('set type auth bearer', () => {
const headers = setHeaders({
auth: {
type: 'bearer',
token: 'Zm9vX2Jhcf==='
}
});
assert.equal(Object.keys(headers).length, 4);
assert.equal(headers['authorization'], 'Bearer Zm9vX2Jhcf===');
});
test('set auth type invalid', () => {
const fnError = function() {
setHeaders({
auth: {
type: 'null',
token: 'Zm9vX2Jhcf==='
}
})
};
expect(function ( ) {
fnError();
}).toThrow(Error(`Auth type 'null' not allowed`));
});
test('set auth with NPM_TOKEN', () => {
process.env.NPM_TOKEN = 'myToken';
const headers = setHeaders({
auth: {
type: 'bearer'
}
});
assert.equal(headers['authorization'], 'Bearer myToken');
delete process.env.NPM_TOKEN;
});
test('set auth with token name and assigns in env', () => {
process.env.NPM_TOKEN_TEST = 'myTokenTest';
const headers = setHeaders({
auth: {
type: 'basic',
token_env: 'NPM_TOKEN_TEST'
}
});
assert.equal(headers['authorization'], 'Basic myTokenTest');
delete process.env.NPM_TOKEN_TEST;
});
test('if token not set', () => {
const fnError = function() {
setHeaders({
auth: {
type: 'basic'
}
});
};
assert.throws(fnError, 'Token is required');
});
});
}