Files
vctp2/server/handler/legacy_gate_test.go
Nathan Coad 1f39b46613
All checks were successful
continuous-integration/drone/push Build is passing
Deprecate legacy VM inventory endpoints and add gating logic
2026-02-13 14:59:19 +11:00

116 lines
3.2 KiB
Go

package handler
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"vctp/internal/settings"
)
func newLegacyGateHandler(enabled bool) *Handler {
cfg := &settings.Settings{Values: &settings.SettingsYML{}}
cfg.Values.Settings.EnableLegacyAPI = enabled
return &Handler{
Logger: newTestLogger(),
Settings: cfg,
}
}
func TestDenyLegacyAPIDisabledByDefault(t *testing.T) {
h := newLegacyGateHandler(false)
rr := httptest.NewRecorder()
denied := h.denyLegacyAPI(rr, "/api/event/vm/create")
if !denied {
t.Fatal("expected legacy API to be denied by default")
}
if rr.Code != http.StatusGone {
t.Fatalf("expected %d, got %d", http.StatusGone, rr.Code)
}
if !strings.Contains(rr.Body.String(), "deprecated") {
t.Fatalf("unexpected response body: %s", rr.Body.String())
}
}
func TestDenyLegacyAPIEnabledViaSettings(t *testing.T) {
h := newLegacyGateHandler(true)
rr := httptest.NewRecorder()
denied := h.denyLegacyAPI(rr, "/api/event/vm/create")
if denied {
t.Fatal("expected legacy API to be allowed when setting is enabled")
}
if rr.Body.Len() != 0 {
t.Fatalf("expected no response body write, got: %s", rr.Body.String())
}
}
func TestVmCreateEventHonorsLegacyGate(t *testing.T) {
t.Run("disabled", func(t *testing.T) {
h := newLegacyGateHandler(false)
req := httptest.NewRequest(http.MethodPost, "/api/event/vm/create", strings.NewReader("{invalid"))
rr := httptest.NewRecorder()
h.VmCreateEvent(rr, req)
if rr.Code != http.StatusGone {
t.Fatalf("expected %d, got %d", http.StatusGone, rr.Code)
}
})
t.Run("enabled", func(t *testing.T) {
h := newLegacyGateHandler(true)
req := httptest.NewRequest(http.MethodPost, "/api/event/vm/create", strings.NewReader("{invalid"))
rr := httptest.NewRecorder()
h.VmCreateEvent(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("expected %d when gate is open, got %d", http.StatusBadRequest, rr.Code)
}
})
}
func TestLegacyInventoryEndpointsAreGatedWhenDisabled(t *testing.T) {
h := newLegacyGateHandler(false)
cases := []struct {
name string
method string
path string
body string
call func(*Handler, *httptest.ResponseRecorder, *http.Request)
}{
{
name: "import vm",
method: http.MethodPost,
path: "/api/import/vm",
body: `{"name":"vm1"}`,
call: func(h *Handler, rr *httptest.ResponseRecorder, req *http.Request) { h.VmImport(rr, req) },
},
{
name: "cleanup vm",
method: http.MethodDelete,
path: "/api/inventory/vm/delete?vm_id=vm-1&datacenter_name=dc1",
call: func(h *Handler, rr *httptest.ResponseRecorder, req *http.Request) { h.VmCleanup(rr, req) },
},
{
name: "update vm details",
method: http.MethodPost,
path: "/api/inventory/vm/update",
call: func(h *Handler, rr *httptest.ResponseRecorder, req *http.Request) { h.VmUpdateDetails(rr, req) },
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(tc.method, tc.path, strings.NewReader(tc.body))
rr := httptest.NewRecorder()
tc.call(h, rr, req)
if rr.Code != http.StatusGone {
t.Fatalf("expected %d, got %d", http.StatusGone, rr.Code)
}
if !strings.Contains(rr.Body.String(), "deprecated") {
t.Fatalf("expected deprecated response, got: %s", rr.Body.String())
}
})
}
}