Public source validation / validate (push) Failing after 3m8s
55 lines
2.4 KiB
Go
55 lines
2.4 KiB
Go
package poolapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/itworx/pulse/internal/auth"
|
|
"github.com/itworx/pulse/internal/pool"
|
|
)
|
|
|
|
type provider struct{ snapshot pool.Snapshot }
|
|
|
|
func (p provider) Snapshot(context.Context) (pool.Snapshot, error) { return p.snapshot, nil }
|
|
|
|
func authenticatedRequest(method, path string) *http.Request {
|
|
request := httptest.NewRequest(method, path, nil)
|
|
return request.WithContext(auth.WithPrincipal(request.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
|
}
|
|
|
|
func TestHandlerRequiresAuthenticationAndReturnsUnknown(t *testing.T) {
|
|
unauthenticated := httptest.NewRecorder()
|
|
Handler{}.ServeHTTP(unauthenticated, httptest.NewRequest(http.MethodGet, "/api/v1/pools", nil))
|
|
if unauthenticated.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status=%d", unauthenticated.Code)
|
|
}
|
|
response := httptest.NewRecorder()
|
|
Handler{}.ServeHTTP(response, authenticatedRequest(http.MethodGet, "/api/v1/pools"))
|
|
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"state":"unknown"`) {
|
|
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandlerReturnsListAndDetailAndRejectsMutations(t *testing.T) {
|
|
snapshot := pool.Snapshot{ContractVersion: pool.ContractVersion, Source: pool.Source{ID: "fixture-pool", Type: "fixture", Freshness: pool.Fresh, State: pool.StateHealthy}, Pools: []pool.Pool{{ID: "cache", Name: "Cache", Filesystem: "btrfs", State: pool.StateDegraded}}, Total: 1, ObservedAt: time.Date(2026, 8, 1, 12, 0, 0, 0, time.UTC)}
|
|
list := httptest.NewRecorder()
|
|
Handler{Provider: provider{snapshot: snapshot}}.ServeHTTP(list, authenticatedRequest(http.MethodGet, "/api/v1/pools?limit=10"))
|
|
if list.Code != http.StatusOK || !strings.Contains(list.Body.String(), `"id":"cache"`) {
|
|
t.Fatalf("status=%d body=%s", list.Code, list.Body.String())
|
|
}
|
|
detail := httptest.NewRecorder()
|
|
Handler{Provider: provider{snapshot: snapshot}}.ServeHTTP(detail, authenticatedRequest(http.MethodGet, "/api/v1/pools/cache"))
|
|
if detail.Code != http.StatusOK || !strings.Contains(detail.Body.String(), `"name":"Cache"`) {
|
|
t.Fatalf("status=%d body=%s", detail.Code, detail.Body.String())
|
|
}
|
|
mutation := httptest.NewRecorder()
|
|
Handler{}.ServeHTTP(mutation, httptest.NewRequest(http.MethodPost, "/api/v1/pools", nil))
|
|
if mutation.Code != http.StatusNotFound {
|
|
t.Fatalf("mutation status=%d", mutation.Code)
|
|
}
|
|
}
|