auth_proxy_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/setting"
  8. . "github.com/smartystreets/goconvey/convey"
  9. )
  10. func TestAuthProxyWithLdapEnabled(t *testing.T) {
  11. Convey("When calling sync grafana user with ldap user", t, func() {
  12. setting.LdapEnabled = true
  13. setting.AuthProxyLdapSyncTtl = 60
  14. servers := []*login.LdapServerConf{{Host: "127.0.0.1"}}
  15. login.LdapCfg = login.LdapConfig{Servers: servers}
  16. mockLdapAuther := mockLdapAuthenticator{}
  17. login.NewLdapAuthenticator = func(server *login.LdapServerConf) login.ILdapAuther {
  18. return &mockLdapAuther
  19. }
  20. signedInUser := m.SignedInUser{}
  21. query := m.GetSignedInUserQuery{Result: &signedInUser}
  22. Convey("When session variable lastLdapSync not set, call syncSignedInUser and set lastLdapSync", func() {
  23. // arrange
  24. session := mockSession{}
  25. ctx := Context{Session: &session}
  26. So(session.Get(SESS_KEY_LASTLDAPSYNC), ShouldBeNil)
  27. // act
  28. syncGrafanaUserWithLdapUser(&ctx, &query)
  29. // assert
  30. So(mockLdapAuther.syncSignedInUserCalled, ShouldBeTrue)
  31. So(session.Get(SESS_KEY_LASTLDAPSYNC), ShouldBeGreaterThan, 0)
  32. })
  33. Convey("When session variable not expired, don't sync and don't change session var", func() {
  34. // arrange
  35. session := mockSession{}
  36. ctx := Context{Session: &session}
  37. now := time.Now().Unix()
  38. session.Set(SESS_KEY_LASTLDAPSYNC, now)
  39. // act
  40. syncGrafanaUserWithLdapUser(&ctx, &query)
  41. // assert
  42. So(session.Get(SESS_KEY_LASTLDAPSYNC), ShouldEqual, now)
  43. So(mockLdapAuther.syncSignedInUserCalled, ShouldBeFalse)
  44. })
  45. Convey("When lastldapsync is expired, session variable should be updated", func() {
  46. // arrange
  47. session := mockSession{}
  48. ctx := Context{Session: &session}
  49. expiredTime := time.Now().Add(time.Duration(-120) * time.Minute).Unix()
  50. session.Set(SESS_KEY_LASTLDAPSYNC, expiredTime)
  51. // act
  52. syncGrafanaUserWithLdapUser(&ctx, &query)
  53. // assert
  54. So(session.Get(SESS_KEY_LASTLDAPSYNC), ShouldBeGreaterThan, expiredTime)
  55. So(mockLdapAuther.syncSignedInUserCalled, ShouldBeTrue)
  56. })
  57. })
  58. }
  59. type mockSession struct {
  60. value interface{}
  61. }
  62. func (s *mockSession) Start(c *Context) error {
  63. return nil
  64. }
  65. func (s *mockSession) Set(k interface{}, v interface{}) error {
  66. s.value = v
  67. return nil
  68. }
  69. func (s *mockSession) Get(k interface{}) interface{} {
  70. return s.value
  71. }
  72. func (s *mockSession) Delete(k interface{}) interface{} {
  73. return nil
  74. }
  75. func (s *mockSession) ID() string {
  76. return ""
  77. }
  78. func (s *mockSession) Release() error {
  79. return nil
  80. }
  81. func (s *mockSession) Destory(c *Context) error {
  82. return nil
  83. }
  84. func (s *mockSession) RegenerateId(c *Context) error {
  85. return nil
  86. }
  87. type mockLdapAuthenticator struct {
  88. syncSignedInUserCalled bool
  89. }
  90. func (a *mockLdapAuthenticator) Login(query *login.LoginUserQuery) error {
  91. return nil
  92. }
  93. func (a *mockLdapAuthenticator) SyncSignedInUser(signedInUser *m.SignedInUser) error {
  94. a.syncSignedInUserCalled = true
  95. return nil
  96. }
  97. func (a *mockLdapAuthenticator) GetGrafanaUserFor(ldapUser *login.LdapUserInfo) (*m.User, error) {
  98. return nil, nil
  99. }
  100. func (a *mockLdapAuthenticator) SyncOrgRoles(user *m.User, ldapUser *login.LdapUserInfo) error {
  101. return nil
  102. }