2017-12-02 11:19:08 +01:00
|
|
|
import assert from 'assert';
|
2018-05-14 20:08:49 +02:00
|
|
|
import ProxyStorage, {DEFAULT_REGISTRY} from '../../src/lib/up-storage';
|
2017-09-30 05:06:42 +02:00
|
|
|
|
|
|
|
function createUplink(config) {
|
|
|
|
const defaultConfig = {
|
2018-05-14 20:08:49 +02:00
|
|
|
url: DEFAULT_REGISTRY
|
2017-09-30 05:06:42 +02:00
|
|
|
};
|
|
|
|
let mergeConfig = Object.assign({}, defaultConfig, config);
|
2017-12-02 11:19:08 +01:00
|
|
|
return new ProxyStorage(mergeConfig, {});
|
2017-09-30 05:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function setHeaders(config, headers) {
|
|
|
|
config = config || {};
|
|
|
|
headers = headers || {};
|
|
|
|
const uplink = createUplink(config);
|
|
|
|
return uplink._setHeaders({
|
|
|
|
headers
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
export default function () {
|
2017-09-30 05:06:42 +02:00
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
describe('uplink auth test', () => {
|
2017-09-30 05:06:42 +02:00
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
test('if set headers empty should return default headers', () => {
|
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-12-02 11:19:08 +01:00
|
|
|
test('if assigns value invalid to attribute auth', () => {
|
2017-09-30 05:06:42 +02:00
|
|
|
const fnError = function () {
|
|
|
|
setHeaders({
|
|
|
|
auth: ''
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-05-20 21:46:54 +02:00
|
|
|
expect(function ( ) {
|
|
|
|
fnError();
|
|
|
|
}).toThrow(Error('Auth invalid'));
|
2017-09-30 05:06:42 +02:00
|
|
|
});
|
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
test('if assigns the header authorization', () => {
|
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-12-02 11:19:08 +01:00
|
|
|
test(
|
|
|
|
'if assigns headers authorization and token the header precedes',
|
|
|
|
() => {
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'bearer',
|
|
|
|
token: 'tokenBearer'
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
'authorization': 'basic tokenBasic'
|
|
|
|
});
|
2017-09-30 05:06:42 +02:00
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
assert.equal(headers['authorization'], 'basic tokenBasic');
|
|
|
|
}
|
|
|
|
);
|
2017-09-30 05:06:42 +02:00
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
test('set type auth basic', () => {
|
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-12-02 11:19:08 +01:00
|
|
|
test('set type auth bearer', () => {
|
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-12-02 11:19:08 +01:00
|
|
|
test('set auth type invalid', () => {
|
|
|
|
const fnError = function() {
|
2017-09-30 05:06:42 +02:00
|
|
|
setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'null',
|
|
|
|
token: 'Zm9vX2Jhcf==='
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
2017-12-02 11:19:08 +01:00
|
|
|
|
2018-05-20 21:46:54 +02:00
|
|
|
expect(function ( ) {
|
|
|
|
fnError();
|
|
|
|
}).toThrow(Error(`Auth type 'null' not allowed`));
|
2017-09-30 05:06:42 +02:00
|
|
|
});
|
|
|
|
|
2017-12-02 11:19:08 +01:00
|
|
|
test('set auth with NPM_TOKEN', () => {
|
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-12-02 11:19:08 +01:00
|
|
|
test('set auth with token name and assigns in env', () => {
|
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-12-02 11:19:08 +01:00
|
|
|
test('if token not set', () => {
|
2017-09-30 05:06:42 +02:00
|
|
|
const fnError = function() {
|
|
|
|
setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'basic'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.throws(fnError, 'Token is required');
|
|
|
|
});
|
|
|
|
});
|
2017-12-02 11:19:08 +01:00
|
|
|
}
|