key store
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-12-06 21:06:40 +01:00
parent 008fd3f545
commit 0be180f632
7 changed files with 162 additions and 9 deletions

22
test/.eslintrc.js Normal file
View File

@ -0,0 +1,22 @@
/*
* 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
*/
/* eslint-disable */
module.exports = {
env: {
commonjs: true,
es6: true,
node: true
},
extends: [ '@sapphirecode/eslint-config-ts' ],
globals: {
Atomics: 'readonly',
SharedArrayBuffer: 'readonly'
},
parserOptions: { ecmaVersion: 2018 }
};

86
test/spec/KeyStore.ts Normal file
View 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 ();
});
});