auth_proxy_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package authproxy
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. . "github.com/smartystreets/goconvey/convey"
  7. "gopkg.in/macaron.v1"
  8. "github.com/grafana/grafana/pkg/infra/remotecache"
  9. "github.com/grafana/grafana/pkg/models"
  10. "github.com/grafana/grafana/pkg/services/ldap"
  11. "github.com/grafana/grafana/pkg/setting"
  12. )
  13. type TestLDAP struct {
  14. ldap.Auth
  15. ID int64
  16. syncCalled bool
  17. }
  18. func (stub *TestLDAP) SyncUser(query *models.LoginUserQuery) error {
  19. stub.syncCalled = true
  20. query.User = &models.User{
  21. Id: stub.ID,
  22. }
  23. return nil
  24. }
  25. func TestMiddlewareContext(t *testing.T) {
  26. Convey("auth_proxy helper", t, func() {
  27. req, _ := http.NewRequest("POST", "http://example.com", nil)
  28. setting.AuthProxyHeaderName = "X-Killa"
  29. name := "markelog"
  30. req.Header.Add(setting.AuthProxyHeaderName, name)
  31. ctx := &models.ReqContext{
  32. Context: &macaron.Context{
  33. Req: macaron.Request{
  34. Request: req,
  35. },
  36. },
  37. }
  38. Convey("gets data from the cache", func() {
  39. store := remotecache.NewFakeStore(t)
  40. key := fmt.Sprintf(CachePrefix, name)
  41. store.Set(key, int64(33), 0)
  42. auth := New(&Options{
  43. Store: store,
  44. Ctx: ctx,
  45. OrgID: 4,
  46. })
  47. id, err := auth.GetUserID()
  48. So(err, ShouldBeNil)
  49. So(id, ShouldEqual, 33)
  50. })
  51. Convey("LDAP", func() {
  52. Convey("gets data from the LDAP", func() {
  53. isLDAPEnabled = func() bool {
  54. return true
  55. }
  56. readLDAPConfig = func() *ldap.Config {
  57. config := &ldap.Config{
  58. Servers: []*ldap.ServerConfig{
  59. {},
  60. },
  61. }
  62. return config
  63. }
  64. defer func() {
  65. isLDAPEnabled = ldap.IsEnabled
  66. readLDAPConfig = ldap.ReadConfig
  67. }()
  68. store := remotecache.NewFakeStore(t)
  69. auth := New(&Options{
  70. Store: store,
  71. Ctx: ctx,
  72. OrgID: 4,
  73. })
  74. stub := &TestLDAP{
  75. ID: 42,
  76. }
  77. auth.LDAP = func(server *ldap.ServerConfig) ldap.IAuth {
  78. return stub
  79. }
  80. id, err := auth.GetUserID()
  81. So(err, ShouldBeNil)
  82. So(id, ShouldEqual, 42)
  83. So(stub.syncCalled, ShouldEqual, true)
  84. })
  85. Convey("gets nice error if ldap is enabled but not configured", func() {
  86. isLDAPEnabled = func() bool {
  87. return true
  88. }
  89. readLDAPConfig = func() *ldap.Config {
  90. config := &ldap.Config{
  91. Servers: []*ldap.ServerConfig{},
  92. }
  93. return config
  94. }
  95. defer func() {
  96. isLDAPEnabled = ldap.IsEnabled
  97. readLDAPConfig = ldap.ReadConfig
  98. }()
  99. store := remotecache.NewFakeStore(t)
  100. auth := New(&Options{
  101. Store: store,
  102. Ctx: ctx,
  103. OrgID: 4,
  104. })
  105. stub := &TestLDAP{
  106. ID: 42,
  107. }
  108. auth.LDAP = func(server *ldap.ServerConfig) ldap.IAuth {
  109. return stub
  110. }
  111. id, err := auth.GetUserID()
  112. So(err, ShouldNotBeNil)
  113. So(err.Error(), ShouldContainSubstring, "Failed to sync user")
  114. So(id, ShouldNotEqual, 42)
  115. So(stub.syncCalled, ShouldEqual, false)
  116. })
  117. })
  118. })
  119. }