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.

102 lines
2.6 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. }
  40. session := loginUser(t, "user2")
  41. for _, invalidUsername := range invalidUsernames {
  42. t.Logf("Testing username %s", invalidUsername)
  43. req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
  44. "_csrf": GetCSRF(t, session, "/user/settings"),
  45. "name": invalidUsername,
  46. "email": "user2@example.com",
  47. })
  48. resp := session.MakeRequest(t, req, http.StatusOK)
  49. htmlDoc := NewHTMLParser(t, resp.Body)
  50. assert.Contains(t,
  51. htmlDoc.doc.Find(".ui.negative.message").Text(),
  52. i18n.Tr("en", "form.alpha_dash_dot_error"),
  53. )
  54. models.AssertNotExistsBean(t, &models.User{Name: invalidUsername})
  55. }
  56. }
  57. func TestRenameReservedUsername(t *testing.T) {
  58. prepareTestEnv(t)
  59. reservedUsernames := []string{
  60. "help",
  61. "user",
  62. "template",
  63. }
  64. session := loginUser(t, "user2")
  65. for _, reservedUsername := range reservedUsernames {
  66. t.Logf("Testing username %s", reservedUsername)
  67. req := NewRequestWithValues(t, "POST", "/user/settings", map[string]string{
  68. "_csrf": GetCSRF(t, session, "/user/settings"),
  69. "name": reservedUsername,
  70. "email": "user2@example.com",
  71. "language": "en-us",
  72. })
  73. resp := session.MakeRequest(t, req, http.StatusFound)
  74. req = NewRequest(t, "GET", test.RedirectURL(resp))
  75. resp = session.MakeRequest(t, req, http.StatusOK)
  76. htmlDoc := NewHTMLParser(t, resp.Body)
  77. assert.Contains(t,
  78. htmlDoc.doc.Find(".ui.negative.message").Text(),
  79. i18n.Tr("en", "user.form.name_reserved", reservedUsername),
  80. )
  81. models.AssertNotExistsBean(t, &models.User{Name: reservedUsername})
  82. }
  83. }