auth_proxy_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package authproxy
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "github.com/grafana/grafana/pkg/infra/remotecache"
  7. "github.com/grafana/grafana/pkg/login"
  8. models "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/setting"
  10. . "github.com/smartystreets/goconvey/convey"
  11. "gopkg.in/macaron.v1"
  12. )
  13. type TestLDAP struct {
  14. login.ILdapAuther
  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. login.LdapCfg = login.LdapConfig{
  54. Servers: []*login.LdapServerConf{
  55. {},
  56. },
  57. }
  58. setting.LdapEnabled = true
  59. store := remotecache.NewFakeStore(t)
  60. auth := New(&Options{
  61. Store: store,
  62. Ctx: ctx,
  63. OrgID: 4,
  64. })
  65. stub := &TestLDAP{
  66. ID: 42,
  67. }
  68. auth.LDAP = func(server *login.LdapServerConf) login.ILdapAuther {
  69. return stub
  70. }
  71. id, err := auth.GetUserID()
  72. So(err, ShouldBeNil)
  73. So(id, ShouldEqual, 42)
  74. So(stub.syncCalled, ShouldEqual, true)
  75. })
  76. Convey("gets nice error if ldap is enabled but not configured", func() {
  77. setting.LdapEnabled = false
  78. store := remotecache.NewFakeStore(t)
  79. auth := New(&Options{
  80. Store: store,
  81. Ctx: ctx,
  82. OrgID: 4,
  83. })
  84. stub := &TestLDAP{
  85. ID: 42,
  86. }
  87. auth.LDAP = func(server *login.LdapServerConf) login.ILdapAuther {
  88. return stub
  89. }
  90. id, err := auth.GetUserID()
  91. So(err, ShouldNotBeNil)
  92. So(id, ShouldNotEqual, 42)
  93. So(stub.syncCalled, ShouldEqual, false)
  94. })
  95. })
  96. })
  97. }