Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test for the issue that crashed when some fields were not retrieved by default #27

Merged
merged 2 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/mongoose-field-encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,13 @@ const fieldEncryption = function(schema, options) {
obj[encryptedFieldName] = false;
obj[encryptedFieldData] = "";
} else {
// If the field has been marked to not be retrieved, it'll be undefined
if (obj[field]) {
// handle strings separately to maintain searchability
const encryptedValue = obj[field];

obj[field] = decrypt(encryptedValue, secret);
obj[encryptedFieldName] = false;
const encryptedValue = obj[field];
obj[field] = decrypt(encryptedValue, secret);
obj[encryptedFieldName] = false;
}
}
}
}
Expand Down
36 changes: 35 additions & 1 deletion test/test-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe("mongoose-field-encryption plugin db", function() {

let NestedFieldEncryptionSchema = new mongoose.Schema({
toEncryptString: { type: String, required: true },
toEncryptStringNotRetrieved: { type: String, select: false },
toEncryptObject: {
nested: String
},
Expand All @@ -24,7 +25,7 @@ describe("mongoose-field-encryption plugin db", function() {
});

NestedFieldEncryptionSchema.plugin(fieldEncryptionPlugin, {
fields: ["toEncryptString", "toEncryptObject", "toEncryptArray", "toEncryptDate"],
fields: ["toEncryptString", "toEncryptObject", "toEncryptArray", "toEncryptDate", "toEncryptStringNotRetrieved"],
secret: "icanhazcheezburger" // should ideally be process.env.SECRET
});

Expand Down Expand Up @@ -290,6 +291,7 @@ describe("mongoose-field-encryption plugin db", function() {
_id: sut._id
},
{
toEncryptString: "yaddayadda",
toEncryptObject: { nested: "snoop" }
}
);
Expand All @@ -299,6 +301,7 @@ describe("mongoose-field-encryption plugin db", function() {
})
.then(foundArray => {
const found = foundArray[0];
expect(found.toEncryptString).to.eql("yaddayadda");
expect(found.toEncryptObject.nested).to.eql("snoop");
});
});
Expand Down Expand Up @@ -332,4 +335,35 @@ describe("mongoose-field-encryption plugin db", function() {
expect(found.toEncryptString).to.eql("already encrypted string");
});
})

it("should decrypt data on find() method call even if some fields are marked as not selectables", () => {
// given
let sut = getSut();

// when
return sut
.save()
.then(() => {
expectEncryptionValues(sut);

return NestedFieldEncryption.findOneAndUpdate(
{
_id: sut._id
},
{
toEncryptString: "yaddayadda",
toEncryptObject: { nested: "snoop" },
toEncryptStringNotRetrieved: "dubidubida"
}
);
})
.then(() => {
return NestedFieldEncryption.find({ _id: sut._id });
})
.then(foundArray => {
const found = foundArray[0];
expect(found.toEncryptString).to.equal("yaddayadda");
expect(found.toEncryptStringNotRetrieved).to.be.undefined;
});
});
});