refactor: usage of constants on sanity test

This commit is contained in:
Juan Picado @jotadeveloper 2018-06-23 09:18:31 +02:00
parent 686f22461f
commit e59f282a27
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
12 changed files with 120 additions and 106 deletions

View File

@ -47,6 +47,7 @@ export const API_ERROR = {
NOT_PACKAGE_UPLINK: 'package doesn\'t exist on uplink',
CONTENT_MISMATCH: 'content length mismatch',
NOT_FILE_UPLINK: 'file doesn\'t exist on uplink',
MAX_USERS_REACHED: 'maximum amount of users reached',
};
export const DEFAULT_NO_README = 'ERROR: No README data found!';

View File

@ -1,4 +1,4 @@
import {HTTP_STATUS} from "../../../src/lib/constants";
import {API_ERROR, HTTP_STATUS} from "../../../src/lib/constants";
export default function(server) {
describe('npm adduser', () => {
@ -22,7 +22,7 @@ export default function(server) {
test('should not register more users', () => {
return server.auth(String(Math.random()), String(Math.random()))
.status(HTTP_STATUS.CONFLICT)
.body_error(/maximum amount of users reached/);
.body_error(API_ERROR.MAX_USERS_REACHED);
});
});
}

View File

@ -4,9 +4,10 @@ import fs from 'fs';
import path from 'path';
import {createTarballHash} from "../../../src/lib/crypto-utils";
import {HTTP_STATUS} from "../../../src/lib/constants";
import {CREDENTIALS, PORT_SERVER_1, PORT_SERVER_2, TARBALL} from "../config.func";
import {CREDENTIALS, DOMAIN_SERVERS, PORT_SERVER_1, PORT_SERVER_2, TARBALL} from "../config.func";
import whoIam from './whoIam';
import ping from './ping';
import {DIST_TAGS} from '../../../src/lib/utils';
function readfile(folderPath) {
return fs.readFileSync(path.join(__dirname, '/', folderPath));
@ -20,6 +21,7 @@ export default function(server: any, server2: any) {
describe('basic test endpoints', () => {
const PKG_NAME:string = 'testpkg';
const PKG_VERSION:string = '0.0.1';
beforeAll(function() {
return server.auth(CREDENTIALS.user, CREDENTIALS.password)
@ -107,7 +109,7 @@ export default function(server: any, server2: any) {
let pkg = getPackage(PKG_NAME);
pkg.dist.shasum = createTarballHash().update('fake').digest('hex');
return server.putVersion(PKG_NAME, '0.0.1', pkg)
return server.putVersion(PKG_NAME, PKG_VERSION, pkg)
.status(HTTP_STATUS.BAD_REQUEST)
.body_error(/shasum error/);
});
@ -118,7 +120,7 @@ export default function(server: any, server2: any) {
const pkg = getPackage(PKG_NAME);
pkg.dist.shasum = createTarballHash().update(readfile('../fixtures/binary')).digest('hex');
return server.putVersion(PKG_NAME, '0.0.1', pkg)
return server.putVersion(PKG_NAME, PKG_VERSION, pkg)
.status(HTTP_STATUS.CREATED)
.body_ok(/published/);
});
@ -129,33 +131,33 @@ export default function(server: any, server2: any) {
describe('should download a package', () => {
beforeAll(function() {
return server.auth('test', 'test')
return server.auth(CREDENTIALS.user, CREDENTIALS.password)
.status(HTTP_STATUS.CREATED)
.body_ok(/'test'/);
.body_ok(new RegExp(CREDENTIALS.user));
});
test('should download a newly created package from server1', () => {
return server.getPackage(PKG_NAME)
.status(200)
.status(HTTP_STATUS.OK)
.then(function (body) {
expect(body.name).toEqual(PKG_NAME);
expect(body.versions['0.0.1'].name).toEqual(PKG_NAME);
expect(body.versions['0.0.1'].dist.tarball).toEqual(`http://localhost:${PORT_SERVER_1}/testpkg/-/${TARBALL}`);
expect(body['dist-tags']).toEqual({
latest: '0.0.1'
expect(body.versions[PKG_VERSION].name).toEqual(PKG_NAME);
expect(body.versions[PKG_VERSION].dist.tarball).toEqual(`http://${DOMAIN_SERVERS}:${PORT_SERVER_1}/${PKG_NAME}/-/${TARBALL}`);
expect(body[DIST_TAGS]).toEqual({
latest: PKG_VERSION
});
});
});
test('should downloading a package from server2', () => {
return server2.getPackage(PKG_NAME)
.status(200)
.status(HTTP_STATUS.OK)
.then(function (body) {
expect(body.name).toEqual(PKG_NAME);
expect(body.versions['0.0.1'].name).toEqual(PKG_NAME);
expect(body.versions['0.0.1'].dist.tarball).toEqual(`http://localhost:${PORT_SERVER_2}/testpkg/-/${TARBALL}`);
expect(body['dist-tags']).toEqual({
latest: '0.0.1'
expect(body.versions[PKG_VERSION].name).toEqual(PKG_NAME);
expect(body.versions[PKG_VERSION].dist.tarball).toEqual(`http://${DOMAIN_SERVERS}:${PORT_SERVER_2}/${PKG_NAME}/-/${TARBALL}`);
expect(body[DIST_TAGS]).toEqual({
latest: PKG_VERSION
});
});
});
@ -168,7 +170,6 @@ export default function(server: any, server2: any) {
describe('handle failures on endpoints', () => {
test('should fails trying to fetch non-existent package', () => {
return server.getPackage(PKG_NAME).status(HTTP_STATUS.NOT_FOUND).body_error(/no such package/);
});
@ -176,7 +177,7 @@ export default function(server: any, server2: any) {
test(
'should fails on publish a version for non existing package',
() => {
return server.putVersion('testpxg', '0.0.1', getPackage('testpxg'))
return server.putVersion('testpxg', PKG_VERSION, getPackage('testpxg'))
.status(HTTP_STATUS.NOT_FOUND)
.body_error(/no such package/);
}

View File

@ -4,6 +4,8 @@ export const CREDENTIALS = {
};
export const TARBALL = 'tarball-blahblah-file.name';
export const PORT_SERVER_APP = '55550';
export const PORT_SERVER_1 = '55551';
export const PORT_SERVER_2 = '55552';
export const PORT_SERVER_3 = '55553';
export const DOMAIN_SERVERS = 'localhost';

View File

@ -1,6 +1,6 @@
import {PORT_SERVER_1, TARBALL} from '../config.func';
import {DOMAIN_SERVERS, PORT_SERVER_1, TARBALL} from '../config.func';
module.exports = function(name, version = '0.0.0', port = PORT_SERVER_1, domain= `http://localhost:${port}`,
module.exports = function(name, version = '0.0.0', port = PORT_SERVER_1, domain= `http://${DOMAIN_SERVERS}:${port}`,
fileName = TARBALL, readme = 'this is a readme') {
return {
name,

View File

@ -8,7 +8,7 @@ import VerdaccioProcess from "../../lib/server_process";
import Server from "../../lib/server";
import ExpressServer from "./simple_server";
import type {IServerBridge} from '../../types';
import {PORT_SERVER_1, PORT_SERVER_2, PORT_SERVER_3} from '../config.func';
import {DOMAIN_SERVERS, PORT_SERVER_1, PORT_SERVER_2, PORT_SERVER_3} from '../config.func';
const EXPRESS_PORT = 55550;
@ -59,7 +59,7 @@ class FunctionalEnvironment extends NodeEnvironment {
const verdaccioConfig = new VerdaccioConfig(
path.join(pathStore, config.storage),
path.join(pathStore, config.config),
`http://localhost:${config.port}/`, config.port);
`http://${DOMAIN_SERVERS}:${config.port}/`, config.port);
console.log(chalk.magentaBright(`Running registry ${config.config} on port ${config.port}`));
const server: IServerBridge = new Server(verdaccioConfig.domainPath);
serverList.push(server);

View File

@ -2,6 +2,7 @@ import _ from 'lodash';
import {HEADERS} from '../../../src/lib/constants';
import {notify} from '../../../src/lib/notify';
import {DOMAIN_SERVERS, PORT_SERVER_APP} from '../config.func';
export default function(express) {
const config = {
@ -10,7 +11,7 @@ export default function(express) {
headers: [{
'Content-Type': HEADERS.JSON
}],
endpoint: "http://localhost:55550/api/notify",
endpoint: `http://${DOMAIN_SERVERS}:${PORT_SERVER_APP}/api/notify`,
content: `{"color":"green","message":"New package published: * {{ name }}*. Publisher name: * {{ publisher.name }} *.","notify":true,"message_format":"text"}`
}
};
@ -114,7 +115,7 @@ export default function(express) {
name: "pkg-test"
};
const configFail = _.cloneDeep(config);
configFail.notify.endpoint = "http://localhost:55550/api/notify/bad";
configFail.notify.endpoint = `http://${DOMAIN_SERVERS}:${PORT_SERVER_APP}/api/notify/bad`;
notify(metadata, configFail, publisherInfo).then(function () {
expect(false).toBe('This service should fails with status code 400');

View File

@ -1,5 +1,5 @@
import {HEADERS, HTTP_STATUS} from '../../../src/lib/constants';
import {PORT_SERVER_1, PORT_SERVER_2} from '../config.func';
import {DOMAIN_SERVERS, PORT_SERVER_1, PORT_SERVER_2} from '../config.func';
import {generateSha} from '../lib/test.utils';
import {DIST_TAGS} from "../../../src/lib/utils";
@ -49,7 +49,7 @@ export default function(server, server2) {
expect(body.name).toBe(SCOPE);
expect(body.versions[PKG_VERSION].name).toBe(SCOPE);
expect(body.versions[PKG_VERSION].dist.tarball).toBe(
`http://localhost:${port}/@test%2fscoped/-/${PKG_NAME}-${PKG_VERSION}.tgz`);
`http://${DOMAIN_SERVERS}:${port}/@test%2fscoped/-/${PKG_NAME}-${PKG_VERSION}.tgz`);
expect(body[DIST_TAGS]).toEqual({latest: PKG_VERSION});
});
@ -70,7 +70,7 @@ export default function(server, server2) {
.then(function(body) {
expect(body.name).toEqual(SCOPE);
expect(body.dist.tarball).toEqual(
`http://localhost:${PORT_SERVER_2}/@test%2fscoped/-/${PKG_NAME}-${PKG_VERSION}.tgz`);
`http://${DOMAIN_SERVERS}:${PORT_SERVER_2}/@test%2fscoped/-/${PKG_NAME}-${PKG_VERSION}.tgz`);
});
});
});

View File

@ -1,7 +1,9 @@
import {readFile} from '../lib/test.utils';
import {createTarballHash} from "../../../src/lib/crypto-utils";
import {HTTP_STATUS} from "../../../src/lib/constants";
import {PORT_SERVER_1, TARBALL} from '../config.func';
import {API_ERROR, HTTP_STATUS} from "../../../src/lib/constants";
import {DOMAIN_SERVERS, PORT_SERVER_1, TARBALL} from '../config.func';
import generatePkg from '../fixtures/package';
import {DIST_TAGS} from '../../../src/lib/utils';
function getBinary() {
return readFile('../fixtures/binary');
@ -12,59 +14,62 @@ export default function (server, server2) {
const PKG_NAME = 'test-nullstorage2';
const PKG_VERSION = '0.0.1';
describe('should check whether test-nullstorage is on server1', () => {
test('trying to fetch non-existent package / null storage', () => {
return server.getPackage('test-nullstorage-nonexist').status(HTTP_STATUS.NOT_FOUND)
.body_error(/no such package/);
});
});
describe('should test a scenario when tarball is being fetch from uplink', () => {
describe('should check whether test-nullstorage is on server2', () => {
beforeAll(function() {
return server2.addPackage(PKG_NAME);
describe(`should check whether ${PKG_NAME} is on server1`, () => {
test('should fails on fetch non-existent package on server1', () => {
return server.getPackage('test-nullstorage-nonexist').status(HTTP_STATUS.NOT_FOUND)
.body_error(API_ERROR.NO_PACKAGE);
});
});
test('should creaate a new package on server2', () => {/* test for before() */});
test('should fails on download a non existent tarball', () => {
return server.getTarball(PKG_NAME, TARBALL)
.status(HTTP_STATUS.NOT_FOUND)
.body_error(/no such file/);
});
describe('test and publish test-nullstorage2 package', () => {
describe(`should check whether ${PKG_NAME} is on server2`, () => {
beforeAll(function() {
return server2.putTarball(PKG_NAME, TARBALL, getBinary())
.status(HTTP_STATUS.CREATED).body_ok(/.*/);
return server2.addPackage(PKG_NAME);
});
beforeAll(function() {
let pkg = require('../fixtures/package')(PKG_NAME);
pkg.dist.shasum = createTarballHash().update(getBinary()).digest('hex');
return server2.putVersion(PKG_NAME, PKG_VERSION, pkg)
.status(HTTP_STATUS.CREATED).body_ok(/published/);
});
test('should create a new package on server2', () => {/* test for before() */});
test('should upload a new version for test-nullstorage2', () => {/* test for before() */});
test('should fetch the newly created published tarball for test-nullstorage2', () => {
test('should fails on download a non existent tarball from server1', () => {
return server.getTarball(PKG_NAME, TARBALL)
.status(HTTP_STATUS.OK)
.then(function(body) {
expect(body).toEqual(getBinary());
});
.status(HTTP_STATUS.NOT_FOUND)
.body_error(/no such file/);
});
test('should check whether the metadata for test-nullstorage2 match', () => {
return server.getPackage(PKG_NAME)
.status(HTTP_STATUS.OK)
.then(function(body) {
expect(body.name).toBe(PKG_NAME);
expect(body.versions[PKG_VERSION].name).toBe(PKG_NAME);
expect(body.versions[PKG_VERSION].dist.tarball).toBe(`http://localhost:${PORT_SERVER_1}/${PKG_NAME}/-/${TARBALL}`);
expect(body['dist-tags']).toEqual({latest: PKG_VERSION});
});
describe(`should succesfully publish ${PKG_NAME} package on server2`, () => {
beforeAll(function() {
return server2.putTarball(PKG_NAME, TARBALL, getBinary())
.status(HTTP_STATUS.CREATED).body_ok(/.*/);
});
beforeAll(function() {
let pkg = generatePkg(PKG_NAME);
pkg.dist.shasum = createTarballHash().update(getBinary()).digest('hex');
return server2.putVersion(PKG_NAME, PKG_VERSION, pkg)
.status(HTTP_STATUS.CREATED).body_ok(/published/);
});
test(`should publish a new version for ${PKG_NAME} on server 2`, () => {/* test for before() */});
test(`should fetch the newly created published tarball for ${PKG_NAME} from server1 on server2`, () => {
return server.getTarball(PKG_NAME, TARBALL)
.status(HTTP_STATUS.OK)
.then(function(body) {
expect(body).toEqual(getBinary());
});
});
test(`should fetch metadata for ${PKG_NAME} match from server1`, () => {
return server.getPackage(PKG_NAME)
.status(HTTP_STATUS.OK)
.then(function(body) {
expect(body.name).toBe(PKG_NAME);
expect(body.versions[PKG_VERSION].name).toBe(PKG_NAME);
expect(body.versions[PKG_VERSION].dist.tarball).toBe(`http://${DOMAIN_SERVERS}:${PORT_SERVER_1}/${PKG_NAME}/-/${TARBALL}`);
expect(body[DIST_TAGS]).toEqual({latest: PKG_VERSION});
});
});
});
});
});
});
}

View File

@ -1,6 +1,9 @@
import {DOMAIN_SERVERS, PORT_SERVER_APP} from '../config.func';
import {API_ERROR, HEADER_TYPE, HTTP_STATUS} from '../../../src/lib/constants';
export default function(server, express) {
describe('test for unexpected client hangs', () => {
describe('shoul test for unexpected client hangs', () => {
let handleResponseTarball;
beforeAll(function() {
@ -13,7 +16,7 @@ export default function(server, express) {
'version': '0.1.0',
'dist': {
'shasum': 'fake',
'tarball': 'http://localhost:55550/testexp-racycrash/-/test.tar.gz',
'tarball': `http://${DOMAIN_SERVERS}:${PORT_SERVER_APP}/testexp-racycrash/-/test.tar.gz`,
},
},
},
@ -27,14 +30,14 @@ export default function(server, express) {
test('should not crash on error if client disconnects', callback => {
handleResponseTarball = function(res) {
res.header('content-length', 1e6);
res.header(HEADER_TYPE.CONTENT_LENGTH, 1e6);
res.write('test test test');
setTimeout(function() {
res.write('-');
// destroy the connection
res.socket.destroy();
cb();
}, 200);
}, HTTP_STATUS.OK);
};
server.request({uri: '/testexp-racycrash/-/test.tar.gz'})
@ -45,7 +48,7 @@ export default function(server, express) {
function cb() {
// test for NOT crashing
server.request({uri: '/testexp-racycrash'})
.status(200)
.status(HTTP_STATUS.OK)
.then(function() {
callback();
});
@ -58,7 +61,7 @@ export default function(server, express) {
};
return server.request({uri: '/testexp-racycrash/-/test.tar.gz'})
.body_error('internal server error');
.body_error(API_ERROR.INTERNAL_SERVER_ERROR);
});
});
}

View File

@ -1,67 +1,68 @@
import assert from 'assert';
import _ from 'lodash';
import {HTTP_STATUS} from '../../../src/lib/constants';
export default function(server) {
describe('Security', () => {
describe('should test security on endpoints', () => {
beforeAll(function () {
return server.addPackage('testpkg-sec');
});
test('bad pkg #1', () => {
test('should fails on fetch bad pkg #1', () => {
return server.getPackage('package.json')
.status(403)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(/invalid package/);
});
test('bad pkg #2', () => {
test('should fails on fetch bad pkg #2', () => {
return server.getPackage('__proto__')
.status(403)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(/invalid package/);
});
test('__proto__, connect stuff', () => {
test('should do not fails on __proto__, connect stuff', () => {
return server.request({uri: '/testpkg-sec?__proto__=1'})
.then(function (body) {
// test for NOT outputting stack trace
assert(!body || typeof(body) === 'object' || body.indexOf('node_modules') === -1);
expect(_.isNil(body) || _.isObject(body) || body.indexOf('node_modules')).toBeTruthy();
// test for NOT crashing
return server.request({uri: '/testpkg-sec'}).status(200);
return server.request({uri: '/testpkg-sec'}).status(HTTP_STATUS.OK);
});
});
test('do not return package.json as an attachment', () => {
test('should fails and do not return package.json as an attachment', () => {
return server.request({uri: '/testpkg-sec/-/package.json'})
.status(403)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(/invalid filename/);
});
test('silly things - reading #1', () => {
test('should fails on fetch silly things - reading #1', () => {
return server.request({uri: '/testpkg-sec/-/../../../../../../../../etc/passwd'})
.status(404);
.status(HTTP_STATUS.NOT_FOUND);
});
test('silly things - reading #2', () => {
test('should fails on fetch silly things - reading #2', () => {
return server.request({uri: '/testpkg-sec/-/%2f%2e%2e%2f%2e%2e%2f%2e%2e%2f%2e%2e%2f%2e%2e%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd'})
.status(403)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(/invalid filename/);
});
test('silly things - writing #1', () => {
test('should fails on fetch silly things - writing #1', () => {
return server.putTarball('testpkg-sec', 'package.json', '{}')
.status(403)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(/invalid filename/);
});
test('silly things - writing #3', () => {
test('should fails on fetch silly things - writing #3', () => {
return server.putTarball('testpkg-sec', 'node_modules', '{}')
.status(403)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(/invalid filename/);
});
test('silly things - writing #4', () => {
test('should fails on fetch silly things - writing #4', () => {
return server.putTarball('testpkg-sec', '../testpkg.tgz', '{}')
.status(403)
.status(HTTP_STATUS.FORBIDDEN)
.body_error(/invalid filename/);
});
});

View File

@ -1,7 +1,7 @@
import assert from 'assert';
import {generateSha} from '../lib/test.utils';
import {HEADERS} from '../../../src/lib/constants';
import {PORT_SERVER_1, PORT_SERVER_2} from '../config.func';
import {HEADERS, HTTP_STATUS} from '../../../src/lib/constants';
import {DOMAIN_SERVERS, PORT_SERVER_1, PORT_SERVER_2} from '../config.func';
export default function(server, server2, express) {
describe('should test preserve tags when publishing something', () => {
@ -42,11 +42,11 @@ export default function(server, server2, express) {
describe('should match dist-tags', () => {
const matchDisTags = (server, port) => {
return server.getPackage('testpkg-preserve')
.status(200)
.status(HTTP_STATUS.OK)
.then(function(body) {
assert.equal(body.name, 'testpkg-preserve');
assert.equal(body.versions['0.0.1'].name, 'testpkg-preserve');
assert.equal(body.versions['0.0.1'].dist.tarball, `http://localhost:${port}/testpkg-preserve/-/testpkg-preserve-0.0.1.tgz`);
assert.equal(body.versions['0.0.1'].dist.tarball, `http://${DOMAIN_SERVERS}:${port}/testpkg-preserve/-/testpkg-preserve-0.0.1.tgz`);
assert.deepEqual(body['dist-tags'], {foo: '0.0.1', latest: '0.0.1'});
});
};
@ -96,13 +96,13 @@ export default function(server, server2, express) {
test('server1 - search', () => {
return server.request({uri: '/-/all'})
.status(200)
.status(HTTP_STATUS.OK)
.then(check);
});
test('server2 - search', () => {
return server2.request({uri: '/-/all'})
.status(200)
.status(HTTP_STATUS.OK)
.then(check);
});