Files
ez-api/internal/api/provider_create_handler_test.go

93 lines
2.3 KiB
Go

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/v1" {
t.Fatalf("expected base_url=https://api.openai.com/v1, 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())
}
}
func TestCreateProviderGoogle_GeminiRequiresAPIKey(t *testing.T) {
h, _ := newTestHandler(t)
r := gin.New()
r.POST("/admin/providers/google", h.CreateProviderGoogle)
reqBody := map[string]any{
"type": "gemini",
"models": []string{"gemini-2.0-flash"},
}
b, _ := json.Marshal(reqBody)
req := httptest.NewRequest(http.MethodPost, "/admin/providers/google", 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())
}
}