mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-15 00:54:06 -08:00
c378f60a25
## Summary Provide details about your pull request and what it adds, fixes, or changes. Photos and videos are recommended. Continue breaking down `UserManagementHelper.ts` ... #### How to test the change: 1. ... ## Issues fixed Include links to Github issue or Community forum post or **Linear ticket**: > Important in order to close automatically and provide context to reviewers ... ## Review / Merge checklist - [ ] PR title and summary are descriptive. **Remember, the title automatically goes into the changelog. Use `(no-changelog)` otherwise.** ([conventions](https://github.com/n8n-io/n8n/blob/master/.github/pull_request_title_conventions.md)) - [ ] [Docs updated](https://github.com/n8n-io/n8n-docs) or follow-up ticket created. - [ ] Tests included. > A bug is not considered fixed, unless a test is added to prevent it from happening again. A feature is not complete without tests. > > *(internal)* You can use Slack commands to trigger [e2e tests](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#a39f9e5ba64a48b58a71d81c837e8227) or [deploy test instance](https://www.notion.so/n8n/How-to-use-Test-Instances-d65f49dfc51f441ea44367fb6f67eb0a?pvs=4#f6a177d32bde4b57ae2da0b8e454bfce) or [deploy early access version on Cloud](https://www.notion.so/n8n/Cloudbot-3dbe779836004972b7057bc989526998?pvs=4#fef2d36ab02247e1a0f65a74f6fb534e).
67 lines
2.3 KiB
TypeScript
67 lines
2.3 KiB
TypeScript
import { randomBytes } from 'crypto';
|
|
import { MIN_PASSWORD_CHAR_LENGTH, MAX_PASSWORD_CHAR_LENGTH } from '@/constants';
|
|
import type { CredentialPayload } from './types';
|
|
import { v4 as uuid } from 'uuid';
|
|
|
|
/**
|
|
* 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');
|
|
}
|
|
|
|
export function randomApiKey() {
|
|
return `n8n_api_${randomBytes(20).toString('hex')}`;
|
|
}
|
|
|
|
export const chooseRandomly = <T>(array: T[]) => array[Math.floor(Math.random() * array.length)];
|
|
|
|
export const randomInteger = (max = 1000) => Math.floor(Math.random() * max);
|
|
|
|
export const randomDigit = () => Math.floor(Math.random() * 10);
|
|
|
|
export const randomPositiveDigit = (): number => {
|
|
const digit = randomDigit();
|
|
|
|
return digit === 0 ? randomPositiveDigit() : digit;
|
|
};
|
|
|
|
const randomUppercaseLetter = () => chooseRandomly('ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''));
|
|
|
|
export const randomValidPassword = () =>
|
|
randomString(MIN_PASSWORD_CHAR_LENGTH, MAX_PASSWORD_CHAR_LENGTH - 2) +
|
|
randomUppercaseLetter() +
|
|
randomDigit();
|
|
|
|
export const randomInvalidPassword = () =>
|
|
chooseRandomly([
|
|
randomString(1, MIN_PASSWORD_CHAR_LENGTH - 1),
|
|
randomString(MAX_PASSWORD_CHAR_LENGTH + 2, MAX_PASSWORD_CHAR_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);
|
|
|
|
export const randomCredentialPayload = (): CredentialPayload => ({
|
|
name: randomName(),
|
|
type: randomName(),
|
|
nodesAccess: [{ nodeType: randomName() }],
|
|
data: { accessToken: randomString(6, 16) },
|
|
});
|
|
|
|
export const uniqueId = () => uuid();
|