2018-06-29 18:20:27 -05:00
|
|
|
import path from 'path';
|
2018-09-05 15:10:27 -05:00
|
|
|
import _ from 'lodash';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2020-11-15 05:14:09 -05:00
|
|
|
import {
|
|
|
|
getMatchedPackagesSpec,
|
|
|
|
normalisePackageAccess,
|
|
|
|
PACKAGE_ACCESS,
|
|
|
|
} from '../src/package-access';
|
2020-11-05 17:22:09 -05:00
|
|
|
import { parseConfigFile } from '../src';
|
2020-03-03 17:59:19 -05:00
|
|
|
|
2020-11-08 09:20:02 -05:00
|
|
|
describe('Package access 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';
|
|
|
|
|
2020-03-03 17:59:19 -05:00
|
|
|
return path.join(__dirname, `./partials/config/${format}/${name}.${format}`);
|
2018-06-29 18:20:27 -05:00
|
|
|
};
|
|
|
|
|
2018-07-02 01:24:45 -05:00
|
|
|
describe('normalisePackageAccess', () => {
|
2020-08-13 16:27:00 -05:00
|
|
|
test('should test basic conversion', () => {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
test('should define an empty publish array even if is not defined in packages', () => {
|
|
|
|
const { packages } = parseConfigFile(parseConfigurationFile('pkgs-basic-no-publish'));
|
2018-10-06 17:45:17 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
test('should define an empty access array even if is not defined in packages', () => {
|
|
|
|
const { packages } = parseConfigFile(parseConfigurationFile('pkgs-basic-no-access'));
|
2018-10-06 17:45:17 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
test('should define an empty proxy array even if is not defined in package', () => {
|
|
|
|
const { packages } = parseConfigFile(parseConfigurationFile('pkgs-basic-no-proxy'));
|
2018-10-06 17:45:17 -05:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
test('should test multi user group definition', () => {
|
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
2020-10-24 06:17:21 -05:00
|
|
|
test(
|
|
|
|
'should normalize deprecated packages into the new ones ' + '(backward props compatible)',
|
|
|
|
() => {
|
|
|
|
const { packages } = parseConfigFile(parseConfigurationFile('deprecated-pkgs-basic'));
|
|
|
|
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();
|
|
|
|
expect(react.access).toBeDefined();
|
|
|
|
|
|
|
|
// Intended checks, Typescript should catch this, we test the runtime part
|
|
|
|
// @ts-ignore
|
|
|
|
expect(react.access).toEqual([]);
|
|
|
|
// @ts-ignore
|
|
|
|
expect(react.publish[0]).toBe('admin');
|
|
|
|
expect(react.proxy).toBeDefined();
|
|
|
|
// @ts-ignore
|
|
|
|
expect(react.proxy).toEqual([]);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
);
|
2018-06-29 18:20:27 -05:00
|
|
|
|
2020-08-13 16:27:00 -05:00
|
|
|
test('should check not default packages access', () => {
|
2019-07-16 01:40:01 -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', () => {
|
2020-08-13 16:27:00 -05:00
|
|
|
const { packages } = parseConfigFile(parseConfigurationFile('pkgs-custom'));
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-07-03 17:12:28 -05:00
|
|
|
expect(getMatchedPackagesSpec('react', packages).proxy).toMatch('facebook');
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-07-03 17:12:28 -05:00
|
|
|
expect(getMatchedPackagesSpec('angular', packages).proxy).toMatch('google');
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-07-03 17:12:28 -05:00
|
|
|
expect(getMatchedPackagesSpec('vue', packages).proxy).toMatch('npmjs');
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-07-03 17:12:28 -05:00
|
|
|
expect(getMatchedPackagesSpec('@scope/vue', packages).proxy).toMatch('npmjs');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should test no ** wildcard on config', () => {
|
2020-08-13 16:27:00 -05:00
|
|
|
const { packages } = parseConfigFile(parseConfigurationFile('pkgs-nosuper-wildcard-custom'));
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-07-03 17:12:28 -05:00
|
|
|
expect(getMatchedPackagesSpec('react', packages).proxy).toMatch('facebook');
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-07-03 17:12:28 -05:00
|
|
|
expect(getMatchedPackagesSpec('angular', packages).proxy).toMatch('google');
|
2019-07-16 01:40:01 -05:00
|
|
|
// @ts-ignore
|
2018-07-03 17:12:28 -05:00
|
|
|
expect(getMatchedPackagesSpec('@fake/angular', packages).proxy).toMatch('npmjs');
|
|
|
|
expect(getMatchedPackagesSpec('vue', packages)).toBeUndefined();
|
|
|
|
expect(getMatchedPackagesSpec('@scope/vue', packages)).toBeUndefined();
|
|
|
|
});
|
|
|
|
});
|
2018-06-29 18:20:27 -05:00
|
|
|
});
|