Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

auth_ldap_test.go 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. "context"
  7. "net/http"
  8. "os"
  9. "strings"
  10. "testing"
  11. "code.gitea.io/gitea/models"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/unknwon/i18n"
  14. )
  15. type ldapUser struct {
  16. UserName string
  17. Password string
  18. FullName string
  19. Email string
  20. OtherEmails []string
  21. IsAdmin bool
  22. SSHKeys []string
  23. }
  24. var gitLDAPUsers = []ldapUser{
  25. {
  26. UserName: "professor",
  27. Password: "professor",
  28. FullName: "Hubert Farnsworth",
  29. Email: "professor@planetexpress.com",
  30. OtherEmails: []string{"hubert@planetexpress.com"},
  31. IsAdmin: true,
  32. },
  33. {
  34. UserName: "hermes",
  35. Password: "hermes",
  36. FullName: "Conrad Hermes",
  37. Email: "hermes@planetexpress.com",
  38. SSHKeys: []string{
  39. "SHA256:qLY06smKfHoW/92yXySpnxFR10QFrLdRjf/GNPvwcW8",
  40. "SHA256:QlVTuM5OssDatqidn2ffY+Lc4YA5Fs78U+0KOHI51jQ",
  41. "SHA256:DXdeUKYOJCSSmClZuwrb60hUq7367j4fA+udNC3FdRI",
  42. },
  43. IsAdmin: true,
  44. },
  45. {
  46. UserName: "fry",
  47. Password: "fry",
  48. FullName: "Philip Fry",
  49. Email: "fry@planetexpress.com",
  50. },
  51. {
  52. UserName: "leela",
  53. Password: "leela",
  54. FullName: "Leela Turanga",
  55. Email: "leela@planetexpress.com",
  56. },
  57. {
  58. UserName: "bender",
  59. Password: "bender",
  60. FullName: "Bender Rodríguez",
  61. Email: "bender@planetexpress.com",
  62. },
  63. }
  64. var otherLDAPUsers = []ldapUser{
  65. {
  66. UserName: "zoidberg",
  67. Password: "zoidberg",
  68. FullName: "John Zoidberg",
  69. Email: "zoidberg@planetexpress.com",
  70. },
  71. {
  72. UserName: "amy",
  73. Password: "amy",
  74. FullName: "Amy Kroker",
  75. Email: "amy@planetexpress.com",
  76. },
  77. }
  78. func skipLDAPTests() bool {
  79. return os.Getenv("TEST_LDAP") != "1"
  80. }
  81. func getLDAPServerHost() string {
  82. host := os.Getenv("TEST_LDAP_HOST")
  83. if len(host) == 0 {
  84. host = "ldap"
  85. }
  86. return host
  87. }
  88. func addAuthSourceLDAP(t *testing.T, sshKeyAttribute string) {
  89. session := loginUser(t, "user1")
  90. csrf := GetCSRF(t, session, "/admin/auths/new")
  91. req := NewRequestWithValues(t, "POST", "/admin/auths/new", map[string]string{
  92. "_csrf": csrf,
  93. "type": "2",
  94. "name": "ldap",
  95. "host": getLDAPServerHost(),
  96. "port": "389",
  97. "bind_dn": "uid=gitea,ou=service,dc=planetexpress,dc=com",
  98. "bind_password": "password",
  99. "user_base": "ou=people,dc=planetexpress,dc=com",
  100. "filter": "(&(objectClass=inetOrgPerson)(memberOf=cn=git,ou=people,dc=planetexpress,dc=com)(uid=%s))",
  101. "admin_filter": "(memberOf=cn=admin_staff,ou=people,dc=planetexpress,dc=com)",
  102. "attribute_username": "uid",
  103. "attribute_name": "givenName",
  104. "attribute_surname": "sn",
  105. "attribute_mail": "mail",
  106. "attribute_ssh_public_key": sshKeyAttribute,
  107. "is_sync_enabled": "on",
  108. "is_active": "on",
  109. })
  110. session.MakeRequest(t, req, http.StatusFound)
  111. }
  112. func TestLDAPUserSignin(t *testing.T) {
  113. if skipLDAPTests() {
  114. t.Skip()
  115. return
  116. }
  117. defer prepareTestEnv(t)()
  118. addAuthSourceLDAP(t, "")
  119. u := gitLDAPUsers[0]
  120. session := loginUserWithPassword(t, u.UserName, u.Password)
  121. req := NewRequest(t, "GET", "/user/settings")
  122. resp := session.MakeRequest(t, req, http.StatusOK)
  123. htmlDoc := NewHTMLParser(t, resp.Body)
  124. assert.Equal(t, u.UserName, htmlDoc.GetInputValueByName("name"))
  125. assert.Equal(t, u.FullName, htmlDoc.GetInputValueByName("full_name"))
  126. assert.Equal(t, u.Email, htmlDoc.GetInputValueByName("email"))
  127. }
  128. func TestLDAPUserSync(t *testing.T) {
  129. if skipLDAPTests() {
  130. t.Skip()
  131. return
  132. }
  133. defer prepareTestEnv(t)()
  134. addAuthSourceLDAP(t, "")
  135. models.SyncExternalUsers(context.Background())
  136. session := loginUser(t, "user1")
  137. // Check if users exists
  138. for _, u := range gitLDAPUsers {
  139. req := NewRequest(t, "GET", "/admin/users?q="+u.UserName)
  140. resp := session.MakeRequest(t, req, http.StatusOK)
  141. htmlDoc := NewHTMLParser(t, resp.Body)
  142. tr := htmlDoc.doc.Find("table.table tbody tr")
  143. if !assert.True(t, tr.Length() == 1) {
  144. continue
  145. }
  146. tds := tr.Find("td")
  147. if !assert.True(t, tds.Length() > 0) {
  148. continue
  149. }
  150. assert.Equal(t, u.UserName, strings.TrimSpace(tds.Find("td:nth-child(2) a").Text()))
  151. assert.Equal(t, u.Email, strings.TrimSpace(tds.Find("td:nth-child(3) span").Text()))
  152. if u.IsAdmin {
  153. assert.True(t, tds.Find("td:nth-child(5) i").HasClass("fa-check-square-o"))
  154. } else {
  155. assert.True(t, tds.Find("td:nth-child(5) i").HasClass("fa-square-o"))
  156. }
  157. }
  158. // Check if no users exist
  159. for _, u := range otherLDAPUsers {
  160. req := NewRequest(t, "GET", "/admin/users?q="+u.UserName)
  161. resp := session.MakeRequest(t, req, http.StatusOK)
  162. htmlDoc := NewHTMLParser(t, resp.Body)
  163. tr := htmlDoc.doc.Find("table.table tbody tr")
  164. assert.True(t, tr.Length() == 0)
  165. }
  166. }
  167. func TestLDAPUserSigninFailed(t *testing.T) {
  168. if skipLDAPTests() {
  169. t.Skip()
  170. return
  171. }
  172. defer prepareTestEnv(t)()
  173. addAuthSourceLDAP(t, "")
  174. u := otherLDAPUsers[0]
  175. testLoginFailed(t, u.UserName, u.Password, i18n.Tr("en", "form.username_password_incorrect"))
  176. }
  177. func TestLDAPUserSSHKeySync(t *testing.T) {
  178. if skipLDAPTests() {
  179. t.Skip()
  180. return
  181. }
  182. defer prepareTestEnv(t)()
  183. addAuthSourceLDAP(t, "sshPublicKey")
  184. models.SyncExternalUsers(context.Background())
  185. // Check if users has SSH keys synced
  186. for _, u := range gitLDAPUsers {
  187. if len(u.SSHKeys) == 0 {
  188. continue
  189. }
  190. session := loginUserWithPassword(t, u.UserName, u.Password)
  191. req := NewRequest(t, "GET", "/user/settings/keys")
  192. resp := session.MakeRequest(t, req, http.StatusOK)
  193. htmlDoc := NewHTMLParser(t, resp.Body)
  194. divs := htmlDoc.doc.Find(".key.list .print.meta")
  195. syncedKeys := make([]string, divs.Length())
  196. for i := 0; i < divs.Length(); i++ {
  197. syncedKeys[i] = strings.TrimSpace(divs.Eq(i).Text())
  198. }
  199. assert.ElementsMatch(t, u.SSHKeys, syncedKeys)
  200. }
  201. }