@@ -78,6 +78,18 @@ describe('encrypted attributes', function () {
78
78
expect ( ( ) => enc . encryptAttribute ( obj , 'value' ) , 'to throw' , / c a n n o t e n c r y p t w i t h o u t ' i d ' / i)
79
79
} )
80
80
81
+ it ( 'should throw when encrypting without id (custom field)' , function ( ) {
82
+ let enc = EncryptedAttributes ( [ 'secret' ] , Object . assign ( this . options , { verifyId : '_id' } ) )
83
+ let obj = { }
84
+ expect ( ( ) => enc . encryptAttribute ( obj , 'value' ) , 'to throw' , / c a n n o t e n c r y p t w i t h o u t ' _ i d ' / i)
85
+ } )
86
+
87
+ it ( 'should encrypt with custom id field name' , function ( ) {
88
+ let enc = EncryptedAttributes ( [ 'secret' ] , Object . assign ( this . options , { verifyId : '_id' } ) )
89
+ let obj = { _id : 1 }
90
+ expect ( ( ) => enc . encryptAttribute ( obj , 'value' ) , 'not to throw' )
91
+ } )
92
+
81
93
it ( 'should encrypt without id when verify id option is false' , function ( ) {
82
94
let enc = EncryptedAttributes ( [ 'secret' ] , Object . assign ( this . options , { verifyId : false } ) )
83
95
let obj = { }
@@ -142,7 +154,15 @@ describe('encrypted attributes', function () {
142
154
let obj = { id : 1 }
143
155
// aad: aes-256-gcm$02$k1
144
156
let invalidId = 'YWVzLTI1Ni1nY20kMiRrMQ==$sK91YfUvv+O8Jx/m$OOQniq8=$WLbWYz7uCQBTNO3Fc+5UvA'
145
- expect ( ( ) => enc . decryptAttribute ( obj , invalidId ) , 'to throw' , / i n v a l i d i d / i)
157
+ expect ( ( ) => enc . decryptAttribute ( obj , invalidId ) , 'to throw' , / i n v a l i d ' i d ' / i)
158
+ } )
159
+
160
+ it ( 'should throw when decrypting with invalid id (custom field)' , function ( ) {
161
+ let enc = EncryptedAttributes ( [ 'secret' ] , Object . assign ( this . options , { verifyId : '_id' } ) )
162
+ let obj = { _id : 1 }
163
+ // aad: aes-256-gcm$02$k1
164
+ let invalidId = 'YWVzLTI1Ni1nY20kMiRrMQ==$sK91YfUvv+O8Jx/m$OOQniq8=$WLbWYz7uCQBTNO3Fc+5UvA'
165
+ expect ( ( ) => enc . decryptAttribute ( obj , invalidId ) , 'to throw' , / i n v a l i d ' _ i d ' / i)
146
166
} )
147
167
148
168
it ( 'should throw when decrypting with invalid key id' , function ( ) {
@@ -176,6 +196,20 @@ describe('encrypted attributes', function () {
176
196
expect ( ( ) => enc . decryptAttribute ( obj , encrypted ) , 'to throw' , / c a n n o t d e c r y p t w i t h o u t ' i d ' / i)
177
197
} )
178
198
199
+ it ( 'should throw when decrypting without id (custom field)' , function ( ) {
200
+ let enc = EncryptedAttributes ( [ 'secret' ] , Object . assign ( this . options , { verifyId : '_id' } ) )
201
+ let obj = { }
202
+ let encrypted = 'YWVzLTI1Ni1nY20kMSRrMQ==$sK91YfUvv+O8Jx/m$OOQniq8=$WLbWYz7uCQBTNO3Fc+5UvA'
203
+ expect ( ( ) => enc . decryptAttribute ( obj , encrypted ) , 'to throw' , / c a n n o t d e c r y p t w i t h o u t ' _ i d ' / i)
204
+ } )
205
+
206
+ it ( 'should decrypt with custom id field name' , function ( ) {
207
+ let enc = EncryptedAttributes ( [ 'secret' ] , Object . assign ( this . options , { verifyId : '_id' } ) )
208
+ let obj = { _id : 1 }
209
+ let encrypted = 'YWVzLTI1Ni1nY20kMSRrMQ==$sK91YfUvv+O8Jx/m$OOQniq8=$WLbWYz7uCQBTNO3Fc+5UvA'
210
+ expect ( ( ) => enc . decryptAttribute ( obj , encrypted ) , 'not to throw' )
211
+ } )
212
+
179
213
it ( 'should decrypt without id when verify id option is false' , function ( ) {
180
214
let enc = EncryptedAttributes ( [ 'secret' ] , Object . assign ( this . options , { verifyId : false } ) )
181
215
let obj = { }
0 commit comments