Files
weichen e96ade076a fix(auth): restrict demo seed users to dev/test environments
Main no longer creates fixed-password demo accounts and legacy anonymous
fallback is limited to explicit dev/test in-memory fixtures.
2026-09-13 02:50:22 +00:00

53 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"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)
}
// withBearerToken 为请求附加 Authorization Bearer Token(Phase 0:不再使用 X-User-ID)。
func withBearerToken(req *http.Request, token string) {
req.Header.Set("Authorization", "Bearer "+token)
}