mirror of
https://github.com/verdaccio/verdaccio.git
synced 2025-03-25 02:32:52 -05:00
refactor: pkg.access unit test
migrate more constants
This commit is contained in:
parent
050f857fb6
commit
946d195431
8 changed files with 30 additions and 15 deletions
|
@ -3,13 +3,14 @@
|
|||
import _ from 'lodash';
|
||||
import minimatch from 'minimatch';
|
||||
import assert from 'assert';
|
||||
import {ErrorCode} from './utils';
|
||||
|
||||
/**
|
||||
* Normalise user list.
|
||||
* @return {Array}
|
||||
*/
|
||||
export function normalizeUserlist(oldFormat: any, newFormat: any) {
|
||||
let result = [];
|
||||
const result = [];
|
||||
/* eslint prefer-rest-params: "off" */
|
||||
|
||||
for (let i=0; i < arguments.length; i++) {
|
||||
|
@ -23,7 +24,7 @@ export function normalizeUserlist(oldFormat: any, newFormat: any) {
|
|||
} else if (Array.isArray(arguments[i])) {
|
||||
result.push(arguments[i]);
|
||||
} else {
|
||||
throw Error('CONFIG: bad package acl (array or string expected): ' + JSON.stringify(arguments[i]));
|
||||
throw ErrorCode.getInternalError('CONFIG: bad package acl (array or string expected): ' + JSON.stringify(arguments[i]));
|
||||
}
|
||||
}
|
||||
return _.flatten(result);
|
||||
|
@ -36,6 +37,7 @@ export function getMatchedPackagesSpec(packages: any, pkg: any) {
|
|||
return packages[i];
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
|
@ -61,3 +63,4 @@ export function normalisePackageAccess(packages: any): any {
|
|||
|
||||
return normalizedPkgs;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@ import {normalisePackageAccess} from './config-utils';
|
|||
|
||||
const assert = require('assert');
|
||||
const _ = require('lodash');
|
||||
// const Error = require('http-errors');
|
||||
const minimatch = require('minimatch');
|
||||
|
||||
const Utils = require('./utils');
|
||||
|
@ -43,6 +42,7 @@ class Config {
|
|||
self[configProp] = config[configProp];
|
||||
}
|
||||
}
|
||||
|
||||
if (!self.user_agent) {
|
||||
self.user_agent = `${pkgName}/${pkgVersion}`;
|
||||
}
|
||||
|
|
|
@ -71,7 +71,8 @@ export const API_ERROR = {
|
|||
INTERNAL_SERVER_ERROR: 'internal server error',
|
||||
UNKNOWN_ERROR: 'unknown error',
|
||||
NOT_PACKAGE_UPLINK: 'package does not exist on uplink',
|
||||
UPLINK_OFFLINE: 'one of the uplinks is down, refuse to publish',
|
||||
UPLINK_OFFLINE_PUBLISH: 'one of the uplinks is down, refuse to publish',
|
||||
UPLINK_OFFLINE: 'uplink is offline',
|
||||
CONTENT_MISMATCH: 'content length mismatch',
|
||||
NOT_FILE_UPLINK: 'file doesn\'t exist on uplink',
|
||||
MAX_USERS_REACHED: 'maximum amount of users reached',
|
||||
|
|
|
@ -171,7 +171,7 @@ export function checkPackageRemote(name: string, isAllowPublishOffline: boolean,
|
|||
return resolve();
|
||||
}
|
||||
|
||||
return reject(ErrorCode.getServiceUnavailable(API_ERROR.UPLINK_OFFLINE));
|
||||
return reject(ErrorCode.getServiceUnavailable(API_ERROR.UPLINK_OFFLINE_PUBLISH));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -105,10 +105,10 @@ class ProxyStorage implements IProxy {
|
|||
|
||||
process.nextTick(function() {
|
||||
if (cb) {
|
||||
cb(ErrorCode.getInternalError('uplink is offline'));
|
||||
cb(ErrorCode.getInternalError(API_ERROR.UPLINK_OFFLINE));
|
||||
}
|
||||
// $FlowFixMe
|
||||
streamRead.emit('error', ErrorCode.getInternalError('uplink is offline'));
|
||||
streamRead.emit('error', ErrorCode.getInternalError(API_ERROR.UPLINK_OFFLINE));
|
||||
});
|
||||
// $FlowFixMe
|
||||
streamRead._read = function() {};
|
||||
|
|
|
@ -4,17 +4,21 @@ import path from 'path';
|
|||
import rimraf from 'rimraf';
|
||||
|
||||
import {HEADERS} from '../../../src/lib/constants';
|
||||
import configDefault from '../partials/config/access';
|
||||
import configDefault from '../partials/config/config_access';
|
||||
import Config from '../../../src/lib/config';
|
||||
import endPointAPI from '../../../src/api/index';
|
||||
import {mockServer} from './mock';
|
||||
import {DOMAIN_SERVERS} from '../../functional/config.functional';
|
||||
|
||||
require('../../../src/lib/logger').setup([]);
|
||||
|
||||
describe('api with no limited access configuration', () => {
|
||||
let config;
|
||||
let app;
|
||||
let mockRegistry;
|
||||
|
||||
beforeAll(function(done) {
|
||||
const mockServerPort = 55530;
|
||||
const store = path.join(__dirname, './partials/store/access-storage');
|
||||
rimraf(store, async () => {
|
||||
const configForTest = _.clone(configDefault);
|
||||
|
@ -24,8 +28,14 @@ describe('api with no limited access configuration', () => {
|
|||
}
|
||||
};
|
||||
configForTest.self_path = store;
|
||||
configForTest.uplinks = {
|
||||
npmjs: {
|
||||
url: `http://${DOMAIN_SERVERS}:${mockServerPort}`
|
||||
}
|
||||
};
|
||||
config = new Config(configForTest);
|
||||
app = await endPointAPI(config);
|
||||
mockRegistry = await mockServer(mockServerPort).init();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
@ -34,9 +44,11 @@ describe('api with no limited access configuration', () => {
|
|||
const store = path.join(__dirname, './partials/store/access-storage');
|
||||
rimraf(store, (err) => {
|
||||
if (err) {
|
||||
mockRegistry[0].stop();
|
||||
return done(err);
|
||||
}
|
||||
|
||||
mockRegistry[0].stop();
|
||||
return done();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -8,7 +8,7 @@ import {setup} from '../../../src/lib/logger';
|
|||
|
||||
import type {Config, UpLinkConf} from '@verdaccio/types';
|
||||
import type {IProxy} from '../../../types/index';
|
||||
import {API_ERROR} from "../../../src/lib/constants";
|
||||
import {API_ERROR, HTTP_STATUS} from "../../../src/lib/constants";
|
||||
import {mockServer} from './mock';
|
||||
import {DOMAIN_SERVERS} from '../../functional/config.functional';
|
||||
|
||||
|
@ -102,8 +102,8 @@ describe('UpStorge', () => {
|
|||
|
||||
stream.on('error', function(err) {
|
||||
expect(err).not.toBeNull();
|
||||
expect(err.statusCode).toBe(404);
|
||||
expect(err.message).toMatch(/file doesn't exist on uplink/);
|
||||
expect(err.statusCode).toBe(HTTP_STATUS.NOT_FOUND);
|
||||
expect(err.message).toMatch(API_ERROR.NOT_FILE_UPLINK);
|
||||
|
||||
done();
|
||||
});
|
||||
|
@ -141,9 +141,9 @@ describe('UpStorge', () => {
|
|||
const streamThirdTry = proxy.fetchTarball(tarball);
|
||||
streamThirdTry.on('error', function(err) {
|
||||
expect(err).not.toBeNull();
|
||||
expect(err.statusCode).toBe(500);
|
||||
expect(err.statusCode).toBe(HTTP_STATUS.INTERNAL_ERROR);
|
||||
expect(proxy.failed_requests).toBe(2);
|
||||
expect(err.message).toMatch(/uplink is offline/);
|
||||
expect(err.message).toMatch(API_ERROR.UPLINK_OFFLINE);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
import path from 'path';
|
||||
import {DEFAULT_REGISTRY} from '../../../../src/lib/constants';
|
||||
|
||||
const config = {
|
||||
storage: path.join(__dirname, '../store/access-storage'),
|
||||
uplinks: {
|
||||
'npmjs': {
|
||||
'url': DEFAULT_REGISTRY
|
||||
'url': 'http://never_use:0000/'
|
||||
}
|
||||
},
|
||||
packages: {
|
Loading…
Add table
Reference in a new issue