mirror of
https://github.com/grafana/grafana.git
synced 2025-07-29 17:22:51 +08:00
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:
@ -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)
|
||||
|
Reference in New Issue
Block a user