Main no longer creates fixed-password demo accounts and legacy anonymous fallback is limited to explicit dev/test in-memory fixtures.
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
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)
|
||
}
|