The portal's 30-day views folded at most 1000 events from the usage list, so busy tenants saw figures that were too low. The summary aggregates every matching event in the store (GROUP BY in Postgres, the same fold in Memory): counts, quantity sums and the latest occurred_at per product and metric, and counts and sums per UTC day and metric. Same filters, tenant pinning for user tokens and route group (usage.read) as GET /v1/usage/events, so the portal's service client needs no new AUTHZ grant. Both stores share the filter (usageWhere / matches). Refs: t80 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
2.7 KiB
Go
79 lines
2.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.breakpilot.com/platform/tenant-registry/internal/authn"
|
|
"git.breakpilot.com/platform/tenant-registry/internal/store"
|
|
)
|
|
|
|
func (f *teamFixture) summary(t *testing.T, path string) (int, store.UsageSummary) {
|
|
t.Helper()
|
|
rr := httptest.NewRecorder()
|
|
f.h.ServeHTTP(rr, httptest.NewRequest("GET", path, nil))
|
|
var out store.UsageSummary
|
|
_ = json.Unmarshal(rr.Body.Bytes(), &out)
|
|
return rr.Code, out
|
|
}
|
|
|
|
// The summary is a usage.read route, like the list: the portal's service
|
|
// client needs no new grant, and a usage.read client still cannot push.
|
|
func TestUsageSummary_authzGroup(t *testing.T) {
|
|
if g := routeGroup("GET", "/v1/usage/summary"); g != "usage.read" {
|
|
t.Fatalf("group: %s", g)
|
|
}
|
|
f := newTeamFixture(t, portal, "portal-svc:usage.read")
|
|
if code, _ := f.summary(t, "/v1/usage/summary?tenant_id="+f.tenant.ID); code != 200 {
|
|
t.Fatalf("usage.read: %d", code)
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
f.h.ServeHTTP(rr, httptest.NewRequest("POST", "/v1/usage/events", nil))
|
|
if rr.Code != 403 {
|
|
t.Fatalf("push with usage.read: %d", rr.Code)
|
|
}
|
|
if code, _ := newTeamFixture(t, portal, "portal-svc:audit").summary(t, "/v1/usage/summary?tenant_id="+f.tenant.ID); code != 403 {
|
|
t.Fatalf("without usage.read: %d", code)
|
|
}
|
|
}
|
|
|
|
// A user token sees its own tenant whatever tenant_id says.
|
|
func TestUsageSummary_userTokenPinnedToOwnTenant(t *testing.T) {
|
|
seed := newTeamFixture(t, nil)
|
|
ctx := context.Background()
|
|
other, err := seed.s.Store.CreateTenant(ctx, store.TenantCreate{Slug: "other-co", Name: "Other"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i, tenant := range []string{seed.tenant.ID, seed.tenant.ID, other.ID} {
|
|
if _, _, err := seed.s.Store.InsertUsageEvent(ctx, store.UsageEvent{IdempotencyKey: fmt.Sprintf("k-%d", i), TenantID: tenant,
|
|
Product: "pruefwerk", Metric: "scan.completed", Quantity: 1, OccurredAt: time.Now()}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
f := newTeamFixture(t, &authn.Principal{Subject: "u-admin", ClientID: "portal", TenantID: seed.tenant.ID})
|
|
f.s.Store = seed.s.Store
|
|
if code, got := f.summary(t, "/v1/usage/summary?tenant_id="+other.ID); code != 200 || got.Events != 2 || got.TenantID != seed.tenant.ID {
|
|
t.Fatalf("user token: %d %+v", code, got)
|
|
}
|
|
}
|
|
|
|
type failingSummaryStore struct{ store.Store }
|
|
|
|
func (failingSummaryStore) SummarizeUsage(context.Context, store.UsageFilter) (*store.UsageSummary, error) {
|
|
return nil, errors.New("boom")
|
|
}
|
|
|
|
func TestUsageSummary_storeError(t *testing.T) {
|
|
f := newTeamFixture(t, nil)
|
|
f.s.Store = failingSummaryStore{f.s.Store}
|
|
if code, _ := f.summary(t, "/v1/usage/summary?tenant_id="+f.tenant.ID); code != 500 {
|
|
t.Fatalf("store error: %d", code)
|
|
}
|
|
}
|