package webui import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/domain/org" "github.com/samcharles93/archie-core/internal/domain/usage" infraaccess "github.com/samcharles93/archie-core/internal/infrastructure/access" ) // usageTotalsStore answers the usage read with no rows: these cases test the // access decision, not the report. type usageTotalsStore struct{} func (usageTotalsStore) UsageTotals(context.Context, int) ([]usage.TotalRow, error) { return nil, nil } // TestUsageNeedsAdminOrOwner pins the grant: usage names who spent what, so it // carries its own action like the audit feed. A viewer's or developer's broad // read must not open it. func TestUsageNeedsAdminOrOwner(t *testing.T) { tests := []struct { role org.Role want int }{ {org.RoleViewer, http.StatusForbidden}, {org.RoleDeveloper, http.StatusForbidden}, {org.RoleAdmin, http.StatusOK}, {org.RoleOwner, http.StatusOK}, } for _, tt := range tests { t.Run(string(tt.role), func(t *testing.T) { principal := access.Principal{ IdentityID: "alice", Org: "acme", Memberships: []org.Membership{{IdentityID: "alice", OrgID: "acme", Role: tt.role}}, } srv := &Server{ Usage: usageTotalsStore{}, Accounts: accountBoundaryStore{}, Access: infraaccess.New(access.ShippedOrgPolicies("acme")), Principals: principalOf(principal), } req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "https://archie.example/api/usage", nil) req.Header.Set("X-Archie-CSRF", "1") sendCookie(req, "archie_session", "valid") rec := httptest.NewRecorder() srv.Handler().ServeHTTP(rec, req) if rec.Code != tt.want { t.Fatalf("status = %d, want %d body=%s", rec.Code, tt.want, rec.Body.String()) } if tt.want != http.StatusOK { return } var report usageReportView if err := json.Unmarshal(rec.Body.Bytes(), &report); err != nil { t.Fatalf("decode report: %v", err) } if report.Days != 30 || report.ByDay == nil || report.ByIdentity == nil { t.Fatalf("report = %+v, want days 30 with empty slices", report) } }) } }