feat(core): Use WebCrypto to generate all random numbers and strings (#9786)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2024-06-19 13:33:57 +02:00
committed by GitHub
parent cfc4db00e3
commit 65c5609ab5
49 changed files with 254 additions and 214 deletions

View File

@@ -1,5 +1,14 @@
import { ALPHABET } from '@/Constants';
import { ApplicationError } from '@/errors/application.error';
import { jsonParse, jsonStringify, deepCopy, isObjectEmpty, fileTypeFromMimeType } from '@/utils';
import {
jsonParse,
jsonStringify,
deepCopy,
isObjectEmpty,
fileTypeFromMimeType,
randomInt,
randomString,
} from '@/utils';
describe('isObjectEmpty', () => {
it('should handle null and undefined', () => {
@@ -237,3 +246,47 @@ describe('fileTypeFromMimeType', () => {
expect(fileTypeFromMimeType('application/pdf')).toEqual('pdf');
});
});
const repeat = (fn: () => void, times = 10) => Array(times).fill(0).forEach(fn);
describe('randomInt', () => {
it('should generate random integers', () => {
repeat(() => {
const result = randomInt(10);
expect(result).toBeLessThanOrEqual(10);
expect(result).toBeGreaterThanOrEqual(0);
});
});
it('should generate random in range', () => {
repeat(() => {
const result = randomInt(10, 100);
expect(result).toBeLessThanOrEqual(100);
expect(result).toBeGreaterThanOrEqual(10);
});
});
});
describe('randomString', () => {
it('should return a random string of the specified length', () => {
repeat(() => {
const result = randomString(42);
expect(result).toHaveLength(42);
});
});
it('should return a random string of the in the length range', () => {
repeat(() => {
const result = randomString(10, 100);
expect(result.length).toBeGreaterThanOrEqual(10);
expect(result.length).toBeLessThanOrEqual(100);
});
});
it('should only contain characters from the specified character set', () => {
repeat(() => {
const result = randomString(1000);
result.split('').every((char) => ALPHABET.includes(char));
});
});
});