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.

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