2018-06-29 18:20:27 -05:00
|
|
|
// @flow
|
2018-07-03 17:12:28 -05:00
|
|
|
|
2018-06-29 18:20:27 -05:00
|
|
|
import path from 'path';
|
2018-09-05 15:10:27 -05:00
|
|
|
import _ from 'lodash';
|
2019-05-20 00:41:12 -05:00
|
|
|
import {spliceURL} from '../../../../src/utils/string';
|
|
|
|
import {parseConfigFile} from '../../../../src/lib/utils';
|
2018-07-04 00:31:51 -05:00
|
|
|
import {
|
|
|
|
getMatchedPackagesSpec,
|
|
|
|
hasProxyTo,
|
|
|
|
normalisePackageAccess, sanityCheckUplinksProps,
|
|
|
|
uplinkSanityCheck
|
2019-05-20 00:41:12 -05:00
|
|
|
} from '../../../../src/lib/config-utils';
|
|
|
|
import {PACKAGE_ACCESS, ROLES} from '../../../../src/lib/constants';
|
2018-06-29 18:20:27 -05:00
|
|
|
|
|
|
|
describe('Config Utilities', () => {
|
|
|
|
|
2019-05-22 00:50:14 -05:00
|
|
|
const parseConfigurationFile = (conf) => {
|
|
|
|
const { name, ext } = path.parse(conf);
|
|
|
|
const format = ext.startsWith('.') ? ext.substring(1) : 'yaml';
|
|
|
|
|
|
|
|
return path.join(__dirname, `../../partials/config/${format}/${name}.${format}`);
|
2018-06-29 18:20:27 -05:00
|
|
|
};
|
|
|
|
|
2018-07-04 00:31:51 -05:00
|
|
|
describe('uplinkSanityCheck', () => {
|
|
|
|
test('should test basic conversion', ()=> {
|
2018-08-21 01:05:34 -05:00
|
|
|
const uplinks = uplinkSanityCheck(parseConfigFile(parseConfigurationFile('uplink-basic')).uplinks);
|
2018-07-04 00:31:51 -05:00
|
|
|
expect(Object.keys(uplinks)).toContain('server1');
|
|
|
|
expect(Object.keys(uplinks)).toContain('server2');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should throw error on blacklisted uplink name', ()=> {
|
2018-08-21 01:05:34 -05:00
|
|
|
const {uplinks} = parseConfigFile(parseConfigurationFile('uplink-wrong'));
|
2018-07-04 00:31:51 -05:00
|
|
|
|
|
|
|
expect(() => {
|
|
|
|
uplinkSanityCheck(uplinks)
|
|
|
|
}).toThrow('CONFIG: reserved uplink name: anonymous');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('sanityCheckUplinksProps', () => {
|
|
|
|
test('should fails if url prop is missing', ()=> {
|
2018-08-21 01:05:34 -05:00
|
|
|
const {uplinks} = parseConfigFile(parseConfigurationFile('uplink-wrong'));
|
2018-07-04 00:31:51 -05:00
|
|
|
expect(() => {
|
|
|
|
sanityCheckUplinksProps(uplinks)
|
|
|
|
}).toThrow('CONFIG: no url for uplink: none-url');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should bypass an empty uplink list', ()=> {
|
|
|
|
expect(sanityCheckUplinksProps([])).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-02 01:24:45 -05:00
|
|
|
describe('normalisePackageAccess', () => {
|
2018-06-29 18:20:27 -05:00
|
|
|
test('should test basic conversion', ()=> {
|
2018-08-21 01:05:34 -05:00
|
|
|
const {packages} = parseConfigFile(parseConfigurationFile('pkgs-basic'));
|
2018-06-29 18:20:27 -05:00
|
|
|
const access = normalisePackageAccess(packages);
|
|
|
|
|
|
|
|
expect(access).toBeDefined();
|
|
|
|
const scoped = access[`${PACKAGE_ACCESS.SCOPE}`];
|
|
|
|
const all = access[`${PACKAGE_ACCESS.ALL}`];
|
|
|
|
|
|
|
|
expect(scoped).toBeDefined();
|
|
|
|
expect(all).toBeDefined();
|
|
|
|
});
|
|
|
|
|
2018-10-06 17:45:17 -05:00
|
|
|
test('should define an empty publish array even if is not defined in packages', ()=> {
|
|
|
|
const {packages} = parseConfigFile(parseConfigurationFile('pkgs-basic-no-publish'));
|
|
|
|
const access = normalisePackageAccess(packages);
|
|
|
|
|
|
|
|
const scoped = access[`${PACKAGE_ACCESS.SCOPE}`];
|
|
|
|
const all = access[`${PACKAGE_ACCESS.ALL}`];
|
|
|
|
// publish must defined
|
|
|
|
expect(scoped.publish).toBeDefined();
|
|
|
|
expect(scoped.publish).toHaveLength(0);
|
|
|
|
expect(all.publish).toBeDefined();
|
|
|
|
expect(all.publish).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should define an empty access array even if is not defined in packages', ()=> {
|
|
|
|
const {packages} = parseConfigFile(parseConfigurationFile('pkgs-basic-no-access'));
|
|
|
|
const access = normalisePackageAccess(packages);
|
|
|
|
|
|
|
|
const scoped = access[`${PACKAGE_ACCESS.SCOPE}`];
|
|
|
|
const all = access[`${PACKAGE_ACCESS.ALL}`];
|
|
|
|
// publish must defined
|
|
|
|
expect(scoped.access).toBeDefined();
|
|
|
|
expect(scoped.access).toHaveLength(0);
|
|
|
|
expect(all.access).toBeDefined();
|
|
|
|
expect(all.access).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should define an empty proxy array even if is not defined in package', ()=> {
|
|
|
|
const {packages} = parseConfigFile(parseConfigurationFile('pkgs-basic-no-proxy'));
|
|
|
|
const access = normalisePackageAccess(packages);
|
|
|
|
|
|
|
|
const scoped = access[`${PACKAGE_ACCESS.SCOPE}`];
|
|
|
|
const all = access[`${PACKAGE_ACCESS.ALL}`];
|
|
|
|
// publish must defined
|
|
|
|
expect(scoped.proxy).toBeDefined();
|
|
|
|
expect(scoped.proxy).toHaveLength(0);
|
|
|
|
expect(all.proxy).toBeDefined();
|
|
|
|
expect(all.proxy).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should test multi user group definition', ()=> {
|
2018-08-21 01:05:34 -05:00
|
|
|
const {packages} = parseConfigFile(parseConfigurationFile('pkgs-multi-group'));
|
2018-06-29 18:20:27 -05:00
|
|
|
const access = normalisePackageAccess(packages);
|
|
|
|
|
|
|
|
expect(access).toBeDefined();
|
|
|
|
const scoped = access[`${PACKAGE_ACCESS.SCOPE}`];
|
|
|
|
|
|
|
|
const all = access[`${PACKAGE_ACCESS.ALL}`];
|
|
|
|
|
|
|
|
expect(scoped).toBeDefined();
|
|
|
|
expect(scoped.access).toContain('$all');
|
|
|
|
expect(scoped.publish).toHaveLength(2);
|
|
|
|
expect(scoped.publish).toContain('admin');
|
|
|
|
expect(scoped.publish).toContain('superadmin');
|
|
|
|
|
|
|
|
expect(all).toBeDefined();
|
|
|
|
expect(all.access).toHaveLength(3);
|
|
|
|
expect(all.access).toContain('$all');
|
|
|
|
expect(all.publish).toHaveLength(1);
|
|
|
|
expect(all.publish).toContain('admin');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2018-10-06 17:45:17 -05:00
|
|
|
test('should normalize deprecated packages into the new ones (backward props compatible)', ()=> {
|
2018-08-21 01:05:34 -05:00
|
|
|
const {packages} = parseConfigFile(parseConfigurationFile('deprecated-pkgs-basic'));
|
2018-06-29 18:20:27 -05:00
|
|
|
const access = normalisePackageAccess(packages);
|
|
|
|
|
|
|
|
expect(access).toBeDefined();
|
|
|
|
const scoped = access[`${PACKAGE_ACCESS.SCOPE}`];
|
|
|
|
const all = access[`${PACKAGE_ACCESS.ALL}`];
|
|
|
|
const react = access['react-*'];
|
|
|
|
|
|
|
|
expect(react).toBeDefined();
|
2018-07-02 01:24:45 -05:00
|
|
|
expect(react.access).toBeDefined();
|
|
|
|
// $FlowFixMe
|
2018-06-29 18:20:27 -05:00
|
|
|
expect(react.access[0]).toBe(ROLES.$ALL);
|
2018-07-02 01:24:45 -05:00
|
|
|
expect(react.publish).toBeDefined();
|
2018-10-05 04:08:54 -05:00
|
|
|
// $FlowFixMe
|
2018-06-29 18:20:27 -05:00
|
|
|
expect(react.publish[0]).toBe('admin');
|
2018-07-02 01:24:45 -05:00
|
|
|
expect(react.proxy).toBeDefined();
|
|
|
|
// $FlowFixMe
|
2018-06-29 18:20:27 -05:00
|
|
|
expect(react.proxy[0]).toBe('uplink2');
|
|
|
|
expect(react.storage).toBeDefined();
|
|
|
|
|
|
|
|
expect(react.storage).toBe('react-storage');
|
|
|
|
expect(scoped).toBeDefined();
|
|
|
|
expect(scoped.storage).not.toBeDefined();
|
|
|
|
expect(all).toBeDefined();
|
|
|
|
expect(all.access).toBeDefined();
|
|
|
|
expect(all.storage).not.toBeDefined();
|
|
|
|
expect(all.publish).toBeDefined();
|
|
|
|
expect(all.proxy).toBeDefined();
|
|
|
|
expect(all.allow_access).toBeUndefined();
|
|
|
|
expect(all.allow_publish).toBeUndefined();
|
|
|
|
expect(all.proxy_access).toBeUndefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should check not default packages access', ()=> {
|
2018-08-21 01:05:34 -05:00
|
|
|
const {packages} = parseConfigFile(parseConfigurationFile('pkgs-empty'));
|
2018-06-29 18:20:27 -05:00
|
|
|
const access = normalisePackageAccess(packages);
|
|
|
|
expect(access).toBeDefined();
|
2018-07-03 17:12:28 -05:00
|
|
|
|
2018-06-29 18:20:27 -05:00
|
|
|
const scoped = access[`${PACKAGE_ACCESS.SCOPE}`];
|
|
|
|
expect(scoped).toBeUndefined();
|
|
|
|
|
2018-09-05 15:10:27 -05:00
|
|
|
// ** should be added by default **
|
2018-07-03 17:12:28 -05:00
|
|
|
const all = access[`${PACKAGE_ACCESS.ALL}`];
|
2018-06-29 18:20:27 -05:00
|
|
|
expect(all).toBeDefined();
|
2018-07-03 17:12:28 -05:00
|
|
|
|
2018-09-05 15:10:27 -05:00
|
|
|
expect(all.access).toBeDefined();
|
|
|
|
expect(_.isArray(all.access)).toBeTruthy();
|
|
|
|
expect(all.publish).toBeDefined();
|
|
|
|
expect(_.isArray(all.publish)).toBeTruthy();
|
2018-06-29 18:20:27 -05:00
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-07-03 17:12:28 -05:00
|
|
|
describe('getMatchedPackagesSpec', () => {
|
|
|
|
test('should test basic config', () => {
|
2018-08-21 01:05:34 -05:00
|
|
|
const {packages} = parseConfigFile(parseConfigurationFile('pkgs-custom'));
|
2018-07-03 17:12:28 -05:00
|
|
|
// $FlowFixMe
|
|
|
|
expect(getMatchedPackagesSpec('react', packages).proxy).toMatch('facebook');
|
|
|
|
// $FlowFixMe
|
|
|
|
expect(getMatchedPackagesSpec('angular', packages).proxy).toMatch('google');
|
|
|
|
// $FlowFixMe
|
|
|
|
expect(getMatchedPackagesSpec('vue', packages).proxy).toMatch('npmjs');
|
|
|
|
// $FlowFixMe
|
|
|
|
expect(getMatchedPackagesSpec('@scope/vue', packages).proxy).toMatch('npmjs');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should test no ** wildcard on config', () => {
|
2018-08-21 01:05:34 -05:00
|
|
|
const {packages} = parseConfigFile(parseConfigurationFile('pkgs-nosuper-wildcard-custom'));
|
2018-07-03 17:12:28 -05:00
|
|
|
// $FlowFixMe
|
|
|
|
expect(getMatchedPackagesSpec('react', packages).proxy).toMatch('facebook');
|
|
|
|
// $FlowFixMe
|
|
|
|
expect(getMatchedPackagesSpec('angular', packages).proxy).toMatch('google');
|
|
|
|
// $FlowFixMe
|
|
|
|
expect(getMatchedPackagesSpec('@fake/angular', packages).proxy).toMatch('npmjs');
|
|
|
|
expect(getMatchedPackagesSpec('vue', packages)).toBeUndefined();
|
|
|
|
expect(getMatchedPackagesSpec('@scope/vue', packages)).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('hasProxyTo', () => {
|
|
|
|
test('should test basic config', () => {
|
2018-08-21 01:05:34 -05:00
|
|
|
const packages = normalisePackageAccess(parseConfigFile(parseConfigurationFile('pkgs-basic')).packages);
|
2018-07-03 17:12:28 -05:00
|
|
|
// react
|
|
|
|
expect(hasProxyTo('react', 'facebook', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('react', 'google', packages)).toBeFalsy();
|
|
|
|
// vue
|
|
|
|
expect(hasProxyTo('vue', 'google', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('vue', 'fake', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('vue', 'npmjs', packages)).toBeTruthy();
|
|
|
|
// angular
|
|
|
|
expect(hasProxyTo('angular', 'google', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('angular', 'facebook', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('angular', 'npmjs', packages)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should test resolve based on custom package access', () => {
|
2018-08-21 01:05:34 -05:00
|
|
|
const packages = normalisePackageAccess(parseConfigFile(parseConfigurationFile('pkgs-custom')).packages);
|
2018-07-03 17:12:28 -05:00
|
|
|
// react
|
|
|
|
expect(hasProxyTo('react', 'facebook', packages)).toBeTruthy();
|
|
|
|
expect(hasProxyTo('react', 'google', packages)).toBeFalsy();
|
|
|
|
// vue
|
|
|
|
expect(hasProxyTo('vue', 'google', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('vue', 'fake', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('vue', 'npmjs', packages)).toBeTruthy();
|
|
|
|
// angular
|
|
|
|
expect(hasProxyTo('angular', 'google', packages)).toBeTruthy();
|
|
|
|
expect(hasProxyTo('angular', 'facebook', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not resolve any proxy', () => {
|
2018-08-21 01:05:34 -05:00
|
|
|
const packages = normalisePackageAccess(parseConfigFile(parseConfigurationFile('pkgs-empty')).packages);
|
2018-07-03 17:12:28 -05:00
|
|
|
// react
|
|
|
|
expect(hasProxyTo('react', 'npmjs', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('react', 'npmjs', packages)).toBeFalsy();
|
|
|
|
// vue
|
|
|
|
expect(hasProxyTo('vue', 'npmjs', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('vue', 'npmjs', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('vue', 'npmjs', packages)).toBeFalsy();
|
|
|
|
// angular
|
|
|
|
expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy();
|
|
|
|
expect(hasProxyTo('angular', 'npmjs', packages)).toBeFalsy();
|
|
|
|
// private
|
|
|
|
expect(hasProxyTo('private', 'fake', packages)).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-06-29 18:20:27 -05:00
|
|
|
describe('spliceURL', () => {
|
|
|
|
test('should splice two strings and generate a url', () => {
|
|
|
|
const url: string = spliceURL('http://domain.com', '/-/static/logo.png');
|
|
|
|
|
|
|
|
expect(url).toMatch('http://domain.com/-/static/logo.png');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should splice a empty strings and generate a url', () => {
|
|
|
|
const url: string = spliceURL('', '/-/static/logo.png');
|
|
|
|
|
|
|
|
expect(url).toMatch('/-/static/logo.png');
|
|
|
|
});
|
|
|
|
});
|
2019-05-22 00:50:14 -05:00
|
|
|
|
|
|
|
describe('JSON', () => {
|
|
|
|
test('parse default.json', () => {
|
|
|
|
const config = parseConfigFile(parseConfigurationFile('default.json'));
|
|
|
|
|
|
|
|
expect(config.storage).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('parse invalid.json', () => {
|
|
|
|
expect(function ( ) {
|
|
|
|
parseConfigFile(parseConfigurationFile('invalid.json'));
|
|
|
|
}).toThrow(/Error/);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('parse not-exists.json', () => {
|
|
|
|
expect(function ( ) {
|
|
|
|
parseConfigFile(parseConfigurationFile('not-exists.json'));
|
|
|
|
}).toThrow(/Error/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('JavaScript', () => {
|
|
|
|
test('parse default.js', () => {
|
|
|
|
const config = parseConfigFile(parseConfigurationFile('default.js'));
|
|
|
|
|
|
|
|
expect(config.storage).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('parse invalid.js', () => {
|
|
|
|
expect(function ( ) {
|
|
|
|
parseConfigFile(parseConfigurationFile('invalid.js'));
|
|
|
|
}).toThrow(/Error/);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('parse not-exists.js', () => {
|
|
|
|
expect(function ( ) {
|
|
|
|
parseConfigFile(parseConfigurationFile('not-exists.js'));
|
|
|
|
}).toThrow(/Error/);
|
|
|
|
});
|
|
|
|
});
|
2018-06-29 18:20:27 -05:00
|
|
|
});
|