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.

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