forked from antfu/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb99be3
commit 0bb635b
Showing
2 changed files
with
115 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,80 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { Sesame } from '../src'; | ||
|
||
describe('Sesame', () => { | ||
it('should create and assert password length', () => { | ||
// Given | ||
const expectedLength = 64; | ||
describe('Sesame ', () => { | ||
describe('Create password ', () => { | ||
it('should create and assert password length', () => { | ||
// Given | ||
const expectedLength = 64; | ||
|
||
// When | ||
const password = Sesame.create(expectedLength); | ||
// When | ||
const password = Sesame.generate(expectedLength); | ||
|
||
// Then | ||
expect(password.length).toEqual(expectedLength); | ||
}); | ||
// Then | ||
expect(password.length).toEqual(expectedLength); | ||
}); | ||
|
||
it('should return a string of the specified length', () => { | ||
// Given a specified length | ||
const length = 10; | ||
// When the password is created | ||
const password = Sesame.create(length); | ||
// Then the password should have the specified length | ||
expect(password).to.have.lengthOf(length); | ||
}); | ||
it('should return a string of the specified length', () => { | ||
// Given a specified length | ||
const length = 10; | ||
// When the password is created | ||
const password = Sesame.generate(length); | ||
// Then the password should have the specified length | ||
expect(password).to.have.length(length); | ||
}); | ||
|
||
it('should only contain characters from the available characters', () => { | ||
// Given a password of length 100 | ||
const password = Sesame.generate(100); | ||
const availableChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*{}(),.;:/<>?| _-+='; | ||
// When we check each character in the password | ||
for (const char of password) { | ||
// Then each character should be in the list of available characters | ||
expect(availableChars).to.include(char); | ||
} | ||
}); | ||
|
||
it('should only contain characters from the available characters', () => { | ||
// Given a password of length 100 | ||
const password = Sesame.create(100); | ||
const availableChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*{}(),.;:/<>?|_-+='; | ||
// When we check each character in the password | ||
for (const char of password) { | ||
// Then each character should be in the list of available characters | ||
expect(availableChars).to.include(char); | ||
} | ||
it('should generate different passwords each time', () => { | ||
// Given two passwords of equal length | ||
const password1 = Sesame.generate(10); | ||
const password2 = Sesame.generate(10); | ||
// When we compare the two passwords | ||
// Then they should not be equal | ||
expect(password1).to.not.equal(password2); | ||
}); | ||
|
||
// Additional test for handling invalid inputs | ||
it('should throw an error for invalid length', () => { | ||
// Given an invalid length | ||
const invalidLength = -1; | ||
// When we try to create a password with the invalid length | ||
// Then it should throw an error | ||
expect(() => Sesame.generate(invalidLength)).to.throw('Length must be a positive integer'); | ||
}); | ||
}); | ||
|
||
it('should generate different passwords each time', () => { | ||
// Given two passwords of equal length | ||
const password1 = Sesame.create(10); | ||
const password2 = Sesame.create(10); | ||
// When we compare the two passwords | ||
// Then they should not be equal | ||
expect(password1).to.not.equal(password2); | ||
describe('Test passwords', () => { | ||
it('should calculate password entropy', () => { | ||
// Given | ||
const password = Sesame.generate(32); | ||
const expectedEntropy = 180; | ||
|
||
// When | ||
const entropy = Sesame.entropy(password); | ||
|
||
// Then | ||
expect(entropy).to.be.at.least(expectedEntropy); | ||
}); | ||
|
||
it('should return zero entropy for an empty password', () => { | ||
// Given | ||
const password = ''; | ||
|
||
// When | ||
const entropy = Sesame.entropy(password); | ||
|
||
// Then | ||
expect(entropy).toEqual(0); | ||
}); | ||
}); | ||
}); |