96 lines
2.8 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';
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', () => {
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 ('foo'))
.toBeTrue ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('bar'))
.toBeTrue ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('baz'))
.toBeTrue ();
});
2022-08-27 16:39:07 +02:00
it ('should blacklist strings', async () => {
await blacklist.add_signature ('foo');
await blacklist.add_signature ('bar');
expect (await blacklist.is_valid ('foo'))
.toBeFalse ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('bar'))
.toBeFalse ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('baz'))
.toBeTrue ();
});
2022-08-27 16:39:07 +02:00
it ('should remove one string', async () => {
await blacklist.remove_signature ('foo');
expect (await blacklist.is_valid ('foo'))
.toBeTrue ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('bar'))
.toBeFalse ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('baz'))
.toBeTrue ();
});
2022-08-27 16:39:07 +02:00
it ('should clear after time', async () => {
jasmine.clock ()
.tick (5000);
2022-08-27 16:39:07 +02:00
await blacklist.add_signature ('baz');
await blacklist.clear (Date.now () - 100);
expect (await blacklist.is_valid ('foo'))
.toBeTrue ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('bar'))
.toBeTrue ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('baz'))
.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 ('foo');
await blacklist.add_signature ('bar');
await blacklist.add_signature ('baz');
expect (await blacklist.is_valid ('foo'))
2021-01-15 14:45:05 +01:00
.toBeFalse ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('bar'))
2021-01-15 14:45:05 +01:00
.toBeFalse ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('baz'))
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 ('foo'))
2021-01-15 14:45:05 +01:00
.toBeTrue ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('bar'))
2021-01-15 14:45:05 +01:00
.toBeTrue ();
2022-08-27 16:39:07 +02:00
expect (await blacklist.is_valid ('baz'))
2021-01-15 14:45:05 +01:00
.toBeTrue ();
});
2022-08-27 16:39:07 +02:00
it ('should export and import data', async () => {
await blacklist.add_signature ('baz');
2021-01-09 12:20:14 +01:00
const exp = blacklist.export_blacklist ();
// eslint-disable-next-line dot-notation
expect (blacklist['_signatures'])
.toEqual (exp);
const bl2 = (new Blacklist);
bl2.import_blacklist (exp);
// eslint-disable-next-line dot-notation
expect (bl2['_signatures'])
.toEqual (exp);
});
});