forked from longtao.wu/droboticflow
77 lines
2.8 KiB
Go
77 lines
2.8 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/productionstats"
|
|
"droboticflow/internal/store"
|
|
"fmt"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
type preparedProductionStore struct {
|
|
store.Store
|
|
calls atomic.Int64
|
|
allowedIdentity string
|
|
}
|
|
|
|
func (s *preparedProductionStore) PreparedProductionResponse(_ context.Context, org string, projects []string, query string) ([]byte, bool) {
|
|
s.calls.Add(1)
|
|
key := fmt.Sprint(org, projects, query)
|
|
if s.allowedIdentity == "" {
|
|
s.allowedIdentity = key
|
|
}
|
|
if key != s.allowedIdentity {
|
|
return nil, false
|
|
}
|
|
return []byte(`{"cachedFixture":true}`), true
|
|
}
|
|
|
|
func TestQueryServingProductionResponseChecksCurrentPermissionBeforeCache(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
_, err := f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{f.task.ProjectID}, Revision: 1}, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &preparedProductionStore{Store: f.store}
|
|
f.handler = NewHandler(s, nil)
|
|
path := "/api/dashboard/production?from=2026-11-01&to=2026-11-12"
|
|
r := f.request("GET", path, f.admin, nil)
|
|
if r.Code != 200 || !strings.Contains(r.Body.String(), "cachedFixture") {
|
|
t.Fatalf("cache not used %d %s", r.Code, r.Body.String())
|
|
}
|
|
before := s.calls.Load()
|
|
for _, user := range []domain.UserRecord{f.annotator, f.reviewer, {ID: "supplier-manager", OrganizationID: domain.DefaultOrganizationID, SupplierID: f.annotator.SupplierID, Role: domain.UserRoleSupplierManager, Status: domain.UserStatusActive}} {
|
|
if r = f.request("GET", path, user, nil); r.Code != 403 {
|
|
t.Fatalf("unauthorized cache read role=%s status=%d", user.Role, r.Code)
|
|
}
|
|
}
|
|
if s.calls.Load() != before {
|
|
t.Fatal("unauthorized request touched prepared response")
|
|
}
|
|
// Revoking a configured project changes identity before any cached body is
|
|
// served, even when its old generation and cache entry still exist.
|
|
_, err = f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{}, Revision: 2}, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.handler = NewHandler(s, nil)
|
|
r = f.request("GET", path, f.admin, nil)
|
|
if r.Code != 200 || strings.Contains(r.Body.String(), "cachedFixture") {
|
|
t.Fatal("revoked project reused old cached body", r.Code)
|
|
}
|
|
}
|
|
|
|
func TestQueryServingPrivateMaterializationsRejectDatabaseBrowser(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
for _, name := range []string{"queryReadModelControl", "queryReadModelData", "queryBuildJobs", "queryBuildParts"} {
|
|
for _, target := range []string{"platform", "business"} {
|
|
if r := f.request("GET", "/api/database/collections/"+name+"/documents?target="+target, f.admin, nil); r.Code < 400 {
|
|
t.Fatalf("private read model leaked: %s %s", name, target)
|
|
}
|
|
}
|
|
}
|
|
}
|