From 644c0981db7b9aff52845dc8ba917f535bf5a897 Mon Sep 17 00:00:00 2001 From: "Juan Picado @jotadeveloper" Date: Wed, 2 Aug 2017 20:46:06 +0200 Subject: [PATCH] fix: configuration files inconsistencies, add unit test --- conf/docker.yaml | 4 ++-- conf/full.yaml | 10 ++++++--- src/lib/config.js | 5 +++-- src/lib/utils.js | 5 +++++ test/unit/config.spec.js | 46 ++++++++++++++++++++++++++-------------- 5 files changed, 47 insertions(+), 23 deletions(-) diff --git a/conf/docker.yaml b/conf/docker.yaml index e0ae1612b..9bc0b43c9 100644 --- a/conf/docker.yaml +++ b/conf/docker.yaml @@ -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 diff --git a/conf/full.yaml b/conf/full.yaml index 8173528b7..35abf797c 100644 --- a/conf/full.yaml +++ b/conf/full.yaml @@ -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 diff --git a/src/lib/config.js b/src/lib/config.js index 51ccd1313..1afe54593 100644 --- a/src/lib/config.js +++ b/src/lib/config.js @@ -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)`); }); diff --git a/src/lib/utils.js b/src/lib/utils.js index b23e74847..c77d2e951 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -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; diff --git a/test/unit/config.spec.js b/test/unit/config.spec.js index 00bb3ce4d..b6c8aa1d5 100644 --- a/test/unit/config.spec.js +++ b/test/unit/config.spec.js @@ -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); - }); });