1
0
Fork 0
mirror of https://github.com/CorentinTh/it-tools.git synced 2025-08-03 20:45:18 +02:00

feat(tool): added token generator

This commit is contained in:
Corentin Thomasset 2022-04-04 00:25:29 +02:00
parent 25a8659786
commit 40dec52c84
No known key found for this signature in database
GPG key ID: DBD997E935996158
5 changed files with 189 additions and 46 deletions

View file

@ -0,0 +1,98 @@
import { expect, describe, it } from 'vitest';
import { createToken } from './token-generator.service';
describe('token-generator', () => {
describe('createToken', () => {
it('should generate an empty string when all params are false', () => {
const token = createToken({
withLowercase: false,
withUppercase: false,
withNumbers: false,
withSymbols: false,
length: 10,
});
expect(token).toHaveLength(0);
});
it('should generate a random string with the specified length', () => {
const createTokenWithLength = (length: number) =>
createToken({
withLowercase: true,
withUppercase: true,
withNumbers: true,
withSymbols: true,
length,
});
expect(createTokenWithLength(5)).toHaveLength(5);
expect(createTokenWithLength(10)).toHaveLength(10);
expect(createTokenWithLength(100)).toHaveLength(100);
});
it('should generate a random string with just uppercase if only withUppercase is set', () => {
const token = createToken({
withLowercase: false,
withUppercase: true,
withNumbers: false,
withSymbols: false,
length: 256,
});
expect(token).toHaveLength(256);
expect(token).toMatch(/^[A-Z]+$/);
});
it('should generate a random string with just lowercase if only withLowercase is set', () => {
const token = createToken({
withLowercase: true,
withUppercase: false,
withNumbers: false,
withSymbols: false,
length: 256,
});
expect(token).toHaveLength(256);
expect(token).toMatch(/^[a-z]+$/);
});
it('should generate a random string with just numbers if only withNumbers is set', () => {
const token = createToken({
withLowercase: false,
withUppercase: false,
withNumbers: true,
withSymbols: false,
length: 256,
});
expect(token).toHaveLength(256);
expect(token).toMatch(/^[0-9]+$/);
});
it('should generate a random string with just symbols if only withSymbols is set', () => {
const token = createToken({
withLowercase: false,
withUppercase: false,
withNumbers: false,
withSymbols: true,
length: 256,
});
expect(token).toHaveLength(256);
expect(token).toMatch(/^[.,;:!?./\-"'#{([-|\\@)\]=}*+]+$/);
});
it('should generate a random string with just letters (case incensitive) with withLowercase and withUppercase', () => {
const token = createToken({
withLowercase: true,
withUppercase: true,
withNumbers: false,
withSymbols: false,
length: 256,
});
expect(token).toHaveLength(256);
expect(token).toMatch(/^[a-zA-Z]+$/);
});
});
});