ldap_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package auth
  2. import (
  3. "testing"
  4. "github.com/grafana/grafana/pkg/bus"
  5. m "github.com/grafana/grafana/pkg/models"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. func TestLdapAuther(t *testing.T) {
  9. Convey("When translating ldap user to grafana user", t, func() {
  10. Convey("Given no ldap group map match", func() {
  11. ldapAuther := NewLdapAuthenticator(&LdapServerConf{})
  12. _, err := ldapAuther.getGrafanaUserFor(&ldapUserInfo{})
  13. So(err, ShouldEqual, ErrInvalidCredentials)
  14. })
  15. var user1 = &m.User{}
  16. ldapAutherScenario("Given wildcard group match", func(sc *scenarioContext) {
  17. ldapAuther := NewLdapAuthenticator(&LdapServerConf{
  18. LdapGroups: []*LdapGroupToOrgRole{
  19. {GroupDN: "*", OrgRole: "Admin", OrgName: "Main"},
  20. },
  21. })
  22. sc.userQueryReturns(user1)
  23. result, err := ldapAuther.getGrafanaUserFor(&ldapUserInfo{})
  24. So(err, ShouldBeNil)
  25. So(result, ShouldEqual, user1)
  26. })
  27. ldapAutherScenario("Given exact group match", func(sc *scenarioContext) {
  28. ldapAuther := NewLdapAuthenticator(&LdapServerConf{
  29. LdapGroups: []*LdapGroupToOrgRole{
  30. {GroupDN: "cn=users", OrgRole: "Admin", OrgName: "Main"},
  31. },
  32. })
  33. sc.userQueryReturns(user1)
  34. result, err := ldapAuther.getGrafanaUserFor(&ldapUserInfo{MemberOf: []string{"cn=users"}})
  35. So(err, ShouldBeNil)
  36. So(result, ShouldEqual, user1)
  37. })
  38. ldapAutherScenario("Given no existing grafana user", func(sc *scenarioContext) {
  39. ldapAuther := NewLdapAuthenticator(&LdapServerConf{
  40. LdapGroups: []*LdapGroupToOrgRole{
  41. {GroupDN: "cn=users", OrgRole: "Admin", OrgName: "Main"},
  42. },
  43. })
  44. sc.userQueryReturns(nil)
  45. result, err := ldapAuther.getGrafanaUserFor(&ldapUserInfo{
  46. Username: "torkelo",
  47. Email: "my@email.com",
  48. MemberOf: []string{"cn=users"},
  49. })
  50. So(err, ShouldBeNil)
  51. Convey("Should create new user", func() {
  52. So(sc.createUserCmd.Login, ShouldEqual, "torkelo")
  53. So(sc.createUserCmd.Email, ShouldEqual, "my@email.com")
  54. })
  55. Convey("Should return new user", func() {
  56. So(result.Login, ShouldEqual, "torkelo")
  57. })
  58. })
  59. })
  60. }
  61. func ldapAutherScenario(desc string, fn scenarioFunc) {
  62. Convey(desc, func() {
  63. defer bus.ClearBusHandlers()
  64. sc := &scenarioContext{}
  65. bus.AddHandler("test", func(cmd *m.CreateUserCommand) error {
  66. sc.createUserCmd = cmd
  67. sc.createUserCmd.Result = m.User{Login: cmd.Login}
  68. return nil
  69. })
  70. fn(sc)
  71. })
  72. }
  73. type scenarioContext struct {
  74. createUserCmd *m.CreateUserCommand
  75. }
  76. func (sc *scenarioContext) userQueryReturns(user *m.User) {
  77. bus.AddHandler("test", func(query *m.GetUserByLoginQuery) error {
  78. if user == nil {
  79. return m.ErrUserNotFound
  80. } else {
  81. query.Result = user
  82. return nil
  83. }
  84. })
  85. }
  86. type scenarioFunc func(c *scenarioContext)