Public source validation / validate (push) Failing after 3m8s
46 lines
1.9 KiB
Go
46 lines
1.9 KiB
Go
package systemstatusapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/itworx/pulse/internal/systemstatus"
|
|
)
|
|
|
|
func TestHandlerServesStatusAndRedactedDiagnostics(t *testing.T) {
|
|
snapshot := systemstatus.Snapshot{Version: "test", OverallState: systemstatus.StateUnknown}
|
|
handler := Handler{
|
|
Snapshot: func(context.Context) (systemstatus.Snapshot, error) { return snapshot, nil },
|
|
Diagnostics: func(context.Context) (Diagnostics, error) {
|
|
return Diagnostics{Status: snapshot, Config: ConfigSummary{DatabaseConfigured: true}, Metrics: "pulse_up 1\n"}, nil
|
|
},
|
|
}
|
|
response := httptest.NewRecorder()
|
|
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/system/status", nil))
|
|
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"overallState":"unknown"`) {
|
|
t.Fatalf("status response = %d %s", response.Code, response.Body.String())
|
|
}
|
|
response = httptest.NewRecorder()
|
|
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/system/diagnostics", nil))
|
|
if response.Code != http.StatusOK || strings.Contains(response.Body.String(), "password") {
|
|
t.Fatalf("diagnostics response = %d %s", response.Code, response.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandlerRejectsUnsupportedMethodAndPath(t *testing.T) {
|
|
handler := Handler{Snapshot: func(context.Context) (systemstatus.Snapshot, error) { return systemstatus.Snapshot{}, nil }}
|
|
response := httptest.NewRecorder()
|
|
handler.ServeHTTP(response, httptest.NewRequest(http.MethodPost, "/api/v1/system/status", nil))
|
|
if response.Code != http.StatusMethodNotAllowed {
|
|
t.Fatalf("method status = %d", response.Code)
|
|
}
|
|
response = httptest.NewRecorder()
|
|
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/system/unknown", nil))
|
|
if response.Code != http.StatusNotFound {
|
|
t.Fatalf("path status = %d", response.Code)
|
|
}
|
|
}
|