Skip to content

Commit

Permalink
fix: configuration files inconsistencies, add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
juanpicado committed Aug 2, 2017
1 parent cda92ac commit 644c098
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
4 changes: 2 additions & 2 deletions conf/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ packages:
'@*/*':
# scoped packages
access: $all
publish: $all
publish: $authenticated
proxy: npmjs

'**':
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions conf/full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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-*':
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)`);
});
Expand Down
5 changes: 5 additions & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand All @@ -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;
46 changes: 30 additions & 16 deletions test/unit/config.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,54 @@ 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() {

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);
});
});

0 comments on commit 644c098

Please sign in to comment.