mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(api): add provider creation endpoints and weight support
- Add `POST /admin/providers/preset` for streamlined creation of official providers (OpenAI, Anthropic, Gemini) - Add `POST /admin/providers/custom` for generic OpenAI-compatible providers - Add `weight` field to provider model and DTOs to enable weighted routing - Update sync service to propagate provider weights - Add unit tests for new creation handlers
This commit is contained in:
70
internal/api/provider_create_handler_test.go
Normal file
70
internal/api/provider_create_handler_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestCreateProviderPreset_OpenAI_SetsBaseURL(t *testing.T) {
|
||||
h, _ := newTestHandler(t)
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/preset", h.CreateProviderPreset)
|
||||
|
||||
reqBody := map[string]any{
|
||||
"preset": "openai",
|
||||
"api_key": "k",
|
||||
"models": []string{"gpt-4o-mini"},
|
||||
}
|
||||
b, _ := json.Marshal(reqBody)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/preset", 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.Type != "openai" {
|
||||
t.Fatalf("expected type openai, got %q", got.Type)
|
||||
}
|
||||
if got.BaseURL != "https://api.openai.com" {
|
||||
t.Fatalf("expected base_url=https://api.openai.com, got %q", got.BaseURL)
|
||||
}
|
||||
if got.Name == "" {
|
||||
t.Fatalf("expected generated name")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateProviderCustom_RequiresBaseURL(t *testing.T) {
|
||||
h, _ := newTestHandler(t)
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/custom", h.CreateProviderCustom)
|
||||
|
||||
reqBody := map[string]any{
|
||||
"name": "c1",
|
||||
"api_key": "k",
|
||||
}
|
||||
b, _ := json.Marshal(reqBody)
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/custom", bytes.NewReader(b))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
rr := httptest.NewRecorder()
|
||||
r.ServeHTTP(rr, req)
|
||||
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d body=%s", rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user