Files
grafana/pkg/services/multildap/testing.go
gotjosh 0cbbb43222 LDAP: Add API endpoint to debug user mapping from LDAP (#18833)
* Move the ReloadLDAPCfg function to the debug file

Appears to be a better suite place for this.

* LDAP: Return the server information when we find a specific user

We allow you to specify multiple LDAP servers as part of LDAP authentication integration. As part of searching for specific users, we need to understand from which server they come from. Returning the server configuration as part of the search will help us do two things:

- Understand in which server we found the user
- Have access the groups specified as part of the server configuration

* LDAP: Adds the /api/admin/ldap/:username endpoint

This endpoint returns a user found within the configured LDAP server(s). Moreso, it provides the mapping information for the user to help administrators understand how the users would be created within Grafana based on the current configuration.

No changes are executed or saved to the database, this is all an in-memory representation of how the final result would look like.
2019-09-03 18:34:44 +01:00

113 lines
2.4 KiB
Go

package multildap
import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/ldap"
)
// MockLDAP represents testing struct for ldap testing
type MockLDAP struct {
dialCalledTimes int
loginCalledTimes int
closeCalledTimes int
usersCalledTimes int
bindCalledTimes int
dialErrReturn error
loginErrReturn error
loginReturn *models.ExternalUserInfo
bindErrReturn error
usersErrReturn error
usersFirstReturn []*models.ExternalUserInfo
usersRestReturn []*models.ExternalUserInfo
}
// Login test fn
func (mock *MockLDAP) Login(*models.LoginUserQuery) (*models.ExternalUserInfo, error) {
mock.loginCalledTimes = mock.loginCalledTimes + 1
return mock.loginReturn, mock.loginErrReturn
}
// Users test fn
func (mock *MockLDAP) Users([]string) ([]*models.ExternalUserInfo, error) {
mock.usersCalledTimes = mock.usersCalledTimes + 1
if mock.usersCalledTimes == 1 {
return mock.usersFirstReturn, mock.usersErrReturn
}
return mock.usersRestReturn, mock.usersErrReturn
}
// UserBind test fn
func (mock *MockLDAP) UserBind(string, string) error {
return nil
}
// Dial test fn
func (mock *MockLDAP) Dial() error {
mock.dialCalledTimes = mock.dialCalledTimes + 1
return mock.dialErrReturn
}
// Close test fn
func (mock *MockLDAP) Close() {
mock.closeCalledTimes = mock.closeCalledTimes + 1
}
func (mock *MockLDAP) Bind() error {
mock.bindCalledTimes++
return mock.bindErrReturn
}
// MockMultiLDAP represents testing struct for multildap testing
type MockMultiLDAP struct {
LoginCalledTimes int
UsersCalledTimes int
UserCalledTimes int
UsersResult []*models.ExternalUserInfo
}
// Login test fn
func (mock *MockMultiLDAP) Login(query *models.LoginUserQuery) (
*models.ExternalUserInfo, error,
) {
mock.LoginCalledTimes = mock.LoginCalledTimes + 1
return nil, nil
}
// Users test fn
func (mock *MockMultiLDAP) Users(logins []string) (
[]*models.ExternalUserInfo, error,
) {
mock.UsersCalledTimes = mock.UsersCalledTimes + 1
return mock.UsersResult, nil
}
// User test fn
func (mock *MockMultiLDAP) User(login string) (
*models.ExternalUserInfo, ldap.ServerConfig, error,
) {
mock.UserCalledTimes = mock.UserCalledTimes + 1
return nil, ldap.ServerConfig{}, nil
}
func setup() *MockLDAP {
mock := &MockLDAP{}
newLDAP = func(config *ldap.ServerConfig) ldap.IServer {
return mock
}
return mock
}
func teardown() {
newLDAP = ldap.New
}