Files
gotea/services/auth/reverseproxy_test.go
T
lotherk 1b490485ff refactor: import Gotea from Gitea v28.4.0
Rename the main module to gotea.dev and the binary to gotea, and update the application name and English locale. Keep the external gitea.dev/sdk and gitea.dev/actionslib module imports unchanged.

Assisted-by: OpenCode:DeepSeek-V4.1-Flash
2026-11-22 12:13:09 +00:00

59 lines
2.2 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package auth
import (
"net/http"
"testing"
"gotea.dev/models/unittest"
user_model "gotea.dev/models/user"
"gotea.dev/modules/session"
"gotea.dev/modules/setting"
"gotea.dev/modules/test"
"gotea.dev/services/contexttest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestReverseProxyIgnoresBot(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
defer test.MockVariableValue(&setting.ReverseProxyAuthUser, "X-WEBAUTH-USER")()
defer test.MockVariableValue(&setting.ReverseProxyAuthEmail, "X-WEBAUTH-EMAIL")()
defer test.MockVariableValue(&setting.Service.EnableReverseProxyEmail, true)()
require.NoError(t, user_model.UpdateUserCols(t.Context(), &user_model.User{ID: 2, Type: user_model.UserTypeBot}, "type"))
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
req.Header.Set(setting.ReverseProxyAuthUser, "user2")
req.Header.Set(setting.ReverseProxyAuthEmail, "user2@example.com")
user, err := (&ReverseProxy{}).Verify(req, nil, nil, nil)
require.NoError(t, err)
assert.Nil(t, user)
}
func TestReverseProxyLastLogin(t *testing.T) {
require.NoError(t, unittest.PrepareTestDatabase())
defer test.MockVariableValue(&setting.ReverseProxyAuthUser, "X-WEBAUTH-USER")()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
require.Zero(t, user.LastLoginUnix)
ctx, resp := contexttest.MockContext(t, "/", contexttest.MockContextOption{SessionStore: session.NewMockMemStore("reverse-proxy-last-login")})
ctx.Req.Header.Set(setting.ReverseProxyAuthUser, user.Name)
rp := &ReverseProxy{CreateSession: true}
_, err := rp.Verify(ctx.Req, resp, ctx, ctx.Session)
require.NoError(t, err)
assert.NotZero(t, unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID}).LastLoginUnix)
user.LastLoginUnix = 1
require.NoError(t, user_model.UpdateUserCols(t.Context(), user, "last_login_unix"))
_, err = rp.Verify(ctx.Req, resp, ctx, ctx.Session)
require.NoError(t, err)
assert.EqualValues(t, 1, unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID}).LastLoginUnix) // no write without a new session
}