LDAP: reduce API and allow its extension (#17209)

* Removes Add/Remove methods

* Publicise necessary fields and methods so we could extend it

* Publicise mock API

* More comments and additional simplifications

* Sync with master

Still having low coverage :/ - should be addressed in #17208
This commit is contained in:
Oleg Gaidarenko
2019-05-27 10:36:49 +03:00
committed by GitHub
parent 5884e235fc
commit de92c360a1
6 changed files with 209 additions and 408 deletions

View File

@ -13,7 +13,7 @@ func TestLDAPHelpers(t *testing.T) {
Convey("serializeUsers()", t, func() {
Convey("simple case", func() {
server := &Server{
config: &ServerConfig{
Config: &ServerConfig{
Attr: AttributeMap{
Username: "username",
Name: "name",
@ -22,7 +22,7 @@ func TestLDAPHelpers(t *testing.T) {
},
SearchBaseDNs: []string{"BaseDNHere"},
},
connection: &mockConnection{},
Connection: &MockConnection{},
log: log.New("test-logger"),
}
@ -46,7 +46,7 @@ func TestLDAPHelpers(t *testing.T) {
Convey("without lastname", func() {
server := &Server{
config: &ServerConfig{
Config: &ServerConfig{
Attr: AttributeMap{
Username: "username",
Name: "name",
@ -55,7 +55,7 @@ func TestLDAPHelpers(t *testing.T) {
},
SearchBaseDNs: []string{"BaseDNHere"},
},
connection: &mockConnection{},
Connection: &MockConnection{},
log: log.New("test-logger"),
}
@ -75,74 +75,9 @@ func TestLDAPHelpers(t *testing.T) {
})
})
Convey("initialBind", t, func() {
Convey("Given bind dn and password configured", func() {
connection := &mockConnection{}
var actualUsername, actualPassword string
connection.bindProvider = func(username, password string) error {
actualUsername = username
actualPassword = password
return nil
}
server := &Server{
connection: connection,
config: &ServerConfig{
BindDN: "cn=%s,o=users,dc=grafana,dc=org",
BindPassword: "bindpwd",
},
}
err := server.initialBind("user", "pwd")
So(err, ShouldBeNil)
So(server.requireSecondBind, ShouldBeTrue)
So(actualUsername, ShouldEqual, "cn=user,o=users,dc=grafana,dc=org")
So(actualPassword, ShouldEqual, "bindpwd")
})
Convey("Given bind dn configured", func() {
connection := &mockConnection{}
var actualUsername, actualPassword string
connection.bindProvider = func(username, password string) error {
actualUsername = username
actualPassword = password
return nil
}
server := &Server{
connection: connection,
config: &ServerConfig{
BindDN: "cn=%s,o=users,dc=grafana,dc=org",
},
}
err := server.initialBind("user", "pwd")
So(err, ShouldBeNil)
So(server.requireSecondBind, ShouldBeFalse)
So(actualUsername, ShouldEqual, "cn=user,o=users,dc=grafana,dc=org")
So(actualPassword, ShouldEqual, "pwd")
})
Convey("Given empty bind dn and password", func() {
connection := &mockConnection{}
unauthenticatedBindWasCalled := false
var actualUsername string
connection.unauthenticatedBindProvider = func(username string) error {
unauthenticatedBindWasCalled = true
actualUsername = username
return nil
}
server := &Server{
connection: connection,
config: &ServerConfig{},
}
err := server.initialBind("user", "pwd")
So(err, ShouldBeNil)
So(server.requireSecondBind, ShouldBeTrue)
So(unauthenticatedBindWasCalled, ShouldBeTrue)
So(actualUsername, ShouldBeEmpty)
})
})
Convey("serverBind()", t, func() {
Convey("Given bind dn and password configured", func() {
connection := &mockConnection{}
connection := &MockConnection{}
var actualUsername, actualPassword string
connection.bindProvider = func(username, password string) error {
actualUsername = username
@ -150,8 +85,8 @@ func TestLDAPHelpers(t *testing.T) {
return nil
}
server := &Server{
connection: connection,
config: &ServerConfig{
Connection: connection,
Config: &ServerConfig{
BindDN: "o=users,dc=grafana,dc=org",
BindPassword: "bindpwd",
},
@ -163,7 +98,7 @@ func TestLDAPHelpers(t *testing.T) {
})
Convey("Given bind dn configured", func() {
connection := &mockConnection{}
connection := &MockConnection{}
unauthenticatedBindWasCalled := false
var actualUsername string
connection.unauthenticatedBindProvider = func(username string) error {
@ -172,8 +107,8 @@ func TestLDAPHelpers(t *testing.T) {
return nil
}
server := &Server{
connection: connection,
config: &ServerConfig{
Connection: connection,
Config: &ServerConfig{
BindDN: "o=users,dc=grafana,dc=org",
},
}
@ -184,7 +119,7 @@ func TestLDAPHelpers(t *testing.T) {
})
Convey("Given empty bind dn and password", func() {
connection := &mockConnection{}
connection := &MockConnection{}
unauthenticatedBindWasCalled := false
var actualUsername string
connection.unauthenticatedBindProvider = func(username string) error {
@ -193,8 +128,8 @@ func TestLDAPHelpers(t *testing.T) {
return nil
}
server := &Server{
connection: connection,
config: &ServerConfig{},
Connection: connection,
Config: &ServerConfig{},
}
err := server.serverBind()
So(err, ShouldBeNil)