-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
74 lines (57 loc) · 1.62 KB
/
test.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
const browser = require('./lib/browser');
const node = require('./lib/node');
it('should export an object', () => {
expect(typeof browser).toBe('object');
});
it('should allow the localStorage key to be customized', () => {
const sampleToken = '12345';
// Save to the default key
browser.save(sampleToken);
// Change the key
browser.configure({
localStorageKey: 'test-key',
});
// No token exists for the new key
expect(browser.get()).toBe(null);
// Save under the new key and verify
browser.save(sampleToken);
expect(browser.get()).toBe(sampleToken);
// Cleanup
browser.delete();
});
it('should save, get, and delete tokens', () => {
const sampleToken = '12345';
// Should not exist yet
expect(browser.get()).toBe(null);
// Save it and verify it's there
browser.save(sampleToken);
expect(browser.get()).toBe(sampleToken);
// Delete it and verify it's gone
browser.delete();
expect(browser.get()).toBe(null);
});
it('should verify if a token has expired', (done) => {
// Configure with a 1-second expiration
node.configure({
secret: 'test',
expiresIn: 1,
});
// Create a new token
node.jwt.sign({
username: 'test_username',
}).then((token) => {
// Save it to localStorage
browser.save(token);
// Make sure it hasn't expired yet
expect(browser.isExpired()).toBe(false);
expect(browser.isExpired(token)).toBe(false);
setTimeout(() => {
// It should now be expired
expect(browser.isExpired()).toBe(true);
expect(browser.isExpired(token)).toBe(true);
// Cleanup
browser.delete();
done();
}, 1000);
});
});