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.
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package server_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"chat-agent-system/internal/auth"
|
|
"chat-agent-system/internal/config"
|
|
)
|
|
|
|
func loginTestAdmin(t *testing.T, handler http.Handler, cfg *config.Config) string {
|
|
t.Helper()
|
|
username := cfg.AdminUsername
|
|
password := 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()
|
|
handler.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
|
|
}
|
|
|
|
func withBearerAuth(req *http.Request, token string) {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
|
|
func loginTestUser(t *testing.T, handler http.Handler, username, password string) string {
|
|
t.Helper()
|
|
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()
|
|
handler.ServeHTTP(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("login %s failed: %d %s", username, 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)
|
|
}
|
|
return session.Token
|
|
}
|
|
|
|
func withUserIDAuth(req *http.Request, token string) {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|