fix: configuration files inconsistencies, add unit test

This commit is contained in:
Juan Picado @jotadeveloper 2017-08-02 20:46:06 +02:00
parent cda92ac5ab
commit 644c0981db
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
5 changed files with 47 additions and 23 deletions

View File

@ -29,7 +29,7 @@ packages:
'@*/*':
# scoped packages
access: $all
publish: $all
publish: $authenticated
proxy: npmjs
'**':
@ -42,7 +42,7 @@ packages:
# allow all known users to publish packages
# (anyone can register by default, remember?)
publish: $all
publish: $authenticated
# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs

View File

@ -67,6 +67,11 @@ uplinks:
#cache: false
packages:
'@*/*':
# scoped packages
access: $all
publish: $authenticated
proxy: npmjs
# uncomment this for packages with "local-" prefix to be available
# for admin only, it's a recommended way of handling private packages
#'local-*':
@ -83,7 +88,7 @@ packages:
access: $all
# allow 'admin' to publish packages
publish: admin
publish: $authenticated
# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs
@ -169,7 +174,7 @@ notify:
# content: '{ "text": "Package *{{ name }}* published to version *{{ dist-tags.latest }}*", "username": "Verdaccio", "icon_emoji": ":package:" }'
# Multiple notification endpoints can be created by specifying a collection
'example-package-1'
'example-package-1':
method: POST
# Only run this notification if the package name matches the regular
# expression
@ -180,7 +185,6 @@ notify:
# packagePatternFlags: i
# If this endpoint requires specific headers, set them here
# as an array of key: value objects.
headers: [{'Content-type': 'application/x-www-form-urlencoded'}]
# headers supports as well a literal object
headers: {'Content-type': 'application/x-www-form-urlencoded'}
# set the URL endpoint for this call

View File

@ -70,9 +70,10 @@ class Config {
assert(!arg.match(/\s/), 'CONFIG: invalid user name: ' + arg);
assert(users[arg] == null, 'CONFIG: duplicate user/uplink name: ' + arg);
users[arg] = true;
}
};
// sanity check for strategic config properties
;['users', 'uplinks', 'packages'].forEach(function(x) {
['users', 'uplinks', 'packages'].forEach(function(x) {
if (self[x] == null) self[x] = {};
assert(Utils.is_object(self[x]), `CONFIG: bad "${x}" value (object expected)`);
});

View File

@ -2,7 +2,9 @@
const assert = require('assert');
const semver = require('semver');
const YAML = require('js-yaml');
const URL = require('url');
const fs = require('fs');
const _ = require('lodash');
const Logger = require('./logger');
const createError = require('http-errors');
@ -348,6 +350,8 @@ const ErrorCode = {
},
};
const parseConfigFile = (config_path) => YAML.safeLoad(fs.readFileSync(config_path, 'utf8'));
module.exports.parseInterval = parseInterval;
module.exports.semver_sort = semverSort;
module.exports.parse_address = parse_address;
@ -363,3 +367,4 @@ module.exports.validate_package = validate_package;
module.exports.getWebProtocol = getWebProtocol;
module.exports.getLatestVersion = getLatestVersion;
module.exports.ErrorCode = ErrorCode;
module.exports.parseConfigFile = parseConfigFile;

View File

@ -6,14 +6,24 @@ const Config = require('../../src/lib/config');
const path = require('path');
const _ = require('lodash');
const resolveConf = (conf) => {
const fullConfigPath = path.join(__dirname, `../../conf/${conf}.yaml`);
return fullConfigPath;
const resolveConf = (conf) => path.join(__dirname, `../../conf/${conf}.yaml`);
const checkUplink = (config) => {
assert.equal(_.isObject(config.uplinks['npmjs']), true);
assert.equal(config.uplinks['npmjs'].url, 'https://registry.npmjs.org');
};
const validateConfigFile = (config) => {
assert.ok(_.isObject(config.uplinks['npmjs']));
}
const checkPackages = (config) => {
assert.equal(_.isObject(config.packages), true);
assert.equal(Object.keys(config.packages).join('|'), '@*/*|**');
assert.equal(config.packages['@*/*'].access, '$all');
assert.equal(config.packages['@*/*'].publish, '$authenticated');
assert.equal(config.packages['@*/*'].proxy, 'npmjs');
assert.equal(config.packages['**'].access, '$all');
assert.equal(config.packages['**'].publish, '$authenticated');
assert.equal(config.packages['**'].proxy, 'npmjs');
assert.equal(config.uplinks['npmjs'].url, 'https://registry.npmjs.org');
};
describe('Config file', function() {
before(function() {
@ -21,25 +31,29 @@ describe('Config file', function() {
this.config = new Config(Utils.parseConfigFile(resolveConf('full')));
});
describe('Config file', function() {
it('parse full.yaml', function () {
describe('Config file', () => {
it('parse full.yaml', () => {
const config = new Config(Utils.parseConfigFile(resolveConf('full')));
validateConfigFile(config);
checkUplink(config);
assert.equal(config.storage, './storage');
assert.equal(config.web.title, 'Verdaccio');
checkPackages(config);
});
it('parse docker.yaml', function () {
it('parse docker.yaml', () => {
const config = new Config(Utils.parseConfigFile(resolveConf('docker')));
validateConfigFile(config);
checkUplink(config);
assert.equal(config.storage, '/verdaccio/storage');
assert.equal(config.auth.htpasswd.file, '/verdaccio/conf/htpasswd');
});
it('parse default.yaml', function () {
it('parse default.yaml', () => {
const config = new Config(Utils.parseConfigFile(resolveConf('default')));
validateConfigFile(config);
checkUplink(config);
assert.equal(config.storage, './storage');
assert.equal(config.auth.htpasswd.file, './htpasswd');
});
});
it('npmjs uplink should have a default cache option that is true', () => {
assert.equal(this.config.uplinks['npmjs'].cache, true);
});
});