ldap_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. LdapGroups: []*LdapGroupToOrgRole{{}},
  13. })
  14. _, err := ldapAuther.getGrafanaUserFor(&ldapUserInfo{})
  15. So(err, ShouldEqual, ErrInvalidCredentials)
  16. })
  17. var user1 = &m.User{}
  18. ldapAutherScenario("Given wildcard group match", func(sc *scenarioContext) {
  19. ldapAuther := NewLdapAuthenticator(&LdapServerConf{
  20. LdapGroups: []*LdapGroupToOrgRole{
  21. {GroupDN: "*", OrgRole: "Admin"},
  22. },
  23. })
  24. sc.userQueryReturns(user1)
  25. result, err := ldapAuther.getGrafanaUserFor(&ldapUserInfo{})
  26. So(err, ShouldBeNil)
  27. So(result, ShouldEqual, user1)
  28. })
  29. ldapAutherScenario("Given exact group match", func(sc *scenarioContext) {
  30. ldapAuther := NewLdapAuthenticator(&LdapServerConf{
  31. LdapGroups: []*LdapGroupToOrgRole{
  32. {GroupDN: "cn=users", OrgRole: "Admin"},
  33. },
  34. })
  35. sc.userQueryReturns(user1)
  36. result, err := ldapAuther.getGrafanaUserFor(&ldapUserInfo{MemberOf: []string{"cn=users"}})
  37. So(err, ShouldBeNil)
  38. So(result, ShouldEqual, user1)
  39. })
  40. ldapAutherScenario("Given no existing grafana user", func(sc *scenarioContext) {
  41. ldapAuther := NewLdapAuthenticator(&LdapServerConf{
  42. LdapGroups: []*LdapGroupToOrgRole{
  43. {GroupDN: "cn=users", OrgRole: "Admin"},
  44. },
  45. })
  46. sc.userQueryReturns(nil)
  47. result, err := ldapAuther.getGrafanaUserFor(&ldapUserInfo{
  48. Username: "torkelo",
  49. Email: "my@email.com",
  50. MemberOf: []string{"cn=users"},
  51. })
  52. So(err, ShouldBeNil)
  53. Convey("Should create new user", func() {
  54. So(sc.createUserCmd.Login, ShouldEqual, "torkelo")
  55. So(sc.createUserCmd.Email, ShouldEqual, "my@email.com")
  56. })
  57. Convey("Should return new user", func() {
  58. So(result.Login, ShouldEqual, "torkelo")
  59. })
  60. })
  61. })
  62. Convey("When syncing ldap groups to grafana org roles", t, func() {
  63. ldapAutherScenario("given no current user orgs", func(sc *scenarioContext) {
  64. ldapAuther := NewLdapAuthenticator(&LdapServerConf{
  65. LdapGroups: []*LdapGroupToOrgRole{
  66. {GroupDN: "cn=users", OrgRole: "Admin"},
  67. },
  68. })
  69. sc.userOrgsQueryReturns([]*m.UserOrgDTO{})
  70. err := ldapAuther.syncOrgRoles(&m.User{}, &ldapUserInfo{
  71. MemberOf: []string{"cn=users"},
  72. })
  73. Convey("Should create new org user", func() {
  74. So(err, ShouldBeNil)
  75. So(sc.addOrgUserCommand, ShouldNotBeNil)
  76. So(sc.addOrgUserCommand.Role, ShouldEqual, m.ROLE_ADMIN)
  77. })
  78. })
  79. })
  80. }
  81. func ldapAutherScenario(desc string, fn scenarioFunc) {
  82. Convey(desc, func() {
  83. defer bus.ClearBusHandlers()
  84. sc := &scenarioContext{}
  85. bus.AddHandler("test", func(cmd *m.CreateUserCommand) error {
  86. sc.createUserCmd = cmd
  87. sc.createUserCmd.Result = m.User{Login: cmd.Login}
  88. return nil
  89. })
  90. bus.AddHandler("test", func(cmd *m.AddOrgUserCommand) error {
  91. sc.addOrgUserCommand = cmd
  92. return nil
  93. })
  94. fn(sc)
  95. })
  96. }
  97. type scenarioContext struct {
  98. createUserCmd *m.CreateUserCommand
  99. addOrgUserCommand *m.AddOrgUserCommand
  100. }
  101. func (sc *scenarioContext) userQueryReturns(user *m.User) {
  102. bus.AddHandler("test", func(query *m.GetUserByLoginQuery) error {
  103. if user == nil {
  104. return m.ErrUserNotFound
  105. } else {
  106. query.Result = user
  107. return nil
  108. }
  109. })
  110. }
  111. func (sc *scenarioContext) userOrgsQueryReturns(orgs []*m.UserOrgDTO) {
  112. bus.AddHandler("test", func(query *m.GetUserOrgListQuery) error {
  113. query.Result = orgs
  114. return nil
  115. })
  116. }
  117. type scenarioFunc func(c *scenarioContext)