0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-02-24 23:55:46 -05:00
verdaccio/test/functional/uplink.auth.js

152 lines
3.8 KiB
JavaScript
Raw Normal View History

import ProxyStorage from '../../src/lib/up-storage';
import {ERROR_CODE, TOKEN_BASIC, TOKEN_BEARER, DEFAULT_REGISTRY} from "../../src/lib/constants";
import {buildToken} from "../../src/lib/utils";
2017-09-30 00:06:42 -03:00
function createUplink(config) {
const defaultConfig = {
url: DEFAULT_REGISTRY
2017-09-30 00:06:42 -03:00
};
let mergeConfig = Object.assign({}, defaultConfig, config);
2017-12-02 11:19:08 +01:00
return new ProxyStorage(mergeConfig, {});
2017-09-30 00:06:42 -03: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 00:06:42 -03:00
2017-12-02 11:19:08 +01:00
describe('uplink auth test', () => {
2017-09-30 00:06:42 -03:00
2017-12-02 11:19:08 +01:00
test('if set headers empty should return default headers', () => {
2017-09-30 00:06:42 -03:00
const headers = setHeaders();
2017-09-30 21:29:13 -03:00
const keys = Object.keys(headers);
const keysExpected = ['Accept', 'Accept-Encoding', 'User-Agent'];
2017-09-30 00:06:42 -03:00
expect(keys).toEqual(keysExpected);
expect(keys).toHaveLength(3);
2017-09-30 00:06:42 -03:00
});
2017-12-02 11:19:08 +01:00
test('if assigns value invalid to attribute auth', () => {
2017-09-30 00:06:42 -03:00
const fnError = function () {
setHeaders({
auth: ''
});
};
expect(function ( ) {
fnError();
}).toThrow(Error('Auth invalid'));
2017-09-30 00:06:42 -03:00
});
2017-12-02 11:19:08 +01:00
test('if assigns the header authorization', () => {
2017-09-30 00:06:42 -03:00
const headers = setHeaders({}, {
'authorization': buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg==')
2017-09-30 00:06:42 -03:00
});
expect(Object.keys(headers)).toHaveLength(4);
expect(headers['authorization']).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
2017-09-30 00:06:42 -03:00
});
2017-12-02 11:19:08 +01:00
test(
'if assigns headers authorization and token the header precedes',
() => {
const headers = setHeaders({
auth: {
type: TOKEN_BEARER,
2017-12-02 11:19:08 +01:00
token: 'tokenBearer'
}
}, {
'authorization': buildToken(TOKEN_BASIC, 'tokenBasic')
2017-12-02 11:19:08 +01:00
});
2017-09-30 00:06:42 -03:00
expect(headers['authorization']).toEqual(buildToken(TOKEN_BASIC, 'tokenBasic'));
2017-12-02 11:19:08 +01:00
}
);
2017-09-30 00:06:42 -03:00
2017-12-02 11:19:08 +01:00
test('set type auth basic', () => {
2017-09-30 00:06:42 -03:00
const headers = setHeaders({
auth: {
type: TOKEN_BASIC,
2017-09-30 00:06:42 -03:00
token: 'Zm9vX2Jhcg=='
}
});
expect(Object.keys(headers)).toHaveLength(4);
expect(headers['authorization']).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
2017-09-30 00:06:42 -03:00
});
2017-12-02 11:19:08 +01:00
test('set type auth bearer', () => {
2017-09-30 00:06:42 -03:00
const headers = setHeaders({
auth: {
type: TOKEN_BEARER,
2017-09-30 00:06:42 -03:00
token: 'Zm9vX2Jhcf==='
}
});
expect(Object.keys(headers)).toHaveLength(4);
expect(headers['authorization']).toEqual(buildToken(TOKEN_BEARER, 'Zm9vX2Jhcf==='));
2017-09-30 00:06:42 -03:00
});
2017-12-02 11:19:08 +01:00
test('set auth type invalid', () => {
const fnError = function() {
2017-09-30 00:06:42 -03:00
setHeaders({
auth: {
type: 'null',
token: 'Zm9vX2Jhcf==='
}
})
};
2017-12-02 11:19:08 +01:00
expect(function ( ) {
fnError();
}).toThrow(Error(`Auth type 'null' not allowed`));
2017-09-30 00:06:42 -03:00
});
2017-12-02 11:19:08 +01:00
test('set auth with NPM_TOKEN', () => {
2017-09-30 00:06:42 -03:00
process.env.NPM_TOKEN = 'myToken';
const headers = setHeaders({
auth: {
type: TOKEN_BEARER
2017-09-30 00:06:42 -03:00
}
});
expect(headers['authorization']).toBe(`${TOKEN_BEARER} myToken`);
2017-09-30 00:06:42 -03:00
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 00:06:42 -03:00
process.env.NPM_TOKEN_TEST = 'myTokenTest';
const headers = setHeaders({
auth: {
type: TOKEN_BASIC,
2017-09-30 00:06:42 -03:00
token_env: 'NPM_TOKEN_TEST'
}
});
expect(headers['authorization']).toBe(buildToken(TOKEN_BASIC, 'myTokenTest'));
2017-09-30 00:06:42 -03:00
delete process.env.NPM_TOKEN_TEST;
});
2017-12-02 11:19:08 +01:00
test('if token not set', () => {
2017-09-30 00:06:42 -03:00
const fnError = function() {
setHeaders({
auth: {
type: TOKEN_BASIC
2017-09-30 00:06:42 -03:00
}
});
};
expect(function( ) {
fnError();
}).toThrow(ERROR_CODE.token_required);
2017-09-30 00:06:42 -03:00
});
});
2017-12-02 11:19:08 +01:00
}