@@ -9,9 +9,14 @@ import * as sinon from 'sinon';
9
9
import { refreshKMSCredentials } from '../../../src/client-side-encryption/providers' ;
10
10
import {
11
11
AWSTemporaryCredentialProvider ,
12
+ type CommandOptions ,
13
+ Connection ,
14
+ type Document ,
12
15
MongoAWSError ,
13
16
type MongoClient ,
14
17
MongoDBAWS ,
18
+ type MongoDBNamespace ,
19
+ type MongoDBResponseConstructor ,
15
20
MongoMissingCredentialsError ,
16
21
MongoServerError ,
17
22
setDifference
@@ -61,6 +66,76 @@ describe('MONGODB-AWS', function () {
61
66
expect ( result ) . to . be . a ( 'number' ) ;
62
67
} ) ;
63
68
69
+ describe ( 'ConversationId' , function ( ) {
70
+ let commandStub : sinon . SinonStub <
71
+ [
72
+ ns : MongoDBNamespace ,
73
+ command : Document ,
74
+ options ?: CommandOptions ,
75
+ responseType ?: MongoDBResponseConstructor
76
+ ] ,
77
+ Promise < any >
78
+ > ;
79
+
80
+ let saslStartResult , saslContinue ;
81
+
82
+ beforeEach ( function ( ) {
83
+ // spy on connection.command, filter for saslStart and saslContinue commands
84
+ commandStub = sinon . stub ( Connection . prototype , 'command' ) . callsFake ( async function (
85
+ ns : MongoDBNamespace ,
86
+ command : Document ,
87
+ options : CommandOptions ,
88
+ responseType ?: MongoDBResponseConstructor
89
+ ) {
90
+ if ( command . saslContinue != null ) {
91
+ saslContinue = { ...command } ;
92
+ }
93
+
94
+ const result = await commandStub . wrappedMethod . call (
95
+ this ,
96
+ ns ,
97
+ command ,
98
+ options ,
99
+ responseType
100
+ ) ;
101
+
102
+ if ( command . saslStart != null ) {
103
+ // Modify the result of the saslStart to check if the saslContinue uses it
104
+ result . conversationId = 999 ;
105
+ saslStartResult = { ...result } ;
106
+ }
107
+
108
+ return result ;
109
+ } ) ;
110
+ } ) ;
111
+
112
+ afterEach ( function ( ) {
113
+ commandStub . restore ( ) ;
114
+ sinon . restore ( ) ;
115
+ } ) ;
116
+
117
+ it ( 'should use conversationId returned by saslStart in saslContinue' , async function ( ) {
118
+ client = this . configuration . newClient ( process . env . MONGODB_URI ) ; // use the URI built by the test environment
119
+
120
+ const err = await client
121
+ . db ( 'aws' )
122
+ . collection ( 'aws_test' )
123
+ . estimatedDocumentCount ( )
124
+ . catch ( e => e ) ;
125
+
126
+ // Expecting the saslContinue to fail since we changed the conversationId
127
+ expect ( err ) . to . be . instanceof ( MongoServerError ) ;
128
+ expect ( err . message ) . to . match ( / M i s m a t c h e d c o n v e r s a t i o n i d / ) ;
129
+
130
+ expect ( saslStartResult ) . to . not . be . undefined ;
131
+ expect ( saslContinue ) . to . not . be . undefined ;
132
+
133
+ expect ( saslStartResult ) . to . have . property ( 'conversationId' , 999 ) ;
134
+
135
+ expect ( saslContinue ) . to . have . property ( 'conversationId' ) . equal ( saslStartResult . conversationId ) ;
136
+ } ) ;
137
+ } ) ;
138
+
64
139
it ( 'should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable' , function ( ) {
65
140
client = this . configuration . newClient ( this . configuration . url ( ) , {
66
141
authMechanismProperties : { AWS_SESSION_TOKEN : '' }
0 commit comments