Files
Agent-System/internal/server/test_auth_helpers.go
T
weichen 3145550a59 feat(security): complete P0 release blocking remediation
Unify auth middleware and remove X-User-ID trust, add project_members
authorization, sandbox workspaces via resolver, and admin-only reconcile
by run_id. Introduce goose migrations with startup schema gate, fail closed
when DB unavailable outside explicit ALLOW_IN_MEMORY, bundle web/dist in
app Dockerfile, and remove frontend pseudo-login fallback.
2026-09-13 02:50:22 +00:00

54 lines
1.4 KiB
Go

package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"chat-agent-system/internal/auth"
)
// loginTestAdmin 使用默认管理员凭证登录,返回 Bearer Token。
func loginTestAdmin(t *testing.T, srv *Server) string {
t.Helper()
username := srv.cfg.AdminUsername
password := srv.cfg.AdminPassword
if username == "" {
username = "admin"
}
if password == "" {
password = "admin-secret-password-123"
}
body, _ := json.Marshal(map[string]string{
"username": username,
"password": password,
})
req := httptest.NewRequest(http.MethodPost, "/api/v1/auth/login", bytes.NewReader(body))
w := httptest.NewRecorder()
srv.Routes().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("admin login failed: %d %s", w.Code, w.Body.String())
}
var session auth.SessionInfo
if err := json.NewDecoder(w.Body).Decode(&session); err != nil {
t.Fatalf("decode login session: %v", err)
}
if session.Token == "" {
t.Fatal("expected non-empty session token")
}
return session.Token
}
// withBearerAuth 为请求附加 Authorization Bearer Token。
func withBearerAuth(req *http.Request, token string) {
req.Header.Set("Authorization", "Bearer "+token)
}
// withUserIDAuth 为请求附加 X-User-ID(用户必须已存在于 auth store)。
func withUserIDAuth(req *http.Request, userID int64) {
req.Header.Set("X-User-ID", strconv.FormatInt(userID, 10))
}