mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-03-27 11:53:04 -04:00
This PR is in preparation of, but independent of, an upcoming suggestion for a feature addition: * The first commit moves a tiny bit of logic into a separate function to prepare for extension of that logic, avoiding duplication * The second commit moves checking for disabled registrations earlier, which, I think, has merits in terms of performance and resilience (hopefully not significant, but who knows?) * The third commit adds simple unit tests for SignUp() and SignUpPost() to avoid the long-ish roundtrip over integration tests * The forth commit introduces `ctx.Data["DisableRegistrationReason"]` for the signup template to use as the reason printed if `.DisableRegistration` to prepare for other reasons to be added Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10915 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: Nils Goroll <nils.goroll@uplex.de> Co-committed-by: Nils Goroll <nils.goroll@uplex.de>
71 lines
2.2 KiB
Go
71 lines
2.2 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package auth
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"forgejo.org/modules/setting"
|
|
"forgejo.org/modules/templates"
|
|
"forgejo.org/modules/test"
|
|
"forgejo.org/services/contexttest"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestUserLogin(t *testing.T) {
|
|
ctx, resp := contexttest.MockContext(t, "/user/login")
|
|
SignIn(ctx)
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
|
|
ctx, resp = contexttest.MockContext(t, "/user/login")
|
|
ctx.IsSigned = true
|
|
SignIn(ctx)
|
|
assert.Equal(t, http.StatusSeeOther, resp.Code)
|
|
assert.Equal(t, "/", test.RedirectURL(resp))
|
|
|
|
ctx, resp = contexttest.MockContext(t, "/user/login?redirect_to=/other")
|
|
ctx.IsSigned = true
|
|
SignIn(ctx)
|
|
assert.Equal(t, "/other", test.RedirectURL(resp))
|
|
|
|
ctx, resp = contexttest.MockContext(t, "/user/login")
|
|
ctx.Req.AddCookie(&http.Cookie{Name: "redirect_to", Value: "/other-cookie"})
|
|
ctx.IsSigned = true
|
|
SignIn(ctx)
|
|
assert.Equal(t, "/other-cookie", test.RedirectURL(resp))
|
|
|
|
ctx, resp = contexttest.MockContext(t, "/user/login?redirect_to="+url.QueryEscape("https://example.com"))
|
|
ctx.IsSigned = true
|
|
SignIn(ctx)
|
|
assert.Equal(t, "/", test.RedirectURL(resp))
|
|
}
|
|
|
|
// NB: Full signup test is in tests/integration/signup_test.go
|
|
// this is to test disabled signup
|
|
func TestSignUpDefault(t *testing.T) {
|
|
ctx, resp := contexttest.MockContext(t, "/user/sign_up",
|
|
contexttest.MockContextOption{Render: templates.HTMLRenderer()})
|
|
SignUp(ctx)
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
assert.Contains(t, resp.Body.String(), ctx.Locale.Tr("username"))
|
|
}
|
|
|
|
func TestSignUpDisabled(t *testing.T) {
|
|
ctx, resp := contexttest.MockContext(t, "/user/sign_up",
|
|
contexttest.MockContextOption{Render: templates.HTMLRenderer()})
|
|
defer test.MockVariableValue(&setting.Service.DisableRegistration, true)()
|
|
SignUp(ctx)
|
|
assert.Equal(t, http.StatusOK, resp.Code)
|
|
assert.Contains(t, resp.Body.String(), ctx.Locale.Tr("auth.disable_register_prompt"))
|
|
}
|
|
|
|
func TestSignUpPostDisabled(t *testing.T) {
|
|
ctx, resp := contexttest.MockContext(t, "/user/sign_up")
|
|
defer test.MockVariableValue(&setting.Service.DisableRegistration, true)()
|
|
SignUpPost(ctx)
|
|
assert.Equal(t, http.StatusForbidden, resp.Code)
|
|
}
|