-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SPSH-1278 New password generation (#731)
* New password generation
- Loading branch information
1 parent
ffbf6e4
commit c5d17ad
Showing
11 changed files
with
85 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { generatePassword } from './password-generator.js'; | ||
|
||
describe('passwordGenerator', () => { | ||
it('should return passwords with the correct length', () => { | ||
for (let i: number = 4; i < 50; i++) { | ||
expect(generatePassword(i)).toHaveLength(i); | ||
} | ||
}); | ||
|
||
it('should always return passwords with at least 4 characters', () => { | ||
for (let i: number = 0; i < 4; i++) { | ||
expect(generatePassword(i)).toHaveLength(4); | ||
} | ||
}); | ||
|
||
it.each([ | ||
{ name: 'lowercase', regex: /[abcdefghijklmnopqrstuvwxyz]/ }, | ||
{ name: 'uppercase', regex: /[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/ }, | ||
{ name: 'numbers', regex: /[0123456789]/ }, | ||
{ name: 'symbols', regex: /[+\-*/%&!?@$#]/ }, | ||
])('Should contain $name', ({ regex }: { regex: RegExp }) => { | ||
// Repeat the test 100 times | ||
for (let i: number = 0; i < 100; i++) { | ||
const password: string = generatePassword(4); | ||
|
||
expect(password).toEqual(expect.stringMatching(regex)); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { randomInt } from 'node:crypto'; | ||
|
||
const LOWERCASE: string = 'abcdefghijklmnopqrstuvwxyz'; | ||
const UPPERCASE: string = LOWERCASE.toUpperCase(); | ||
const NUMBERS: string = '0123456789'; | ||
const SYMBOLS: string = '+-*/%&!?@$#'; | ||
const ALL_CHARACTERS: string = LOWERCASE + UPPERCASE + NUMBERS + SYMBOLS; | ||
|
||
function randomChar(str: string): string { | ||
return str.charAt(randomInt(str.length)); | ||
} | ||
|
||
/** | ||
* Generates a random password with the following rules: | ||
* - At least one lowercase character | ||
* - At least one uppercase character | ||
* - At least one number | ||
* - At least one symbol | ||
* | ||
* Passwords will always be at least 4 characters long, to fulfill these rules | ||
* @param length The length of the password | ||
*/ | ||
export function generatePassword(length: number = 8): string { | ||
let password: string = ''; | ||
password += randomChar(LOWERCASE); // One lowercase char | ||
password += randomChar(UPPERCASE); // One uppercase char | ||
password += randomChar(NUMBERS); // One number | ||
password += randomChar(SYMBOLS); // One symbol | ||
|
||
for (let i: number = password.length; i < length; i++) { | ||
password += randomChar(ALL_CHARACTERS); // Fill the rest with random characters | ||
} | ||
|
||
return password; | ||
} |