112 lines
3.4 KiB
TypeScript
Raw Normal View History

/*
* 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
*/
2021-01-09 12:20:14 +01:00
import blacklist, { Blacklist } from '../../lib/Blacklist';
import { generate_token_id, parse_token_id } from '../../lib/token_id';
2021-01-07 15:43:54 +01:00
import { clock_finalize, clock_setup } from '../Helper';
// eslint-disable-next-line max-lines-per-function
describe ('blacklist', () => {
const token1 = generate_token_id (new Date (Date.now () + 3600000));
const token2 = generate_token_id (new Date (Date.now () + 3600000));
const token3 = generate_token_id (new Date (Date.now () + 3600000));
beforeAll (() => {
2021-01-07 15:43:54 +01:00
clock_setup ();
});
2021-01-06 22:43:03 +01:00
afterAll (() => {
2021-01-07 15:43:54 +01:00
clock_finalize ();
2021-01-06 22:43:03 +01:00
});
2022-08-27 16:39:07 +02:00
it ('should validate any string', async () => {
expect (await blacklist.is_valid (token1))
.toBeTrue ();
expect (await blacklist.is_valid (token2))
.toBeTrue ();
expect (await blacklist.is_valid (token3))
.toBeTrue ();
});
2022-08-27 16:39:07 +02:00
it ('should blacklist strings', async () => {
await blacklist.add_signature (token1);
await blacklist.add_signature (token2);
expect (await blacklist.is_valid (token1))
.toBeFalse ();
expect (await blacklist.is_valid (token2))
.toBeFalse ();
expect (await blacklist.is_valid (token3))
.toBeTrue ();
});
2022-08-27 16:39:07 +02:00
it ('should remove one string', async () => {
await blacklist.remove_signature (token1);
expect (await blacklist.is_valid (token1))
.toBeTrue ();
expect (await blacklist.is_valid (token2))
.toBeFalse ();
expect (await blacklist.is_valid (token3))
.toBeTrue ();
});
2022-08-27 16:39:07 +02:00
it ('should clear after time', async () => {
jasmine.clock ()
.tick (5000);
await blacklist.add_signature (token3);
2022-08-27 16:39:07 +02:00
await blacklist.clear (Date.now () - 100);
expect (await blacklist.is_valid (token1))
.toBeTrue ();
expect (await blacklist.is_valid (token2))
.toBeTrue ();
expect (await blacklist.is_valid (token3))
.toBeFalse ();
});
2021-01-09 12:20:14 +01:00
2022-08-27 16:39:07 +02:00
it ('should clear all', async () => {
await blacklist.add_signature (token1);
await blacklist.add_signature (token2);
await blacklist.add_signature (token3);
expect (await blacklist.is_valid (token1))
2021-01-15 14:45:05 +01:00
.toBeFalse ();
expect (await blacklist.is_valid (token2))
2021-01-15 14:45:05 +01:00
.toBeFalse ();
expect (await blacklist.is_valid (token3))
2021-01-15 14:45:05 +01:00
.toBeFalse ();
2022-08-27 16:39:07 +02:00
await blacklist.clear ();
expect (await blacklist.is_valid (token1))
2021-01-15 14:45:05 +01:00
.toBeTrue ();
expect (await blacklist.is_valid (token2))
2021-01-15 14:45:05 +01:00
.toBeTrue ();
expect (await blacklist.is_valid (token3))
2021-01-15 14:45:05 +01:00
.toBeTrue ();
});
2022-08-27 16:39:07 +02:00
it ('should export and import data', async () => {
const time = new Date;
const token = generate_token_id (time);
await blacklist.add_signature (token);
2021-01-09 12:20:14 +01:00
// eslint-disable-next-line dot-notation
expect (blacklist['_signatures'])
.toEqual ([
{
token_id: token,
iat: time.getTime (),
valid_until: time
}
]);
const exp = blacklist.export_blacklist ();
expect (exp)
.toEqual ([ { token_id: token, iat: time.getTime () } ]);
2021-01-09 12:20:14 +01:00
const bl2 = (new Blacklist);
bl2.import_blacklist (exp);
// eslint-disable-next-line dot-notation
expect (bl2['_signatures'])
// eslint-disable-next-line dot-notation
.toEqual (blacklist['_signatures']);
2021-01-09 12:20:14 +01:00
});
});