@@ -797,6 +797,133 @@ describe('encryption integration tests', () => {
797797 } ) ;
798798 } ) ;
799799
800+ describe ( 'duplicate keys in discriminators' , function ( ) {
801+ beforeEach ( async function ( ) {
802+ connection = createConnection ( ) ;
803+ } ) ;
804+ describe ( 'csfle' , function ( ) {
805+ it ( 'throws on duplicate keys declared on different discriminators' , async function ( ) {
806+ const schema = new Schema ( {
807+ name : {
808+ type : String , encrypt : { keyId : [ keyId ] , algorithm }
809+ }
810+ } , {
811+ encryptionType : 'csfle'
812+ } ) ;
813+ model = connection . model ( 'Schema' , schema ) ;
814+ discrim1 = model . discriminator ( 'Test' , new Schema ( {
815+ age : {
816+ type : Int32 , encrypt : { keyId : [ keyId ] , algorithm }
817+ }
818+ } , {
819+ encryptionType : 'csfle'
820+ } ) ) ;
821+
822+ discrim2 = model . discriminator ( 'Test2' , new Schema ( {
823+ age : {
824+ type : Int32 , encrypt : { keyId : [ keyId ] , algorithm }
825+ }
826+ } , {
827+ encryptionType : 'csfle'
828+ } ) ) ;
829+
830+ const error = await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
831+ dbName : 'db' , autoEncryption : {
832+ keyVaultNamespace : 'keyvault.datakeys' ,
833+ kmsProviders : { local : { key : LOCAL_KEY } } ,
834+ extraOptions : {
835+ cryptdSharedLibRequired : true ,
836+ cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
837+ }
838+ }
839+ } ) . catch ( e => e ) ;
840+
841+ assert . ok ( error instanceof Error ) ;
842+ assert . match ( error . message , / C a n n o t h a v e d u p l i c a t e k e y s i n d i s c r i m i n a t o r s w i t h e n c r y p t i o n / ) ;
843+ } ) ;
844+ it ( 'throws on duplicate keys declared on root and child discriminators' , async function ( ) {
845+ const schema = new Schema ( {
846+ name : {
847+ type : String , encrypt : { keyId : [ keyId ] , algorithm }
848+ }
849+ } , {
850+ encryptionType : 'csfle'
851+ } ) ;
852+ model = connection . model ( 'Schema' , schema ) ;
853+ assert . throws ( ( ) => model . discriminator ( 'Test' , new Schema ( {
854+ name : {
855+ type : String , encrypt : { keyId : [ keyId ] , algorithm }
856+ }
857+ } , {
858+ encryptionType : 'csfle'
859+ } ) ) ,
860+ / c a n n o t d e c l a r e a n e n c r y p t e d f i e l d o n c h i l d s c h e m a o v e r r i d i n g b a s e s c h e m a \. k e y = n a m e /
861+ ) ;
862+ } ) ;
863+ } ) ;
864+
865+ describe ( 'queryable encryption' , function ( ) {
866+ it ( 'throws on duplicate keys declared on different discriminators' , async function ( ) {
867+ const schema = new Schema ( {
868+ name : {
869+ type : String , encrypt : { keyId }
870+ }
871+ } , {
872+ encryptionType : 'queryableEncryption'
873+ } ) ;
874+ model = connection . model ( 'Schema' , schema ) ;
875+ discrim1 = model . discriminator ( 'Test' , new Schema ( {
876+ age : {
877+ type : Int32 , encrypt : { keyId : keyId2 }
878+ }
879+ } , {
880+ encryptionType : 'queryableEncryption'
881+ } ) ) ;
882+
883+ discrim2 = model . discriminator ( 'Test2' , new Schema ( {
884+ age : {
885+ type : Int32 , encrypt : { keyId : keyId3 }
886+ }
887+ } , {
888+ encryptionType : 'queryableEncryption'
889+ } ) ) ;
890+
891+ const error = await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
892+ dbName : 'db' , autoEncryption : {
893+ keyVaultNamespace : 'keyvault.datakeys' ,
894+ kmsProviders : { local : { key : LOCAL_KEY } } ,
895+ extraOptions : {
896+ cryptdSharedLibRequired : true ,
897+ cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
898+ }
899+ }
900+ } ) . catch ( e => e ) ;
901+
902+ assert . ok ( error instanceof Error ) ;
903+ assert . match ( error . message , / C a n n o t h a v e d u p l i c a t e k e y s i n d i s c r i m i n a t o r s w i t h e n c r y p t i o n / ) ;
904+ } ) ;
905+ it ( 'throws on duplicate keys declared on root and child discriminators' , async function ( ) {
906+ const schema = new Schema ( {
907+ name : {
908+ type : String , encrypt : { keyId }
909+ }
910+ } , {
911+ encryptionType : 'queryableEncryption'
912+ } ) ;
913+ model = connection . model ( 'Schema' , schema ) ;
914+ assert . throws ( ( ) => model . discriminator ( 'Test' , new Schema ( {
915+ name : {
916+ type : String , encrypt : { keyId : keyId2 }
917+ }
918+ } , {
919+ encryptionType : 'queryableEncryption'
920+ } ) ) ,
921+ / c a n n o t d e c l a r e a n e n c r y p t e d f i e l d o n c h i l d s c h e m a o v e r r i d i n g b a s e s c h e m a \. k e y = n a m e /
922+ ) ;
923+ } ) ;
924+ } ) ;
925+ } ) ;
926+
800927 } ) ;
801928 } ) ;
802929} ) ;
0 commit comments