/* * 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 , December 2020 */ import ks from '../../lib/KeyStore'; const frame = 60; /* 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 = 10 * frame; 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 + (frame / 2)); expect (key) .toEqual (keys[0].key); }); it ('should generate a new key after time frame is over', () => { jasmine.clock () .tick (frame * 1000); const iat = (new Date) .getTime () / 1000; const duration = 10 * frame; 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 + frame)) .toThrowError ('key could not be found'); }); it ('should delete a key after it expires', () => { jasmine.clock () .tick (10000 * frame); 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); }); it ('should reject key generation of expired keys', () => { const iat = ((new Date) .getTime () / 1000) - 2; const duration = 5; expect (() => ks.get_key (iat, duration)) .toThrowError ('cannot create already expired keys'); }); it ('key should live as long as the longest created token', () => { const base = new Date; base.setSeconds (2, 0); jasmine.clock () .mockDate (base); jasmine.clock () .tick (24 * 60 * 60 * 1000); const iat = (new Date) .getTime () / 1000; const duration1 = frame; const duration2 = frame * 10; const key1 = ks.get_key (iat, duration1); const step = 0.9 * frame; jasmine.clock () .tick (step * 1000); const key2 = ks.get_key (iat + step, duration2); expect (key1) .toEqual (key2); jasmine.clock () .tick (5000 * frame); const keyv = ks.get_key (iat + step); expect (keyv) .toEqual (key1); }); // required use case: insert keys for verification of old tokens afterAll (() => { jasmine.clock () .tick (24 * 60 * 60 * 1000); jasmine.clock () .uninstall (); }); });