77package topology
88
99import (
10+ "context"
1011 "fmt"
1112 "net/url"
1213 "reflect"
@@ -17,6 +18,10 @@ import (
1718 "go.mongodb.org/mongo-driver/v2/internal/require"
1819 "go.mongodb.org/mongo-driver/v2/mongo/options"
1920 "go.mongodb.org/mongo-driver/v2/x/mongo/driver"
21+ "go.mongodb.org/mongo-driver/v2/x/mongo/driver/description"
22+ "go.mongodb.org/mongo-driver/v2/x/mongo/driver/drivertest"
23+ "go.mongodb.org/mongo-driver/v2/x/mongo/driver/mnet"
24+ "go.mongodb.org/mongo-driver/v2/x/mongo/driver/xoptions"
2025)
2126
2227func TestDirectConnectionFromConnString (t * testing.T ) {
@@ -85,6 +90,76 @@ func TestLoadBalancedFromConnString(t *testing.T) {
8590 }
8691}
8792
93+ type testAuthenticator struct {}
94+
95+ var _ driver.Authenticator = & testAuthenticator {}
96+
97+ func (a * testAuthenticator ) Auth (context.Context , * driver.AuthConfig ) error {
98+ return fmt .Errorf ("test error" )
99+ }
100+
101+ func (a * testAuthenticator ) Reauth (context.Context , * driver.AuthConfig ) error {
102+ return nil
103+ }
104+
105+ func TestAuthenticateToAnything (t * testing.T ) {
106+ t .Parallel ()
107+
108+ cases := []struct {
109+ name string
110+ set func (* options.ClientOptions ) error
111+ require func (* testing.T , error )
112+ }{
113+ {
114+ name : "default" ,
115+ set : func (* options.ClientOptions ) error { return nil },
116+ require : func (t * testing.T , err error ) {
117+ require .NoError (t , err )
118+ },
119+ },
120+ {
121+ name : "positive" ,
122+ set : func (opt * options.ClientOptions ) error {
123+ return xoptions .SetInternalClientOptions (opt , "authenticateToAnything" , true )
124+ },
125+ require : func (t * testing.T , err error ) {
126+ require .EqualError (t , err , "auth error: test error" )
127+ },
128+ },
129+ {
130+ name : "negative" ,
131+ set : func (opt * options.ClientOptions ) error {
132+ return xoptions .SetInternalClientOptions (opt , "authenticateToAnything" , false )
133+ },
134+ require : func (t * testing.T , err error ) {
135+ require .NoError (t , err )
136+ },
137+ },
138+ }
139+
140+ describer := & drivertest.ChannelConn {
141+ Desc : description.Server {Kind : description .ServerKindRSArbiter },
142+ }
143+ for _ , tc := range cases {
144+ tc := tc
145+
146+ t .Run (tc .name , func (t * testing.T ) {
147+ t .Parallel ()
148+
149+ opt := options .Client ().SetAuth (options.Credential {Username : "foo" , Password : "bar" })
150+ err := tc .set (opt )
151+ require .NoError (t , err , "error setting authenticateToAnything: %v" , err )
152+ cfg , err := NewConfigFromOptionsWithAuthenticator (opt , nil , & testAuthenticator {})
153+ require .NoError (t , err , "error constructing topology config: %v" , err )
154+
155+ srvrCfg := newServerConfig (defaultConnectionTimeout , cfg .ServerOpts ... )
156+ connCfg := newConnectionConfig (srvrCfg .connectionOpts ... )
157+ err = connCfg .handshaker .FinishHandshake (context .TODO (), & mnet.Connection {Describer : describer })
158+ tc .require (t , err )
159+ })
160+ }
161+ }
162+
88163func TestTopologyNewConfig (t * testing.T ) {
89164 t .Run ("default ServerSelectionTimeout" , func (t * testing.T ) {
90165 cfg , err := NewConfig (options .Client (), nil )
0 commit comments