gitea源码

signup_test.go 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "testing"
  9. "code.gitea.io/gitea/models/db"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/test"
  14. "code.gitea.io/gitea/modules/translation"
  15. "code.gitea.io/gitea/tests"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func TestSignup(t *testing.T) {
  19. defer tests.PrepareTestEnv(t)()
  20. defer test.MockVariableValue(&setting.Service.EnableCaptcha, false)()
  21. req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
  22. "user_name": "exampleUser",
  23. "email": "exampleUser@example.com",
  24. "password": "examplePassword!1",
  25. "retype": "examplePassword!1",
  26. })
  27. MakeRequest(t, req, http.StatusSeeOther)
  28. // should be able to view new user's page
  29. req = NewRequest(t, "GET", "/exampleUser")
  30. MakeRequest(t, req, http.StatusOK)
  31. }
  32. func TestSignupAsRestricted(t *testing.T) {
  33. defer tests.PrepareTestEnv(t)()
  34. defer test.MockVariableValue(&setting.Service.EnableCaptcha, false)()
  35. defer test.MockVariableValue(&setting.Service.DefaultUserIsRestricted, true)()
  36. req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
  37. "user_name": "restrictedUser",
  38. "email": "restrictedUser@example.com",
  39. "password": "examplePassword!1",
  40. "retype": "examplePassword!1",
  41. })
  42. MakeRequest(t, req, http.StatusSeeOther)
  43. // should be able to view new user's page
  44. req = NewRequest(t, "GET", "/restrictedUser")
  45. MakeRequest(t, req, http.StatusOK)
  46. user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "restrictedUser"})
  47. assert.True(t, user2.IsRestricted)
  48. }
  49. func TestSignupEmailValidation(t *testing.T) {
  50. defer tests.PrepareTestEnv(t)()
  51. defer test.MockVariableValue(&setting.Service.EnableCaptcha, false)()
  52. tests := []struct {
  53. email string
  54. wantStatus int
  55. wantMsg string
  56. }{
  57. {"exampleUser@example.com\r\n", http.StatusOK, translation.NewLocale("en-US").TrString("form.email_invalid")},
  58. {"exampleUser@example.com\r", http.StatusOK, translation.NewLocale("en-US").TrString("form.email_invalid")},
  59. {"exampleUser@example.com\n", http.StatusOK, translation.NewLocale("en-US").TrString("form.email_invalid")},
  60. {"exampleUser@example.com", http.StatusSeeOther, ""},
  61. }
  62. for i, test := range tests {
  63. req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
  64. "user_name": fmt.Sprintf("exampleUser%d", i),
  65. "email": test.email,
  66. "password": "examplePassword!1",
  67. "retype": "examplePassword!1",
  68. })
  69. resp := MakeRequest(t, req, test.wantStatus)
  70. if test.wantMsg != "" {
  71. htmlDoc := NewHTMLParser(t, resp.Body)
  72. assert.Equal(t,
  73. test.wantMsg,
  74. strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
  75. )
  76. }
  77. }
  78. }
  79. func TestSignupEmailActive(t *testing.T) {
  80. defer tests.PrepareTestEnv(t)()
  81. defer test.MockVariableValue(&setting.Service.RegisterEmailConfirm, true)()
  82. // try to sign up and send the activation email
  83. req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
  84. "user_name": "Test-User-1",
  85. "email": "EmAiL-1@example.com",
  86. "password": "password1",
  87. "retype": "password1",
  88. })
  89. resp := MakeRequest(t, req, http.StatusOK)
  90. assert.Contains(t, resp.Body.String(), `A new confirmation email has been sent to <b>EmAiL-1@example.com</b>.`)
  91. // access "user/activate" means trying to re-send the activation email
  92. session := loginUserWithPassword(t, "test-user-1", "password1")
  93. resp = session.MakeRequest(t, NewRequest(t, "GET", "/user/activate"), http.StatusOK)
  94. assert.Contains(t, resp.Body.String(), "You have already requested an activation email recently")
  95. // access anywhere else will see an "Activate Your Account" prompt, and there is a chance to change email
  96. resp = session.MakeRequest(t, NewRequest(t, "GET", "/user/issues"), http.StatusOK)
  97. assert.Contains(t, resp.Body.String(), `<input id="change-email" name="change_email" `)
  98. // post to "user/activate" with a new email
  99. session.MakeRequest(t, NewRequestWithValues(t, "POST", "/user/activate", map[string]string{"change_email": "email-changed@example.com"}), http.StatusSeeOther)
  100. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "Test-User-1"})
  101. assert.Equal(t, "email-changed@example.com", user.Email)
  102. email := unittest.AssertExistsAndLoadBean(t, &user_model.EmailAddress{Email: "email-changed@example.com"})
  103. assert.False(t, email.IsActivated)
  104. assert.True(t, email.IsPrimary)
  105. // generate an activation code from lower-cased email
  106. activationCode := user_model.GenerateUserTimeLimitCode(&user_model.TimeLimitCodeOptions{Purpose: user_model.TimeLimitCodeActivateAccount}, user)
  107. // and update the user email to case-sensitive, it shouldn't affect the verification later
  108. _, _ = db.Exec(t.Context(), "UPDATE `user` SET email=? WHERE id=?", "EmAiL-changed@example.com", user.ID)
  109. user = unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "Test-User-1"})
  110. assert.Equal(t, "EmAiL-changed@example.com", user.Email)
  111. // access "user/activate" with a valid activation code, then get the "verify password" page
  112. resp = session.MakeRequest(t, NewRequest(t, "GET", "/user/activate?code="+activationCode), http.StatusOK)
  113. assert.Contains(t, resp.Body.String(), `<input id="verify-password"`)
  114. // try to use a wrong password, it should fail
  115. req = NewRequestWithValues(t, "POST", "/user/activate", map[string]string{
  116. "code": activationCode,
  117. "password": "password-wrong",
  118. })
  119. resp = session.MakeRequest(t, req, http.StatusOK)
  120. assert.Contains(t, resp.Body.String(), `Your password does not match`)
  121. assert.Contains(t, resp.Body.String(), `<input id="verify-password"`)
  122. user = unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "Test-User-1"})
  123. assert.False(t, user.IsActive)
  124. // then use a correct password, the user should be activated
  125. req = NewRequestWithValues(t, "POST", "/user/activate", map[string]string{
  126. "code": activationCode,
  127. "password": "password1",
  128. })
  129. resp = session.MakeRequest(t, req, http.StatusSeeOther)
  130. assert.Equal(t, "/", test.RedirectURL(resp))
  131. user = unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "Test-User-1"})
  132. assert.True(t, user.IsActive)
  133. }