package server import ( "context" "encoding/json" "git.a-wan.net/alan/pipelines/internal/client" "git.a-wan.net/alan/pipelines/internal/protocol" "net/http" "net/http/httptest" "net/url" "strings" "testing" "time" ) func TestStatusRPCReportsServerAndNtfyWithoutSecrets(t *testing.T) { ntfy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/v1/health" { t.Error("unexpected probe", r.URL.Path) } w.Write([]byte(`{"healthy":true}`)) })) defer ntfy.Close() t.Setenv("PIPELINES_NTFY_URL", ntfy.URL+"/phone") t.Setenv("PIPELINES_NTFY_PUBLIC_URL", ntfy.URL) t.Setenv("PIPELINES_NTFY_TOKEN", "never-return-this-token") srv := httptest.NewServer(Handler(context.Background())) defer srv.Close() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() c, err := client.Open(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect") if err != nil { t.Fatal(err) } defer c.Close() response, err := c.Call(ctx, protocol.Message{Type: protocol.GetStatus}) if err != nil { t.Fatal(err) } s := response.Status if s == nil || s.Server.Health != "healthy" || s.Ntfy.Health != "healthy" || s.Ntfy.URL != ntfy.URL || s.Topic != "phone" || len(s.Addresses) == 0 || s.Hostname == "" || s.Server.Port == "" || s.Ntfy.Port == "" || s.StartedAt == "" || len(s.Ntfy.Candidates) == 0 { t.Fatal("incomplete status", s) } encoded, _ := json.Marshal(s) if strings.Contains(string(encoded), "never-return") { t.Fatal("credentials leaked", string(encoded)) } if !strings.Contains(s.Server.URL, s.Server.Port) || !strings.Contains(s.Note, "not reachability checks") { t.Fatal("ports or safety disclaimer missing", s) } } func TestStatusUnconfiguredUnhealthyAndIPv6Candidates(t *testing.T) { t.Setenv("PIPELINES_NTFY_URL", "") t.Setenv("PIPELINES_NTFY_PUBLIC_URL", "") r := httptest.NewRequest("GET", "http://localhost/connect", nil) if s := systemStatus(r, time.Now(), nil); s.Ntfy.Health != "not configured" { t.Fatal(s) } for _, body := range []string{`{"healthy":false}`, `not-json`} { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(body)) })) t.Setenv("PIPELINES_NTFY_URL", srv.URL+"/topic") s := systemStatus(r, time.Now(), nil) srv.Close() if s.Ntfy.Health != "unhealthy" || s.Ntfy.URL != "" || s.Ntfy.Port == "" { t.Fatal(s) } } addresses := []protocol.NetworkAddress{{Interface: "test", IP: "::1"}, {Interface: "test", IP: "192.0.2.1"}} urls := candidateURLs(addresses, "http", "8090", "/topic") if urls[0] != "http://[::1]:8090/topic" || urls[1] != "http://192.0.2.1:8090/topic" { t.Fatal(urls) } if localEndpoint(&url.URL{Host: "remote.example:8090", Scheme: "http"}, "test", addresses) { t.Fatal("remote endpoint guessed local") } }