26
26
using MongoDB . Driver . Core . Connections ;
27
27
using System . Threading . Tasks ;
28
28
using MongoDB . Bson . TestHelpers . XunitExtensions ;
29
+ using MongoDB . Driver . Core . Misc ;
29
30
30
31
namespace MongoDB . Driver . Core . Authentication
31
32
{
@@ -39,9 +40,8 @@ public class MongoDBX509AuthenticatorTests
39
40
new BuildInfoResult ( new BsonDocument ( "version" , "2.6.0" ) ) ) ;
40
41
41
42
[ Theory ]
42
- [ InlineData ( null ) ]
43
43
[ InlineData ( "" ) ]
44
- public void Constructor_should_throw_an_ArgumentException_when_username_is_null_or_empty ( string username )
44
+ public void Constructor_should_throw_an_ArgumentException_when_username_is_empty ( string username )
45
45
{
46
46
Action act = ( ) => new MongoDBX509Authenticator ( username ) ;
47
47
@@ -58,6 +58,7 @@ public void Authenticate_should_throw_an_AuthenticationException_when_authentica
58
58
59
59
var reply = MessageHelper . BuildNoDocumentsReturnedReply < RawBsonDocument > ( ) ;
60
60
var connection = new MockConnection ( __serverId ) ;
61
+ connection . Description = CreateConnectionDescription ( new SemanticVersion ( 3 , 2 , 0 ) ) ;
61
62
connection . EnqueueReplyMessage ( reply ) ;
62
63
63
64
Action act ;
@@ -85,6 +86,7 @@ public void Authenticate_should_not_throw_when_authentication_succeeds(
85
86
RawBsonDocumentHelper . FromJson ( "{ok: 1}" ) ) ;
86
87
87
88
var connection = new MockConnection ( __serverId ) ;
89
+ connection . Description = CreateConnectionDescription ( new SemanticVersion ( 3 , 2 , 0 ) ) ;
88
90
connection. EnqueueReplyMessage ( reply ) ;
89
91
90
92
Action act;
@@ -99,5 +101,75 @@ public void Authenticate_should_not_throw_when_authentication_succeeds(
99
101
100
102
act . ShouldNotThrow ( ) ;
101
103
}
104
+
105
+ [ Theory ]
106
+ [ ParameterAttributeData ]
107
+ public void Authenticate_should_throw_when_username_is_null_and_server_does_not_support_null_username (
108
+ [ Values ( false , true ) ]
109
+ bool async)
110
+ {
111
+ var subject = new MongoDBX509Authenticator ( null ) ;
112
+
113
+ var reply = MessageHelper. BuildReply< RawBsonDocument> (
114
+ RawBsonDocumentHelper . FromJson ( "{ok: 1}" ) ) ;
115
+
116
+ var connection = new MockConnection( __serverId ) ;
117
+ connection. Description = CreateConnectionDescription ( new SemanticVersion ( 3 , 2 , 0 ) ) ;
118
+ connection. EnqueueReplyMessage ( reply ) ;
119
+
120
+ Exception exception;
121
+ if ( async)
122
+ {
123
+ exception = Record . Exception ( ( ) => subject . AuthenticateAsync ( connection , __description , CancellationToken . None ) . GetAwaiter ( ) . GetResult ( ) ) ;
124
+ }
125
+ else
126
+ {
127
+ exception = Record . Exception ( ( ) => subject . Authenticate ( connection , __description , CancellationToken . None ) ) ;
128
+ }
129
+
130
+ exception . Should ( ) . BeOfType < MongoConnectionException > ( ) ;
131
+ }
132
+
133
+ [ Theory ]
134
+ [ ParameterAttributeData ]
135
+ public void Authenticate_should_not_throw_when_username_is_null_and_server_support_null_username (
136
+ [ Values ( false , true ) ]
137
+ bool async)
138
+ {
139
+ var subject = new MongoDBX509Authenticator ( null ) ;
140
+
141
+ var reply = MessageHelper. BuildReply< RawBsonDocument> (
142
+ RawBsonDocumentHelper . FromJson ( "{ok: 1}" ) ) ;
143
+
144
+ var connection = new MockConnection( __serverId ) ;
145
+ connection. Description = CreateConnectionDescription ( new SemanticVersion ( 3 , 4 , 0 ) ) ;
146
+ connection. EnqueueReplyMessage ( reply ) ;
147
+
148
+ Exception exception;
149
+ if ( async)
150
+ {
151
+ exception = Record . Exception ( ( ) => subject . AuthenticateAsync ( connection , __description , CancellationToken . None ) . GetAwaiter ( ) . GetResult ( ) ) ;
152
+ }
153
+ else
154
+ {
155
+ exception = Record . Exception ( ( ) => subject . Authenticate ( connection , __description , CancellationToken . None ) ) ;
156
+ }
157
+
158
+ exception . Should ( ) . BeNull ( ) ;
159
+ }
160
+
161
+ // private methods
162
+ private ConnectionDescription CreateConnectionDescription ( SemanticVersion serverVersion )
163
+ {
164
+ var clusterId = new ClusterId ( 1 ) ;
165
+ var serverId = new ServerId ( clusterId , new DnsEndPoint ( "localhost" , 27017 ) ) ;
166
+ var connectionId = new ConnectionId ( serverId , 1 ) ;
167
+ var isMasterResult = new IsMasterResult ( new BsonDocument ( ) ) ;
168
+ var buildInfoResult = new BuildInfoResult ( new BsonDocument
169
+ {
170
+ { "version" , serverVersion . ToString ( ) }
171
+ } ) ;
172
+ return new ConnectionDescription ( connectionId , isMasterResult , buildInfoResult ) ;
173
+ }
102
174
}
103
175
}
0 commit comments