예제 #1
0
 private void AssertAuthenticationSucceeds(
     MongoClientSettings settings,
     bool async,
     bool speculativeAuthenticatationShouldSucceedIfPossible = true)
 {
     // If we don't use a DisposableClient, the second run of AuthenticationSucceedsWithMongoDB_X509_mechanism
     // will fail because the backing Cluster's connections will be associated with a dropped user
     using (var client = DriverTestConfiguration.CreateDisposableclient(settings))
     {
         // The first command executed with the MongoClient triggers either the sync or async variation of the
         // MongoClient's IAuthenticator
         if (async)
         {
             _ = client.ListDatabaseNamesAsync().GetAwaiter().GetResult().ToList();
         }
         else
         {
             _ = client.ListDatabaseNames().ToList();
         }
         if (Feature.SpeculativeAuthentication.IsSupported(CoreTestConfiguration.ServerVersion) &&
             speculativeAuthenticatationShouldSucceedIfPossible)
         {
             var cancellationToken = CancellationToken.None;
             var serverSelector    = new ReadPreferenceServerSelector(settings.ReadPreference);
             var server            = client.Cluster.SelectServer(serverSelector, cancellationToken);
             var channel           = server.GetChannel(cancellationToken);
             var isMasterResult    = channel.ConnectionDescription.IsMasterResult;
             isMasterResult.SpeculativeAuthenticate.Should().NotBeNull();
         }
     }
 }
예제 #2
0
        private void AssertAuthenticationFails(MongoClientSettings settings, bool async)
        {
            using (var client = DriverTestConfiguration.CreateDisposableclient(settings))
            {
                Exception exception;
                if (async)
                {
                    exception = Record.Exception(() => client.ListDatabaseNamesAsync().GetAwaiter().GetResult().ToList());
                }
                else
                {
                    exception = Record.Exception(() => client.ListDatabaseNames().ToList());
                }

                exception.Should().BeOfType <MongoAuthenticationException>();
            }
        }