ldap_helpers_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package ldap
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "gopkg.in/ldap.v3"
  6. )
  7. func TestLDAPHelpers(t *testing.T) {
  8. Convey("isMemberOf()", t, func() {
  9. Convey("Wildcard", func() {
  10. result := isMemberOf([]string{}, "*")
  11. So(result, ShouldBeTrue)
  12. })
  13. Convey("Should find one", func() {
  14. result := isMemberOf([]string{"one", "Two", "three"}, "two")
  15. So(result, ShouldBeTrue)
  16. })
  17. Convey("Should not find one", func() {
  18. result := isMemberOf([]string{"one", "Two", "three"}, "twos")
  19. So(result, ShouldBeFalse)
  20. })
  21. })
  22. Convey("getUsersIteration()", t, func() {
  23. Convey("it should execute twice for 600 users", func() {
  24. logins := make([]string, 600)
  25. i := 0
  26. result := getUsersIteration(logins, func(previous, current int) error {
  27. i++
  28. if i == 1 {
  29. So(previous, ShouldEqual, 0)
  30. So(current, ShouldEqual, 500)
  31. } else {
  32. So(previous, ShouldEqual, 500)
  33. So(current, ShouldEqual, 600)
  34. }
  35. return nil
  36. })
  37. So(i, ShouldEqual, 2)
  38. So(result, ShouldBeNil)
  39. })
  40. Convey("it should execute three times for 1500 users", func() {
  41. logins := make([]string, 1500)
  42. i := 0
  43. result := getUsersIteration(logins, func(previous, current int) error {
  44. i++
  45. if i == 1 {
  46. So(previous, ShouldEqual, 0)
  47. So(current, ShouldEqual, 500)
  48. } else if i == 2 {
  49. So(previous, ShouldEqual, 500)
  50. So(current, ShouldEqual, 1000)
  51. } else {
  52. So(previous, ShouldEqual, 1000)
  53. So(current, ShouldEqual, 1500)
  54. }
  55. return nil
  56. })
  57. So(i, ShouldEqual, 3)
  58. So(result, ShouldBeNil)
  59. })
  60. Convey("it should execute once for 400 users", func() {
  61. logins := make([]string, 400)
  62. i := 0
  63. result := getUsersIteration(logins, func(previous, current int) error {
  64. i++
  65. if i == 1 {
  66. So(previous, ShouldEqual, 0)
  67. So(current, ShouldEqual, 400)
  68. }
  69. return nil
  70. })
  71. So(i, ShouldEqual, 1)
  72. So(result, ShouldBeNil)
  73. })
  74. Convey("it should not execute for 0 users", func() {
  75. logins := make([]string, 0)
  76. i := 0
  77. result := getUsersIteration(logins, func(previous, current int) error {
  78. i++
  79. return nil
  80. })
  81. So(i, ShouldEqual, 0)
  82. So(result, ShouldBeNil)
  83. })
  84. })
  85. Convey("getAttribute()", t, func() {
  86. Convey("Should get DN", func() {
  87. entry := &ldap.Entry{
  88. DN: "test",
  89. }
  90. result := getAttribute("dn", entry)
  91. So(result, ShouldEqual, "test")
  92. })
  93. Convey("Should get username", func() {
  94. value := []string{"roelgerrits"}
  95. entry := &ldap.Entry{
  96. Attributes: []*ldap.EntryAttribute{
  97. {
  98. Name: "username", Values: value,
  99. },
  100. },
  101. }
  102. result := getAttribute("username", entry)
  103. So(result, ShouldEqual, value[0])
  104. })
  105. Convey("Should not get anything", func() {
  106. value := []string{"roelgerrits"}
  107. entry := &ldap.Entry{
  108. Attributes: []*ldap.EntryAttribute{
  109. {
  110. Name: "killa", Values: value,
  111. },
  112. },
  113. }
  114. result := getAttribute("username", entry)
  115. So(result, ShouldEqual, "")
  116. })
  117. })
  118. Convey("getArrayAttribute()", t, func() {
  119. Convey("Should get DN", func() {
  120. entry := &ldap.Entry{
  121. DN: "test",
  122. }
  123. result := getArrayAttribute("dn", entry)
  124. So(result, ShouldResemble, []string{"test"})
  125. })
  126. Convey("Should get username", func() {
  127. value := []string{"roelgerrits"}
  128. entry := &ldap.Entry{
  129. Attributes: []*ldap.EntryAttribute{
  130. {
  131. Name: "username", Values: value,
  132. },
  133. },
  134. }
  135. result := getArrayAttribute("username", entry)
  136. So(result, ShouldResemble, value)
  137. })
  138. Convey("Should not get anything", func() {
  139. value := []string{"roelgerrits"}
  140. entry := &ldap.Entry{
  141. Attributes: []*ldap.EntryAttribute{
  142. {
  143. Name: "username", Values: value,
  144. },
  145. },
  146. }
  147. result := getArrayAttribute("something", entry)
  148. So(result, ShouldResemble, []string{})
  149. })
  150. })
  151. }