@@ -6,6 +6,7 @@ const isBsonType = require('../../lib/helpers/isBsonType');
66const { Schema, createConnection } = require ( '../../lib' ) ;
77const { ObjectId, Double, Int32, Decimal128 } = require ( 'bson' ) ;
88const fs = require ( 'fs' ) ;
9+ const mongoose = require ( '../../lib' ) ;
910
1011const LOCAL_KEY = Buffer . from ( 'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk' , 'base64' ) ;
1112
@@ -19,37 +20,46 @@ function isEncryptedValue(object, property) {
1920 assert . ok ( value . sub_type === 6 , `auto encryption for property ${ property } failed: not subtype 6.` ) ;
2021}
2122
22- describe ( 'ci' , ( ) => {
23-
23+ describe ( 'encryption integration tests' , ( ) => {
2424 const cachedUri = process . env . MONGOOSE_TEST_URI ;
2525 const cachedLib = process . env . CRYPT_SHARED_LIB_PATH ;
2626
2727 before ( function ( ) {
2828 const cwd = process . cwd ( ) ;
2929 const file = fs . readFileSync ( cwd + '/data/mo-expansion.yml' , { encoding : 'utf-8' } ) . trim ( ) . split ( '\n' ) ;
30+
31+ // matches `key="value"` and extracts key and value.
3032 const regex = / ^ (?< key > .* ) : " (?< value > .* ) " $ / ;
31- const variables = file . map ( ( line ) => regex . exec ( line . trim ( ) ) . groups ) . reduce ( ( acc , { key, value } ) => ( { ... acc , [ key ] : value } ) , { } ) ;
33+ const variables = Object . fromEntries ( file . map ( ( line ) => regex . exec ( line . trim ( ) ) . groups ) . map ( ( { key, value } ) => [ key , value ] ) ) ;
3234 console . log ( 'File contents' , file ) ;
3335 console . log ( 'Variables' , variables ) ;
34- process . env . CRYPT_SHARED_LIB_PATH = variables . CRYPT_SHARED_LIB_PATH ;
35- process . env . MONGOOSE_TEST_URI = variables . MONGODB_URI ;
36+
37+ process . env . CRYPT_SHARED_LIB_PATH ??= variables . CRYPT_SHARED_LIB_PATH ;
38+ process . env . MONGOOSE_TEST_URI ??= variables . MONGODB_URI ;
3639 } ) ;
3740
3841 after ( function ( ) {
3942 process . env . CRYPT_SHARED_LIB_PATH = cachedLib ;
4043 process . env . MONGOOSE_TEST_URI = cachedUri ;
4144 } ) ;
4245
43- describe ( 'environmental variables' , ( ) => {
46+ describe ( 'meta: environmental variables are correctly set up ' , ( ) => {
4447 it ( 'MONGOOSE_TEST_URI is set' , async function ( ) {
4548 const uri = process . env . MONGOOSE_TEST_URI ;
46- console . log ( 'MONGOOSE_TEST_URI=' , uri ) ;
4749 assert . ok ( uri ) ;
4850 } ) ;
4951
52+ it ( 'MONGOOSE_TEST_URI points to running cluster' , async function ( ) {
53+ try {
54+ const connection = await mongoose . connect ( process . env . MONGOOSE_TEST_URI ) ;
55+ await connection . disconnect ( ) ;
56+ } catch ( error ) {
57+ throw new Error ( 'Unable to connect to running cluster' , { cause : error } ) ;
58+ }
59+ } ) ;
60+
5061 it ( 'CRYPT_SHARED_LIB_PATH is set' , async function ( ) {
5162 const shared_library_path = process . env . CRYPT_SHARED_LIB_PATH ;
52- console . log ( 'CRYPT_SHARED_LIB_PATH=' , shared_library_path ) ;
5363 assert . ok ( shared_library_path ) ;
5464 } ) ;
5565 } ) ;
@@ -98,11 +108,11 @@ describe('ci', () => {
98108 { type : Double , name : 'double' , input : new Double ( 1.5 ) }
99109 ] ;
100110
101- for ( const { type, name, input, expected } of basicSchemaTypes ) {
102- this . afterEach ( async function ( ) {
103- await connection ?. close ( ) ;
104- } ) ;
111+ afterEach ( async function ( ) {
112+ await connection ?. close ( ) ;
113+ } ) ;
105114
115+ for ( const { type, name, input, expected } of basicSchemaTypes ) {
106116 // eslint-disable-next-line no-inner-declarations
107117 async function test ( ) {
108118 const [ { _id } ] = await model . insertMany ( [ { field : input } ] ) ;
0 commit comments