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.

signup_test.go 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "fmt"
  7. "net/http"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/setting"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/unknwon/i18n"
  14. )
  15. func TestSignup(t *testing.T) {
  16. defer prepareTestEnv(t)()
  17. setting.Service.EnableCaptcha = false
  18. req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
  19. "user_name": "exampleUser",
  20. "email": "exampleUser@example.com",
  21. "password": "examplePassword!1",
  22. "retype": "examplePassword!1",
  23. })
  24. MakeRequest(t, req, http.StatusFound)
  25. // should be able to view new user's page
  26. req = NewRequest(t, "GET", "/exampleUser")
  27. MakeRequest(t, req, http.StatusOK)
  28. }
  29. func TestSignupAsRestricted(t *testing.T) {
  30. defer prepareTestEnv(t)()
  31. setting.Service.EnableCaptcha = false
  32. setting.Service.DefaultUserIsRestricted = true
  33. req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
  34. "user_name": "restrictedUser",
  35. "email": "restrictedUser@example.com",
  36. "password": "examplePassword!1",
  37. "retype": "examplePassword!1",
  38. })
  39. MakeRequest(t, req, http.StatusFound)
  40. // should be able to view new user's page
  41. req = NewRequest(t, "GET", "/restrictedUser")
  42. MakeRequest(t, req, http.StatusOK)
  43. user2 := models.AssertExistsAndLoadBean(t, &models.User{Name: "restrictedUser"}).(*models.User)
  44. assert.True(t, user2.IsRestricted)
  45. }
  46. func TestSignupEmail(t *testing.T) {
  47. defer prepareTestEnv(t)()
  48. setting.Service.EnableCaptcha = false
  49. tests := []struct {
  50. email string
  51. wantStatus int
  52. wantMsg string
  53. }{
  54. {"exampleUser@example.com\r\n", http.StatusOK, i18n.Tr("en", "form.email_invalid", nil)},
  55. {"exampleUser@example.com\r", http.StatusOK, i18n.Tr("en", "form.email_invalid", nil)},
  56. {"exampleUser@example.com\n", http.StatusOK, i18n.Tr("en", "form.email_invalid", nil)},
  57. {"exampleUser@example.com", http.StatusFound, ""},
  58. }
  59. for i, test := range tests {
  60. req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{
  61. "user_name": fmt.Sprintf("exampleUser%d", i),
  62. "email": test.email,
  63. "password": "examplePassword!1",
  64. "retype": "examplePassword!1",
  65. })
  66. resp := MakeRequest(t, req, test.wantStatus)
  67. if test.wantMsg != "" {
  68. htmlDoc := NewHTMLParser(t, resp.Body)
  69. assert.Equal(t,
  70. test.wantMsg,
  71. strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
  72. )
  73. }
  74. }
  75. }