@@ -8,6 +8,12 @@ const { ObjectId, Double, Int32, Decimal128 } = require('bson');
88
99const LOCAL_KEY = Buffer . from ( 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk' , 'base64' ) ;
1010
11+ assert . isEncryptedValue = function isEncryptedValue ( object , property ) {
12+ const value = object [ property ] ;
13+ assert . ok ( isBsonType ( value , 'Binary' ) , `auto encryption for property ${ property } failed: not a BSON binary.` ) ;
14+ assert . ok ( value . sub_type === 6 , `auto encryption for property ${ property } failed: not subtype 6.` ) ;
15+ } ;
16+
1117describe ( 'ci' , ( ) => {
1218 describe ( 'environmental variables' , ( ) => {
1319 it ( 'MONGOOSE_TEST_URI is set' , async function ( ) {
@@ -21,7 +27,7 @@ describe('ci', () => {
2127 } ) ;
2228 } ) ;
2329
24- let keyId , keyId2 ;
30+ let keyId , keyId2 , keyId3 ;
2531 let utilClient ;
2632
2733 beforeEach ( async function ( ) {
@@ -34,6 +40,7 @@ describe('ci', () => {
3440 } ) ;
3541 keyId = await clientEncryption . createDataKey ( 'local' ) ;
3642 keyId2 = await clientEncryption . createDataKey ( 'local' ) ;
43+ keyId3 = await clientEncryption . createDataKey ( 'local' ) ;
3744 await keyVaultClient . close ( ) ;
3845
3946 utilClient = new mdb . MongoClient ( process . env . MONGOOSE_TEST_URI ) ;
@@ -65,7 +72,6 @@ describe('ci', () => {
6572 ] ;
6673
6774 for ( const { type, name, input, expected } of basicSchemaTypes ) {
68-
6975 this . afterEach ( async function ( ) {
7076 await connection ?. close ( ) ;
7177 } ) ;
@@ -588,5 +594,172 @@ describe('ci', () => {
588594 }
589595 } ) ;
590596 } ) ;
597+
598+ describe ( 'Models with discriminators' , function ( ) {
599+ let discrim1 , discrim2 , model ;
600+
601+ describe ( 'csfle' , function ( ) {
602+ beforeEach ( async function ( ) {
603+ connection = createConnection ( ) ;
604+
605+ const schema = new Schema ( {
606+ name : {
607+ type : String , encrypt : { keyId : [ keyId ] , algorithm }
608+ }
609+ } , {
610+ encryptionType : 'csfle'
611+ } ) ;
612+ model = connection . model ( 'Schema' , schema ) ;
613+ discrim1 = model . discriminator ( 'Test' , new Schema ( {
614+ age : {
615+ type : Int32 , encrypt : { keyId : [ keyId ] , algorithm }
616+ }
617+ } , {
618+ encryptionType : 'csfle'
619+ } ) ) ;
620+
621+ discrim2 = model . discriminator ( 'Test2' , new Schema ( {
622+ dob : {
623+ type : Int32 , encrypt : { keyId : [ keyId ] , algorithm }
624+ }
625+ } , {
626+ encryptionType : 'csfle'
627+ } ) ) ;
628+
629+
630+ await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
631+ dbName : 'db' , autoEncryption : {
632+ keyVaultNamespace : 'keyvault.datakeys' ,
633+ kmsProviders : { local : { key : LOCAL_KEY } } ,
634+ extraOptions : {
635+ cryptdSharedLibRequired : true ,
636+ cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
637+ }
638+ }
639+ } ) ;
640+ } ) ;
641+ it ( 'encrypts' , async function ( ) {
642+ {
643+ const doc = new discrim1 ( { name : 'bailey' , age : 32 } ) ;
644+ await doc . save ( ) ;
645+
646+ const encryptedDoc = await utilClient . db ( 'db' ) . collection ( 'schemas' ) . findOne ( { _id : doc . _id } ) ;
647+
648+ assert . isEncryptedValue ( encryptedDoc , 'age' ) ;
649+ }
650+
651+ {
652+ const doc = new discrim2 ( { name : 'bailey' , dob : 32 } ) ;
653+ await doc . save ( ) ;
654+
655+ const encryptedDoc = await utilClient . db ( 'db' ) . collection ( 'schemas' ) . findOne ( { _id : doc . _id } ) ;
656+
657+ assert . isEncryptedValue ( encryptedDoc , 'dob' ) ;
658+ }
659+ } ) ;
660+
661+ it ( 'decrypts' , async function ( ) {
662+ {
663+ const doc = new discrim1 ( { name : 'bailey' , age : 32 } ) ;
664+ await doc . save ( ) ;
665+
666+ const decryptedDoc = await discrim1 . findOne ( { _id : doc . _id } ) ;
667+
668+ assert . equal ( decryptedDoc . age , 32 ) ;
669+ }
670+
671+ {
672+ const doc = new discrim2 ( { name : 'bailey' , dob : 32 } ) ;
673+ await doc . save ( ) ;
674+
675+ const decryptedDoc = await discrim2 . findOne ( { _id : doc . _id } ) ;
676+
677+ assert . equal ( decryptedDoc . dob , 32 ) ;
678+ }
679+ } ) ;
680+ } ) ;
681+
682+
683+ describe ( 'qe' , function ( ) {
684+ beforeEach ( async function ( ) {
685+ connection = createConnection ( ) ;
686+
687+ const schema = new Schema ( {
688+ name : {
689+ type : String , encrypt : { keyId }
690+ }
691+ } , {
692+ encryptionType : 'queryableEncryption'
693+ } ) ;
694+ model = connection . model ( 'Schema' , schema ) ;
695+ discrim1 = model . discriminator ( 'Test' , new Schema ( {
696+ age : {
697+ type : Int32 , encrypt : { keyId : keyId2 }
698+ }
699+ } , {
700+ encryptionType : 'queryableEncryption'
701+ } ) ) ;
702+
703+ discrim2 = model . discriminator ( 'Test2' , new Schema ( {
704+ dob : {
705+ type : Int32 , encrypt : { keyId : keyId3 }
706+ }
707+ } , {
708+ encryptionType : 'queryableEncryption'
709+ } ) ) ;
710+
711+ await connection . openUri ( process . env . MONGOOSE_TEST_URI , {
712+ dbName : 'db' , autoEncryption : {
713+ keyVaultNamespace : 'keyvault.datakeys' ,
714+ kmsProviders : { local : { key : LOCAL_KEY } } ,
715+ extraOptions : {
716+ cryptdSharedLibRequired : true ,
717+ cryptSharedLibPath : process . env . CRYPT_SHARED_LIB_PATH
718+ }
719+ }
720+ } ) ;
721+ } ) ;
722+ it ( 'encrypts' , async function ( ) {
723+ {
724+ const doc = new discrim1 ( { name : 'bailey' , age : 32 } ) ;
725+ await doc . save ( ) ;
726+
727+ const encryptedDoc = await utilClient . db ( 'db' ) . collection ( 'schemas' ) . findOne ( { _id : doc . _id } ) ;
728+
729+ assert . isEncryptedValue ( encryptedDoc , 'age' ) ;
730+ }
731+
732+ {
733+ const doc = new discrim2 ( { name : 'bailey' , dob : 32 } ) ;
734+ await doc . save ( ) ;
735+
736+ const encryptedDoc = await utilClient . db ( 'db' ) . collection ( 'schemas' ) . findOne ( { _id : doc . _id } ) ;
737+
738+ assert . isEncryptedValue ( encryptedDoc , 'dob' ) ;
739+ }
740+ } ) ;
741+
742+ it ( 'decrypts' , async function ( ) {
743+ {
744+ const doc = new discrim1 ( { name : 'bailey' , age : 32 } ) ;
745+ await doc . save ( ) ;
746+
747+ const decryptedDoc = await discrim1 . findOne ( { _id : doc . _id } ) ;
748+
749+ assert . equal ( decryptedDoc . age , 32 ) ;
750+ }
751+
752+ {
753+ const doc = new discrim2 ( { name : 'bailey' , dob : 32 } ) ;
754+ await doc . save ( ) ;
755+
756+ const decryptedDoc = await discrim2 . findOne ( { _id : doc . _id } ) ;
757+
758+ assert . equal ( decryptedDoc . dob , 32 ) ;
759+ }
760+ } ) ;
761+ } ) ;
762+
763+ } ) ;
591764 } ) ;
592765} ) ;
0 commit comments