@@ -78,6 +78,18 @@ describe('encrypted attributes', function () {
7878 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)
7979 } )
8080
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+
8193 it ( 'should encrypt without id when verify id option is false' , function ( ) {
8294 let enc = EncryptedAttributes ( [ 'secret' ] , Object . assign ( this . options , { verifyId : false } ) )
8395 let obj = { }
@@ -142,7 +154,15 @@ describe('encrypted attributes', function () {
142154 let obj = { id : 1 }
143155 // aad: aes-256-gcm$02$k1
144156 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)
146166 } )
147167
148168 it ( 'should throw when decrypting with invalid key id' , function ( ) {
@@ -176,6 +196,20 @@ describe('encrypted attributes', function () {
176196 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)
177197 } )
178198
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+
179213 it ( 'should decrypt without id when verify id option is false' , function ( ) {
180214 let enc = EncryptedAttributes ( [ 'secret' ] , Object . assign ( this . options , { verifyId : false } ) )
181215 let obj = { }
0 commit comments