Files
felhom-controller/controller/internal/api/geo_test.go
T
admin 02a4ba0491 controller: immediate hub report push on geo change + always-report geo (B)
- Geo settings save and manual geo sync now fire an out-of-band, non-blocking hub
  report push (Router.reportPushNow seam, wired in main.go to BuildReport+Push in a
  goroutine) so the hub reflects the new geo state / clears a stale last_sync_error
  within seconds instead of after the next ~15-min cycle. Scope: geo handlers only.
- builder.go always populates report.GeoRestriction (Enabled=false, empty countries
  when nil/disabled) via new buildGeoRestrictionReport helper, so the hub always
  renders the geo section ("Inaktív" when off) instead of hiding it via omitempty.
- Tests: geo save success → push once; invalid country → no push (companion);
  buildGeoRestrictionReport(nil) → non-nil disabled (companion vs old nil-omit).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-16 13:00:32 +02:00

58 lines
1.7 KiB
Go

package api
import (
"bytes"
"io"
"log"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"gitea.dooplex.hu/admin/felhom-controller/internal/config"
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
)
func newGeoTestRouter(t *testing.T) (*Router, *int) {
t.Helper()
sett, err := settings.Load(filepath.Join(t.TempDir(), "settings.json"), log.New(io.Discard, "", 0))
if err != nil {
t.Fatalf("settings.Load: %v", err)
}
calls := 0
r := &Router{cfg: &config.Config{}, sett: sett, logger: log.New(io.Discard, "", 0)}
r.triggerReportPush = func() { calls++ }
return r, &calls
}
func postGeoSettings(t *testing.T, r *Router, body string) *httptest.ResponseRecorder {
t.Helper()
req := httptest.NewRequest(http.MethodPost, "/api/geo/settings", bytes.NewReader([]byte(body)))
rec := httptest.NewRecorder()
r.geoUpdateSettings(rec, req)
return rec
}
func TestGeoUpdateSettings_Success_PushesReport(t *testing.T) {
r, calls := newGeoTestRouter(t)
rec := postGeoSettings(t, r, `{"enabled":true,"allowed_countries":["HU"]}`)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d, want 200; body=%s", rec.Code, rec.Body.String())
}
if *calls != 1 {
t.Fatalf("report push called %d times, want 1 (successful save)", *calls)
}
}
// COMPANION: a validation-failed save (invalid country code) must NOT push a report.
func TestGeoUpdateSettings_InvalidCountry_NoPush(t *testing.T) {
r, calls := newGeoTestRouter(t)
rec := postGeoSettings(t, r, `{"enabled":true,"allowed_countries":["XX"]}`)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400 (invalid country code)", rec.Code)
}
if *calls != 0 {
t.Fatalf("report push called %d times, want 0 (validation failed before save)", *calls)
}
}