54 lines
1.9 KiB
Go
54 lines
1.9 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
postgresstore "knowledge-governance/internal/platform/postgres"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
type monitorTasksTestStore struct {
|
|
fakeJobStore
|
|
filter postgresstore.MonitorTaskFilter
|
|
}
|
|
|
|
func (s *monitorTasksTestStore) ListMonitorTasks(_ context.Context, f postgresstore.MonitorTaskFilter) (postgresstore.MonitorTaskPage, error) {
|
|
s.filter = f
|
|
return postgresstore.MonitorTaskPage{Items: []postgresstore.MonitorTask{}, Total: 145, Limit: f.Limit, Offset: f.Offset}, nil
|
|
}
|
|
func TestMonitorTasksRoute(t *testing.T) {
|
|
s := &monitorTasksTestStore{}
|
|
req := httptest.NewRequest("GET", "/api/v1/monitor/tasks?status=active&limit=20&offset=40", nil)
|
|
req = req.WithContext(context.WithValue(req.Context(), sessionKey{}, sessionIdentity{Username: "tester", Role: roleViewer}))
|
|
w := httptest.NewRecorder()
|
|
h, err := NewHandler(Config{Store: s})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h.ServeHTTP(w, req)
|
|
if w.Code != 200 {
|
|
t.Fatalf("status %d: %s", w.Code, w.Body.String())
|
|
}
|
|
if s.filter.Status != "active" || s.filter.Offset != 40 {
|
|
t.Fatalf("filter %#v", s.filter)
|
|
}
|
|
for _, query := range []string{"status=garbage", "stage=internal&scope=internal", "limit=101", "offset=-1", "since=bad"} {
|
|
r := httptest.NewRequest("GET", "/api/v1/monitor/tasks?"+query, nil).WithContext(req.Context())
|
|
rr := httptest.NewRecorder()
|
|
h.ServeHTTP(rr, r)
|
|
if rr.Code != 400 {
|
|
t.Fatalf("invalid %s: %d", query, rr.Code)
|
|
}
|
|
}
|
|
unauth := httptest.NewRecorder()
|
|
h.ServeHTTP(unauth, httptest.NewRequest("GET", "/api/v1/monitor/tasks", nil))
|
|
if unauth.Code != 401 {
|
|
t.Fatalf("unauthenticated %d", unauth.Code)
|
|
}
|
|
removal := httptest.NewRecorder()
|
|
h.ServeHTTP(removal, httptest.NewRequest("POST", "/api/v1/test-publications/example/removal-retry", nil).WithContext(req.Context()))
|
|
if removal.Code != 403 {
|
|
t.Fatalf("viewer removal %d", removal.Code)
|
|
}
|
|
}
|