Public source validation / validate (push) Failing after 3m8s
64 lines
2.4 KiB
Go
64 lines
2.4 KiB
Go
package arrayapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/itworx/pulse/internal/array"
|
|
"github.com/itworx/pulse/internal/auth"
|
|
)
|
|
|
|
type provider struct{ snapshot array.Snapshot }
|
|
|
|
func (p provider) Snapshot(context.Context) (array.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/array", nil))
|
|
if unauthenticated.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status=%d", unauthenticated.Code)
|
|
}
|
|
response := httptest.NewRecorder()
|
|
Handler{}.ServeHTTP(response, authenticatedRequest(http.MethodGet, "/api/v1/array"))
|
|
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"state":"unknown"`) {
|
|
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
}
|
|
func TestHandlerReturnsSnapshotAndRejectsMutations(t *testing.T) {
|
|
snapshot := array.UnknownSnapshot(time.Date(2026, 8, 1, 12, 0, 0, 0, time.UTC), "fixture-array", "fixture", "test")
|
|
response := httptest.NewRecorder()
|
|
Handler{Provider: provider{snapshot: snapshot}}.ServeHTTP(response, authenticatedRequest(http.MethodGet, "/api/v1/array"))
|
|
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"id":"fixture-array"`) {
|
|
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
mutation := httptest.NewRecorder()
|
|
Handler{}.ServeHTTP(mutation, authenticatedRequest(http.MethodPost, "/api/v1/array/check"))
|
|
if mutation.Code != http.StatusNotFound {
|
|
t.Fatalf("status=%d", mutation.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandlerEncodesEmptyCollectionsAsArrays(t *testing.T) {
|
|
response := httptest.NewRecorder()
|
|
Handler{Provider: provider{snapshot: array.Snapshot{}}}.ServeHTTP(response, authenticatedRequest(http.MethodGet, "/api/v1/array"))
|
|
var body struct {
|
|
Members []array.Member `json:"members"`
|
|
History []array.Check `json:"history"`
|
|
}
|
|
if err := json.Unmarshal(response.Body.Bytes(), &body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if body.Members == nil || body.History == nil {
|
|
t.Fatalf("empty collections must be JSON arrays: %s", response.Body.String())
|
|
}
|
|
}
|