Replace state blob with resource-oriented API endpoints
Split /api/v1/state into dedicated resource endpoints: - /api/v1/operator, /api/v1/central-domain - /api/v1/ddns/config, /api/v1/dns/settings - /api/v1/dhcp/config (moved from /dnsmasq/dhcp/) - /api/v1/nftables/config, /api/v1/haproxy/routes Each page now talks to its own resource instead of deep-merging a blob. Removes useConfig hook, CentralState type, and legacy config methods.
This commit is contained in:
@@ -9,31 +9,22 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/wild-cloud/wild-central/internal/storage"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func TestGetState_ReturnsWrappedResponse(t *testing.T) {
|
||||
func TestGetOperator(t *testing.T) {
|
||||
api, tmpDir := setupTestAPI(t)
|
||||
|
||||
// Write a config with a central domain
|
||||
cfg := map[string]any{
|
||||
"cloud": map[string]any{
|
||||
"central": map[string]any{
|
||||
"domain": "central.example.com",
|
||||
},
|
||||
},
|
||||
"operator": map[string]any{
|
||||
"email": "test@example.com",
|
||||
},
|
||||
// Write state with operator email
|
||||
state := map[string]any{
|
||||
"operator": map[string]any{"email": "test@example.com"},
|
||||
}
|
||||
data, _ := yaml.Marshal(cfg)
|
||||
storage.WriteFile(filepath.Join(tmpDir, "state.yaml"), data, 0644)
|
||||
data, _ := yaml.Marshal(state)
|
||||
os.WriteFile(filepath.Join(tmpDir, "state.yaml"), data, 0644)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/v1/state", nil)
|
||||
req := httptest.NewRequest("GET", "/api/v1/operator", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
api.GetState(w, req)
|
||||
api.GetOperator(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
@@ -41,34 +32,47 @@ func TestGetState_ReturnsWrappedResponse(t *testing.T) {
|
||||
|
||||
var resp map[string]any
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
|
||||
// Must have "configured" and "state" keys
|
||||
if resp["configured"] != true {
|
||||
t.Errorf("expected configured=true, got %v", resp["configured"])
|
||||
}
|
||||
state, ok := resp["state"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected state to be a map, got %T", resp["state"])
|
||||
}
|
||||
|
||||
// Verify nested structure is preserved
|
||||
cloud, _ := state["cloud"].(map[string]any)
|
||||
central, _ := cloud["central"].(map[string]any)
|
||||
if central["domain"] != "central.example.com" {
|
||||
t.Errorf("expected domain central.example.com, got %v", central["domain"])
|
||||
if resp["email"] != "test@example.com" {
|
||||
t.Errorf("expected email test@example.com, got %v", resp["email"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetState_EmptyReturnsNotConfigured(t *testing.T) {
|
||||
func TestUpdateOperator(t *testing.T) {
|
||||
api, tmpDir := setupTestAPI(t)
|
||||
|
||||
// Ensure no state file exists
|
||||
os.Remove(filepath.Join(tmpDir, "state.yaml"))
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/v1/state", nil)
|
||||
body, _ := json.Marshal(map[string]string{"email": "new@example.com"})
|
||||
req := httptest.NewRequest("PUT", "/api/v1/operator", bytes.NewReader(body))
|
||||
w := httptest.NewRecorder()
|
||||
api.UpdateOperator(w, req)
|
||||
|
||||
api.GetState(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// Verify persisted
|
||||
data, _ := os.ReadFile(filepath.Join(tmpDir, "state.yaml"))
|
||||
var state map[string]any
|
||||
yaml.Unmarshal(data, &state)
|
||||
op := state["operator"].(map[string]any)
|
||||
if op["email"] != "new@example.com" {
|
||||
t.Errorf("expected email new@example.com, got %v", op["email"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetCentralDomain(t *testing.T) {
|
||||
api, tmpDir := setupTestAPI(t)
|
||||
|
||||
state := map[string]any{
|
||||
"cloud": map[string]any{
|
||||
"central": map[string]any{"domain": "central.example.com"},
|
||||
},
|
||||
}
|
||||
data, _ := yaml.Marshal(state)
|
||||
os.WriteFile(filepath.Join(tmpDir, "state.yaml"), data, 0644)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/v1/central-domain", nil)
|
||||
w := httptest.NewRecorder()
|
||||
api.GetCentralDomain(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
@@ -76,9 +80,27 @@ func TestGetState_EmptyReturnsNotConfigured(t *testing.T) {
|
||||
|
||||
var resp map[string]any
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp["domain"] != "central.example.com" {
|
||||
t.Errorf("expected domain central.example.com, got %v", resp["domain"])
|
||||
}
|
||||
}
|
||||
|
||||
if resp["configured"] != false {
|
||||
t.Errorf("expected configured=false, got %v", resp["configured"])
|
||||
func TestUpdateCentralDomain(t *testing.T) {
|
||||
api, _ := setupTestAPI(t)
|
||||
|
||||
body, _ := json.Marshal(map[string]string{"domain": "new.example.com"})
|
||||
req := httptest.NewRequest("PUT", "/api/v1/central-domain", bytes.NewReader(body))
|
||||
w := httptest.NewRecorder()
|
||||
api.UpdateCentralDomain(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
var resp map[string]any
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
if resp["domain"] != "new.example.com" {
|
||||
t.Errorf("expected domain new.example.com, got %v", resp["domain"])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -103,12 +125,10 @@ func TestGetGlobalSecrets_RedactsLeafValues(t *testing.T) {
|
||||
var resp map[string]any
|
||||
json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
|
||||
// Top-level leaf should be redacted
|
||||
if resp["topLevelSecret"] != "********" {
|
||||
t.Errorf("expected topLevelSecret redacted, got %v", resp["topLevelSecret"])
|
||||
}
|
||||
|
||||
// Nested map structure should be preserved
|
||||
cf, ok := resp["cloudflare"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected cloudflare to be a map, got %T", resp["cloudflare"])
|
||||
@@ -116,9 +136,6 @@ func TestGetGlobalSecrets_RedactsLeafValues(t *testing.T) {
|
||||
if cf["apiToken"] != "********" {
|
||||
t.Errorf("expected apiToken redacted, got %v", cf["apiToken"])
|
||||
}
|
||||
if cf["zoneId"] != "********" {
|
||||
t.Errorf("expected zoneId redacted, got %v", cf["zoneId"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetGlobalSecrets_RawShowsValues(t *testing.T) {
|
||||
@@ -146,39 +163,6 @@ func TestGetGlobalSecrets_RawShowsValues(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateState(t *testing.T) {
|
||||
api, tmpDir := setupTestAPI(t)
|
||||
|
||||
update := map[string]any{
|
||||
"cloud": map[string]any{
|
||||
"central": map[string]any{
|
||||
"domain": "new.example.com",
|
||||
},
|
||||
},
|
||||
}
|
||||
body, _ := json.Marshal(update)
|
||||
|
||||
req := httptest.NewRequest("PUT", "/api/v1/state", bytes.NewReader(body))
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
api.UpdateState(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// Verify file was written
|
||||
data, _ := os.ReadFile(filepath.Join(tmpDir, "state.yaml"))
|
||||
var cfg map[string]any
|
||||
yaml.Unmarshal(data, &cfg)
|
||||
|
||||
cloud := cfg["cloud"].(map[string]any)
|
||||
central := cloud["central"].(map[string]any)
|
||||
if central["domain"] != "new.example.com" {
|
||||
t.Errorf("expected domain new.example.com, got %v", central["domain"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloudflareVerify_NoToken(t *testing.T) {
|
||||
api, _ := setupTestAPI(t)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user