auth_proxy_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package middleware
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/grafana/grafana/pkg/login"
  6. m "github.com/grafana/grafana/pkg/models"
  7. "github.com/grafana/grafana/pkg/services/session"
  8. "github.com/grafana/grafana/pkg/setting"
  9. . "github.com/smartystreets/goconvey/convey"
  10. "gopkg.in/macaron.v1"
  11. )
  12. func TestAuthProxyWithLdapEnabled(t *testing.T) {
  13. Convey("When calling sync grafana user with ldap user", t, func() {
  14. setting.LdapEnabled = true
  15. setting.AuthProxyLdapSyncTtl = 60
  16. servers := []*login.LdapServerConf{{Host: "127.0.0.1"}}
  17. login.LdapCfg = login.LdapConfig{Servers: servers}
  18. mockLdapAuther := mockLdapAuthenticator{}
  19. login.NewLdapAuthenticator = func(server *login.LdapServerConf) login.ILdapAuther {
  20. return &mockLdapAuther
  21. }
  22. Convey("When user logs in, call SyncUser", func() {
  23. // arrange
  24. sess := newMockSession()
  25. ctx := m.ReqContext{Session: &sess}
  26. So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeNil)
  27. // act
  28. syncGrafanaUserWithLdapUser(&m.LoginUserQuery{
  29. ReqContext: &ctx,
  30. Username: "test",
  31. })
  32. // assert
  33. So(mockLdapAuther.syncUserCalled, ShouldBeTrue)
  34. So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeGreaterThan, 0)
  35. })
  36. Convey("When session variable not expired, don't sync and don't change session var", func() {
  37. // arrange
  38. sess := newMockSession()
  39. ctx := m.ReqContext{Session: &sess}
  40. now := time.Now().Unix()
  41. sess.Set(session.SESS_KEY_LASTLDAPSYNC, now)
  42. sess.Set(AUTH_PROXY_SESSION_VAR, "test")
  43. // act
  44. syncGrafanaUserWithLdapUser(&m.LoginUserQuery{
  45. ReqContext: &ctx,
  46. Username: "test",
  47. })
  48. // assert
  49. So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldEqual, now)
  50. So(mockLdapAuther.syncUserCalled, ShouldBeFalse)
  51. })
  52. Convey("When lastldapsync is expired, session variable should be updated", func() {
  53. // arrange
  54. sess := newMockSession()
  55. ctx := m.ReqContext{Session: &sess}
  56. expiredTime := time.Now().Add(time.Duration(-120) * time.Minute).Unix()
  57. sess.Set(session.SESS_KEY_LASTLDAPSYNC, expiredTime)
  58. sess.Set(AUTH_PROXY_SESSION_VAR, "test")
  59. // act
  60. syncGrafanaUserWithLdapUser(&m.LoginUserQuery{
  61. ReqContext: &ctx,
  62. Username: "test",
  63. })
  64. // assert
  65. So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeGreaterThan, expiredTime)
  66. So(mockLdapAuther.syncUserCalled, ShouldBeTrue)
  67. })
  68. })
  69. }
  70. type mockSession struct {
  71. value map[interface{}]interface{}
  72. }
  73. func newMockSession() mockSession {
  74. session := mockSession{}
  75. session.value = make(map[interface{}]interface{})
  76. return session
  77. }
  78. func (s *mockSession) Start(c *macaron.Context) error {
  79. return nil
  80. }
  81. func (s *mockSession) Set(k interface{}, v interface{}) error {
  82. s.value[k] = v
  83. return nil
  84. }
  85. func (s *mockSession) Get(k interface{}) interface{} {
  86. return s.value[k]
  87. }
  88. func (s *mockSession) Delete(k interface{}) interface{} {
  89. delete(s.value, k)
  90. return nil
  91. }
  92. func (s *mockSession) ID() string {
  93. return ""
  94. }
  95. func (s *mockSession) Release() error {
  96. return nil
  97. }
  98. func (s *mockSession) Destory(c *macaron.Context) error {
  99. return nil
  100. }
  101. func (s *mockSession) RegenerateId(c *macaron.Context) error {
  102. return nil
  103. }
  104. type mockLdapAuthenticator struct {
  105. syncUserCalled bool
  106. }
  107. func (a *mockLdapAuthenticator) Login(query *m.LoginUserQuery) error {
  108. return nil
  109. }
  110. func (a *mockLdapAuthenticator) SyncUser(query *m.LoginUserQuery) error {
  111. a.syncUserCalled = true
  112. return nil
  113. }
  114. func (a *mockLdapAuthenticator) GetGrafanaUserFor(ctx *m.ReqContext, ldapUser *login.LdapUserInfo) (*m.User, error) {
  115. return nil, nil
  116. }