2020-08-13 16:27:00 -05:00
|
|
|
import { buildToken } from '@verdaccio/utils';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2020-09-16 23:48:16 -05:00
|
|
|
import {
|
|
|
|
ERROR_CODE,
|
|
|
|
TOKEN_BASIC,
|
|
|
|
TOKEN_BEARER,
|
|
|
|
DEFAULT_REGISTRY,
|
|
|
|
HEADERS,
|
|
|
|
} from '@verdaccio/dev-commons';
|
2020-08-13 16:27:00 -05:00
|
|
|
import { setup } from '@verdaccio/logger';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
|
|
|
import { ProxyStorage } from '../src/up-storage';
|
2018-06-19 15:02:29 -05:00
|
|
|
|
|
|
|
setup([]);
|
|
|
|
|
|
|
|
function createUplink(config) {
|
|
|
|
const defaultConfig = {
|
2020-08-13 16:27:00 -05:00
|
|
|
url: DEFAULT_REGISTRY,
|
2018-06-19 15:02:29 -05:00
|
|
|
};
|
2020-03-03 17:59:19 -05:00
|
|
|
const mergeConfig = Object.assign({}, defaultConfig, config);
|
2019-08-16 14:20:18 -05:00
|
|
|
// @ts-ignore
|
2018-06-19 15:02:29 -05:00
|
|
|
return new ProxyStorage(mergeConfig, {});
|
|
|
|
}
|
|
|
|
|
2019-08-16 14:20:18 -05:00
|
|
|
function setHeaders(config: unknown = {}, headers: unknown = {}) {
|
2018-06-19 15:02:29 -05:00
|
|
|
const uplink = createUplink(config);
|
2020-12-13 15:29:00 -05:00
|
|
|
// @ts-ignore
|
2018-06-19 15:02:29 -05:00
|
|
|
return uplink._setHeaders({
|
2020-08-13 16:27:00 -05:00
|
|
|
headers,
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-20 00:33:39 -05:00
|
|
|
describe('uplink headers auth test', () => {
|
2018-06-19 15:02:29 -05:00
|
|
|
test('if set headers empty should return default headers', () => {
|
|
|
|
const headers = setHeaders();
|
|
|
|
const keys = Object.keys(headers);
|
2019-05-19 14:37:43 -05:00
|
|
|
const keysExpected = [HEADERS.ACCEPT, HEADERS.ACCEPT_ENCODING, HEADERS.USER_AGENT];
|
2018-06-19 15:02:29 -05:00
|
|
|
|
|
|
|
expect(keys).toEqual(keysExpected);
|
|
|
|
expect(keys).toHaveLength(3);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('if assigns value invalid to attribute auth', () => {
|
|
|
|
const fnError = function () {
|
|
|
|
setHeaders({
|
2020-08-13 16:27:00 -05:00
|
|
|
auth: '',
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
expect(function () {
|
2018-06-19 15:02:29 -05:00
|
|
|
fnError();
|
|
|
|
}).toThrow(Error('Auth invalid'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('if assigns the header authorization', () => {
|
2020-08-13 16:27:00 -05:00
|
|
|
const headers = setHeaders(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='),
|
|
|
|
}
|
|
|
|
);
|
2018-06-19 15:02:29 -05:00
|
|
|
|
|
|
|
expect(Object.keys(headers)).toHaveLength(4);
|
2019-05-19 14:37:43 -05:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
test('if assigns headers authorization and token the header precedes', () => {
|
|
|
|
const headers = setHeaders(
|
|
|
|
{
|
2018-06-19 15:02:29 -05:00
|
|
|
auth: {
|
|
|
|
type: TOKEN_BEARER,
|
2020-08-13 16:27:00 -05:00
|
|
|
token: 'tokenBearer',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'tokenBasic'),
|
|
|
|
}
|
|
|
|
);
|
2018-06-19 15:02:29 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'tokenBasic'));
|
|
|
|
});
|
2018-06-19 15:02:29 -05:00
|
|
|
|
|
|
|
test('set type auth basic', () => {
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: TOKEN_BASIC,
|
2020-08-13 16:27:00 -05:00
|
|
|
token: 'Zm9vX2Jhcg==',
|
|
|
|
},
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(Object.keys(headers)).toHaveLength(4);
|
2019-05-19 14:37:43 -05:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('set type auth bearer', () => {
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: TOKEN_BEARER,
|
2020-08-13 16:27:00 -05:00
|
|
|
token: 'Zm9vX2Jhcf===',
|
|
|
|
},
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(Object.keys(headers)).toHaveLength(4);
|
2019-05-19 14:37:43 -05:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BEARER, 'Zm9vX2Jhcf==='));
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
test('set auth type invalid', () => {
|
2020-08-13 16:27:00 -05:00
|
|
|
const fnError = function () {
|
2018-06-19 15:02:29 -05:00
|
|
|
setHeaders({
|
|
|
|
auth: {
|
|
|
|
type: 'null',
|
2020-08-13 16:27:00 -05:00
|
|
|
token: 'Zm9vX2Jhcf===',
|
|
|
|
},
|
|
|
|
});
|
2018-06-19 15:02:29 -05:00
|
|
|
};
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
expect(function () {
|
2018-06-19 15:02:29 -05:00
|
|
|
fnError();
|
|
|
|
}).toThrow(Error(`Auth type 'null' not allowed`));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('set auth with NPM_TOKEN', () => {
|
|
|
|
process.env.NPM_TOKEN = 'myToken';
|
|
|
|
const headers = setHeaders({
|
|
|
|
auth: {
|
2020-08-13 16:27:00 -05:00
|
|
|
type: TOKEN_BEARER,
|
|
|
|
},
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
|
2019-05-19 14:37:43 -05:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toBe(buildToken(TOKEN_BEARER, 'myToken'));
|
2018-06-19 15:02:29 -05: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,
|
2020-08-13 16:27:00 -05:00
|
|
|
token_env: 'NPM_TOKEN_TEST',
|
|
|
|
},
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
|
2019-05-19 14:37:43 -05:00
|
|
|
expect(headers[HEADERS.AUTHORIZATION]).toBe(buildToken(TOKEN_BASIC, 'myTokenTest'));
|
2018-06-19 15:02:29 -05:00
|
|
|
delete process.env.NPM_TOKEN_TEST;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('if token not set', () => {
|
2020-08-13 16:27:00 -05:00
|
|
|
const fnError = function () {
|
2018-06-19 15:02:29 -05:00
|
|
|
setHeaders({
|
|
|
|
auth: {
|
2020-08-13 16:27:00 -05:00
|
|
|
type: TOKEN_BASIC,
|
|
|
|
},
|
2018-06-19 15:02:29 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
expect(function () {
|
2018-06-19 15:02:29 -05:00
|
|
|
fnError();
|
|
|
|
}).toThrow(ERROR_CODE.token_required);
|
|
|
|
});
|
|
|
|
});
|