Files
vctp2/server/handler/legacy_gate_test.go
Nathan Coad c66679a71f
All checks were successful
continuous-integration/drone/push Build is passing
more index cleanups to optimise space
2026-02-08 15:40:42 +11:00

70 lines
1.9 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)
}
})
}