Public source validation / validate (push) Failing after 3m8s
58 lines
2.6 KiB
Go
58 lines
2.6 KiB
Go
package containerapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/itworx/pulse/internal/auth"
|
|
"github.com/itworx/pulse/internal/container"
|
|
)
|
|
|
|
type provider struct{ value container.Snapshot }
|
|
|
|
func (p provider) Snapshot(context.Context) (container.Snapshot, error) { return p.value, nil }
|
|
func authRequest(method, path string) *http.Request {
|
|
r := httptest.NewRequest(method, path, nil)
|
|
return r.WithContext(auth.WithPrincipal(r.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
|
}
|
|
|
|
func TestHandlerListDetailAndNoMutation(t *testing.T) {
|
|
snapshot := container.Snapshot{ContractVersion: container.ContractVersion, Source: container.Source{ID: "agent", State: "healthy"}, Containers: []container.Container{{ID: "abc", Name: "media", State: "running", Health: "unhealthy"}}, Total: 1}
|
|
h := Handler{Provider: provider{value: snapshot}}
|
|
list := httptest.NewRecorder()
|
|
h.ServeHTTP(list, authRequest(http.MethodGet, "/api/v1/containers?limit=1"))
|
|
if list.Code != http.StatusOK || !strings.Contains(list.Body.String(), `"name":"media"`) {
|
|
t.Fatalf("status=%d body=%s", list.Code, list.Body.String())
|
|
}
|
|
detail := httptest.NewRecorder()
|
|
h.ServeHTTP(detail, authRequest(http.MethodGet, "/api/v1/containers/abc"))
|
|
if detail.Code != http.StatusOK || !strings.Contains(detail.Body.String(), `"container":{"id":"abc"`) {
|
|
t.Fatalf("status=%d body=%s", detail.Code, detail.Body.String())
|
|
}
|
|
mutate := httptest.NewRecorder()
|
|
h.ServeHTTP(mutate, authRequest(http.MethodPost, "/api/v1/containers/abc/restart"))
|
|
if mutate.Code != http.StatusNotFound {
|
|
t.Fatalf("mutation=%d", mutate.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandlerRequiresAuthentication(t *testing.T) {
|
|
response := httptest.NewRecorder()
|
|
Handler{}.ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/api/v1/containers", nil))
|
|
if response.Code != http.StatusUnauthorized {
|
|
t.Fatalf("status=%d", response.Code)
|
|
}
|
|
}
|
|
|
|
func TestHandlerAppliesFiltersBeforePagination(t *testing.T) {
|
|
snapshot := container.Snapshot{Source: container.Source{ID: "agent", State: "healthy"}, Containers: []container.Container{{ID: "a", Name: "api", State: "running", Health: "healthy"}, {ID: "b", Name: "database", State: "running", Health: "healthy"}}, Total: 2}
|
|
response := httptest.NewRecorder()
|
|
Handler{Provider: provider{value: snapshot}}.ServeHTTP(response, authRequest(http.MethodGet, "/api/v1/containers?limit=1&q=database&state=running&health=healthy&sort=name"))
|
|
if response.Code != http.StatusOK || !strings.Contains(response.Body.String(), `"name":"database"`) || !strings.Contains(response.Body.String(), `"total":1`) {
|
|
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
}
|