/* * 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 blacklist, { Blacklist } from '../../lib/Blacklist'; import { clock_finalize, clock_setup } from '../Helper'; // eslint-disable-next-line max-lines-per-function describe ('blacklist', () => { beforeAll (() => { clock_setup (); }); afterAll (() => { clock_finalize (); }); it ('should validate any string', async () => { expect (await blacklist.is_valid ('foo')) .toBeTrue (); expect (await blacklist.is_valid ('bar')) .toBeTrue (); expect (await blacklist.is_valid ('baz')) .toBeTrue (); }); it ('should blacklist strings', async () => { await blacklist.add_signature ('foo'); await blacklist.add_signature ('bar'); expect (await blacklist.is_valid ('foo')) .toBeFalse (); expect (await blacklist.is_valid ('bar')) .toBeFalse (); expect (await blacklist.is_valid ('baz')) .toBeTrue (); }); it ('should remove one string', async () => { await blacklist.remove_signature ('foo'); expect (await blacklist.is_valid ('foo')) .toBeTrue (); expect (await blacklist.is_valid ('bar')) .toBeFalse (); expect (await blacklist.is_valid ('baz')) .toBeTrue (); }); it ('should clear after time', async () => { jasmine.clock () .tick (5000); await blacklist.add_signature ('baz'); await blacklist.clear (Date.now () - 100); expect (await blacklist.is_valid ('foo')) .toBeTrue (); expect (await blacklist.is_valid ('bar')) .toBeTrue (); expect (await blacklist.is_valid ('baz')) .toBeFalse (); }); 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')) .toBeFalse (); expect (await blacklist.is_valid ('bar')) .toBeFalse (); expect (await blacklist.is_valid ('baz')) .toBeFalse (); await blacklist.clear (); expect (await blacklist.is_valid ('foo')) .toBeTrue (); expect (await blacklist.is_valid ('bar')) .toBeTrue (); expect (await blacklist.is_valid ('baz')) .toBeTrue (); }); it ('should export and import data', async () => { await blacklist.add_signature ('baz'); 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); }); });