Skip to content

Commit 99d9499

Browse files
committed
add test for changing a falsy value
1 parent 110ee96 commit 99d9499

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

test/test-db.js

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
"use strict";
2-
31
const expect = require("chai").expect;
42

5-
const Promise = require("bluebird");
63
const mongoose = require("mongoose");
74
const { setupMongoose } = require("./setup");
8-
mongoose.Promise = Promise;
95
mongoose.set("bufferCommands", false);
106

117
const fieldEncryptionPlugin = require("../lib/mongoose-field-encryption").fieldEncryption;
@@ -19,10 +15,8 @@ describe("mongoose-field-encryption plugin db", function () {
1915
mongoose
2016
.connect(uri, {
2117
useNewUrlParser: true,
22-
promiseLibrary: Promise,
2318
autoIndex: false,
2419
useUnifiedTopology: true,
25-
keepAlive: 1,
2620
})
2721
.then(function () {
2822
done();
@@ -643,26 +637,26 @@ describe("mongoose-field-encryption plugin db", function () {
643637
// console.log(sut);
644638

645639
expect(sut.__enc_toEncryptString).to.be.true;
646-
expect(sut.toEncryptString).to.equal('37373539656562373263336135633161:9af345f139de3d5397f513a2ab105607');
647-
640+
expect(sut.toEncryptString).to.equal("37373539656562373263336135633161:9af345f139de3d5397f513a2ab105607");
641+
648642
expect(sut.toEncryptArray).to.be.undefined;
649643
expect(sut.__enc_toEncryptArray).to.be.true;
650644
expect(sut.__enc_toEncryptArray_d).to.equal(
651645
"37373539656562373263336135633161:b897c78694f3ad8533e246e21386e5d4"
652646
);
653-
647+
654648
expect(sut.toEncryptDate).to.be.undefined;
655649
expect(sut.__enc_toEncryptDate).to.be.true;
656650
expect(sut.__enc_toEncryptDate_d).to.equal(
657651
"37373539656562373263336135633161:da4568a1046a687ecdf7dc65793d44725d67efcefa88508339750a074e485f25"
658652
);
659-
653+
660654
expect(sut.toEncryptNumber).to.be.undefined;
661655
expect(sut.__enc_toEncryptNumber).to.be.true;
662656
expect(sut.__enc_toEncryptNumber_d).to.equal(
663657
"37373539656562373263336135633161:512c4a442a26f20f8ef81577d2fded37"
664658
);
665-
659+
666660
expect(sut.toEncryptBoolean).to.be.undefined;
667661
expect(sut.__enc_toEncryptBoolean).to.be.true;
668662
expect(sut.__enc_toEncryptBoolean_d).to.equal(
@@ -682,14 +676,50 @@ describe("mongoose-field-encryption plugin db", function () {
682676

683677
expect(found.toEncryptDate).to.deep.equal(new Date(0));
684678
expect(found.__enc_toEncryptDate).to.be.false;
685-
679+
686680
expect(found.toEncryptNumber).to.equal(0);
687681
expect(found.__enc_toEncryptDate).to.be.false;
688-
682+
689683
expect(found.toEncryptBoolean).to.equal(false);
690684
expect(found.__enc_toEncryptDate).to.be.false;
691-
692685
});
693686
});
687+
688+
it("should encrypt and decrypt empty string and record a change", async function () {
689+
// given
690+
const FalsyRecordSchema = {
691+
toEncryptString: { type: String, required: false, default: "" },
692+
};
693+
694+
const FalsyRecordEncryptionSchema = new mongoose.Schema(FalsyRecordSchema);
695+
FalsyRecordEncryptionSchema.plugin(fieldEncryptionPlugin, {
696+
fields: ["toEncryptString"],
697+
secret: "icanhazcheezburger",
698+
saltGenerator: (secret) => secret.slice(0, 16),
699+
});
700+
701+
const FalsyRecordEncryptionModel = mongoose.model("FalsyRecordEncryptionModel", FalsyRecordEncryptionSchema);
702+
const sut = new FalsyRecordEncryptionModel({
703+
toEncryptString: "",
704+
});
705+
706+
// when
707+
await sut.save();
708+
709+
// then
710+
expect(sut.__enc_toEncryptString).to.be.true;
711+
expect(sut.toEncryptString).to.equal("37373539656562373263336135633161:9af345f139de3d5397f513a2ab105607");
712+
713+
const found = await FalsyRecordEncryptionModel.findOne({ _id: sut._id });
714+
715+
expect(found.toEncryptString).to.equal("");
716+
expect(found.__enc_toEncryptString).to.be.false;
717+
718+
found.toEncryptString = "value";
719+
await found.save();
720+
721+
expect(found.toEncryptString).to.equal("37373539656562373263336135633161:8335a0fb01aab80cfea6a61653a329aa");
722+
expect(found.__enc_toEncryptString).to.be.true;
723+
});
694724
});
695725
});

0 commit comments

Comments
 (0)