This commit is contained in:
86
test/spec/KeyStore.ts
Normal file
86
test/spec/KeyStore.ts
Normal file
@ -0,0 +1,86 @@
|
||||
import ks from '../../lib/KeyStore';
|
||||
|
||||
/* eslint-disable-next-line max-lines-per-function */
|
||||
describe ('key store', () => {
|
||||
beforeAll (() => {
|
||||
jasmine.clock ()
|
||||
.install ();
|
||||
const base_date = (new Date);
|
||||
base_date.setSeconds (2);
|
||||
jasmine.clock ()
|
||||
.mockDate (base_date);
|
||||
});
|
||||
|
||||
const keys: {key:string, iat:number}[] = [];
|
||||
|
||||
it ('should generate a new key', () => {
|
||||
const iat = (new Date)
|
||||
.getTime () / 1000;
|
||||
const duration = 600;
|
||||
const key = ks.get_key (iat, duration);
|
||||
expect (typeof key)
|
||||
.toEqual ('string');
|
||||
expect (key.length)
|
||||
.toEqual (64);
|
||||
keys.push ({ iat, key });
|
||||
});
|
||||
|
||||
it ('should return the generated key', () => {
|
||||
const key = ks.get_key (keys[0].iat);
|
||||
expect (key)
|
||||
.toEqual (keys[0].key);
|
||||
});
|
||||
|
||||
it ('should return the same key on a different time', () => {
|
||||
const key = ks.get_key (keys[0].iat + 30);
|
||||
expect (key)
|
||||
.toEqual (keys[0].key);
|
||||
});
|
||||
|
||||
it ('should generate a new key after 60 seconds', () => {
|
||||
jasmine.clock ()
|
||||
.tick (60000);
|
||||
const iat = (new Date)
|
||||
.getTime () / 1000;
|
||||
const duration = 600;
|
||||
const key = ks.get_key (iat, duration);
|
||||
expect (typeof key)
|
||||
.toEqual ('string');
|
||||
expect (key.length)
|
||||
.toEqual (64);
|
||||
expect (key).not.toEqual (keys[0].key);
|
||||
keys.push ({ iat, key });
|
||||
});
|
||||
|
||||
it ('should return both keys', () => {
|
||||
const key = ks.get_key (keys[0].iat);
|
||||
expect (key)
|
||||
.toEqual (keys[0].key);
|
||||
const k2 = ks.get_key (keys[1].iat);
|
||||
expect (k2)
|
||||
.toEqual (keys[1].key);
|
||||
});
|
||||
|
||||
it ('should throw on non existing key', () => {
|
||||
expect (() => ks.get_key (keys[1].iat + 60))
|
||||
.toThrowError ('key could not be found');
|
||||
});
|
||||
|
||||
it ('should delete a key after it expires', () => {
|
||||
jasmine.clock ()
|
||||
.tick (600000);
|
||||
expect (() => ks.get_key (keys[0].iat))
|
||||
.toThrowError ('key could not be found');
|
||||
});
|
||||
|
||||
it ('should still retrieve the second key', () => {
|
||||
const key = ks.get_key (keys[1].iat);
|
||||
expect (key)
|
||||
.toEqual (keys[1].key);
|
||||
});
|
||||
|
||||
afterAll (() => {
|
||||
jasmine.clock ()
|
||||
.uninstall ();
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user