ldap_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package ldap
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. ldap "gopkg.in/ldap.v3"
  6. "github.com/grafana/grafana/pkg/infra/log"
  7. )
  8. func TestAuth(t *testing.T) {
  9. Convey("Add()", t, func() {
  10. connection := &mockConnection{}
  11. auth := &Server{
  12. config: &ServerConfig{
  13. SearchBaseDNs: []string{"BaseDNHere"},
  14. },
  15. connection: connection,
  16. log: log.New("test-logger"),
  17. }
  18. Convey("Adds user", func() {
  19. err := auth.Add(
  20. "cn=ldap-tuz,ou=users,dc=grafana,dc=org",
  21. map[string][]string{
  22. "mail": {"ldap-viewer@grafana.com"},
  23. "userPassword": {"grafana"},
  24. "objectClass": {
  25. "person",
  26. "top",
  27. "inetOrgPerson",
  28. "organizationalPerson",
  29. },
  30. "sn": {"ldap-tuz"},
  31. "cn": {"ldap-tuz"},
  32. },
  33. )
  34. hasMail := false
  35. hasUserPassword := false
  36. hasObjectClass := false
  37. hasSN := false
  38. hasCN := false
  39. So(err, ShouldBeNil)
  40. So(connection.addParams.Controls, ShouldBeNil)
  41. So(connection.addCalled, ShouldBeTrue)
  42. So(
  43. connection.addParams.DN,
  44. ShouldEqual,
  45. "cn=ldap-tuz,ou=users,dc=grafana,dc=org",
  46. )
  47. attrs := connection.addParams.Attributes
  48. for _, value := range attrs {
  49. if value.Type == "mail" {
  50. So(value.Vals, ShouldContain, "ldap-viewer@grafana.com")
  51. hasMail = true
  52. }
  53. if value.Type == "userPassword" {
  54. hasUserPassword = true
  55. So(value.Vals, ShouldContain, "grafana")
  56. }
  57. if value.Type == "objectClass" {
  58. hasObjectClass = true
  59. So(value.Vals, ShouldContain, "person")
  60. So(value.Vals, ShouldContain, "top")
  61. So(value.Vals, ShouldContain, "inetOrgPerson")
  62. So(value.Vals, ShouldContain, "organizationalPerson")
  63. }
  64. if value.Type == "sn" {
  65. hasSN = true
  66. So(value.Vals, ShouldContain, "ldap-tuz")
  67. }
  68. if value.Type == "cn" {
  69. hasCN = true
  70. So(value.Vals, ShouldContain, "ldap-tuz")
  71. }
  72. }
  73. So(hasMail, ShouldBeTrue)
  74. So(hasUserPassword, ShouldBeTrue)
  75. So(hasObjectClass, ShouldBeTrue)
  76. So(hasSN, ShouldBeTrue)
  77. So(hasCN, ShouldBeTrue)
  78. })
  79. })
  80. Convey("Remove()", t, func() {
  81. connection := &mockConnection{}
  82. auth := &Server{
  83. config: &ServerConfig{
  84. SearchBaseDNs: []string{"BaseDNHere"},
  85. },
  86. connection: connection,
  87. log: log.New("test-logger"),
  88. }
  89. Convey("Removes the user", func() {
  90. dn := "cn=ldap-tuz,ou=users,dc=grafana,dc=org"
  91. err := auth.Remove(dn)
  92. So(err, ShouldBeNil)
  93. So(connection.delCalled, ShouldBeTrue)
  94. So(connection.delParams.Controls, ShouldBeNil)
  95. So(connection.delParams.DN, ShouldEqual, dn)
  96. })
  97. })
  98. Convey("Users()", t, func() {
  99. Convey("find one user", func() {
  100. mockConnection := &mockConnection{}
  101. entry := ldap.Entry{
  102. DN: "dn", Attributes: []*ldap.EntryAttribute{
  103. {Name: "username", Values: []string{"roelgerrits"}},
  104. {Name: "surname", Values: []string{"Gerrits"}},
  105. {Name: "email", Values: []string{"roel@test.com"}},
  106. {Name: "name", Values: []string{"Roel"}},
  107. {Name: "memberof", Values: []string{"admins"}},
  108. }}
  109. result := ldap.SearchResult{Entries: []*ldap.Entry{&entry}}
  110. mockConnection.setSearchResult(&result)
  111. // Set up attribute map without surname and email
  112. server := &Server{
  113. config: &ServerConfig{
  114. Attr: AttributeMap{
  115. Username: "username",
  116. Name: "name",
  117. MemberOf: "memberof",
  118. },
  119. SearchBaseDNs: []string{"BaseDNHere"},
  120. },
  121. connection: mockConnection,
  122. log: log.New("test-logger"),
  123. }
  124. searchResult, err := server.Users([]string{"roelgerrits"})
  125. So(err, ShouldBeNil)
  126. So(searchResult, ShouldNotBeNil)
  127. // User should be searched in ldap
  128. So(mockConnection.searchCalled, ShouldBeTrue)
  129. // No empty attributes should be added to the search request
  130. So(len(mockConnection.searchAttributes), ShouldEqual, 3)
  131. })
  132. })
  133. }