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