Public source validation / validate (push) Failing after 3m8s
45 lines
1.6 KiB
Go
45 lines
1.6 KiB
Go
package eventapi
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/itworx/pulse/internal/auth"
|
|
)
|
|
|
|
type fakeStore struct{ limit int }
|
|
|
|
func (s *fakeStore) List(_ context.Context, limit int) ([]Event, error) {
|
|
s.limit = limit
|
|
return []Event{{ID: "event-1", Type: "discovery.completed", Severity: "info", Summary: "Inventaris bijgewerkt", Attributes: []byte(`{}`)}}, nil
|
|
}
|
|
|
|
func TestHandlerListsBoundedEvents(t *testing.T) {
|
|
store := &fakeStore{}
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/events?limit=25", nil)
|
|
req = req.WithContext(auth.WithPrincipal(req.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
|
res := httptest.NewRecorder()
|
|
Handler{Store: store}.ServeHTTP(res, req)
|
|
if res.Code != http.StatusOK || store.limit != 25 || !strings.Contains(res.Body.String(), `"id":"event-1"`) {
|
|
t.Fatalf("status=%d limit=%d body=%s", res.Code, store.limit, res.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHandlerRejectsInvalidLimitAndAnonymous(t *testing.T) {
|
|
res := httptest.NewRecorder()
|
|
Handler{Store: &fakeStore{}}.ServeHTTP(res, httptest.NewRequest(http.MethodGet, "/api/v1/events", nil))
|
|
if res.Code != http.StatusUnauthorized {
|
|
t.Fatalf("anonymous status=%d", res.Code)
|
|
}
|
|
req := httptest.NewRequest(http.MethodGet, "/api/v1/events?limit=101", nil)
|
|
req = req.WithContext(auth.WithPrincipal(req.Context(), auth.Principal{Subject: "viewer", Role: auth.RoleViewer}))
|
|
res = httptest.NewRecorder()
|
|
Handler{Store: &fakeStore{}}.ServeHTTP(res, req)
|
|
if res.Code != http.StatusBadRequest {
|
|
t.Fatalf("limit status=%d", res.Code)
|
|
}
|
|
}
|