mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
87 lines
2.4 KiB
Go
87 lines
2.4 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/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 newTestHandlerWithNamespace(t *testing.T) (*Handler, *gorm.DB, *miniredis.Miniredis) {
|
|
t.Helper()
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
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.Provider{}, &model.Binding{}, &model.Namespace{}); 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, mr
|
|
}
|
|
|
|
func TestNamespaceCRUD_DeleteCleansBindings(t *testing.T) {
|
|
h, db, _ := newTestHandlerWithNamespace(t)
|
|
|
|
if err := db.Create(&model.Binding{
|
|
Namespace: "ns1",
|
|
PublicModel: "m1",
|
|
RouteGroup: "default",
|
|
SelectorType: "exact",
|
|
SelectorValue: "m1",
|
|
Status: "active",
|
|
}).Error; err != nil {
|
|
t.Fatalf("create binding: %v", err)
|
|
}
|
|
|
|
r := gin.New()
|
|
r.POST("/admin/namespaces", h.CreateNamespace)
|
|
r.DELETE("/admin/namespaces/:id", h.DeleteNamespace)
|
|
|
|
body := []byte(`{"name":"ns1","description":"demo"}`)
|
|
req := httptest.NewRequest(http.MethodPost, "/admin/namespaces", bytes.NewReader(body))
|
|
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 created model.Namespace
|
|
if err := json.Unmarshal(rr.Body.Bytes(), &created); err != nil {
|
|
t.Fatalf("unmarshal: %v", err)
|
|
}
|
|
|
|
delReq := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/admin/namespaces/%d", created.ID), nil)
|
|
delRec := httptest.NewRecorder()
|
|
r.ServeHTTP(delRec, delReq)
|
|
|
|
if delRec.Code != http.StatusOK {
|
|
t.Fatalf("expected 200, got %d body=%s", delRec.Code, delRec.Body.String())
|
|
}
|
|
|
|
var remaining int64
|
|
if err := db.Model(&model.Binding{}).Where("namespace = ?", "ns1").Count(&remaining).Error; err != nil {
|
|
t.Fatalf("count bindings: %v", err)
|
|
}
|
|
if remaining != 0 {
|
|
t.Fatalf("expected bindings deleted, got %d", remaining)
|
|
}
|
|
}
|