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.

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