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.

231 lines
5.7 KiB

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