auth_proxy_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package authproxy
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "testing"
  7. . "github.com/smartystreets/goconvey/convey"
  8. "gopkg.in/macaron.v1"
  9. "github.com/grafana/grafana/pkg/bus"
  10. "github.com/grafana/grafana/pkg/infra/remotecache"
  11. "github.com/grafana/grafana/pkg/models"
  12. "github.com/grafana/grafana/pkg/services/ldap"
  13. "github.com/grafana/grafana/pkg/services/multildap"
  14. "github.com/grafana/grafana/pkg/setting"
  15. )
  16. type TestMultiLDAP struct {
  17. multildap.MultiLDAP
  18. ID int64
  19. userCalled bool
  20. loginCalled bool
  21. }
  22. func (stub *TestMultiLDAP) Login(query *models.LoginUserQuery) (
  23. *models.ExternalUserInfo, error,
  24. ) {
  25. stub.loginCalled = true
  26. result := &models.ExternalUserInfo{
  27. UserId: stub.ID,
  28. }
  29. return result, nil
  30. }
  31. func (stub *TestMultiLDAP) User(login string) (
  32. *models.ExternalUserInfo,
  33. error,
  34. ) {
  35. stub.userCalled = true
  36. result := &models.ExternalUserInfo{
  37. UserId: stub.ID,
  38. }
  39. return result, nil
  40. }
  41. func TestMiddlewareContext(t *testing.T) {
  42. Convey("auth_proxy helper", t, func() {
  43. req, _ := http.NewRequest("POST", "http://example.com", nil)
  44. setting.AuthProxyHeaderName = "X-Killa"
  45. name := "markelog"
  46. req.Header.Add(setting.AuthProxyHeaderName, name)
  47. ctx := &models.ReqContext{
  48. Context: &macaron.Context{
  49. Req: macaron.Request{
  50. Request: req,
  51. },
  52. },
  53. }
  54. Convey("logs in user from the cache", func() {
  55. store := remotecache.NewFakeStore(t)
  56. key := fmt.Sprintf(CachePrefix, name)
  57. store.Set(key, int64(33), 0)
  58. auth := New(&Options{
  59. Store: store,
  60. Ctx: ctx,
  61. OrgID: 4,
  62. })
  63. id, err := auth.Login()
  64. So(err, ShouldBeNil)
  65. So(id, ShouldEqual, 33)
  66. })
  67. Convey("LDAP", func() {
  68. Convey("logs in via LDAP", func() {
  69. bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
  70. cmd.Result = &models.User{
  71. Id: 42,
  72. }
  73. return nil
  74. })
  75. isLDAPEnabled = func() bool {
  76. return true
  77. }
  78. stub := &TestMultiLDAP{
  79. ID: 42,
  80. }
  81. getLDAPConfig = func() (*ldap.Config, error) {
  82. config := &ldap.Config{
  83. Servers: []*ldap.ServerConfig{
  84. {
  85. SearchBaseDNs: []string{"BaseDNHere"},
  86. },
  87. },
  88. }
  89. return config, nil
  90. }
  91. newLDAP = func(servers []*ldap.ServerConfig) multildap.IMultiLDAP {
  92. return stub
  93. }
  94. defer func() {
  95. newLDAP = multildap.New
  96. isLDAPEnabled = ldap.IsEnabled
  97. getLDAPConfig = ldap.GetConfig
  98. }()
  99. store := remotecache.NewFakeStore(t)
  100. server := New(&Options{
  101. Store: store,
  102. Ctx: ctx,
  103. OrgID: 4,
  104. })
  105. id, err := server.Login()
  106. So(err, ShouldBeNil)
  107. So(id, ShouldEqual, 42)
  108. So(stub.userCalled, ShouldEqual, true)
  109. })
  110. Convey("gets nice error if ldap is enabled but not configured", func() {
  111. isLDAPEnabled = func() bool {
  112. return true
  113. }
  114. getLDAPConfig = func() (*ldap.Config, error) {
  115. return nil, errors.New("Something went wrong")
  116. }
  117. defer func() {
  118. newLDAP = multildap.New
  119. isLDAPEnabled = ldap.IsEnabled
  120. getLDAPConfig = ldap.GetConfig
  121. }()
  122. store := remotecache.NewFakeStore(t)
  123. auth := New(&Options{
  124. Store: store,
  125. Ctx: ctx,
  126. OrgID: 4,
  127. })
  128. stub := &TestMultiLDAP{
  129. ID: 42,
  130. }
  131. newLDAP = func(servers []*ldap.ServerConfig) multildap.IMultiLDAP {
  132. return stub
  133. }
  134. id, err := auth.Login()
  135. So(err, ShouldNotBeNil)
  136. So(err.Error(), ShouldContainSubstring, "Failed to get the user")
  137. So(id, ShouldNotEqual, 42)
  138. So(stub.loginCalled, ShouldEqual, false)
  139. })
  140. })
  141. })
  142. }