vibe-proxy/backend/internal/api/handlers/management/auth_files_upload_test.go
2026-08-24 00:10:41 +02:00

69 lines
2.1 KiB
Go

package management
import (
"bytes"
"encoding/json"
"mime/multipart"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
coreauth "github.com/router-for-me/CLIProxyAPI/v7/sdk/cliproxy/auth"
)
func TestUploadAuthFile_PreservesPriorityAttributes(t *testing.T) {
t.Setenv("MANAGEMENT_PASSWORD", "")
gin.SetMode(gin.TestMode)
authDir := t.TempDir()
manager := coreauth.NewManager(nil, nil, nil)
h := NewHandlerWithoutConfigFilePath(&config.Config{AuthDir: authDir}, manager)
content := `{"type":"codex","email":"midai0530@gmail.com","priority":98}`
var body bytes.Buffer
writer := multipart.NewWriter(&body)
part, err := writer.CreateFormFile("file", "codex-midai0530@gmail.com-plus.json")
if err != nil {
t.Fatalf("failed to create multipart file: %v", err)
}
if _, err = part.Write([]byte(content)); err != nil {
t.Fatalf("failed to write multipart content: %v", err)
}
if err = writer.Close(); err != nil {
t.Fatalf("failed to close multipart writer: %v", err)
}
rec := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(rec)
req := httptest.NewRequest(http.MethodPost, "/v0/management/auth-files", &body)
req.Header.Set("Content-Type", writer.FormDataContentType())
ctx.Request = req
h.UploadAuthFile(ctx)
if rec.Code != http.StatusOK {
t.Fatalf("expected upload status %d, got %d with body %s", http.StatusOK, rec.Code, rec.Body.String())
}
var payload map[string]any
if err = json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if status, _ := payload["status"].(string); status != "ok" {
t.Fatalf("expected status ok, got %#v", payload["status"])
}
auth, ok := manager.GetByID("codex-midai0530@gmail.com-plus.json")
if !ok || auth == nil {
t.Fatalf("expected uploaded auth record to exist")
}
if got := auth.Attributes["priority"]; got != "98" {
t.Fatalf("priority attribute = %q, want %q", got, "98")
}
if got := auth.Metadata["priority"]; got != float64(98) {
t.Fatalf("priority metadata = %#v, want 98", got)
}
}