Skip to content

Commit 644c098

Browse files
committed
fix: configuration files inconsistencies, add unit test
1 parent cda92ac commit 644c098

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

conf/docker.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ packages:
2929
'@*/*':
3030
# scoped packages
3131
access: $all
32-
publish: $all
32+
publish: $authenticated
3333
proxy: npmjs
3434

3535
'**':
@@ -42,7 +42,7 @@ packages:
4242

4343
# allow all known users to publish packages
4444
# (anyone can register by default, remember?)
45-
publish: $all
45+
publish: $authenticated
4646

4747
# if package is not available locally, proxy requests to 'npmjs' registry
4848
proxy: npmjs

conf/full.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ uplinks:
6767
#cache: false
6868

6969
packages:
70+
'@*/*':
71+
# scoped packages
72+
access: $all
73+
publish: $authenticated
74+
proxy: npmjs
7075
# uncomment this for packages with "local-" prefix to be available
7176
# for admin only, it's a recommended way of handling private packages
7277
#'local-*':
@@ -83,7 +88,7 @@ packages:
8388
access: $all
8489

8590
# allow 'admin' to publish packages
86-
publish: admin
91+
publish: $authenticated
8792

8893
# if package is not available locally, proxy requests to 'npmjs' registry
8994
proxy: npmjs
@@ -169,7 +174,7 @@ notify:
169174
# content: '{ "text": "Package *{{ name }}* published to version *{{ dist-tags.latest }}*", "username": "Verdaccio", "icon_emoji": ":package:" }'
170175

171176
# Multiple notification endpoints can be created by specifying a collection
172-
'example-package-1'
177+
'example-package-1':
173178
method: POST
174179
# Only run this notification if the package name matches the regular
175180
# expression
@@ -180,7 +185,6 @@ notify:
180185
# packagePatternFlags: i
181186
# If this endpoint requires specific headers, set them here
182187
# as an array of key: value objects.
183-
headers: [{'Content-type': 'application/x-www-form-urlencoded'}]
184188
# headers supports as well a literal object
185189
headers: {'Content-type': 'application/x-www-form-urlencoded'}
186190
# set the URL endpoint for this call

src/lib/config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ class Config {
7070
assert(!arg.match(/\s/), 'CONFIG: invalid user name: ' + arg);
7171
assert(users[arg] == null, 'CONFIG: duplicate user/uplink name: ' + arg);
7272
users[arg] = true;
73-
}
73+
};
74+
7475
// sanity check for strategic config properties
75-
;['users', 'uplinks', 'packages'].forEach(function(x) {
76+
['users', 'uplinks', 'packages'].forEach(function(x) {
7677
if (self[x] == null) self[x] = {};
7778
assert(Utils.is_object(self[x]), `CONFIG: bad "${x}" value (object expected)`);
7879
});

src/lib/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
const assert = require('assert');
44
const semver = require('semver');
5+
const YAML = require('js-yaml');
56
const URL = require('url');
7+
const fs = require('fs');
68
const _ = require('lodash');
79
const Logger = require('./logger');
810
const createError = require('http-errors');
@@ -348,6 +350,8 @@ const ErrorCode = {
348350
},
349351
};
350352

353+
const parseConfigFile = (config_path) => YAML.safeLoad(fs.readFileSync(config_path, 'utf8'));
354+
351355
module.exports.parseInterval = parseInterval;
352356
module.exports.semver_sort = semverSort;
353357
module.exports.parse_address = parse_address;
@@ -363,3 +367,4 @@ module.exports.validate_package = validate_package;
363367
module.exports.getWebProtocol = getWebProtocol;
364368
module.exports.getLatestVersion = getLatestVersion;
365369
module.exports.ErrorCode = ErrorCode;
370+
module.exports.parseConfigFile = parseConfigFile;

test/unit/config.spec.js

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,54 @@ const Config = require('../../src/lib/config');
66
const path = require('path');
77
const _ = require('lodash');
88

9-
const resolveConf = (conf) => {
10-
const fullConfigPath = path.join(__dirname, `../../conf/${conf}.yaml`);
11-
return fullConfigPath;
9+
const resolveConf = (conf) => path.join(__dirname, `../../conf/${conf}.yaml`);
10+
11+
const checkUplink = (config) => {
12+
assert.equal(_.isObject(config.uplinks['npmjs']), true);
13+
assert.equal(config.uplinks['npmjs'].url, 'https://registry.npmjs.org');
1214
};
1315

14-
const validateConfigFile = (config) => {
15-
assert.ok(_.isObject(config.uplinks['npmjs']));
16-
}
16+
const checkPackages = (config) => {
17+
assert.equal(_.isObject(config.packages), true);
18+
assert.equal(Object.keys(config.packages).join('|'), '@*/*|**');
19+
assert.equal(config.packages['@*/*'].access, '$all');
20+
assert.equal(config.packages['@*/*'].publish, '$authenticated');
21+
assert.equal(config.packages['@*/*'].proxy, 'npmjs');
22+
assert.equal(config.packages['**'].access, '$all');
23+
assert.equal(config.packages['**'].publish, '$authenticated');
24+
assert.equal(config.packages['**'].proxy, 'npmjs');
25+
assert.equal(config.uplinks['npmjs'].url, 'https://registry.npmjs.org');
26+
};
1727

1828
describe('Config file', function() {
1929
before(function() {
2030

2131
this.config = new Config(Utils.parseConfigFile(resolveConf('full')));
2232
});
2333

24-
describe('Config file', function() {
25-
it('parse full.yaml', function () {
34+
describe('Config file', () => {
35+
it('parse full.yaml', () => {
2636
const config = new Config(Utils.parseConfigFile(resolveConf('full')));
27-
validateConfigFile(config);
37+
checkUplink(config);
38+
assert.equal(config.storage, './storage');
39+
assert.equal(config.web.title, 'Verdaccio');
40+
checkPackages(config);
2841
});
2942

30-
it('parse docker.yaml', function () {
43+
it('parse docker.yaml', () => {
3144
const config = new Config(Utils.parseConfigFile(resolveConf('docker')));
32-
validateConfigFile(config);
45+
checkUplink(config);
46+
assert.equal(config.storage, '/verdaccio/storage');
47+
assert.equal(config.auth.htpasswd.file, '/verdaccio/conf/htpasswd');
3348
});
3449

35-
it('parse default.yaml', function () {
50+
it('parse default.yaml', () => {
3651
const config = new Config(Utils.parseConfigFile(resolveConf('default')));
37-
validateConfigFile(config);
52+
checkUplink(config);
53+
assert.equal(config.storage, './storage');
54+
assert.equal(config.auth.htpasswd.file, './htpasswd');
3855
});
3956
});
4057

41-
it('npmjs uplink should have a default cache option that is true', () => {
42-
assert.equal(this.config.uplinks['npmjs'].cache, true);
43-
});
4458
});
4559

0 commit comments

Comments
 (0)