0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-04-01 02:42:23 -05:00

refactor: test tokens

more usage of constants for tokens
This commit is contained in:
Juan Picado @jotadeveloper 2018-06-19 21:21:25 +02:00
parent 22dd80aa7d
commit 3d19f22ba0
No known key found for this signature in database
GPG key ID: 18AC54485952D158
6 changed files with 74 additions and 53 deletions

View file

@ -5,3 +5,11 @@ export const HEADERS = {
JSON_CHARSET: 'application/json; charset=utf-8',
OCTET_STREAM: 'application/octet-stream',
};
export const ERROR_CODE = {
token_required: 'token is required',
};
export const TOKEN_BASIC = 'Basic';
export const TOKEN_BEARER = 'Bearer';
export const DEFAULT_REGISTRY = 'https://registry.npmjs.org/';

View file

@ -6,16 +6,15 @@ import _ from 'lodash';
import request from 'request';
import Stream from 'stream';
import URL from 'url';
import {parseInterval, isObject, ErrorCode} from './utils';
import {parseInterval, isObject, ErrorCode, buildToken} from './utils';
import {ReadTarball} from '@verdaccio/streams';
import {HEADERS} from '../lib/constants';
import {ERROR_CODE, TOKEN_BASIC, TOKEN_BEARER, HEADERS} from './constants';
import type {
Config,
UpLinkConf,
Callback,
Headers,
Logger,
Config,
UpLinkConf,
Callback,
Headers,
Logger,
} from '@verdaccio/types';
import type {IProxy} from '../../types';
@ -37,10 +36,6 @@ const setConfig = (config, key, def) => {
return _.isNil(config[key]) === false ? config[key] : def;
};
export const TOKEN_BASIC = 'basic';
export const TOKEN_BEARER = 'bearer';
export const DEFAULT_REGISTRY = 'https://registry.npmjs.org/';
/**
* Implements Storage interface
* (same for storage.js, local-storage.js, up-storage.js)
@ -295,15 +290,15 @@ class ProxyStorage implements IProxy {
} else if (_.isBoolean(tokenConf.token_env) && tokenConf.token_env) {
token = process.env.NPM_TOKEN;
} else {
this.logger.error('token is required' );
this._throwErrorAuth('token is required');
this.logger.error(ERROR_CODE.token_required);
this._throwErrorAuth(ERROR_CODE.token_required);
}
} else {
token = process.env.NPM_TOKEN;
}
if (_.isNil(token)) {
this._throwErrorAuth('token is required');
this._throwErrorAuth(ERROR_CODE.token_required);
}
// define type Auth allow basic and bearer
@ -331,12 +326,14 @@ class ProxyStorage implements IProxy {
* @private
*/
_setHeaderAuthorization(headers: any, type: string, token: any) {
if (type !== TOKEN_BEARER && type !== TOKEN_BASIC) {
this._throwErrorAuth(`Auth type '${type}' not allowed`);
const _type: string = type.toLowerCase();
if (_type !== TOKEN_BEARER.toLowerCase() && _type !== TOKEN_BASIC.toLowerCase()) {
this._throwErrorAuth(`Auth type '${_type}' not allowed`);
}
type = _.upperFirst(type);
headers['authorization'] = `${type} ${token}`;
headers['authorization'] = buildToken(type, token);
}
/**

View file

@ -464,6 +464,14 @@ function parseReadme(packageName: string, readme: string): string {
return marked('ERROR: No README data found!');
}
export function capitalizeFirstLetter(character: string) {
return character[0].toUpperCase() + character.slice(1);
}
export function buildToken(type: string, token: string) {
return `${capitalizeFirstLetter(type)} ${token}`;
}
export {
addGravatarSupport,
deleteProperties,

View file

@ -1,21 +1,26 @@
import assert from 'assert';
import crypto from 'crypto';
import {createTarballHash} from "../../src/lib/crypto-utils";
function readfile(x) {
return require('fs').readFileSync(__dirname + '/' + x);
}
const binary = 'fixtures/binary';
const pkgName = 'testpkg-gh29';
const pkgContent = 'blahblah';
export default function (server, server2) {
test('downloading non-existent tarball #1 / srv2', () => {
return server2.getTarball('testpkg-gh29', 'blahblah')
return server2.getTarball(pkgName, pkgContent)
.status(404)
.body_error(/no such package/);
});
describe('pkg-gh29', () => {
beforeAll(function() {
return server.putPackage('testpkg-gh29', require('./fixtures/package')('testpkg-gh29'))
return server.putPackage(pkgName, require('./fixtures/package')(pkgName))
.status(201)
.body_ok(/created new package/);
});
@ -23,14 +28,14 @@ export default function (server, server2) {
test('creating new package / srv1', () => {});
test('downloading non-existent tarball #2 / srv2', () => {
return server2.getTarball('testpkg-gh29', 'blahblah')
return server2.getTarball(pkgName, pkgContent)
.status(404)
.body_error(/no such file available/);
});
describe('tarball', () => {
beforeAll(function() {
return server.putTarball('testpkg-gh29', 'blahblah', readfile('fixtures/binary'))
return server.putTarball(pkgName, pkgContent, readfile(binary))
.status(201)
.body_ok(/.*/);
});
@ -39,10 +44,9 @@ export default function (server, server2) {
describe('pkg version', () => {
beforeAll(function() {
const pkg = require('./fixtures/package')('testpkg-gh29');
pkg.dist.shasum = crypto.createHash('sha1').update(readfile('fixtures/binary')).digest('hex');
return server.putVersion('testpkg-gh29', '0.0.1', pkg)
const pkg = require('./fixtures/package')(pkgName);
pkg.dist.shasum = createTarballHash().update(readfile(binary)).digest('hex');
return server.putVersion(pkgName, '0.0.1', pkg)
.status(201)
.body_ok(/published/);
});
@ -50,10 +54,10 @@ export default function (server, server2) {
test('uploading new package version / srv1', () => {});
test('downloading newly created tarball / srv2', () => {
return server2.getTarball('testpkg-gh29', 'blahblah')
return server2.getTarball(pkgName, pkgContent)
.status(200)
.then(function(body) {
assert.deepEqual(body, readfile('fixtures/binary'));
expect(body).toEqual(readfile(binary));
});
});
});

View file

@ -1,5 +1,6 @@
import assert from 'assert';
import ProxyStorage, {DEFAULT_REGISTRY} from '../../src/lib/up-storage';
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";
function createUplink(config) {
const defaultConfig = {
@ -27,8 +28,8 @@ export default function () {
const keys = Object.keys(headers);
const keysExpected = ['Accept', 'Accept-Encoding', 'User-Agent'];
assert.deepEqual(keys, keysExpected);
assert.equal(keys.length, 3);
expect(keys).toEqual(keysExpected);
expect(keys).toHaveLength(3);
});
test('if assigns value invalid to attribute auth', () => {
@ -45,11 +46,11 @@ export default function () {
test('if assigns the header authorization', () => {
const headers = setHeaders({}, {
'authorization': 'basic Zm9vX2Jhcg=='
'authorization': buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg==')
});
assert.equal(Object.keys(headers).length, 4);
assert.equal(headers['authorization'], 'basic Zm9vX2Jhcg==');
expect(Object.keys(headers)).toHaveLength(4);
expect(headers['authorization']).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
});
test(
@ -57,39 +58,39 @@ export default function () {
() => {
const headers = setHeaders({
auth: {
type: 'bearer',
type: TOKEN_BEARER,
token: 'tokenBearer'
}
}, {
'authorization': 'basic tokenBasic'
'authorization': buildToken(TOKEN_BASIC, 'tokenBasic')
});
assert.equal(headers['authorization'], 'basic tokenBasic');
expect(headers['authorization']).toEqual(buildToken(TOKEN_BASIC, 'tokenBasic'));
}
);
test('set type auth basic', () => {
const headers = setHeaders({
auth: {
type: 'basic',
type: TOKEN_BASIC,
token: 'Zm9vX2Jhcg=='
}
});
assert.equal(Object.keys(headers).length, 4);
assert.equal(headers['authorization'], 'Basic Zm9vX2Jhcg==');
expect(Object.keys(headers)).toHaveLength(4);
expect(headers['authorization']).toEqual(buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg=='));
});
test('set type auth bearer', () => {
const headers = setHeaders({
auth: {
type: 'bearer',
type: TOKEN_BEARER,
token: 'Zm9vX2Jhcf==='
}
});
assert.equal(Object.keys(headers).length, 4);
assert.equal(headers['authorization'], 'Bearer Zm9vX2Jhcf===');
expect(Object.keys(headers)).toHaveLength(4);
expect(headers['authorization']).toEqual(buildToken(TOKEN_BEARER, 'Zm9vX2Jhcf==='));
});
test('set auth type invalid', () => {
@ -111,11 +112,11 @@ export default function () {
process.env.NPM_TOKEN = 'myToken';
const headers = setHeaders({
auth: {
type: 'bearer'
type: TOKEN_BEARER
}
});
assert.equal(headers['authorization'], 'Bearer myToken');
expect(headers['authorization']).toBe(`${TOKEN_BEARER} myToken`);
delete process.env.NPM_TOKEN;
});
@ -123,12 +124,12 @@ export default function () {
process.env.NPM_TOKEN_TEST = 'myTokenTest';
const headers = setHeaders({
auth: {
type: 'basic',
type: TOKEN_BASIC,
token_env: 'NPM_TOKEN_TEST'
}
});
assert.equal(headers['authorization'], 'Basic myTokenTest');
expect(headers['authorization']).toBe(buildToken(TOKEN_BASIC, 'myTokenTest'));
delete process.env.NPM_TOKEN_TEST;
});
@ -137,12 +138,14 @@ export default function () {
const fnError = function() {
setHeaders({
auth: {
type: 'basic'
type: TOKEN_BASIC
}
});
};
assert.throws(fnError, 'Token is required');
expect(function( ) {
fnError();
}).toThrow(ERROR_CODE.token_required);
});
});
}

View file

@ -1,6 +1,6 @@
// @flow
import _ from 'lodash';
import ProxyStorage, {DEFAULT_REGISTRY} from '../../../src/lib/up-storage';
import ProxyStorage from '../../../src/lib/up-storage';
import AppConfig from '../../../src/lib/config';
// $FlowFixMe
import configExample from '../partials/config/index';
@ -8,6 +8,7 @@ import {setup} from '../../../src/lib/logger';
import type {Config, UpLinkConf} from '@verdaccio/types';
import type {IProxy} from '../../../types/index';
import {DEFAULT_REGISTRY} from "../../../src/lib/constants";
setup([]);