Skip to content

Commit 66b8813

Browse files
committed
add tests
1 parent 16f8f28 commit 66b8813

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

x/mongo/driver/auth/auth_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
package auth_test
88

99
import (
10+
"context"
11+
"fmt"
1012
"net/http"
1113
"testing"
1214

1315
"github.com/google/go-cmp/cmp"
1416
"go.mongodb.org/mongo-driver/v2/internal/require"
1517
"go.mongodb.org/mongo-driver/v2/x/bsonx/bsoncore"
18+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
1619
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/auth"
20+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/description"
21+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/drivertest"
22+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/mnet"
1723
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/wiremessage"
1824
)
1925

@@ -101,3 +107,56 @@ func compareResponses(t *testing.T, wm []byte, expectedPayload bsoncore.Document
101107
t.Errorf("Payloads don't match. got %v; want %v", actualPayload, expectedPayload)
102108
}
103109
}
110+
111+
type testAuthenticator struct{}
112+
113+
func (a *testAuthenticator) Auth(context.Context, *driver.AuthConfig) error {
114+
return fmt.Errorf("test error")
115+
}
116+
117+
func (a *testAuthenticator) Reauth(context.Context, *driver.AuthConfig) error {
118+
return nil
119+
}
120+
121+
func TestPerformAuthentication(t *testing.T) {
122+
t.Parallel()
123+
124+
cases := []struct {
125+
name string
126+
needToPerform bool
127+
assert func(*testing.T, error)
128+
}{
129+
{
130+
name: "positive",
131+
needToPerform: true,
132+
assert: func(t *testing.T, err error) {
133+
require.EqualError(t, err, "auth error: test error")
134+
},
135+
},
136+
{
137+
name: "negative",
138+
needToPerform: false,
139+
assert: func(t *testing.T, err error) {
140+
require.NoError(t, err)
141+
},
142+
},
143+
}
144+
mnetconn := mnet.NewConnection(&drivertest.ChannelConn{})
145+
for _, tc := range cases {
146+
tc := tc
147+
148+
t.Run(tc.name, func(t *testing.T) {
149+
t.Parallel()
150+
151+
handshaker := auth.Handshaker(nil, &auth.HandshakeOptions{
152+
Authenticator: &testAuthenticator{},
153+
PerformAuthentication: func(description.Server) bool {
154+
return tc.needToPerform
155+
},
156+
})
157+
158+
err := handshaker.FinishHandshake(context.Background(), mnetconn)
159+
tc.assert(t, err)
160+
})
161+
}
162+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (C) MongoDB, Inc. 2025-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
package options
8+
9+
import (
10+
"testing"
11+
12+
internalOptions "go.mongodb.org/mongo-driver/v2/internal/options"
13+
"go.mongodb.org/mongo-driver/v2/internal/require"
14+
"go.mongodb.org/mongo-driver/v2/mongo/options"
15+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
16+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/drivertest"
17+
)
18+
19+
func TestSetInternalClientOptions(t *testing.T) {
20+
t.Parallel()
21+
22+
cases := []struct {
23+
key string
24+
value any
25+
}{
26+
{
27+
key: "authenticateToAnything",
28+
value: true,
29+
},
30+
}
31+
for _, tc := range cases {
32+
tc := tc
33+
34+
t.Run(tc.key, func(t *testing.T) {
35+
t.Parallel()
36+
37+
opts := options.Client()
38+
err := SetInternalClientOptions(opts, tc.key, tc.value)
39+
require.NoError(t, err, "error setting %s: %v", tc.key, err)
40+
v := internalOptions.Value(opts.Custom, tc.key)
41+
require.Equal(t, tc.value, v, "expected %v, got %v", tc.value, v)
42+
})
43+
}
44+
45+
t.Run("crypt", func(t *testing.T) {
46+
t.Parallel()
47+
48+
c := driver.NewCrypt(&driver.CryptOptions{})
49+
opts := options.Client()
50+
err := SetInternalClientOptions(opts, "crypt", c)
51+
require.NoError(t, err, "error setting crypt: %v", err)
52+
require.Equal(t, c, opts.Crypt, "expected %v, got %v", c, opts.Crypt)
53+
})
54+
55+
t.Run("deployment", func(t *testing.T) {
56+
t.Parallel()
57+
58+
d := &drivertest.MockDeployment{}
59+
opts := options.Client()
60+
err := SetInternalClientOptions(opts, "deployment", d)
61+
require.NoError(t, err, "error setting deployment: %v", err)
62+
require.Equal(t, d, opts.Deployment, "expected %v, got %v", d, opts.Deployment)
63+
})
64+
}

x/mongo/driver/topology/topology_options_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package topology
88

99
import (
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+
xoptions "go.mongodb.org/mongo-driver/v2/x/mongo/driver/options"
2025
)
2126

2227
func TestDirectConnectionFromConnString(t *testing.T) {
@@ -85,6 +90,74 @@ func TestLoadBalancedFromConnString(t *testing.T) {
8590
}
8691
}
8792

93+
type testAuthenticator struct{}
94+
95+
func (a *testAuthenticator) Auth(context.Context, *driver.AuthConfig) error {
96+
return fmt.Errorf("test error")
97+
}
98+
99+
func (a *testAuthenticator) Reauth(context.Context, *driver.AuthConfig) error {
100+
return nil
101+
}
102+
103+
func TestAuthenticateToAnything(t *testing.T) {
104+
t.Parallel()
105+
106+
cases := []struct {
107+
name string
108+
set func(*options.ClientOptions) error
109+
assert func(*testing.T, error)
110+
}{
111+
{
112+
name: "default",
113+
set: func(*options.ClientOptions) error { return nil },
114+
assert: func(t *testing.T, err error) {
115+
require.NoError(t, err)
116+
},
117+
},
118+
{
119+
name: "positive",
120+
set: func(opt *options.ClientOptions) error {
121+
return xoptions.SetInternalClientOptions(opt, "authenticateToAnything", true)
122+
},
123+
assert: func(t *testing.T, err error) {
124+
require.EqualError(t, err, "auth error: test error")
125+
},
126+
},
127+
{
128+
name: "negative",
129+
set: func(opt *options.ClientOptions) error {
130+
return xoptions.SetInternalClientOptions(opt, "authenticateToAnything", false)
131+
},
132+
assert: func(t *testing.T, err error) {
133+
require.NoError(t, err)
134+
},
135+
},
136+
}
137+
138+
describer := &drivertest.ChannelConn{
139+
Desc: description.Server{Kind: description.ServerKindRSArbiter},
140+
}
141+
for _, tc := range cases {
142+
tc := tc
143+
144+
t.Run(tc.name, func(t *testing.T) {
145+
t.Parallel()
146+
147+
opt := options.Client().SetAuth(options.Credential{Username: "foo", Password: "bar"})
148+
err := tc.set(opt)
149+
require.NoError(t, err, "error setting authenticateToAnything: %v", err)
150+
cfg, err := NewConfigFromOptionsWithAuthenticator(opt, nil, &testAuthenticator{})
151+
require.NoError(t, err, "error constructing topology config: %v", err)
152+
153+
srvrCfg := newServerConfig(defaultConnectionTimeout, cfg.ServerOpts...)
154+
connCfg := newConnectionConfig(srvrCfg.connectionOpts...)
155+
err = connCfg.handshaker.FinishHandshake(context.TODO(), &mnet.Connection{Describer: describer})
156+
tc.assert(t, err)
157+
})
158+
}
159+
}
160+
88161
func TestTopologyNewConfig(t *testing.T) {
89162
t.Run("default ServerSelectionTimeout", func(t *testing.T) {
90163
cfg, err := NewConfig(options.Client(), nil)

0 commit comments

Comments
 (0)