| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package login
- import (
- "errors"
- "testing"
- . "github.com/smartystreets/goconvey/convey"
- m "github.com/grafana/grafana/pkg/models"
- LDAP "github.com/grafana/grafana/pkg/services/ldap"
- "github.com/grafana/grafana/pkg/setting"
- )
- var errTest = errors.New("Test error")
- func TestLdapLogin(t *testing.T) {
- Convey("Login using ldap", t, func() {
- Convey("Given ldap enabled and no server configured", func() {
- setting.LdapEnabled = true
- ldapLoginScenario("When login", func(sc *ldapLoginScenarioContext) {
- sc.withLoginResult(false)
- readLDAPConfig = func() *LDAP.Config {
- config := &LDAP.Config{
- Servers: []*LDAP.ServerConfig{},
- }
- return config
- }
- enabled, err := loginUsingLdap(sc.loginUserQuery)
- Convey("it should return true", func() {
- So(enabled, ShouldBeTrue)
- })
- Convey("it should return no LDAP servers error", func() {
- So(err, ShouldEqual, ErrNoLDAPServers)
- })
- Convey("it should not call ldap login", func() {
- So(sc.ldapAuthenticatorMock.loginCalled, ShouldBeFalse)
- })
- })
- })
- Convey("Given ldap disabled", func() {
- setting.LdapEnabled = false
- ldapLoginScenario("When login", func(sc *ldapLoginScenarioContext) {
- sc.withLoginResult(false)
- enabled, err := loginUsingLdap(&m.LoginUserQuery{
- Username: "user",
- Password: "pwd",
- })
- Convey("it should return false", func() {
- So(enabled, ShouldBeFalse)
- })
- Convey("it should not return error", func() {
- So(err, ShouldBeNil)
- })
- Convey("it should not call ldap login", func() {
- So(sc.ldapAuthenticatorMock.loginCalled, ShouldBeFalse)
- })
- })
- })
- })
- }
- func mockLdapAuthenticator(valid bool) *mockAuth {
- mock := &mockAuth{
- validLogin: valid,
- }
- newLDAP = func(server *LDAP.ServerConfig) LDAP.IAuth {
- return mock
- }
- return mock
- }
- type mockAuth struct {
- validLogin bool
- loginCalled bool
- }
- func (auth *mockAuth) Login(query *m.LoginUserQuery) error {
- auth.loginCalled = true
- if !auth.validLogin {
- return errTest
- }
- return nil
- }
- func (auth *mockAuth) Users() ([]*LDAP.UserInfo, error) {
- return nil, nil
- }
- func (auth *mockAuth) SyncUser(query *m.LoginUserQuery) error {
- return nil
- }
- func (auth *mockAuth) GetGrafanaUserFor(ctx *m.ReqContext, ldapUser *LDAP.UserInfo) (*m.User, error) {
- return nil, nil
- }
- type ldapLoginScenarioContext struct {
- loginUserQuery *m.LoginUserQuery
- ldapAuthenticatorMock *mockAuth
- }
- type ldapLoginScenarioFunc func(c *ldapLoginScenarioContext)
- func ldapLoginScenario(desc string, fn ldapLoginScenarioFunc) {
- Convey(desc, func() {
- mock := &mockAuth{}
- sc := &ldapLoginScenarioContext{
- loginUserQuery: &m.LoginUserQuery{
- Username: "user",
- Password: "pwd",
- IpAddress: "192.168.1.1:56433",
- },
- ldapAuthenticatorMock: mock,
- }
- readLDAPConfig = func() *LDAP.Config {
- config := &LDAP.Config{
- Servers: []*LDAP.ServerConfig{
- {
- Host: "",
- },
- },
- }
- return config
- }
- newLDAP = func(server *LDAP.ServerConfig) LDAP.IAuth {
- return mock
- }
- defer func() {
- newLDAP = LDAP.New
- readLDAPConfig = LDAP.ReadConfig
- }()
- fn(sc)
- })
- }
- func (sc *ldapLoginScenarioContext) withLoginResult(valid bool) {
- sc.ldapAuthenticatorMock = mockLdapAuthenticator(valid)
- }
|