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
|
|
|
|
*/
|
|
|
|
|
2021-01-09 12:20:14 +01:00
|
|
|
import blacklist, { Blacklist } from '../../lib/Blacklist';
|
2022-09-09 15:53:38 +02:00
|
|
|
import { generate_token_id } from '../../lib/token_id';
|
2021-01-07 15:43:54 +01:00
|
|
|
import { clock_finalize, clock_setup } from '../Helper';
|
2020-12-13 13:37:11 +01:00
|
|
|
|
|
|
|
// eslint-disable-next-line max-lines-per-function
|
|
|
|
describe ('blacklist', () => {
|
2022-09-09 15:49:53 +02:00
|
|
|
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));
|
|
|
|
|
2020-12-13 13:37:11 +01:00
|
|
|
beforeAll (() => {
|
2021-01-07 15:43:54 +01:00
|
|
|
clock_setup ();
|
2020-12-13 13:37:11 +01:00
|
|
|
});
|
|
|
|
|
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 () => {
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token1))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeTrue ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token2))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeTrue ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token3))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeTrue ();
|
|
|
|
});
|
|
|
|
|
2022-08-27 16:39:07 +02:00
|
|
|
it ('should blacklist strings', async () => {
|
2022-09-09 15:49:53 +02:00
|
|
|
await blacklist.add_signature (token1);
|
|
|
|
await blacklist.add_signature (token2);
|
|
|
|
expect (await blacklist.is_valid (token1))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeFalse ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token2))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeFalse ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token3))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeTrue ();
|
|
|
|
});
|
|
|
|
|
2022-08-27 16:39:07 +02:00
|
|
|
it ('should remove one string', async () => {
|
2022-09-09 15:49:53 +02:00
|
|
|
await blacklist.remove_signature (token1);
|
|
|
|
expect (await blacklist.is_valid (token1))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeTrue ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token2))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeFalse ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token3))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeTrue ();
|
|
|
|
});
|
|
|
|
|
2022-08-27 16:39:07 +02:00
|
|
|
it ('should clear after time', async () => {
|
2020-12-13 13:37:11 +01:00
|
|
|
jasmine.clock ()
|
|
|
|
.tick (5000);
|
2022-09-09 15:49:53 +02:00
|
|
|
await blacklist.add_signature (token3);
|
2022-08-27 16:39:07 +02:00
|
|
|
await blacklist.clear (Date.now () - 100);
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token1))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeTrue ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token2))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeTrue ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token3))
|
2020-12-13 13:37:11 +01:00
|
|
|
.toBeFalse ();
|
|
|
|
});
|
2021-01-09 12:20:14 +01:00
|
|
|
|
2022-08-27 16:39:07 +02:00
|
|
|
it ('should clear all', async () => {
|
2022-09-09 15:49:53 +02:00
|
|
|
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 ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token2))
|
2021-01-15 14:45:05 +01:00
|
|
|
.toBeFalse ();
|
2022-09-09 15:49:53 +02:00
|
|
|
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 ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token1))
|
2021-01-15 14:45:05 +01:00
|
|
|
.toBeTrue ();
|
2022-09-09 15:49:53 +02:00
|
|
|
expect (await blacklist.is_valid (token2))
|
2021-01-15 14:45:05 +01:00
|
|
|
.toBeTrue ();
|
2022-09-09 15:49:53 +02:00
|
|
|
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 () => {
|
2022-09-09 15:49:53 +02:00
|
|
|
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'])
|
2022-09-09 15:49:53 +02:00
|
|
|
.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'])
|
2022-09-09 15:49:53 +02:00
|
|
|
// eslint-disable-next-line dot-notation
|
|
|
|
.toEqual (blacklist['_signatures']);
|
2021-01-09 12:20:14 +01:00
|
|
|
});
|
2020-12-13 13:37:11 +01:00
|
|
|
});
|