n8n/packages/cli/test/integration/shared/random.ts
Iván Ovejero 7625421b81
test: Fix randomly failing UM tests (#3061)
*  Declutter test logs

* 🐛 Fix random passwords length

* 🐛 Fix password hashing in test user creation

* 🐛 Hash leftover password

*  Improve error message for `compare`

*  Restore `randomInvalidPassword` contant

*  Mock Telemetry module to prevent `--forceExit`

*  Silence logger

*  Simplify condition

*  Unhash password in payload
2022-04-01 17:48:02 +02:00

42 lines
1.7 KiB
TypeScript

import { randomBytes } from 'crypto';
import { MAX_PASSWORD_LENGTH, MIN_PASSWORD_LENGTH } from '../../../src/databases/entities/User';
/**
* Create a random alphanumeric string of random length between two limits, both inclusive.
* Limits should be even numbers (round down otherwise).
*/
export function randomString(min: number, max: number) {
const randomInteger = Math.floor(Math.random() * (max - min) + min) + 1;
return randomBytes(randomInteger / 2).toString('hex');
}
const chooseRandomly = <T>(array: T[]) => array[Math.floor(Math.random() * array.length)];
const randomDigit = () => Math.floor(Math.random() * 10);
const randomUppercaseLetter = () => chooseRandomly('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''));
export const randomValidPassword = () =>
randomString(MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH - 2) + randomUppercaseLetter() + randomDigit();
export const randomInvalidPassword = () =>
chooseRandomly([
randomString(1, MIN_PASSWORD_LENGTH - 1),
randomString(MAX_PASSWORD_LENGTH + 2, MAX_PASSWORD_LENGTH + 100),
'abcdefgh', // valid length, no number, no uppercase
'abcdefg1', // valid length, has number, no uppercase
'abcdefgA', // valid length, no number, has uppercase
'abcdefA', // invalid length, no number, has uppercase
'abcdef1', // invalid length, has number, no uppercase
'abcdeA1', // invalid length, has number, has uppercase
'abcdefg', // invalid length, no number, no uppercase
]);
export const randomEmail = () => `${randomName()}@${randomName()}.${randomTopLevelDomain()}`;
const POPULAR_TOP_LEVEL_DOMAINS = ['com', 'org', 'net', 'io', 'edu'];
const randomTopLevelDomain = () => chooseRandomly(POPULAR_TOP_LEVEL_DOMAINS);
export const randomName = () => randomString(4, 8);