0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-06 22:40:26 -05:00

refactor: using constants

relocate uplink token test to unit test
This commit is contained in:
Juan Picado @jotadeveloper 2018-06-19 22:02:29 +02:00
parent 3d19f22ba0
commit d8455dd15a
No known key found for this signature in database
GPG key ID: 18AC54485952D158
7 changed files with 199 additions and 186 deletions

View file

@ -13,3 +13,13 @@ export const ERROR_CODE = {
export const TOKEN_BASIC = 'Basic'; export const TOKEN_BASIC = 'Basic';
export const TOKEN_BEARER = 'Bearer'; export const TOKEN_BEARER = 'Bearer';
export const DEFAULT_REGISTRY = 'https://registry.npmjs.org/'; export const DEFAULT_REGISTRY = 'https://registry.npmjs.org/';
export const HTTP_STATUS = {
INTERNAL_ERROR: 500,
OK: 200,
CREATED: 201,
NOT_FOUND: 404,
BAD_REQUEST: 400,
UNAUTHORIZED: 401,
};

View file

@ -19,14 +19,13 @@ import notify from './notifications/notify';
import incomplete from './sanity/incomplete'; import incomplete from './sanity/incomplete';
import mirror from './sanity/mirror'; import mirror from './sanity/mirror';
import readme from './readme/readme'; import readme from './readme/readme';
import gh29 from './gh29'; import gh29 from './scenarios/gh29';
import nullstorage from './sanity/nullstorage'; import nullstorage from './sanity/nullstorage';
import racycrash from './sanity/racycrash'; import racycrash from './sanity/racycrash';
import security from './sanity/security'; import security from './sanity/security';
import race from './performance/race'; import race from './performance/race';
import pluginsAuth from './plugins/auth'; import pluginsAuth from './plugins/auth';
import upLinkCache from './uplink.cache'; import upLinkCache from './uplinks/cache';
import upLinkAuth from './uplink.auth';
describe('functional test verdaccio', function() { describe('functional test verdaccio', function() {
jest.setTimeout(10000); jest.setTimeout(10000);
@ -55,7 +54,6 @@ describe('functional test verdaccio', function() {
notify(app); notify(app);
// requires packages published to server1/server2 // requires packages published to server1/server2
upLinkCache(server1, server2, server3); upLinkCache(server1, server2, server3);
upLinkAuth();
adduser(server1); adduser(server1);
logout(server1); logout(server1);
basic(server1, server2); basic(server1, server2);

View file

@ -1,10 +1,15 @@
import {createTarballHash} from "../../src/lib/crypto-utils"; import {createTarballHash} from "../../../src/lib/crypto-utils";
import {HTTP_STATUS} from "../../../src/lib/constants";
import fs from 'fs';
import path from 'path';
function readfile(x) { function readfile(filePath) {
return require('fs').readFileSync(__dirname + '/' + x); const folder = path.join(__dirname , filePath);
return fs.readFileSync(folder);
} }
const binary = 'fixtures/binary'; const binary = '../fixtures/binary';
const pkgName = 'testpkg-gh29'; const pkgName = 'testpkg-gh29';
const pkgContent = 'blahblah'; const pkgContent = 'blahblah';
@ -12,7 +17,7 @@ export default function (server, server2) {
test('downloading non-existent tarball #1 / srv2', () => { test('downloading non-existent tarball #1 / srv2', () => {
return server2.getTarball(pkgName, pkgContent) return server2.getTarball(pkgName, pkgContent)
.status(404) .status(HTTP_STATUS.NOT_FOUND)
.body_error(/no such package/); .body_error(/no such package/);
}); });
@ -20,8 +25,8 @@ export default function (server, server2) {
beforeAll(function() { beforeAll(function() {
return server.putPackage(pkgName, require('./fixtures/package')(pkgName)) return server.putPackage(pkgName, require('../fixtures/package')(pkgName))
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/created new package/); .body_ok(/created new package/);
}); });
@ -29,14 +34,14 @@ export default function (server, server2) {
test('downloading non-existent tarball #2 / srv2', () => { test('downloading non-existent tarball #2 / srv2', () => {
return server2.getTarball(pkgName, pkgContent) return server2.getTarball(pkgName, pkgContent)
.status(404) .status(HTTP_STATUS.NOT_FOUND)
.body_error(/no such file available/); .body_error(/no such file available/);
}); });
describe('tarball', () => { describe('tarball', () => {
beforeAll(function() { beforeAll(function() {
return server.putTarball(pkgName, pkgContent, readfile(binary)) return server.putTarball(pkgName, pkgContent, readfile(binary))
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/.*/); .body_ok(/.*/);
}); });
@ -44,10 +49,10 @@ export default function (server, server2) {
describe('pkg version', () => { describe('pkg version', () => {
beforeAll(function() { beforeAll(function() {
const pkg = require('./fixtures/package')(pkgName); const pkg = require('../fixtures/package')(pkgName);
pkg.dist.shasum = createTarballHash().update(readfile(binary)).digest('hex'); pkg.dist.shasum = createTarballHash().update(readfile(binary)).digest('hex');
return server.putVersion(pkgName, '0.0.1', pkg) return server.putVersion(pkgName, '0.0.1', pkg)
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/published/); .body_ok(/published/);
}); });
@ -55,7 +60,7 @@ export default function (server, server2) {
test('downloading newly created tarball / srv2', () => { test('downloading newly created tarball / srv2', () => {
return server2.getTarball(pkgName, pkgContent) return server2.getTarball(pkgName, pkgContent)
.status(200) .status(HTTP_STATUS.OK)
.then(function(body) { .then(function(body) {
expect(body).toEqual(readfile(binary)); expect(body).toEqual(readfile(binary));
}); });

View file

@ -1,151 +0,0 @@
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 = {
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
});
}
export default function () {
describe('uplink auth test', () => {
test('if set headers empty should return default headers', () => {
const headers = setHeaders();
const keys = Object.keys(headers);
const keysExpected = ['Accept', 'Accept-Encoding', '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({}, {
'authorization': buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg==')
});
expect(Object.keys(headers)).toHaveLength(4);
expect(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'
}
}, {
'authorization': buildToken(TOKEN_BASIC, 'tokenBasic')
});
expect(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['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['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['authorization']).toBe(`${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['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);
});
});
}

View file

@ -2,13 +2,14 @@ import fs from 'fs';
import path from 'path'; import path from 'path';
import assert from 'assert'; import assert from 'assert';
import crypto from 'crypto'; import crypto from 'crypto';
import {readFile} from './lib/test.utils'; import {readFile} from '../lib/test.utils';
import {HTTP_STATUS} from "../../../src/lib/constants";
function getBinary() { function getBinary() {
return readFile('../fixtures/binary'); return readFile('../fixtures/binary');
} }
const STORAGE = 'store/test-storage3'; const STORAGE = '../store/test-storage3';
const TARBALL = 'blahblah'; const TARBALL = 'blahblah';
const PKG_GH131 = 'pkg-gh131'; const PKG_GH131 = 'pkg-gh131';
const PKG_GH1312 = 'pkg-gh1312'; const PKG_GH1312 = 'pkg-gh1312';
@ -22,33 +23,32 @@ export default function (server, server2, server3) {
describe('storage tarball cache test', () => { describe('storage tarball cache test', () => {
//more info #131 //more info #131
beforeAll(function () { beforeAll(function () {
return server.addPackage(PKG_GH131); return server.addPackage(PKG_GH131);
}); });
beforeAll(function () { beforeAll(function () {
return server.putTarball(PKG_GH131, TARBALL, getBinary()) return server.putTarball(PKG_GH131, TARBALL, getBinary())
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/.*/); .body_ok(/.*/);
}); });
beforeAll(function () { beforeAll(function () {
const pkg = require('./fixtures/package')(PKG_GH131); const pkg = require('../fixtures/package')(PKG_GH131);
pkg.dist.shasum = crypto.createHash('sha1').update(getBinary()).digest('hex'); pkg.dist.shasum = crypto.createHash('sha1').update(getBinary()).digest('hex');
return server.putVersion(PKG_GH131, '0.0.1', pkg) return server.putVersion(PKG_GH131, '0.0.1', pkg)
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/published/); .body_ok(/published/);
}); });
beforeAll(function () { beforeAll(function () {
return server3.getPackage(PKG_GH131).status(200); return server3.getPackage(PKG_GH131).status(HTTP_STATUS.OK);
}); });
beforeAll(function () { beforeAll(function () {
return server3.getTarball(PKG_GH131, TARBALL) return server3.getTarball(PKG_GH131, TARBALL)
.status(200); .status(HTTP_STATUS.OK);
}); });
test('should be caching packages from uplink server1', () => { test('should be caching packages from uplink server1', () => {
@ -61,27 +61,27 @@ export default function (server, server2, server3) {
beforeAll(function () { beforeAll(function () {
return server2.putTarball(PKG_GH1312, TARBALL, getBinary()) return server2.putTarball(PKG_GH1312, TARBALL, getBinary())
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/.*/); .body_ok(/.*/);
}); });
beforeAll(function () { beforeAll(function () {
const pkg = require('./fixtures/package')(PKG_GH1312); const pkg = require('../fixtures/package')(PKG_GH1312);
pkg.dist.shasum = crypto.createHash('sha1').update(getBinary()).digest('hex'); pkg.dist.shasum = crypto.createHash('sha1').update(getBinary()).digest('hex');
return server2.putVersion(PKG_GH1312, '0.0.1', pkg) return server2.putVersion(PKG_GH1312, '0.0.1', pkg)
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok(/published/); .body_ok(/published/);
}); });
beforeAll(function () { beforeAll(function () {
return server3.getPackage(PKG_GH1312) return server3.getPackage(PKG_GH1312)
.status(200); .status(HTTP_STATUS.OK);
}); });
beforeAll(function () { beforeAll(function () {
return server3.getTarball(PKG_GH1312, TARBALL) return server3.getTarball(PKG_GH1312, TARBALL)
.status(200); .status(HTTP_STATUS.OK);
}); });
test('must not be caching packages from uplink server2', () => { test('must not be caching packages from uplink server2', () => {

View file

@ -4,10 +4,11 @@ import _ from 'lodash';
import assert from 'assert'; import assert from 'assert';
import smartRequest from './request'; import smartRequest from './request';
import type {IServerBridge} from '../types'; import type {IServerBridge} from '../types';
import {HEADERS} from '../../src/lib/constants'; import {HEADERS, HTTP_STATUS, TOKEN_BASIC} from '../../src/lib/constants';
import {buildToken} from "../../src/lib/utils";
const buildAuthHeader = (user, pass): string => { const buildAuthHeader = (user, pass): string => {
return `Basic ${(new Buffer(`${user}:${pass}`)).toString('base64')}`; return buildToken(TOKEN_BASIC, new Buffer(`${user}:${pass}`).toString('base64'));
}; };
export default class Server implements IServerBridge { export default class Server implements IServerBridge {
@ -22,7 +23,6 @@ export default class Server implements IServerBridge {
} }
request(options: any): any { request(options: any): any {
// console.log("--->$$$$ REQUEST", options);
assert(options.uri); assert(options.uri);
const headers = options.headers || {}; const headers = options.headers || {};
@ -182,14 +182,14 @@ export default class Server implements IServerBridge {
addPackage(name: string) { addPackage(name: string) {
return this.putPackage(name, require('../functional/fixtures/package')(name)) return this.putPackage(name, require('../functional/fixtures/package')(name))
.status(201) .status(HTTP_STATUS.CREATED)
.body_ok('created new package'); .body_ok('created new package');
} }
whoami() { whoami() {
return this.request({ return this.request({
uri: '/-/whoami' uri: '/-/whoami'
}).status(200) }).status(HTTP_STATUS.OK)
.then(function(body) { .then(function(body) {
return body.username; return body.username;
}); });
@ -198,7 +198,7 @@ export default class Server implements IServerBridge {
ping() { ping() {
return this.request({ return this.request({
uri: '/-/ping' uri: '/-/ping'
}).status(200) }).status(HTTP_STATUS.OK)
.then(function(body) { .then(function(body) {
return body; return body;
}); });

View file

@ -0,0 +1,151 @@
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";
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 auth test', () => {
test('if set headers empty should return default headers', () => {
const headers = setHeaders();
const keys = Object.keys(headers);
const keysExpected = ['Accept', 'Accept-Encoding', '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({}, {
'authorization': buildToken(TOKEN_BASIC, 'Zm9vX2Jhcg==')
});
expect(Object.keys(headers)).toHaveLength(4);
expect(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'
}
}, {
'authorization': buildToken(TOKEN_BASIC, 'tokenBasic')
});
expect(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['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['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['authorization']).toBe(`${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['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);
});
});