Files
grafana/pkg/services/ldap/testing.go
T

104 lines
2.3 KiB
Go
Raw Normal View History

2019-06-13 17:47:52 +03:00
package ldap
import (
"crypto/tls"
"gopkg.in/ldap.v3"
2022-05-20 12:45:18 -04:00
//TODO(sh0rez): remove once import cycle resolved
_ "github.com/grafana/grafana/pkg/api/response"
2019-06-13 17:47:52 +03:00
)
2021-09-14 10:49:37 +02:00
type searchFunc = func(request *ldap.SearchRequest) (*ldap.SearchResult, error)
2019-06-13 17:47:52 +03:00
// MockConnection struct for testing
type MockConnection struct {
2021-09-14 10:49:37 +02:00
SearchFunc searchFunc
2019-06-13 17:47:52 +03:00
SearchCalled bool
SearchAttributes []string
AddParams *ldap.AddRequest
AddCalled bool
DelParams *ldap.DelRequest
DelCalled bool
CloseCalled bool
2019-07-05 17:49:00 +03:00
UnauthenticatedBindCalled bool
BindCalled bool
BindProvider func(username, password string) error
UnauthenticatedBindProvider func() error
2019-06-13 17:47:52 +03:00
}
// Bind mocks Bind connection function
func (c *MockConnection) Bind(username, password string) error {
2019-07-05 17:49:00 +03:00
c.BindCalled = true
if c.BindProvider != nil {
return c.BindProvider(username, password)
2019-06-13 17:47:52 +03:00
}
return nil
}
// UnauthenticatedBind mocks UnauthenticatedBind connection function
func (c *MockConnection) UnauthenticatedBind(username string) error {
2019-07-05 17:49:00 +03:00
c.UnauthenticatedBindCalled = true
if c.UnauthenticatedBindProvider != nil {
return c.UnauthenticatedBindProvider()
2019-06-13 17:47:52 +03:00
}
return nil
}
// Close mocks Close connection function
func (c *MockConnection) Close() {
c.CloseCalled = true
}
2019-06-13 17:47:52 +03:00
func (c *MockConnection) setSearchResult(result *ldap.SearchResult) {
2021-09-14 10:49:37 +02:00
c.SearchFunc = func(request *ldap.SearchRequest) (*ldap.SearchResult, error) {
return result, nil
}
2019-06-13 17:47:52 +03:00
}
func (c *MockConnection) setSearchError(err error) {
2021-09-14 10:49:37 +02:00
c.SearchFunc = func(request *ldap.SearchRequest) (*ldap.SearchResult, error) {
return nil, err
}
}
func (c *MockConnection) setSearchFunc(fn searchFunc) {
c.SearchFunc = fn
2019-06-13 17:47:52 +03:00
}
// Search mocks Search connection function
func (c *MockConnection) Search(sr *ldap.SearchRequest) (*ldap.SearchResult, error) {
c.SearchCalled = true
c.SearchAttributes = sr.Attributes
2021-09-14 10:49:37 +02:00
return c.SearchFunc(sr)
2019-06-13 17:47:52 +03:00
}
// Add mocks Add connection function
func (c *MockConnection) Add(request *ldap.AddRequest) error {
c.AddCalled = true
c.AddParams = request
return nil
}
// Del mocks Del connection function
func (c *MockConnection) Del(request *ldap.DelRequest) error {
c.DelCalled = true
c.DelParams = request
return nil
}
// StartTLS mocks StartTLS connection function
func (c *MockConnection) StartTLS(*tls.Config) error {
return nil
}