mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
feat(api): allow batch status updates
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/ez-api/ez-api/internal/model"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -108,3 +110,53 @@ func TestAdmin_FetchProviderModels_OpenAICompatible(t *testing.T) {
|
||||
t.Fatalf("expected models to update, got %q", updated.Models)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdmin_BatchProviders_Status(t *testing.T) {
|
||||
h, db := newTestHandler(t)
|
||||
|
||||
banUntil := time.Now().Add(2 * time.Hour).UTC()
|
||||
p := &model.Provider{
|
||||
Name: "p1",
|
||||
Type: "openai",
|
||||
BaseURL: "https://api.openai.com/v1",
|
||||
Group: "default",
|
||||
Models: "gpt-4o-mini",
|
||||
Status: "manual_disabled",
|
||||
BanReason: "bad",
|
||||
BanUntil: &banUntil,
|
||||
}
|
||||
if err := db.Create(p).Error; err != nil {
|
||||
t.Fatalf("create provider: %v", err)
|
||||
}
|
||||
|
||||
r := gin.New()
|
||||
r.POST("/admin/providers/batch", h.BatchProviders)
|
||||
|
||||
payload := map[string]any{
|
||||
"action": "status",
|
||||
"status": "active",
|
||||
"ids": []uint{p.ID},
|
||||
}
|
||||
b, _ := json.Marshal(payload)
|
||||
req := httptest.NewRequest(http.MethodPost, "/admin/providers/batch", bytes.NewReader(b))
|
||||
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 updated model.Provider
|
||||
if err := db.First(&updated, p.ID).Error; err != nil {
|
||||
t.Fatalf("reload provider: %v", err)
|
||||
}
|
||||
if updated.Status != "active" {
|
||||
t.Fatalf("expected status active, got %q", updated.Status)
|
||||
}
|
||||
if updated.BanReason != "" {
|
||||
t.Fatalf("expected ban_reason cleared, got %q", updated.BanReason)
|
||||
}
|
||||
if updated.BanUntil != nil {
|
||||
t.Fatalf("expected ban_until cleared, got %v", updated.BanUntil)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user