File size: 1,185 Bytes
e3cb794
 
 
a18eb38
e3cb794
 
 
 
 
 
34a23a6
e3cb794
 
 
 
34a23a6
e3cb794
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import { expect } from 'chai';
import { generateSessionToken } from '../src/helpers';

describe('Helpers', () => {
  it('should generate a token of a correct length', () => {
    const length = 16; // Specify the length of the token to generate
    const token = generateSessionToken(length);

    // Since the token is hexadecimal, its string length should be twice the byte length
    expect(token).to.be.a('string');
    expect(token.length).to.equal(length);
  });

  it('should generate a unique token each time', () => {
    const tokens = new Set<string>();
    const tokenCount = 1000; // Generate a number of tokens to test uniqueness
    const length = 16;

    for (let i = 0; i < tokenCount; i++) {
      const token = generateSessionToken(length);
      tokens.add(token);
    }

    // If all tokens are unique, the set's size should match the number of generated tokens
    expect(tokens.size).to.equal(tokenCount);
  });

  it('should throw an exception if an invalid length is provided', () => {
    const invalidLength = 0; // Zero or negative length or other invalid values could be tested
    expect(() => generateSessionToken(invalidLength)).to.throw();
  });
});