Skip to content

Commit

Permalink
Ensure authsource defaults to dbname before admin
Browse files Browse the repository at this point in the history
GODRIVER-486

Change-Id: Ie79cf08351b9f2bbbf1a4a47f548827ffee89cbf
  • Loading branch information
skriptble committed Jul 10, 2018
1 parent 88e57ff commit 4503c54
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/connstring/connstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,10 @@ func (p *parser) setDefaultAuthParams(dbName string) error {
}
case "":
if p.AuthSource == "" {
p.AuthSource = "admin"
p.AuthSource = dbName
if p.AuthSource == "" {
p.AuthSource = "admin"
}
}
default:
return fmt.Errorf("invalid auth mechanism")
Expand Down
25 changes: 25 additions & 0 deletions core/connstring/connstring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,31 @@ func TestAuthMechanism(t *testing.T) {
}
}

func TestAuthSource(t *testing.T) {
tests := []struct {
s string
expected string
err bool
}{
{s: "foobar?authSource=bazqux", expected: "bazqux"},
{s: "foobar", expected: "foobar"},
{s: "", expected: "admin"},
}

for _, test := range tests {
s := fmt.Sprintf("mongodb://user:pass@localhost/%s", test.s)
t.Run(s, func(t *testing.T) {
cs, err := connstring.Parse(s)
if test.err {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, test.expected, cs.AuthSource)
}
})
}
}

func TestConnect(t *testing.T) {
tests := []struct {
s string
Expand Down

0 comments on commit 4503c54

Please sign in to comment.