2016-02-23 07:22:28 -06:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/login"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2018-03-06 16:59:45 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/session"
|
2016-02-23 07:22:28 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2018-03-06 16:59:45 -06:00
|
|
|
"gopkg.in/macaron.v1"
|
2016-02-23 07:22:28 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuthProxyWithLdapEnabled(t *testing.T) {
|
|
|
|
Convey("When calling sync grafana user with ldap user", t, func() {
|
|
|
|
|
|
|
|
setting.LdapEnabled = true
|
|
|
|
setting.AuthProxyLdapSyncTtl = 60
|
|
|
|
|
|
|
|
servers := []*login.LdapServerConf{{Host: "127.0.0.1"}}
|
2016-12-12 02:52:56 -06:00
|
|
|
login.LdapCfg = login.LdapConfig{Servers: servers}
|
2016-02-23 07:22:28 -06:00
|
|
|
mockLdapAuther := mockLdapAuthenticator{}
|
|
|
|
|
|
|
|
login.NewLdapAuthenticator = func(server *login.LdapServerConf) login.ILdapAuther {
|
|
|
|
return &mockLdapAuther
|
|
|
|
}
|
|
|
|
|
|
|
|
signedInUser := m.SignedInUser{}
|
|
|
|
query := m.GetSignedInUserQuery{Result: &signedInUser}
|
|
|
|
|
|
|
|
Convey("When session variable lastLdapSync not set, call syncSignedInUser and set lastLdapSync", func() {
|
|
|
|
// arrange
|
2018-03-06 16:59:45 -06:00
|
|
|
sess := mockSession{}
|
2018-03-07 10:54:50 -06:00
|
|
|
ctx := m.ReqContext{Session: &sess}
|
2018-03-06 16:59:45 -06:00
|
|
|
So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeNil)
|
2016-02-23 07:22:28 -06:00
|
|
|
|
|
|
|
// act
|
|
|
|
syncGrafanaUserWithLdapUser(&ctx, &query)
|
|
|
|
|
|
|
|
// assert
|
|
|
|
So(mockLdapAuther.syncSignedInUserCalled, ShouldBeTrue)
|
2018-03-06 16:59:45 -06:00
|
|
|
So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeGreaterThan, 0)
|
2016-02-23 07:22:28 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When session variable not expired, don't sync and don't change session var", func() {
|
|
|
|
// arrange
|
2018-03-06 16:59:45 -06:00
|
|
|
sess := mockSession{}
|
2018-03-07 10:54:50 -06:00
|
|
|
ctx := m.ReqContext{Session: &sess}
|
2016-02-23 07:22:28 -06:00
|
|
|
now := time.Now().Unix()
|
2018-03-06 16:59:45 -06:00
|
|
|
sess.Set(session.SESS_KEY_LASTLDAPSYNC, now)
|
2016-02-23 07:22:28 -06:00
|
|
|
|
|
|
|
// act
|
|
|
|
syncGrafanaUserWithLdapUser(&ctx, &query)
|
|
|
|
|
|
|
|
// assert
|
2018-03-06 16:59:45 -06:00
|
|
|
So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldEqual, now)
|
2016-02-23 07:22:28 -06:00
|
|
|
So(mockLdapAuther.syncSignedInUserCalled, ShouldBeFalse)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("When lastldapsync is expired, session variable should be updated", func() {
|
|
|
|
// arrange
|
2018-03-06 16:59:45 -06:00
|
|
|
sess := mockSession{}
|
2018-03-07 10:54:50 -06:00
|
|
|
ctx := m.ReqContext{Session: &sess}
|
2016-02-23 07:22:28 -06:00
|
|
|
expiredTime := time.Now().Add(time.Duration(-120) * time.Minute).Unix()
|
2018-03-06 16:59:45 -06:00
|
|
|
sess.Set(session.SESS_KEY_LASTLDAPSYNC, expiredTime)
|
2016-02-23 07:22:28 -06:00
|
|
|
|
|
|
|
// act
|
|
|
|
syncGrafanaUserWithLdapUser(&ctx, &query)
|
|
|
|
|
|
|
|
// assert
|
2018-03-06 16:59:45 -06:00
|
|
|
So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeGreaterThan, expiredTime)
|
2016-02-23 07:22:28 -06:00
|
|
|
So(mockLdapAuther.syncSignedInUserCalled, ShouldBeTrue)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockSession struct {
|
|
|
|
value interface{}
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:59:45 -06:00
|
|
|
func (s *mockSession) Start(c *macaron.Context) error {
|
2016-02-23 07:22:28 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockSession) Set(k interface{}, v interface{}) error {
|
|
|
|
s.value = v
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockSession) Get(k interface{}) interface{} {
|
|
|
|
return s.value
|
|
|
|
}
|
|
|
|
|
2017-03-23 09:52:08 -05:00
|
|
|
func (s *mockSession) Delete(k interface{}) interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 07:22:28 -06:00
|
|
|
func (s *mockSession) ID() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockSession) Release() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:59:45 -06:00
|
|
|
func (s *mockSession) Destory(c *macaron.Context) error {
|
2016-02-23 07:22:28 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-06 16:59:45 -06:00
|
|
|
func (s *mockSession) RegenerateId(c *macaron.Context) error {
|
2017-08-07 03:20:46 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 07:22:28 -06:00
|
|
|
type mockLdapAuthenticator struct {
|
|
|
|
syncSignedInUserCalled bool
|
|
|
|
}
|
|
|
|
|
2018-03-19 13:08:55 -05:00
|
|
|
func (a *mockLdapAuthenticator) Login(ctx *m.ReqContext, query *m.LoginUserQuery) error {
|
2016-02-23 07:22:28 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-19 13:08:55 -05:00
|
|
|
func (a *mockLdapAuthenticator) SyncSignedInUser(ctx *m.ReqContext, signedInUser *m.SignedInUser) error {
|
2016-02-23 07:22:28 -06:00
|
|
|
a.syncSignedInUserCalled = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-19 13:08:55 -05:00
|
|
|
func (a *mockLdapAuthenticator) GetGrafanaUserFor(ctx *m.ReqContext, ldapUser *login.LdapUserInfo) (*m.User, error) {
|
2016-02-23 07:22:28 -06:00
|
|
|
return nil, nil
|
|
|
|
}
|