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.

204 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. defer prepareTestEnv(t)()
  15. req := NewRequest(t, "GET", "/user2")
  16. MakeRequest(t, req, http.StatusOK)
  17. }
  18. func TestRenameUsername(t *testing.T) {
  19. defer 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. defer 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. defer 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. "search",
  78. }
  79. session := loginUser(t, "user2")
  80. for _, reservedUsername := range reservedUsernames {
  81. t.Logf("Testing username %s", reservedUsername)
  82. req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
  83. "_csrf": GetCSRF(t, session, "/user/settings"),
  84. "name": reservedUsername,
  85. "email": "user2@example.com",
  86. "language": "en-us",
  87. })
  88. resp := session.MakeRequest(t, req, http.StatusFound)
  89. req = NewRequest(t, "GET", test.RedirectURL(resp))
  90. resp = session.MakeRequest(t, req, http.StatusOK)
  91. htmlDoc := NewHTMLParser(t, resp.Body)
  92. assert.Contains(t,
  93. htmlDoc.doc.Find(".ui.negative.message").Text(),
  94. i18n.Tr("en", "user.form.name_reserved", reservedUsername),
  95. )
  96. models.AssertNotExistsBean(t, &models.User{Name: reservedUsername})
  97. }
  98. }
  99. func TestExportUserGPGKeys(t *testing.T) {
  100. defer prepareTestEnv(t)()
  101. //Export empty key list
  102. testExportUserGPGKeys(t, "user1", `-----BEGIN PGP PUBLIC KEY BLOCK-----
  103. =twTO
  104. -----END PGP PUBLIC KEY BLOCK-----
  105. `)
  106. //Import key
  107. //User1 <user1@example.com>
  108. session := loginUser(t, "user1")
  109. token := getTokenForLoggedInUser(t, session)
  110. testCreateGPGKey(t, session.MakeRequest, token, http.StatusCreated, `-----BEGIN PGP PUBLIC KEY BLOCK-----
  111. mQENBFyy/VUBCADJ7zbM20Z1RWmFoVgp5WkQfI2rU1Vj9cQHes9i42wVLLtcbPeo
  112. QzubgzvMPITDy7nfWxgSf83E23DoHQ1ACFbQh/6eFSRrjsusp3YQ/08NSfPPbcu8
  113. 0M5G+VGwSfzS5uEcwBVQmHyKdcOZIERTNMtYZx1C3bjLD1XVJHvWz9D72Uq4qeO3
  114. 8SR+lzp5n6ppUakcmRnxt3nGRBj1+hEGkdgzyPo93iy+WioegY2lwCA9xMEo5dah
  115. BmYxWx51zyiXYlReTaxlyb3/nuSUt8IcW3Q8zjdtJj4Nu8U1SpV8EdaA1I9IPbHW
  116. 510OSLmD3XhqHH5m6mIxL1YoWxk3V7gpDROtABEBAAG0GVVzZXIxIDx1c2VyMUBl
  117. eGFtcGxlLmNvbT6JAU4EEwEIADgWIQTQEbrYxmXsp1z3j7z9+v0I6RSEHwUCXLL9
  118. VQIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRD9+v0I6RSEH22YCACFqL5+
  119. 6M0m18AMC/pumcpnnmvAS1GrrKTF8nOROA1augZwp1WCNuKw2R6uOJIHANrYECSn
  120. u7+j6GBP2gbIW8mSAzS6HWCs7GGiPpVtT4wcu8wljUI6BxjpyZtoEkriyBjt6HfK
  121. rkegbkuySoJvjq4IcO5D1LB1JWgsUjMYQJj/ZpBIzVtjG9QtFSOiT1Hct4PoZHdC
  122. nsdSgyCkwRZXG+u3kT/wP9F663ba4o16vYlz3dCGo66lF2tyoG3qcyZ1OUzUrnuv
  123. 96ytAzT6XIhrE0nVoBprMxFF5zExotJD3bHjcGBFNLf944bhjKee3U6t9+OsfJVC
  124. l7N5xxIawCuTQdbfuQENBFyy/VUBCADe61yGEoTwKfsOKIhxLaNoRmD883O0tiWt
  125. soO/HPj9dPQLTOiwXgSgSCd8C+LNxGKct87wgFozpah4tDLC6c0nALuHJ0SLbkfz
  126. 55aRhLeOOcrAydatDp72GroXzqpZ0xZBk5wjIWdgEol2GmVRM8QGbeuakU/HVz5y
  127. lPzxUUocgdbSi3GE3zbzijQzVJdyL/kw/KP7pKT/PPKKJ2C5NQDLy0XGKEHddXGR
  128. EWKkVlRalxq/TjfaMR0bi3MpezBsQmp99ATPO/d7trayZUxQHRtXzGFiOXfDHATr
  129. qN730sODjqvU+mpc/SHCRwh9qWDjZRHSuKU5YDBjb5jIQJivZsQ/ABEBAAGJATYE
  130. GAEIACAWIQTQEbrYxmXsp1z3j7z9+v0I6RSEHwUCXLL9VQIbDAAKCRD9+v0I6RSE
  131. H7WoB/4tXl+97rQ6owPCGSVp1Xbwt2521V7COgsOFRVTRTryEWxRW8mm0S7wQvax
  132. C0TLXKur6NVYQMn01iyL+FZzRpEWNuYF3f9QeeLJ/+l2DafESNhNTy17+RPmacK6
  133. 21dccpqchByVw/UMDeHSyjQLiG2lxzt8Gfx2gHmSbrq3aWovTGyz6JTffZvfy/n2
  134. 0Hm437OBPazO0gZyXhdV2PE5RSUfvAgm44235tcV5EV0d32TJDfv61+Vr2GUbah6
  135. 7XhJ1v6JYuh8kaYaEz8OpZDeh7f6Ho6PzJrsy/TKTKhGgZNINj1iaPFyOkQgKR5M
  136. GrE0MHOxUbc9tbtyk0F1SuzREUBH
  137. =DDXw
  138. -----END PGP PUBLIC KEY BLOCK-----
  139. `)
  140. //Export new key
  141. testExportUserGPGKeys(t, "user1", `-----BEGIN PGP PUBLIC KEY BLOCK-----
  142. xsBNBFyy/VUBCADJ7zbM20Z1RWmFoVgp5WkQfI2rU1Vj9cQHes9i42wVLLtcbPeo
  143. QzubgzvMPITDy7nfWxgSf83E23DoHQ1ACFbQh/6eFSRrjsusp3YQ/08NSfPPbcu8
  144. 0M5G+VGwSfzS5uEcwBVQmHyKdcOZIERTNMtYZx1C3bjLD1XVJHvWz9D72Uq4qeO3
  145. 8SR+lzp5n6ppUakcmRnxt3nGRBj1+hEGkdgzyPo93iy+WioegY2lwCA9xMEo5dah
  146. BmYxWx51zyiXYlReTaxlyb3/nuSUt8IcW3Q8zjdtJj4Nu8U1SpV8EdaA1I9IPbHW
  147. 510OSLmD3XhqHH5m6mIxL1YoWxk3V7gpDROtABEBAAHNGVVzZXIxIDx1c2VyMUBl
  148. eGFtcGxlLmNvbT7CwI4EEwEIADgWIQTQEbrYxmXsp1z3j7z9+v0I6RSEHwUCXLL9
  149. VQIbAwULCQgHAgYVCgkICwIEFgIDAQIeAQIXgAAKCRD9+v0I6RSEH22YCACFqL5+
  150. 6M0m18AMC/pumcpnnmvAS1GrrKTF8nOROA1augZwp1WCNuKw2R6uOJIHANrYECSn
  151. u7+j6GBP2gbIW8mSAzS6HWCs7GGiPpVtT4wcu8wljUI6BxjpyZtoEkriyBjt6HfK
  152. rkegbkuySoJvjq4IcO5D1LB1JWgsUjMYQJj/ZpBIzVtjG9QtFSOiT1Hct4PoZHdC
  153. nsdSgyCkwRZXG+u3kT/wP9F663ba4o16vYlz3dCGo66lF2tyoG3qcyZ1OUzUrnuv
  154. 96ytAzT6XIhrE0nVoBprMxFF5zExotJD3bHjcGBFNLf944bhjKee3U6t9+OsfJVC
  155. l7N5xxIawCuTQdbfzsBNBFyy/VUBCADe61yGEoTwKfsOKIhxLaNoRmD883O0tiWt
  156. soO/HPj9dPQLTOiwXgSgSCd8C+LNxGKct87wgFozpah4tDLC6c0nALuHJ0SLbkfz
  157. 55aRhLeOOcrAydatDp72GroXzqpZ0xZBk5wjIWdgEol2GmVRM8QGbeuakU/HVz5y
  158. lPzxUUocgdbSi3GE3zbzijQzVJdyL/kw/KP7pKT/PPKKJ2C5NQDLy0XGKEHddXGR
  159. EWKkVlRalxq/TjfaMR0bi3MpezBsQmp99ATPO/d7trayZUxQHRtXzGFiOXfDHATr
  160. qN730sODjqvU+mpc/SHCRwh9qWDjZRHSuKU5YDBjb5jIQJivZsQ/ABEBAAHCwHYE
  161. GAEIACAWIQTQEbrYxmXsp1z3j7z9+v0I6RSEHwUCXLL9VQIbDAAKCRD9+v0I6RSE
  162. H7WoB/4tXl+97rQ6owPCGSVp1Xbwt2521V7COgsOFRVTRTryEWxRW8mm0S7wQvax
  163. C0TLXKur6NVYQMn01iyL+FZzRpEWNuYF3f9QeeLJ/+l2DafESNhNTy17+RPmacK6
  164. 21dccpqchByVw/UMDeHSyjQLiG2lxzt8Gfx2gHmSbrq3aWovTGyz6JTffZvfy/n2
  165. 0Hm437OBPazO0gZyXhdV2PE5RSUfvAgm44235tcV5EV0d32TJDfv61+Vr2GUbah6
  166. 7XhJ1v6JYuh8kaYaEz8OpZDeh7f6Ho6PzJrsy/TKTKhGgZNINj1iaPFyOkQgKR5M
  167. GrE0MHOxUbc9tbtyk0F1SuzREUBH
  168. =WFf5
  169. -----END PGP PUBLIC KEY BLOCK-----
  170. `)
  171. }
  172. func testExportUserGPGKeys(t *testing.T, user, expected string) {
  173. session := loginUser(t, user)
  174. t.Logf("Testing username %s export gpg keys", user)
  175. req := NewRequest(t, "GET", "/"+user+".gpg")
  176. resp := session.MakeRequest(t, req, http.StatusOK)
  177. //t.Log(resp.Body.String())
  178. assert.Equal(t, expected, resp.Body.String())
  179. }