forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap_login_test.go
180 lines (142 loc) · 3.62 KB
/
ldap_login_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package login
import (
"errors"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/ldap"
"github.com/grafana/grafana/pkg/services/multildap"
"github.com/grafana/grafana/pkg/setting"
. "github.com/smartystreets/goconvey/convey"
)
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)
getLDAPConfig = func() (*ldap.Config, error) {
config := &ldap.Config{
Servers: []*ldap.ServerConfig{},
}
return config, nil
}
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, errTest)
})
Convey("it should not call ldap login", func() {
So(sc.LDAPAuthenticatorMock.loginCalled, ShouldBeTrue)
})
})
})
Convey("Given ldap disabled", func() {
setting.LDAPEnabled = false
LDAPLoginScenario("When login", func(sc *LDAPLoginScenarioContext) {
sc.withLoginResult(false)
enabled, err := loginUsingLDAP(&models.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)
})
})
})
})
}
type mockAuth struct {
validLogin bool
loginCalled bool
pingCalled bool
}
func (auth *mockAuth) Ping() ([]*multildap.ServerStatus, error) {
auth.pingCalled = true
return nil, nil
}
func (auth *mockAuth) Login(query *models.LoginUserQuery) (
*models.ExternalUserInfo,
error,
) {
auth.loginCalled = true
if !auth.validLogin {
return nil, errTest
}
return nil, nil
}
func (auth *mockAuth) Users(logins []string) (
[]*models.ExternalUserInfo,
error,
) {
return nil, nil
}
func (auth *mockAuth) User(login string) (
*models.ExternalUserInfo,
ldap.ServerConfig,
error,
) {
return nil, ldap.ServerConfig{}, nil
}
func (auth *mockAuth) Add(dn string, values map[string][]string) error {
return nil
}
func (auth *mockAuth) Remove(dn string) error {
return nil
}
func mockLDAPAuthenticator(valid bool) *mockAuth {
mock := &mockAuth{
validLogin: valid,
}
newLDAP = func(servers []*ldap.ServerConfig) multildap.IMultiLDAP {
return mock
}
return mock
}
type LDAPLoginScenarioContext struct {
loginUserQuery *models.LoginUserQuery
LDAPAuthenticatorMock *mockAuth
}
type LDAPLoginScenarioFunc func(c *LDAPLoginScenarioContext)
func LDAPLoginScenario(desc string, fn LDAPLoginScenarioFunc) {
Convey(desc, func() {
mock := &mockAuth{}
sc := &LDAPLoginScenarioContext{
loginUserQuery: &models.LoginUserQuery{
Username: "user",
Password: "pwd",
IpAddress: "192.168.1.1:56433",
},
LDAPAuthenticatorMock: mock,
}
getLDAPConfig = func() (*ldap.Config, error) {
config := &ldap.Config{
Servers: []*ldap.ServerConfig{
{
Host: "",
},
},
}
return config, nil
}
newLDAP = func(server []*ldap.ServerConfig) multildap.IMultiLDAP {
return mock
}
defer func() {
newLDAP = multildap.New
getLDAPConfig = multildap.GetConfig
}()
fn(sc)
})
}
func (sc *LDAPLoginScenarioContext) withLoginResult(valid bool) {
sc.LDAPAuthenticatorMock = mockLDAPAuthenticator(valid)
}