2020-12-13 13:37:11 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) Sapphirecode - All Rights Reserved
|
|
|
|
* This file is part of Auth-Server-Helper which is released under MIT.
|
|
|
|
* See file 'LICENSE' for full license details.
|
|
|
|
* Created by Timo Hocker <timo@scode.ovh>, December 2020
|
|
|
|
*/
|
|
|
|
|
2020-12-06 21:06:40 +01:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
2020-12-06 21:29:11 +01:00
|
|
|
it ('should reject key generation of expired keys', () => {
|
|
|
|
const iat = ((new Date)
|
|
|
|
.getTime () / 1000) - 10;
|
|
|
|
const duration = 5;
|
|
|
|
expect (() => ks.get_key (iat, duration))
|
|
|
|
.toThrowError ('cannot create already expired keys');
|
|
|
|
});
|
|
|
|
|
2020-12-06 21:06:40 +01:00
|
|
|
afterAll (() => {
|
2020-12-28 14:53:14 +01:00
|
|
|
jasmine.clock ()
|
|
|
|
.tick (24 * 60 * 60 * 1000);
|
2020-12-06 21:06:40 +01:00
|
|
|
jasmine.clock ()
|
|
|
|
.uninstall ();
|
|
|
|
});
|
|
|
|
});
|