const stubLogger = require('../stub-logger');
describe('MysteryBox', function () {
- const noExpectedException = 'did not get expected exception';
let mb, options, object;
beforeEach(function () {
options = {
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 () {
});
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
});
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 () {
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 () {
});
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