0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-12-16 21:56:25 -05:00

chore(config): increase test coverage (#4382)

* chore(config): increase test coverage

* adjust length (32 bytes in hex)
This commit is contained in:
Marc Bernard 2024-01-06 11:31:32 +01:00 committed by GitHub
parent e1efd79b60
commit 4d9632424d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 111 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'@verdaccio/config': patch
---
chore(config): increase test coverage

View file

@ -3,8 +3,7 @@ const config = require('../../jest/config');
module.exports = Object.assign({}, config, {
coverageThreshold: {
global: {
// FIXME: increase to 90
lines: 85,
lines: 90,
},
},
});

View file

@ -27,7 +27,7 @@ export const PACKAGE_ACCESS = {
export function normalizeUserList(groupsList: any): any {
const result: any[] = [];
if (_.isNil(groupsList)) {
if (_.isNil(groupsList) || _.isEmpty(groupsList)) {
return result;
}

View file

@ -0,0 +1,42 @@
import { getUserAgent } from '../src';
describe('getUserAgent', () => {
test('should return custom user agent when customUserAgent is true', () => {
const customUserAgent = true;
const version = '1.0.0';
const name = 'MyAgent';
const result = getUserAgent(customUserAgent, version, name);
expect(result).toBe('MyAgent/1.0.0');
});
test('should return custom user agent when customUserAgent is a non-empty string', () => {
const customUserAgent = 'CustomAgent/1.0.0';
const version = '1.0.0';
const name = 'MyAgent';
const result = getUserAgent(customUserAgent, version, name);
expect(result).toBe('CustomAgent/1.0.0');
});
test('should return "hidden" when customUserAgent is false', () => {
const customUserAgent = false;
const version = '1.0.0';
const name = 'MyAgent';
const result = getUserAgent(customUserAgent, version, name);
expect(result).toBe('hidden');
});
test('should return default user agent when customUserAgent is undefined', () => {
const version = '1.0.0';
const name = 'MyAgent';
const result = getUserAgent(undefined, version, name);
expect(result).toBe('MyAgent/1.0.0');
});
});

View file

@ -13,6 +13,7 @@ describe('Config builder', () => {
proxy: 'some',
})
.addLogger({ level: 'info', type: 'stdout', format: 'json' })
.addAuth({ htpasswd: { file: '.htpasswd' } })
.addStorage('/tmp/verdaccio')
.addSecurity({ api: { legacy: true } });
expect(config.getConfig()).toEqual({
@ -21,6 +22,11 @@ describe('Config builder', () => {
legacy: true,
},
},
auth: {
htpasswd: {
file: '.htpasswd',
},
},
storage: '/tmp/verdaccio',
packages: {
'upstream/*': {

View file

@ -106,6 +106,20 @@ describe('checkSecretKey', () => {
const config = new Config(parseConfigFile(resolveConf('default')));
expect(typeof config.checkSecretKey('') === 'string').toBeTruthy();
});
test('with enhanced legacy signature', () => {
const config = new Config(parseConfigFile(resolveConf('default')));
config.security.enhancedLegacySignature = true;
expect(typeof config.checkSecretKey() === 'string').toBeTruthy();
expect(config.secret.length).toBe(32);
});
test('without enhanced legacy signature', () => {
const config = new Config(parseConfigFile(resolveConf('default')));
config.security.enhancedLegacySignature = false;
expect(typeof config.checkSecretKey() === 'string').toBeTruthy();
expect(config.secret.length).toBe(64);
});
});
describe('getMatchedPackagesSpec', () => {
@ -159,3 +173,18 @@ describe('VERDACCIO_STORAGE_PATH', () => {
delete process.env.VERDACCIO_STORAGE_PATH;
});
});
describe('configPath', () => {
test('should set configPath in config', () => {
const defaultConfig = parseConfigFile(resolveConf('default'));
const config = new Config(defaultConfig);
expect(config.getConfigPath()).toBe(path.join(__dirname, '../src/conf/default.yaml'));
});
test('should throw an error if configPath is not provided', () => {
const defaultConfig = parseConfigFile(resolveConf('default'));
defaultConfig.configPath = '';
defaultConfig.config_path = '';
expect(() => new Config(defaultConfig)).toThrow('configPath property is required');
});
});

View file

@ -1,7 +1,7 @@
import _ from 'lodash';
import { parseConfigFile } from '../src';
import { PACKAGE_ACCESS, normalisePackageAccess } from '../src/package-access';
import { PACKAGE_ACCESS, normalisePackageAccess, normalizeUserList } from '../src/package-access';
import { parseConfigurationFile } from './utils';
describe('Package access utilities', () => {
@ -123,4 +123,30 @@ describe('Package access utilities', () => {
expect(_.isArray(all.publish)).toBeTruthy();
});
});
describe('normaliseUserList', () => {
test('should normalize user list', () => {
const groupsList = 'admin superadmin';
const result = normalizeUserList(groupsList);
expect(result).toEqual(['admin', 'superadmin']);
});
test('should normalize empty user list', () => {
const groupsList = '';
const result = normalizeUserList(groupsList);
expect(result).toEqual([]);
});
test('should normalize user list array', () => {
const groupsList = ['admin', 'superadmin'];
const result = normalizeUserList(groupsList);
expect(result).toEqual(['admin', 'superadmin']);
});
test('should throw error for invalid user list', () => {
const groupsList = { group: 'admin' };
expect(() => {
normalizeUserList(groupsList);
}).toThrow('CONFIG: bad package acl (array or string expected): {"group":"admin"}');
});
});
});