47 lines
1.8 KiB
Go
47 lines
1.8 KiB
Go
package notify
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"gitea.dooplex.hu/admin/felhom-controller/internal/settings"
|
|
)
|
|
|
|
// R-1199 reopened: the push names its source on the wire and returns the hub's "kept"; the read sends the box's own
|
|
// customer id with its key and decodes the hub's answer. The hub side: felhom.eu hub household_prefs.go.
|
|
func TestR1199Reopened_TheWire(t *testing.T) {
|
|
var gotSource, gotQuery, gotAuth string
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotAuth = r.Header.Get("Authorization")
|
|
if r.Method == http.MethodGet {
|
|
gotQuery = r.URL.RawQuery
|
|
w.Write([]byte(`{"exists":true,"household_saved":true,"email":"f@example.hu","enabled_events":["node_down"],"cooldown_hours":6}`))
|
|
return
|
|
}
|
|
var body map[string]any
|
|
json.NewDecoder(r.Body).Decode(&body)
|
|
gotSource, _ = body["source"].(string)
|
|
w.Write([]byte(`{"status":"kept","household_saved":true}`))
|
|
}))
|
|
defer srv.Close()
|
|
sett, _ := settings.Load(filepath.Join(t.TempDir(), "settings.json"), log.New(io.Discard, "", 0))
|
|
n := New(srv.URL, "box-key", "demo-felhom", sett, log.New(io.Discard, "", 0), false)
|
|
|
|
status, err := n.SyncPreferencesAs(PrefsSourceDefault, "", []string{"node_down"}, 6, false)
|
|
if err != nil || status != "kept" || gotSource != "default" {
|
|
t.Fatalf("push: status=%q err=%v source=%q", status, err, gotSource)
|
|
}
|
|
hp, err := n.FetchPreferences()
|
|
if err != nil || !hp.HouseholdSaved || len(hp.EnabledEvents) != 1 || gotQuery != "customer_id=demo-felhom" || gotAuth != "Bearer box-key" {
|
|
t.Fatalf("read: %+v err=%v query=%q", hp, err, gotQuery)
|
|
}
|
|
if err := n.SyncPreferences("f@example.hu", nil, 6, false); err != nil || gotSource != "household" {
|
|
t.Fatalf("the page save must be the household's: err=%v source=%q", err, gotSource)
|
|
}
|
|
}
|