mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
106 lines
2.9 KiB
Go
106 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/ez-api/ez-api/internal/dto"
|
|
"github.com/ez-api/ez-api/internal/model"
|
|
"github.com/ez-api/ez-api/internal/service"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func newTestHandler(t *testing.T) (*Handler, *gorm.DB) {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
// Use a unique in-memory DB per test to avoid cross-test interference.
|
|
dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", t.Name())
|
|
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatalf("open sqlite: %v", err)
|
|
}
|
|
if err := db.AutoMigrate(&model.Master{}, &model.Key{}, &model.Provider{}, &model.Model{}, &model.Binding{}, &model.LogRecord{}); err != nil {
|
|
t.Fatalf("migrate: %v", err)
|
|
}
|
|
|
|
mr := miniredis.RunT(t)
|
|
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
|
sync := service.NewSyncService(rdb)
|
|
|
|
return NewHandler(db, db, sync, nil, rdb, nil), db
|
|
}
|
|
|
|
func TestCreateProvider_DefaultsVertexLocationGlobal(t *testing.T) {
|
|
h, _ := newTestHandler(t)
|
|
|
|
r := gin.New()
|
|
r.POST("/admin/providers", h.CreateProvider)
|
|
|
|
reqBody := dto.ProviderDTO{
|
|
Name: "g1",
|
|
Type: "vertex-express",
|
|
Group: "default",
|
|
Models: []string{"gemini-3-pro-preview"},
|
|
}
|
|
b, _ := json.Marshal(reqBody)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/admin/providers", bytes.NewReader(b))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rr := httptest.NewRecorder()
|
|
r.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusCreated {
|
|
t.Fatalf("expected 201, got %d body=%s", rr.Code, rr.Body.String())
|
|
}
|
|
var got model.Provider
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if got.GoogleLocation != "global" {
|
|
t.Fatalf("expected google_location=global, got %q", got.GoogleLocation)
|
|
}
|
|
}
|
|
|
|
func TestUpdateProvider_DefaultsVertexLocationGlobalWhenMissing(t *testing.T) {
|
|
h, db := newTestHandler(t)
|
|
|
|
existing := &model.Provider{
|
|
Name: "g2",
|
|
Type: "vertex",
|
|
Group: "default",
|
|
Models: "gemini-3-pro-preview",
|
|
Status: "active",
|
|
}
|
|
if err := db.Create(existing).Error; err != nil {
|
|
t.Fatalf("create provider: %v", err)
|
|
}
|
|
|
|
r := gin.New()
|
|
r.PUT("/admin/providers/:id", h.UpdateProvider)
|
|
|
|
req := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/admin/providers/%d", existing.ID), bytes.NewReader([]byte(`{}`)))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rr := httptest.NewRecorder()
|
|
r.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
|
|
}
|
|
var got model.Provider
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
if got.GoogleLocation != "global" {
|
|
t.Fatalf("expected google_location=global, got %q", got.GoogleLocation)
|
|
}
|
|
}
|