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