verdaccio/packages/proxy/test/headers.auth.spec.ts

154 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-03-03 23:59:19 +01:00
import {buildToken} from "@verdaccio/utils";
import {ERROR_CODE, TOKEN_BASIC, TOKEN_BEARER, DEFAULT_REGISTRY, HEADERS} from "@verdaccio/dev-commons";
import {setup} from '@verdaccio/logger';
import { ProxyStorage } from '../src/up-storage';
setup([]);
function createUplink(config) {
const defaultConfig = {
url: DEFAULT_REGISTRY
};
2020-03-03 23:59:19 +01:00
const mergeConfig = Object.assign({}, defaultConfig, config);
// @ts-ignore
return new ProxyStorage(mergeConfig, {});
}
function setHeaders(config: unknown = {}, headers: unknown = {}) {
const uplink = createUplink(config);
// @ts-ignore
return uplink._setHeaders({
headers
});
}
2019-05-20 07:33:39 +02:00
describe('uplink headers auth test', () => {
2020-03-03 23:59:19 +01:00
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];
expect(keys).toEqual(keysExpected);
expect(keys).toHaveLength(3);
});
test('if assigns value invalid to attribute auth', () => {
const fnError = function () {
setHeaders({
auth: ''
});
};
2020-03-03 23:59:19 +01:00
expect(function ( ) {
fnError();
}).toThrow(Error('Auth invalid'));
});
test('if assigns the header authorization', () => {
2020-03-03 23:59:19 +01:00
const headers = setHeaders({}, {
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg==')
});
expect(Object.keys(headers)).toHaveLength(4);
2019-05-19 21:37:43 +02:00
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
});
2020-03-03 23:59:19 +01:00
test(
'if assigns headers authorization and token the header precedes',
() => {
const headers = setHeaders({
auth: {
type: TOKEN_BEARER,
token: 'tokenBearer'
}
2020-03-03 23:59:19 +01:00
}, {
2019-05-19 21:37:43 +02:00
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'tokenBasic')
2020-03-03 23:59:19 +01:00
});
2020-03-03 23:59:19 +01:00
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'tokenBasic'));
}
);
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=='));
});
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==='));
});
test('set auth type invalid', () => {
2020-03-03 23:59:19 +01:00
const fnError = function() {
setHeaders({
auth: {
type: 'null',
token: 'Zm9vX2Jhcf==='
}
2020-03-03 23:59:19 +01:00
})
};
2020-03-03 23:59:19 +01:00
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'));
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'));
delete process.env.NPM_TOKEN_TEST;
});
2020-03-03 23:59:19 +01:00
test('if token not set', () => {
2020-03-03 23:59:19 +01:00
const fnError = function() {
setHeaders({
auth: {
type: TOKEN_BASIC
}
});
};
2020-03-03 23:59:19 +01:00
expect(function( ) {
fnError();
}).toThrow(ERROR_CODE.token_required);
});
});