From: Justin Wind Date: Sat, 4 Mar 2023 19:27:12 +0000 (-0800) Subject: cleanup tests X-Git-Tag: v1.2.1~1 X-Git-Url: http://git.squeep.com/?p=squeep-mystery-box;a=commitdiff_plain;h=ab327ba8b0fa3089187a971e15682165f7700c9e cleanup tests --- diff --git a/test/lib/mystery-box.js b/test/lib/mystery-box.js index a48bad8..2f2b734 100644 --- a/test/lib/mystery-box.js +++ b/test/lib/mystery-box.js @@ -8,7 +8,6 @@ const MysteryBox = require('../../lib/mystery-box'); const stubLogger = require('../stub-logger'); describe('MysteryBox', function () { - const noExpectedException = 'did not get expected exception'; let mb, options, object; beforeEach(function () { options = { @@ -22,12 +21,7 @@ describe('MysteryBox', function () { describe('constructor', function () { it('needs a secret', async function () { options = {}; - try { - mb = new MysteryBox(stubLogger, options); - assert.fail(noExpectedException); - } catch (e) { - assert.strictEqual(e.message, 'missing encryption secret', noExpectedException); - } + assert.rejects(() => new MysteryBox(stubLogger, options)); }); it('accepts multiple secrets', async function () { @@ -47,42 +41,23 @@ describe('MysteryBox', function () { }); it('covers options', function () { - try { - mb = new MysteryBox(stubLogger); - assert.fail(noExpectedException); - } catch (e) { - assert.strictEqual(e.message, 'missing encryption secret', noExpectedException); - } + assert.rejects(() => new MysteryBox(stubLogger)); }); it('covers bad flags', function () { options.defaultFlags = 300; - try { - mb = new MysteryBox(stubLogger, options); - assert.fail(noExpectedException); - } catch (e) { - assert(e instanceof RangeError, noExpectedException); - } + assert.rejects(() => new MysteryBox(stubLogger, options), RangeError); }); it('covers missing ciphers', function () { sinon.stub(MysteryBox._test.crypto, 'getCiphers').returns(['rot13']); - try { - mb = new MysteryBox(stubLogger, options); - assert.fail(noExpectedException); - } catch (e) { - assert.strictEqual(e.message, 'no supported versions available', noExpectedException); - } + assert.rejects(() => new MysteryBox(stubLogger, options)); }); }); // constructor describe('_keyFromSecret', function () { it('covers invalid', async function () { - try { - await MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32); - } catch (e) { - assert(e instanceof RangeError); - } + assert.rejects(() => MysteryBox._keyFromSecret('unknown deriver', 'secret', 'salt', 32), RangeError); }); }); // _keyFromSecret @@ -92,24 +67,14 @@ describe('MysteryBox', function () { }); it('covers packing unsupported version', async function () { - try { - await mb.pack({}, 0); - assert.fail(noExpectedException); - } catch (e) { - assert(e instanceof RangeError, noExpectedException); - } + assert.rejects(() => mb.pack({}, 0), RangeError); }); it('covers unpacking unsupported version', async function () { const badBuffer = Buffer.alloc(128); badBuffer.writeUInt8(0, 0); // No such thing as version 0 const badPayload = badBuffer.toString('base64'); - try { - await mb.unpack(badPayload); - assert.fail(noExpectedException); - } catch (e) { - assert(e instanceof RangeError, noExpectedException); - } + assert.rejects(() => mb.unpack(badPayload), RangeError); }); it('encrypts and decrypts default version', async function () { @@ -156,12 +121,7 @@ describe('MysteryBox', function () { flarp: 13, }; const oldEncrypted = await oldmb.pack(object); - try { - await newmb.unpack(oldEncrypted); - assert.fail(noExpectedException); - } catch (e) { - assert(e instanceof Error); - } + assert.rejects(() => newmb.unpack(oldEncrypted)); }); it('encrypts and decrypts all available versions +brotli', async function () { @@ -221,23 +181,13 @@ describe('MysteryBox', function () { }); it('handles undefined', async function () { - try { - await mb.unpack(); - assert.fail(noExpectedException); - } catch (e) { - assert(e instanceof RangeError, noExpectedException); - } + assert.rejects(() => mb.unpack(), RangeError); }); it('handles incomplete', async function () { this.slow(500); const encryptedResult = await mb.pack({ foo: 'bar' }); - try { - await mb.unpack(encryptedResult.slice(0, 6)); - assert.fail(noExpectedException); - } catch (e) { - assert(e instanceof RangeError, noExpectedException); - } + assert.rejects(() => mb.unpack(encryptedResult.slice(0, 6)), RangeError); }); }); // pack, unpack