You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth_ldap_test.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright 2018 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "net/http"
  7. "os"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "github.com/Unknwon/i18n"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. type ldapUser struct {
  15. UserName string
  16. Password string
  17. FullName string
  18. Email string
  19. OtherEmails []string
  20. IsAdmin bool
  21. SSHKeys []string
  22. }
  23. var gitLDAPUsers = []ldapUser{
  24. {
  25. UserName: "professor",
  26. Password: "professor",
  27. FullName: "Hubert Farnsworth",
  28. Email: "professor@planetexpress.com",
  29. OtherEmails: []string{"hubert@planetexpress.com"},
  30. IsAdmin: true,
  31. },
  32. {
  33. UserName: "hermes",
  34. Password: "hermes",
  35. FullName: "Conrad Hermes",
  36. Email: "hermes@planetexpress.com",
  37. IsAdmin: true,
  38. },
  39. {
  40. UserName: "fry",
  41. Password: "fry",
  42. FullName: "Philip Fry",
  43. Email: "fry@planetexpress.com",
  44. },
  45. {
  46. UserName: "leela",
  47. Password: "leela",
  48. FullName: "Leela Turanga",
  49. Email: "leela@planetexpress.com",
  50. },
  51. {
  52. UserName: "bender",
  53. Password: "bender",
  54. FullName: "Bender Rodríguez",
  55. Email: "bender@planetexpress.com",
  56. },
  57. }
  58. var otherLDAPUsers = []ldapUser{
  59. {
  60. UserName: "zoidberg",
  61. Password: "zoidberg",
  62. FullName: "John Zoidberg",
  63. Email: "zoidberg@planetexpress.com",
  64. },
  65. {
  66. UserName: "amy",
  67. Password: "amy",
  68. FullName: "Amy Kroker",
  69. Email: "amy@planetexpress.com",
  70. },
  71. }
  72. func skipLDAPTests() bool {
  73. return os.Getenv("TEST_LDAP") != "1"
  74. }
  75. func getLDAPServerHost() string {
  76. host := os.Getenv("TEST_LDAP_HOST")
  77. if len(host) == 0 {
  78. host = "ldap"
  79. }
  80. return host
  81. }
  82. func addAuthSourceLDAP(t *testing.T) {
  83. session := loginUser(t, "user1")
  84. csrf := GetCSRF(t, session, "/admin/auths/new")
  85. req := NewRequestWithValues(t, "POST", "/admin/auths/new", map[string]string{
  86. "_csrf": csrf,
  87. "type": "2",
  88. "name": "ldap",
  89. "host": getLDAPServerHost(),
  90. "port": "389",
  91. "bind_dn": "uid=gitea,ou=service,dc=planetexpress,dc=com",
  92. "bind_password": "password",
  93. "user_base": "ou=people,dc=planetexpress,dc=com",
  94. "filter": "(&(objectClass=inetOrgPerson)(memberOf=cn=git,ou=people,dc=planetexpress,dc=com)(uid=%s))",
  95. "admin_filter": "(memberOf=cn=admin_staff,ou=people,dc=planetexpress,dc=com)",
  96. "attribute_username": "uid",
  97. "attribute_name": "givenName",
  98. "attribute_surname": "sn",
  99. "attribute_mail": "mail",
  100. "is_sync_enabled": "on",
  101. "is_active": "on",
  102. })
  103. session.MakeRequest(t, req, http.StatusFound)
  104. }
  105. func TestLDAPUserSignin(t *testing.T) {
  106. if skipLDAPTests() {
  107. t.Skip()
  108. return
  109. }
  110. prepareTestEnv(t)
  111. addAuthSourceLDAP(t)
  112. u := gitLDAPUsers[0]
  113. session := loginUserWithPassword(t, u.UserName, u.Password)
  114. req := NewRequest(t, "GET", "/user/settings")
  115. resp := session.MakeRequest(t, req, http.StatusOK)
  116. htmlDoc := NewHTMLParser(t, resp.Body)
  117. assert.Equal(t, u.UserName, htmlDoc.GetInputValueByName("name"))
  118. assert.Equal(t, u.FullName, htmlDoc.GetInputValueByName("full_name"))
  119. assert.Equal(t, u.Email, htmlDoc.GetInputValueByName("email"))
  120. }
  121. func TestLDAPUserSync(t *testing.T) {
  122. if skipLDAPTests() {
  123. t.Skip()
  124. return
  125. }
  126. prepareTestEnv(t)
  127. addAuthSourceLDAP(t)
  128. models.SyncExternalUsers()
  129. session := loginUser(t, "user1")
  130. // Check if users exists
  131. for _, u := range gitLDAPUsers {
  132. req := NewRequest(t, "GET", "/admin/users?q="+u.UserName)
  133. resp := session.MakeRequest(t, req, http.StatusOK)
  134. htmlDoc := NewHTMLParser(t, resp.Body)
  135. tr := htmlDoc.doc.Find("table.table tbody tr")
  136. if !assert.True(t, tr.Length() == 1) {
  137. continue
  138. }
  139. tds := tr.Find("td")
  140. if !assert.True(t, tds.Length() > 0) {
  141. continue
  142. }
  143. assert.Equal(t, u.UserName, strings.TrimSpace(tds.Find("td:nth-child(2) a").Text()))
  144. assert.Equal(t, u.Email, strings.TrimSpace(tds.Find("td:nth-child(3) span").Text()))
  145. if u.IsAdmin {
  146. assert.True(t, tds.Find("td:nth-child(5) i").HasClass("fa-check-square-o"))
  147. } else {
  148. assert.True(t, tds.Find("td:nth-child(5) i").HasClass("fa-square-o"))
  149. }
  150. }
  151. // Check if no users exist
  152. for _, u := range otherLDAPUsers {
  153. req := NewRequest(t, "GET", "/admin/users?q="+u.UserName)
  154. resp := session.MakeRequest(t, req, http.StatusOK)
  155. htmlDoc := NewHTMLParser(t, resp.Body)
  156. tr := htmlDoc.doc.Find("table.table tbody tr")
  157. assert.True(t, tr.Length() == 0)
  158. }
  159. }
  160. func TestLDAPUserSigninFailed(t *testing.T) {
  161. if skipLDAPTests() {
  162. t.Skip()
  163. return
  164. }
  165. prepareTestEnv(t)
  166. addAuthSourceLDAP(t)
  167. u := otherLDAPUsers[0]
  168. testLoginFailed(t, u.UserName, u.Password, i18n.Tr("en", "form.username_password_incorrect"))
  169. }