mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-01-06 22:40:26 -05:00
test: fix broken test for validate parameters
This commit is contained in:
parent
b1eb7c6278
commit
4bf6b3b844
5 changed files with 71 additions and 79 deletions
41
test/unit/modules/api/validate.api.params.middleware.spec.js
Normal file
41
test/unit/modules/api/validate.api.params.middleware.spec.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
// ensure that all arguments are validated
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
/**
|
||||
* Validate.
|
||||
app.param('package', validate_pkg);
|
||||
app.param('filename', validate_name);
|
||||
app.param('tag', validate_name);
|
||||
app.param('version', validate_name);
|
||||
app.param('revision', validate_name);
|
||||
app.param('token', validate_name);
|
||||
*/
|
||||
describe('api endpoint app.param()', () => {
|
||||
const file = '../endpoint/index.js';
|
||||
let m;
|
||||
const requirePath = path.normalize(path.join(__dirname + '/../../../../src/api/web/', file));
|
||||
const source = fs.readFileSync(requirePath, 'utf8');
|
||||
const very_scary_regexp = /\n\s*app\.(\w+)\s*\(\s*(("[^"]*")|('[^']*'))\s*,/g;
|
||||
const appParams = {};
|
||||
|
||||
while ((m = very_scary_regexp.exec(source)) != null) {
|
||||
if (m[1] === 'set') continue;
|
||||
|
||||
let inner = m[2].slice(1, m[2].length-1);
|
||||
var t;
|
||||
|
||||
inner.split('/').forEach(function(x) {
|
||||
t = x.match(/^:([^?:]*)\??$/);
|
||||
if (m[1] === 'param') {
|
||||
appParams[x] = 'ok';
|
||||
} else if (t) {
|
||||
appParams[t[1]] = appParams[t[1]] || m[0].trim();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
test.each(Object.keys(appParams))('should validate ":%s"', (param) => {
|
||||
expect(appParams[param]).toEqual('ok');
|
||||
});
|
||||
});
|
|
@ -1,27 +1,12 @@
|
|||
import _ from 'lodash';
|
||||
import {parseAddress as parse} from '../../../src/lib/utils';
|
||||
import {DEFAULT_DOMAIN, DEFAULT_PORT} from '../../../src/lib/constants';
|
||||
import {parseAddress as parse} from '../../../../src/lib/utils';
|
||||
import {DEFAULT_DOMAIN, DEFAULT_PORT} from '../../../../src/lib/constants';
|
||||
|
||||
describe('Parse listen address', () => {
|
||||
function addTest(uri, proto, host, port) {
|
||||
test(`should parse ${uri}`, () => {
|
||||
const parsed = parse(uri);
|
||||
const useCases = [];
|
||||
|
||||
if (_.isNull(proto)) {
|
||||
expect(parsed).toBeNull();
|
||||
} else if (port) {
|
||||
expect(parsed).toEqual({
|
||||
proto,
|
||||
host,
|
||||
port,
|
||||
});
|
||||
} else {
|
||||
expect(parsed).toEqual({
|
||||
proto,
|
||||
path: host,
|
||||
});
|
||||
}
|
||||
});
|
||||
function addTest(uri, proto, host, port) {
|
||||
useCases.push([uri, proto, host, port]);
|
||||
}
|
||||
|
||||
addTest(DEFAULT_PORT, 'http', DEFAULT_DOMAIN, DEFAULT_PORT);
|
||||
|
@ -44,4 +29,23 @@ describe('Parse listen address', () => {
|
|||
addTest('blah://4873', null);
|
||||
addTest('https://blah:4873///', null);
|
||||
addTest('unix:1234', 'http', 'unix', '1234'); // not unix socket
|
||||
|
||||
test.each(useCases)(`should parse (%s - %s - %s - %s)`, (uri, proto, host, port) => {
|
||||
const parsed = parse(uri);
|
||||
|
||||
if (_.isNull(proto)) {
|
||||
expect(parsed).toBeNull();
|
||||
} else if (port) {
|
||||
expect(parsed).toEqual({
|
||||
proto,
|
||||
host,
|
||||
port,
|
||||
});
|
||||
} else {
|
||||
expect(parsed).toEqual({
|
||||
proto,
|
||||
path: host,
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
|
@ -1,6 +1,6 @@
|
|||
// @flow
|
||||
import {generateGravatarUrl, GENERIC_AVATAR } from '../../../src/utils/user';
|
||||
import { spliceURL } from '../../../src/utils/string';
|
||||
import {generateGravatarUrl, GENERIC_AVATAR } from '../../../../src/utils/user';
|
||||
import { spliceURL } from '../../../../src/utils/string';
|
||||
import {
|
||||
validateName,
|
||||
convertDistRemoteToLocalTarballUrls,
|
||||
|
@ -16,10 +16,10 @@ import {
|
|||
sortByName,
|
||||
formatAuthor,
|
||||
isHTTPProtocol,
|
||||
} from '../../../src/lib/utils';
|
||||
import { DIST_TAGS, DEFAULT_USER } from '../../../src/lib/constants';
|
||||
import Logger, { setup } from '../../../src/lib/logger';
|
||||
import { readFile } from '../../functional/lib/test.utils';
|
||||
} from '../../../../src/lib/utils';
|
||||
import { DIST_TAGS, DEFAULT_USER } from '../../../../src/lib/constants';
|
||||
import Logger, { setup } from '../../../../src/lib/logger';
|
||||
import { readFile } from '../../../functional/lib/test.utils';
|
||||
|
||||
const readmeFile = (fileName: string = 'markdown.md') =>
|
||||
readFile(`../../unit/partials/readme/${fileName}`);
|
|
@ -1,53 +0,0 @@
|
|||
// ensure that all arguments are validated
|
||||
const path = require('path');
|
||||
|
||||
/**
|
||||
* Validate.
|
||||
app.param('package', validate_pkg);
|
||||
app.param('filename', validate_name);
|
||||
app.param('tag', validate_name);
|
||||
app.param('version', validate_name);
|
||||
app.param('revision', validate_name);
|
||||
app.param('token', validate_name);
|
||||
*/
|
||||
describe('api endpoint app.param()', () => {
|
||||
|
||||
// to avoid a failure on run test
|
||||
test('empty block', () => {});
|
||||
runTest('../endpoint/index.js');
|
||||
});
|
||||
|
||||
function runTest(file) {
|
||||
return function() {
|
||||
|
||||
let m;
|
||||
const requirePath = path.normalize(path.join(__dirname + '/../../../src/api/web/', file));
|
||||
const source = require('fs').readFileSync(requirePath, 'utf8');
|
||||
const very_scary_regexp = /\n\s*app\.(\w+)\s*\(\s*(("[^"]*")|('[^']*'))\s*,/g;
|
||||
const appParams = {};
|
||||
|
||||
// look up for matches in the source code
|
||||
while ((m = very_scary_regexp.exec(source)) != null) {
|
||||
if (m[1] === 'set') continue;
|
||||
|
||||
let inner = m[2].slice(1, m[2].length-1);
|
||||
var t;
|
||||
|
||||
inner.split('/').forEach(function(x) {
|
||||
t = x.match(/^:([^?:]*)\??$/);
|
||||
if (m[1] === 'param') {
|
||||
appParams[x] = 'ok';
|
||||
} else if (t) {
|
||||
appParams[t[1]] = appParams[t[1]] || m[0].trim();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Object.keys(appParams).forEach(function(param) {
|
||||
test('should validate ":'+param+'"', () => {
|
||||
expect(appParams[param]).toEqual('ok');
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in a new issue