-
Notifications
You must be signed in to change notification settings - Fork 30
/
session.spec.js
74 lines (63 loc) · 2.43 KB
/
session.spec.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import sinon from 'sinon'
import hash from 'hash.js'
import sessionUtils from './session'
describe('Store utils > sessionUtils', function () {
describe('getSessionID', function () {
let generateSessionIDSpy
beforeEach(function () {
generateSessionIDSpy = sinon.spy(sessionUtils, 'generateSessionID')
})
afterEach(function () {
generateSessionIDSpy.restore()
sessionStorage.removeItem('session_id')
})
it('should return the generated id', function () {
const id = sessionUtils.getSessionID()
expect(id).to.a('string')
})
it('it should call generateSessionID if there is not a stored id in session or local storage', function () {
sessionUtils.getSessionID()
expect(generateSessionIDSpy).to.have.been.called()
})
it('it should retrieve id from session if it exists', function () {
const stored = { id: 'foobar', ttl: (Date.now() + 50000) }
sessionStorage.setItem('session_id', JSON.stringify(stored))
sessionUtils.getSessionID()
expect(generateSessionIDSpy).to.have.not.been.called()
})
})
describe('generateSessionID', function () {
after(function () {
sessionStorage.removeItem('session_id')
})
it('should use the hash.js module\'s sha256 utility for id generation', function () {
const hashSpy = sinon.spy(hash, 'sha256')
sessionUtils.generateSessionID()
expect(hashSpy).to.have.been.called()
hashSpy.restore()
})
it('should call Math.random() when generating the id', function () {
const mathSpy = sinon.spy(Math, 'random')
sessionUtils.generateSessionID()
expect(mathSpy).to.have.been.called()
mathSpy.restore()
})
it('should call Date.now() when generating the id', function () {
const dateSpy = sinon.spy(Date, 'now')
sessionUtils.generateSessionID()
expect(dateSpy).to.have.been.called()
dateSpy.restore()
})
it('should return an object with the generated id', function () {
const result = sessionUtils.generateSessionID()
expect(result).to.be.an('object')
expect(result).to.have.property('id')
expect(result.id).to.be.a('string')
})
it('should store the stringified generated id and Date object in session storage', function () {
const result = JSON.stringify(sessionUtils.generateSessionID())
const stored = sessionStorage.getItem('session_id')
expect(result).to.equal(stored)
})
})
})