2018-06-19 22:02:29 +02:00
|
|
|
import ProxyStorage from '../../../src/lib/up-storage';
|
2019-05-19 21:37:43 +02:00
|
|
|
import {ERROR_CODE, TOKEN_BASIC, TOKEN_BEARER, DEFAULT_REGISTRY, HEADERS} from "../../../src/lib/constants";
|
2018-06-19 22:02:29 +02:00
|
|
|
import {buildToken} from "../../../src/lib/utils";
|
|
|
|
import {setup} from '../../../src/lib/logger';
|
|
|
|
|
|
|
|
setup([]);
|
|
|
|
|
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('uplink auth test', () => {
|
|
|
|
|
|
|
|
test('if set headers empty should return default headers', () => {
|
|
|
|
const headers = setHeaders();
|
|
|
|
const keys = Object.keys(headers);
|
2019-05-19 21:37:43 +02:00
|
|
|
const keysExpected = [HEADERS.ACCEPT, HEADERS.ACCEPT_ENCODING, HEADERS.USER_AGENT];
|
2018-06-19 22:02:29 +02:00
|
|
|
|
|
|
|
expect(keys).toEqual(keysExpected);
|
|
|
|
expect(keys).toHaveLength(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({}, {
|
2019-05-19 21:37:43 +02:00
|
|
|
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg==')
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(Object.keys(headers)).toHaveLength(4);
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test(
|
|
|
|
'if assigns headers authorization and token the header precedes',
|
|
|
|
() => {
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: TOKEN_BEARER,
|
|
|
|
token: 'tokenBearer'
|
|
|
|
}
|
|
|
|
}, {
|
2019-05-19 21:37:43 +02:00
|
|
|
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'tokenBasic')
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'tokenBasic'));
|
2018-06-19 22:02:29 +02:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
test('set type auth basic', () => {
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: TOKEN_BASIC,
|
|
|
|
token: 'Zm9vX2Jhcg=='
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(Object.keys(headers)).toHaveLength(4);
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('set type auth bearer', () => {
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: TOKEN_BEARER,
|
|
|
|
token: 'Zm9vX2Jhcf==='
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(Object.keys(headers)).toHaveLength(4);
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BEARER, 'Zm9vX2Jhcf==='));
|
2018-06-19 22:02:29 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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: TOKEN_BEARER
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toBe(buildToken(TOKEN_BEARER, 'myToken'));
|
2018-06-19 22:02:29 +02:00
|
|
|
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: TOKEN_BASIC,
|
|
|
|
token_env: 'NPM_TOKEN_TEST'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-19 21:37:43 +02:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toBe(buildToken(TOKEN_BASIC, 'myTokenTest'));
|
2018-06-19 22:02:29 +02:00
|
|
|
delete process.env.NPM_TOKEN_TEST;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('if token not set', () => {
|
|
|
|
const fnError = function() {
|
|
|
|
setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: TOKEN_BASIC
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(function( ) {
|
|
|
|
fnError();
|
|
|
|
}).toThrow(ERROR_CODE.token_required);
|
|
|
|
});
|
|
|
|
});
|