loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at forgejo 235 lines 8.1 kB view raw
1// Copyright 2017 The Gitea Authors. All rights reserved. 2// Copyright 2025 The Forgejo Authors. All rights reserved. 3// SPDX-License-Identifier: MIT 4 5package integration 6 7import ( 8 "fmt" 9 "net/http" 10 "strings" 11 "testing" 12 13 "forgejo.org/models/unittest" 14 user_model "forgejo.org/models/user" 15 "forgejo.org/modules/cache" 16 "forgejo.org/modules/setting" 17 "forgejo.org/modules/test" 18 "forgejo.org/modules/translation" 19 "forgejo.org/tests" 20 21 "github.com/stretchr/testify/assert" 22) 23 24func TestSignup(t *testing.T) { 25 defer tests.PrepareTestEnv(t)() 26 27 setting.Service.EnableCaptcha = false 28 29 req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{ 30 "user_name": "exampleUser", 31 "email": "exampleUser@example.com", 32 "password": "examplePassword!1", 33 "retype": "examplePassword!1", 34 }) 35 MakeRequest(t, req, http.StatusSeeOther) 36 37 // should be able to view new user's page 38 req = NewRequest(t, "GET", "/exampleUser") 39 MakeRequest(t, req, http.StatusOK) 40} 41 42func TestSignupAsRestricted(t *testing.T) { 43 defer tests.PrepareTestEnv(t)() 44 45 setting.Service.EnableCaptcha = false 46 setting.Service.DefaultUserIsRestricted = true 47 48 req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{ 49 "user_name": "restrictedUser", 50 "email": "restrictedUser@example.com", 51 "password": "examplePassword!1", 52 "retype": "examplePassword!1", 53 }) 54 MakeRequest(t, req, http.StatusSeeOther) 55 56 // should be able to view new user's page 57 req = NewRequest(t, "GET", "/restrictedUser") 58 MakeRequest(t, req, http.StatusOK) 59 60 user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "restrictedUser"}) 61 assert.True(t, user2.IsRestricted) 62} 63 64func TestSignupEmail(t *testing.T) { 65 defer tests.PrepareTestEnv(t)() 66 67 setting.Service.EnableCaptcha = false 68 69 tests := []struct { 70 email string 71 wantStatus int 72 wantMsg string 73 }{ 74 {"exampleUser@example.com\r\n", http.StatusOK, translation.NewLocale("en-US").TrString("form.email_invalid")}, 75 {"exampleUser@example.com\r", http.StatusOK, translation.NewLocale("en-US").TrString("form.email_invalid")}, 76 {"exampleUser@example.com\n", http.StatusOK, translation.NewLocale("en-US").TrString("form.email_invalid")}, 77 {"exampleUser@example.com", http.StatusSeeOther, ""}, 78 } 79 80 for i, test := range tests { 81 req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{ 82 "user_name": fmt.Sprintf("exampleUser%d", i), 83 "email": test.email, 84 "password": "examplePassword!1", 85 "retype": "examplePassword!1", 86 }) 87 resp := MakeRequest(t, req, test.wantStatus) 88 if test.wantMsg != "" { 89 htmlDoc := NewHTMLParser(t, resp.Body) 90 assert.Equal(t, 91 test.wantMsg, 92 strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()), 93 ) 94 } 95 } 96} 97 98func TestSignupEmailChangeForInactiveUser(t *testing.T) { 99 defer tests.PrepareTestEnv(t)() 100 101 // Disable the captcha & enable email confirmation for registrations 102 defer test.MockVariableValue(&setting.Service.EnableCaptcha, false)() 103 defer test.MockVariableValue(&setting.Service.RegisterEmailConfirm, true)() 104 105 // Create user 106 req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{ 107 "user_name": "exampleUserX", 108 "email": "wrong-email@example.com", 109 "password": "examplePassword!1", 110 "retype": "examplePassword!1", 111 }) 112 MakeRequest(t, req, http.StatusOK) 113 114 session := loginUserWithPassword(t, "exampleUserX", "examplePassword!1") 115 116 // Verify that the initial e-mail is the wrong one. 117 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "exampleUserX"}) 118 assert.Equal(t, "wrong-email@example.com", user.Email) 119 120 // Change the email address 121 req = NewRequestWithValues(t, "POST", "/user/activate", map[string]string{ 122 "email": "fine-email@example.com", 123 }) 124 session.MakeRequest(t, req, http.StatusSeeOther) 125 126 // Verify that the email was updated 127 user = unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "exampleUserX"}) 128 assert.Equal(t, "fine-email@example.com", user.Email) 129 130 // Try to change the email again 131 req = NewRequestWithValues(t, "POST", "/user/activate", map[string]string{ 132 "email": "wrong-again@example.com", 133 }) 134 session.MakeRequest(t, req, http.StatusSeeOther) 135 // Verify that the email was NOT updated 136 user = unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "exampleUserX"}) 137 assert.Equal(t, "fine-email@example.com", user.Email) 138} 139 140func TestSignupEmailChangeForActiveUser(t *testing.T) { 141 defer tests.PrepareTestEnv(t)() 142 143 // Disable the captcha & enable email confirmation for registrations 144 defer test.MockVariableValue(&setting.Service.EnableCaptcha, false)() 145 defer test.MockVariableValue(&setting.Service.RegisterEmailConfirm, false)() 146 147 // Create user 148 req := NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{ 149 "user_name": "exampleUserY", 150 "email": "wrong-email-2@example.com", 151 "password": "examplePassword!1", 152 "retype": "examplePassword!1", 153 }) 154 MakeRequest(t, req, http.StatusSeeOther) 155 156 session := loginUserWithPassword(t, "exampleUserY", "examplePassword!1") 157 158 // Verify that the initial e-mail is the wrong one. 159 user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "exampleUserY"}) 160 assert.Equal(t, "wrong-email-2@example.com", user.Email) 161 162 // Changing the email for a validated address is not available 163 req = NewRequestWithValues(t, "POST", "/user/activate", map[string]string{ 164 "email": "fine-email-2@example.com", 165 }) 166 session.MakeRequest(t, req, http.StatusNotFound) 167 168 // Verify that the email remained unchanged 169 user = unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "exampleUserY"}) 170 assert.Equal(t, "wrong-email-2@example.com", user.Email) 171} 172 173func TestSignupImageCaptcha(t *testing.T) { 174 defer tests.PrepareTestEnv(t)() 175 defer test.MockVariableValue(&setting.Service.RegisterEmailConfirm, false)() 176 defer test.MockVariableValue(&setting.Service.EnableCaptcha, true)() 177 defer test.MockVariableValue(&setting.Service.CaptchaType, "image")() 178 c := cache.GetCache() 179 180 req := NewRequest(t, "GET", "/user/sign_up") 181 resp := MakeRequest(t, req, http.StatusOK) 182 htmlDoc := NewHTMLParser(t, resp.Body) 183 184 idCaptcha, ok := htmlDoc.Find("input[name='img-captcha-id']").Attr("value") 185 assert.True(t, ok) 186 187 digits, ok := c.Get("captcha:" + idCaptcha).(string) 188 assert.True(t, ok) 189 assert.Len(t, digits, 6) 190 191 digitStr := "" 192 // Convert digits to ASCII digits. 193 for _, digit := range digits { 194 digitStr += string(digit + '0') 195 } 196 197 req = NewRequestWithValues(t, "POST", "/user/sign_up", map[string]string{ 198 "user_name": "captcha-test", 199 "email": "captcha-test@example.com", 200 "password": "examplePassword!1", 201 "retype": "examplePassword!1", 202 "img-captcha-id": idCaptcha, 203 "img-captcha-response": digitStr, 204 }) 205 MakeRequest(t, req, http.StatusSeeOther) 206 207 loginUserWithPassword(t, "captcha-test", "examplePassword!1") 208 209 unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "captcha-test", IsActive: true}) 210} 211 212func TestSignupFormUI(t *testing.T) { 213 defer tests.PrepareTestEnv(t)() 214 t.Run("UI", func(t *testing.T) { 215 // Mock alternative auth ways as enabled 216 defer test.MockVariableValue(&setting.Service.EnableOpenIDSignIn, true)() 217 defer test.MockVariableValue(&setting.Service.EnableOpenIDSignUp, true)() 218 t.Run("Internal registration enabled", func(t *testing.T) { 219 defer test.MockVariableValue(&setting.Service.AllowOnlyExternalRegistration, false)() 220 req := NewRequest(t, "GET", "/user/sign_up") 221 resp := MakeRequest(t, req, http.StatusOK) 222 htmlDoc := NewHTMLParser(t, resp.Body) 223 htmlDoc.AssertElement(t, "form[action='/user/sign_up'] input#user_name", true) 224 htmlDoc.AssertElement(t, ".divider-text", true) 225 }) 226 t.Run("Internal registration disabled", func(t *testing.T) { 227 defer test.MockVariableValue(&setting.Service.AllowOnlyExternalRegistration, true)() 228 req := NewRequest(t, "GET", "/user/sign_up") 229 resp := MakeRequest(t, req, http.StatusOK) 230 htmlDoc := NewHTMLParser(t, resp.Body) 231 htmlDoc.AssertElement(t, "form[action='/user/sign_up'] input#user_name", false) 232 htmlDoc.AssertElement(t, ".divider-text", false) 233 }) 234 }) 235}