0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-23 22:27:34 -05:00
verdaccio/test/unit/modules/uplinks/headers.auth.spec.ts
Juan Picado @jotadeveloper 66f4197236
feat: convert project to typescript (#1374)
* chore: test

* chore: add

* chore: more progress

* chore: progress in migration, fix prettier parser

* chore: reduce tsc errors

* chore: refactor storage utils types

* chore: refactor utils types

* chore: refactor local storage types

* chore: refactor config utils types

* chore: refactor tsc types

* refactor: apply eslint fix, tabs etc

* chore: fix lint errors

* test: update unit test conf to typescript setup

few test refactored to typescript

* chore: enable more unit test

migrate to typescript

* chore: migrate storage test to tsc

* chore: migrate up storage test to tsc

* refactor: enable plugin and auth test

* chore: migrate plugin loader test

* chore: update dependencies

* chore: migrate functional test to typescript

* chore: add codecove

* chore: update express

* chore: downgrade puppeteer

The latest version does not seems to work properly fine.

* chore: update dependencies
2019-07-16 08:40:01 +02:00

151 lines
3.8 KiB
TypeScript

import ProxyStorage from '../../../../src/lib/up-storage';
import {ERROR_CODE, TOKEN_BASIC, TOKEN_BEARER, DEFAULT_REGISTRY, HEADERS} from "../../../../src/lib/constants";
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 headers auth test', () => {
test('if set headers empty should return default headers', () => {
const headers = setHeaders();
const keys = Object.keys(headers);
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: ''
});
};
expect(function ( ) {
fnError();
}).toThrow(Error('Auth invalid'));
});
test('if assigns the header authorization', () => {
const headers = setHeaders({}, {
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg==')
});
expect(Object.keys(headers)).toHaveLength(4);
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
});
test(
'if assigns headers authorization and token the header precedes',
() => {
const headers = setHeaders({
auth: {
type: TOKEN_BEARER,
token: 'tokenBearer'
}
}, {
[HEADERS.AUTHORIZATION]: buildToken(TOKEN_BASIC, 'tokenBasic')
});
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);
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);
expect(headers[HEADERS.AUTHORIZATION]).toEqual(buildToken(TOKEN_BEARER, 'Zm9vX2Jhcf==='));
});
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
}
});
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'
}
});
expect(headers[HEADERS.AUTHORIZATION]).toBe(buildToken(TOKEN_BASIC, 'myTokenTest'));
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);
});
});