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.

203 lines
7.0 KiB

  1. // Copyright 2017 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. "testing"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/test"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/unknwon/i18n"
  12. )
  13. func TestViewUser(t *testing.T) {
  14. prepareTestEnv(t)
  15. req := NewRequest(t, "GET", "/user2")
  16. MakeRequest(t, req, http.StatusOK)
  17. }
  18. func TestRenameUsername(t *testing.T) {
  19. prepareTestEnv(t)
  20. session := loginUser(t, "user2")
  21. req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
  22. "_csrf": GetCSRF(t, session, "/user/settings"),
  23. "name": "newUsername",
  24. "email": "user2@example.com",
  25. "language": "en-us",
  26. })
  27. session.MakeRequest(t, req, http.StatusFound)
  28. models.AssertExistsAndLoadBean(t, &models.User{Name: "newUsername"})
  29. models.AssertNotExistsBean(t, &models.User{Name: "user2"})
  30. }
  31. func TestRenameInvalidUsername(t *testing.T) {
  32. prepareTestEnv(t)
  33. invalidUsernames := []string{
  34. "%2f*",
  35. "%2f.",
  36. "%2f..",
  37. "%00",
  38. "thisHas ASpace",
  39. "p<A>tho>lo<gical",
  40. }
  41. session := loginUser(t, "user2")
  42. for _, invalidUsername := range invalidUsernames {
  43. t.Logf("Testing username %s", invalidUsername)
  44. req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
  45. "_csrf": GetCSRF(t, session, "/user/settings"),
  46. "name": invalidUsername,
  47. "email": "user2@example.com",
  48. })
  49. resp := session.MakeRequest(t, req, http.StatusOK)
  50. htmlDoc := NewHTMLParser(t, resp.Body)
  51. assert.Contains(t,
  52. htmlDoc.doc.Find(".ui.negative.message").Text(),
  53. i18n.Tr("en", "form.alpha_dash_dot_error"),
  54. )
  55. models.AssertNotExistsBean(t, &models.User{Name: invalidUsername})
  56. }
  57. }
  58. func TestRenameReservedUsername(t *testing.T) {
  59. prepareTestEnv(t)
  60. reservedUsernames := []string{
  61. "admin",
  62. "api",
  63. "attachments",
  64. "avatars",
  65. "explore",
  66. "help",
  67. "install",
  68. "issues",
  69. "login",
  70. "metrics",
  71. "notifications",
  72. "org",
  73. "pulls",
  74. "repo",
  75. "template",
  76. "user",
  77. }
  78. session := loginUser(t, "user2")
  79. for _, reservedUsername := range reservedUsernames {
  80. t.Logf("Testing username %s", reservedUsername)
  81. req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
  82. "_csrf": GetCSRF(t, session, "/user/settings"),
  83. "name": reservedUsername,
  84. "email": "user2@example.com",
  85. "language": "en-us",
  86. })
  87. resp := session.MakeRequest(t, req, http.StatusFound)
  88. req = NewRequest(t, "GET", test.RedirectURL(resp))
  89. resp = session.MakeRequest(t, req, http.StatusOK)
  90. htmlDoc := NewHTMLParser(t, resp.Body)
  91. assert.Contains(t,
  92. htmlDoc.doc.Find(".ui.negative.message").Text(),
  93. i18n.Tr("en", "user.form.name_reserved", reservedUsername),
  94. )
  95. models.AssertNotExistsBean(t, &models.User{Name: reservedUsername})
  96. }
  97. }
  98. func TestExportUserGPGKeys(t *testing.T) {
  99. prepareTestEnv(t)
  100. //Export empty key list
  101. testExportUserGPGKeys(t, "user1", `-----BEGIN PGP PUBLIC KEY BLOCK-----
  102. =twTO
  103. -----END PGP PUBLIC KEY BLOCK-----
  104. `)
  105. //Import key
  106. //User1 <user1@example.com>
  107. session := loginUser(t, "user1")
  108. token := getTokenForLoggedInUser(t, session)
  109. testCreateGPGKey(t, session.MakeRequest, token, http.StatusCreated, `-----BEGIN PGP PUBLIC KEY BLOCK-----
  110. mQENBFyy/VUBCADJ7zbM20Z1RWmFoVgp5WkQfI2rU1Vj9cQHes9i42wVLLtcbPeo
  111. QzubgzvMPITDy7nfWxgSf83E23DoHQ1ACFbQh/6eFSRrjsusp3YQ/08NSfPPbcu8
  112. 0M5G+VGwSfzS5uEcwBVQmHyKdcOZIERTNMtYZx1C3bjLD1XVJHvWz9D72Uq4qeO3
  113. 8SR+lzp5n6ppUakcmRnxt3nGRBj1+hEGkdgzyPo93iy+WioegY2lwCA9xMEo5dah
  114. BmYxWx51zyiXYlReTaxlyb3/nuSUt8IcW3Q8zjdtJj4Nu8U1SpV8EdaA1I9IPbHW
  115. 510OSLmD3XhqHH5m6mIxL1YoWxk3V7gpDROtABEBAAG0GVVzZXIxIDx1c2VyMUBl
  116. eGFtcGxlLmNvbT6JAU4EEwEIADgWIQTQEbrYxmXsp1z3j7z9+v0I6RSEHwUCXLL9
  117. VQIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRD9+v0I6RSEH22YCACFqL5+
  118. 6M0m18AMC/pumcpnnmvAS1GrrKTF8nOROA1augZwp1WCNuKw2R6uOJIHANrYECSn
  119. u7+j6GBP2gbIW8mSAzS6HWCs7GGiPpVtT4wcu8wljUI6BxjpyZtoEkriyBjt6HfK
  120. rkegbkuySoJvjq4IcO5D1LB1JWgsUjMYQJj/ZpBIzVtjG9QtFSOiT1Hct4PoZHdC
  121. nsdSgyCkwRZXG+u3kT/wP9F663ba4o16vYlz3dCGo66lF2tyoG3qcyZ1OUzUrnuv
  122. 96ytAzT6XIhrE0nVoBprMxFF5zExotJD3bHjcGBFNLf944bhjKee3U6t9+OsfJVC
  123. l7N5xxIawCuTQdbfuQENBFyy/VUBCADe61yGEoTwKfsOKIhxLaNoRmD883O0tiWt
  124. soO/HPj9dPQLTOiwXgSgSCd8C+LNxGKct87wgFozpah4tDLC6c0nALuHJ0SLbkfz
  125. 55aRhLeOOcrAydatDp72GroXzqpZ0xZBk5wjIWdgEol2GmVRM8QGbeuakU/HVz5y
  126. lPzxUUocgdbSi3GE3zbzijQzVJdyL/kw/KP7pKT/PPKKJ2C5NQDLy0XGKEHddXGR
  127. EWKkVlRalxq/TjfaMR0bi3MpezBsQmp99ATPO/d7trayZUxQHRtXzGFiOXfDHATr
  128. qN730sODjqvU+mpc/SHCRwh9qWDjZRHSuKU5YDBjb5jIQJivZsQ/ABEBAAGJATYE
  129. GAEIACAWIQTQEbrYxmXsp1z3j7z9+v0I6RSEHwUCXLL9VQIbDAAKCRD9+v0I6RSE
  130. H7WoB/4tXl+97rQ6owPCGSVp1Xbwt2521V7COgsOFRVTRTryEWxRW8mm0S7wQvax
  131. C0TLXKur6NVYQMn01iyL+FZzRpEWNuYF3f9QeeLJ/+l2DafESNhNTy17+RPmacK6
  132. 21dccpqchByVw/UMDeHSyjQLiG2lxzt8Gfx2gHmSbrq3aWovTGyz6JTffZvfy/n2
  133. 0Hm437OBPazO0gZyXhdV2PE5RSUfvAgm44235tcV5EV0d32TJDfv61+Vr2GUbah6
  134. 7XhJ1v6JYuh8kaYaEz8OpZDeh7f6Ho6PzJrsy/TKTKhGgZNINj1iaPFyOkQgKR5M
  135. GrE0MHOxUbc9tbtyk0F1SuzREUBH
  136. =DDXw
  137. -----END PGP PUBLIC KEY BLOCK-----
  138. `)
  139. //Export new key
  140. testExportUserGPGKeys(t, "user1", `-----BEGIN PGP PUBLIC KEY BLOCK-----
  141. xsBNBFyy/VUBCADJ7zbM20Z1RWmFoVgp5WkQfI2rU1Vj9cQHes9i42wVLLtcbPeo
  142. QzubgzvMPITDy7nfWxgSf83E23DoHQ1ACFbQh/6eFSRrjsusp3YQ/08NSfPPbcu8
  143. 0M5G+VGwSfzS5uEcwBVQmHyKdcOZIERTNMtYZx1C3bjLD1XVJHvWz9D72Uq4qeO3
  144. 8SR+lzp5n6ppUakcmRnxt3nGRBj1+hEGkdgzyPo93iy+WioegY2lwCA9xMEo5dah
  145. BmYxWx51zyiXYlReTaxlyb3/nuSUt8IcW3Q8zjdtJj4Nu8U1SpV8EdaA1I9IPbHW
  146. 510OSLmD3XhqHH5m6mIxL1YoWxk3V7gpDROtABEBAAHNGVVzZXIxIDx1c2VyMUBl
  147. eGFtcGxlLmNvbT7CwI4EEwEIADgWIQTQEbrYxmXsp1z3j7z9+v0I6RSEHwUCXLL9
  148. VQIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRD9+v0I6RSEH22YCACFqL5+
  149. 6M0m18AMC/pumcpnnmvAS1GrrKTF8nOROA1augZwp1WCNuKw2R6uOJIHANrYECSn
  150. u7+j6GBP2gbIW8mSAzS6HWCs7GGiPpVtT4wcu8wljUI6BxjpyZtoEkriyBjt6HfK
  151. rkegbkuySoJvjq4IcO5D1LB1JWgsUjMYQJj/ZpBIzVtjG9QtFSOiT1Hct4PoZHdC
  152. nsdSgyCkwRZXG+u3kT/wP9F663ba4o16vYlz3dCGo66lF2tyoG3qcyZ1OUzUrnuv
  153. 96ytAzT6XIhrE0nVoBprMxFF5zExotJD3bHjcGBFNLf944bhjKee3U6t9+OsfJVC
  154. l7N5xxIawCuTQdbfzsBNBFyy/VUBCADe61yGEoTwKfsOKIhxLaNoRmD883O0tiWt
  155. soO/HPj9dPQLTOiwXgSgSCd8C+LNxGKct87wgFozpah4tDLC6c0nALuHJ0SLbkfz
  156. 55aRhLeOOcrAydatDp72GroXzqpZ0xZBk5wjIWdgEol2GmVRM8QGbeuakU/HVz5y
  157. lPzxUUocgdbSi3GE3zbzijQzVJdyL/kw/KP7pKT/PPKKJ2C5NQDLy0XGKEHddXGR
  158. EWKkVlRalxq/TjfaMR0bi3MpezBsQmp99ATPO/d7trayZUxQHRtXzGFiOXfDHATr
  159. qN730sODjqvU+mpc/SHCRwh9qWDjZRHSuKU5YDBjb5jIQJivZsQ/ABEBAAHCwHYE
  160. GAEIACAWIQTQEbrYxmXsp1z3j7z9+v0I6RSEHwUCXLL9VQIbDAAKCRD9+v0I6RSE
  161. H7WoB/4tXl+97rQ6owPCGSVp1Xbwt2521V7COgsOFRVTRTryEWxRW8mm0S7wQvax
  162. C0TLXKur6NVYQMn01iyL+FZzRpEWNuYF3f9QeeLJ/+l2DafESNhNTy17+RPmacK6
  163. 21dccpqchByVw/UMDeHSyjQLiG2lxzt8Gfx2gHmSbrq3aWovTGyz6JTffZvfy/n2
  164. 0Hm437OBPazO0gZyXhdV2PE5RSUfvAgm44235tcV5EV0d32TJDfv61+Vr2GUbah6
  165. 7XhJ1v6JYuh8kaYaEz8OpZDeh7f6Ho6PzJrsy/TKTKhGgZNINj1iaPFyOkQgKR5M
  166. GrE0MHOxUbc9tbtyk0F1SuzREUBH
  167. =WFf5
  168. -----END PGP PUBLIC KEY BLOCK-----
  169. `)
  170. }
  171. func testExportUserGPGKeys(t *testing.T, user, expected string) {
  172. session := loginUser(t, user)
  173. t.Logf("Testing username %s export gpg keys", user)
  174. req := NewRequest(t, "GET", "/"+user+".gpg")
  175. resp := session.MakeRequest(t, req, http.StatusOK)
  176. //t.Log(resp.Body.String())
  177. assert.Equal(t, expected, resp.Body.String())
  178. }