/* * 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 { generate_token_id } from '../../lib/token_id'; 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 (() => { clock_setup (); }); afterAll (() => { clock_finalize (); }); 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 (); }); 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 (); }); 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 (); }); it ('should clear after time', async () => { jasmine.clock () .tick (5000); await blacklist.add_signature (token3); 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 (); }); 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)) .toBeFalse (); expect (await blacklist.is_valid (token2)) .toBeFalse (); expect (await blacklist.is_valid (token3)) .toBeFalse (); await blacklist.clear (); expect (await blacklist.is_valid (token1)) .toBeTrue (); expect (await blacklist.is_valid (token2)) .toBeTrue (); expect (await blacklist.is_valid (token3)) .toBeTrue (); }); it ('should export and import data', async () => { const time = new Date; const token = generate_token_id (time); await blacklist.add_signature (token); // 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 () } ]); 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']); }); });