auth_proxy_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. signedInUser := m.SignedInUser{}
  23. query := m.GetSignedInUserQuery{Result: &signedInUser}
  24. Convey("When session variable lastLdapSync not set, call syncSignedInUser and set lastLdapSync", func() {
  25. // arrange
  26. sess := mockSession{}
  27. ctx := m.ReqContext{Session: &sess}
  28. So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeNil)
  29. // act
  30. syncGrafanaUserWithLdapUser(&ctx, &query)
  31. // assert
  32. So(mockLdapAuther.syncSignedInUserCalled, ShouldBeTrue)
  33. So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeGreaterThan, 0)
  34. })
  35. Convey("When session variable not expired, don't sync and don't change session var", func() {
  36. // arrange
  37. sess := mockSession{}
  38. ctx := m.ReqContext{Session: &sess}
  39. now := time.Now().Unix()
  40. sess.Set(session.SESS_KEY_LASTLDAPSYNC, now)
  41. // act
  42. syncGrafanaUserWithLdapUser(&ctx, &query)
  43. // assert
  44. So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldEqual, now)
  45. So(mockLdapAuther.syncSignedInUserCalled, ShouldBeFalse)
  46. })
  47. Convey("When lastldapsync is expired, session variable should be updated", func() {
  48. // arrange
  49. sess := mockSession{}
  50. ctx := m.ReqContext{Session: &sess}
  51. expiredTime := time.Now().Add(time.Duration(-120) * time.Minute).Unix()
  52. sess.Set(session.SESS_KEY_LASTLDAPSYNC, expiredTime)
  53. // act
  54. syncGrafanaUserWithLdapUser(&ctx, &query)
  55. // assert
  56. So(sess.Get(session.SESS_KEY_LASTLDAPSYNC), ShouldBeGreaterThan, expiredTime)
  57. So(mockLdapAuther.syncSignedInUserCalled, ShouldBeTrue)
  58. })
  59. })
  60. }
  61. type mockSession struct {
  62. value interface{}
  63. }
  64. func (s *mockSession) Start(c *macaron.Context) error {
  65. return nil
  66. }
  67. func (s *mockSession) Set(k interface{}, v interface{}) error {
  68. s.value = v
  69. return nil
  70. }
  71. func (s *mockSession) Get(k interface{}) interface{} {
  72. return s.value
  73. }
  74. func (s *mockSession) Delete(k interface{}) interface{} {
  75. return nil
  76. }
  77. func (s *mockSession) ID() string {
  78. return ""
  79. }
  80. func (s *mockSession) Release() error {
  81. return nil
  82. }
  83. func (s *mockSession) Destory(c *macaron.Context) error {
  84. return nil
  85. }
  86. func (s *mockSession) RegenerateId(c *macaron.Context) error {
  87. return nil
  88. }
  89. type mockLdapAuthenticator struct {
  90. syncSignedInUserCalled bool
  91. }
  92. func (a *mockLdapAuthenticator) Login(query *login.LoginUserQuery) error {
  93. return nil
  94. }
  95. func (a *mockLdapAuthenticator) SyncSignedInUser(signedInUser *m.SignedInUser) error {
  96. a.syncSignedInUserCalled = true
  97. return nil
  98. }
  99. func (a *mockLdapAuthenticator) GetGrafanaUserFor(ldapUser *login.LdapUserInfo) (*m.User, error) {
  100. return nil, nil
  101. }
  102. func (a *mockLdapAuthenticator) SyncOrgRoles(user *m.User, ldapUser *login.LdapUserInfo) error {
  103. return nil
  104. }