package store import ( "context" "database/sql" "errors" "fmt" "strings" "testing" ) func TestApprovalBackendAtomicity(t *testing.T) { for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} { t.Run(fmt.Sprint(b), func(t *testing.T) { s, d := accountBackendStore(t, b) ctx := context.Background() if err := s.EnsurePublicationApprovalSchema(); err != nil { t.Fatal(err) } request := ApprovalRequest{DomainUUID: d, ScopeKind: "spot", ScopeUUID: "sp", SubjectHash: strings.Repeat("a", 64), SubjectJSON: `{}`, RequesterUUID: "requester", IdempotencyKey: "same"} type result struct { request ApprovalRequest inserted bool err error } create := func(req ApprovalRequest) result { tx, e := s.DB.Begin() if e != nil { return result{err: e} } defer tx.Rollback() r, new, e := s.CreateApprovalRequestTx(tx, req) if e == nil { e = tx.Commit() } return result{r, new, e} } results := make(chan result, 2) for i := 0; i < 2; i++ { go func() { results <- create(request) }() } id := "" inserted := 0 for i := 0; i < 2; i++ { r := <-results if r.err != nil { t.Fatal(r.err) } if id != "" && id != r.request.UUID { t.Fatal("idempotency split") } id = r.request.UUID if r.inserted { inserted++ } } if inserted != 1 { t.Fatal("duplicate request", inserted) } if err := s.DecideApprovalRequestScoped(ctx, "outside", id, "reviewer", "approved", "", 1); !errors.Is(err, sql.ErrNoRows) { t.Fatal("cross-domain decision", err) } decisions := make(chan error, 2) for i := 0; i < 2; i++ { go func(i int) { decisions <- s.DecideApprovalRequestScoped(ctx, d, id, fmt.Sprint(i), "approved", "", 1) }(i) } winners := 0 for i := 0; i < 2; i++ { err := <-decisions if err == nil { winners++ } else if !errors.Is(err, ErrApprovalTransition) { t.Fatal(err) } } if winners != 1 { t.Fatal("two approval winners", winners) } var count int if err := s.DB.QueryRow(`SELECT count(*) FROM publication_decisions`).Scan(&count); err != nil || count != 1 { t.Fatal("partial/duplicate decisions", count, err) } request.IdempotencyKey = "expired" request.ExpiresAtMS = 1 r := create(request) if r.err != nil { t.Fatal(r.err) } if err := s.DecideApprovalRequestScoped(ctx, d, r.request.UUID, "reviewer", "approved", "", 1); !errors.Is(err, ErrApprovalTransition) { t.Fatal("expired request approved", err) } if n, err := s.ExpireApprovalRequests(ctx, 2, 1); err != nil || n != 1 { t.Fatal("bounded expiry", n, err) } request.IdempotencyKey = "rollback" request.ExpiresAtMS = 0 r = create(request) if r.err != nil { t.Fatal(r.err) } if _, err := s.DB.Exec(`ALTER TABLE publication_decisions RENAME COLUMN justification TO unavailable_justification`); err != nil { t.Fatal(err) } if err := s.DecideApprovalRequestScoped(ctx, d, r.request.UUID, "reviewer", "approved", "", 1); err == nil { t.Fatal("missing decision persistence accepted") } pending, err := s.GetApprovalRequest(ctx, d, r.request.UUID) if err != nil || pending.State != "pending" { t.Fatal("partial approval", pending, err) } }) } } type publicationFixture struct { s *Store domain, actor string t *testing.T } func (f publicationFixture) exec(q string, args ...any) { f.t.Helper() if _, err := f.s.DB.Exec(q, args...); err != nil { f.t.Fatal(err) } } func newPublicationBackend(t *testing.T, b storeSQLBackend) publicationFixture { t.Helper() s, d := localityBackendStore(t, b) if err := s.ensurePublicationSchema(); err != nil { t.Fatal(err) } p, err := s.BootstrapOwner(d, "owner", "synthetic publication password", "Owner", "") if err != nil { t.Fatal(err) } f := publicationFixture{s, d, p.UUID, t} f.exec(`INSERT INTO zones(uuid,domain_uuid,kind,name,path_prefix,created_at_ms,updated_at_ms) VALUES(`+s.sqlValues(7)+`)`, "q", d, "quartier", "q", "/news_%", 1, 1) f.exec(`INSERT INTO zones(uuid,domain_uuid,kind,parent_uuid,name,path_prefix,created_at_ms,updated_at_ms) VALUES(`+s.sqlValues(8)+`)`, "st", d, "street", "q", "st", "/news_%/st", 1, 1) f.exec(`INSERT INTO spots(uuid,domain_uuid,name,path_prefix,created_at_ms,updated_at_ms) VALUES(`+s.sqlValues(6)+`)`, "sp", d, "sp", "/news_%/st/sp", 1, 1) f.exec(`INSERT INTO street_spots(street_uuid,spot_uuid,created_at_ms) VALUES('st','sp',1)`) if err = s.SetConfigInDomain(d, "authorization_mode", "enforcing"); err != nil { t.Fatal(err) } for _, node := range []struct{ kind, id string }{{"domain", d}, {"quartier", "q"}, {"spot", "sp"}} { g, e := s.CityGovernance(context.Background(), d, p.UUID, node.kind, node.id) if e != nil { t.Fatal(e) } public := "public" _, e = s.ChangeCityGovernance(context.Background(), d, p.UUID, node.kind, node.id, CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &public, ConfirmRelaxation: true}, false) if e != nil { t.Fatal(e) } } return f } func (f publicationFixture) page(object, path, rev string, stamp int64) { f.exec(`INSERT INTO objects(uuid,kind,created_at) VALUES(`+f.s.sqlValues(3)+`)`, object, "page", 1) f.exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at) VALUES(`+f.s.sqlValues(4)+`)`, f.domain, path, object, 1) f.revision(object, rev, stamp) } func (f publicationFixture) revision(object, rev string, stamp int64) { f.exec(`INSERT INTO revisions(uuid,object_uuid,document_hash,lang,content_type,filename,created_at_ms,created_at,body) VALUES(`+f.s.sqlValues(9)+`)`, rev, object, strings.Repeat("a", 64), "ja", "text/html", "", stamp, stamp/1000, "") } func (f publicationFixture) approve() LocalityPublication { f.t.Helper() ctx := context.Background() p, err := f.s.LocalityPublication(ctx, f.domain, f.actor, "spot", "sp", nil) if err != nil { f.t.Fatal(err) } p, err = f.s.LocalityPublication(ctx, f.domain, f.actor, "spot", "sp", &PublicationChange{PublicationVersion: p.PublicationVersion, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, ImpactToken: p.ImpactToken, Enabled: true, Confirm: true}) if err != nil { f.t.Fatal(err) } return p } func (f publicationFixture) automatic(kind, id, mode string) { f.t.Helper() ctx := context.Background() p, err := f.s.AutoPublication(ctx, f.domain, f.actor, kind, id, nil) if err != nil { f.t.Fatal(err) } _, err = f.s.AutoPublication(ctx, f.domain, f.actor, kind, id, &AutoPublicationChange{Mode: mode, Version: p.Version, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Confirm: true, ImpactToken: p.Impact.ImpactToken}) if err != nil { f.t.Fatal(err) } } func (f publicationFixture) expect(path, revision string) { f.t.Helper() v, err := f.s.AnonymousRevision(context.Background(), f.domain, path, []string{"ja"}) if revision == "" { if !errors.Is(err, sql.ErrNoRows) { f.t.Fatalf("%s unexpectedly published: %v %v", path, v, err) } return } if err != nil || v.UUID != revision { f.t.Fatalf("%s: %v %v want %s", path, v, err, revision) } } func TestPublicationBackendManual(t *testing.T) { for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} { t.Run(fmt.Sprint(b), func(t *testing.T) { f := newPublicationBackend(t, b) s, d := f.s, f.domain ctx := context.Background() path := "/news_%/st/sp/日本語" f.page("obj", path, "r1", 1) f.expect(path, "") preview, err := s.LocalityPublication(ctx, d, f.actor, "spot", "sp", nil) if err != nil || len(preview.Entries) != 1 { t.Fatal(preview, err) } f.revision("obj", "r2", 2) if _, err = s.LocalityPublication(ctx, d, f.actor, "spot", "sp", &PublicationChange{PublicationVersion: preview.PublicationVersion, Epoch: preview.Epoch, TopologyVersion: preview.TopologyVersion, ImpactToken: preview.ImpactToken, Enabled: true, Confirm: true}); !errors.Is(err, ErrPublicationImpact) { t.Fatal("stale content approved", err) } approved := f.approve() f.expect(path, "r2") nav, err := s.PublicNavigation(ctx, d) if err != nil || len(nav) != 1 || nav[0].Path != path { t.Fatal(nav, err) } if err = s.SetConfigInDomain(d, "authorization_mode", "permissive"); err == nil { t.Fatal("publication enforcement removed") } generation, err := s.NavigationGeneration(ctx) if err != nil { t.Fatal(err) } f.revision("obj", "r3", 3) f.expect(path, "") next, err := s.NavigationGeneration(ctx) if err != nil || next <= generation { t.Fatal("generation did not advance", err) } if _, err = s.LocalityPublication(ctx, d, f.actor, "spot", "sp", &PublicationChange{PublicationVersion: approved.PublicationVersion, Epoch: approved.Epoch, TopologyVersion: approved.TopologyVersion, Enabled: false}); err != nil { t.Fatal("revocation", err) } f.approve() f.expect(path, "r3") f.exec(`UPDATE campuses SET enabled=0 WHERE uuid=(SELECT campus_uuid FROM domains WHERE uuid=`+s.sqlParam(1)+`)`, d) f.expect(path, "") f.exec(`UPDATE campuses SET enabled=1 WHERE uuid=(SELECT campus_uuid FROM domains WHERE uuid=`+s.sqlParam(1)+`)`, d) f.expect(path, "r3") f.exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at) VALUES(`+s.sqlValues(4)+`)`, d, "/outside", "obj", 1) f.expect(path, "") nav, err = s.PublicNavigation(ctx, d) if err != nil || len(nav) != 0 { t.Fatal("alias did not revoke publication", nav, err) } if b == storePostgreSQL { if err = s.ensurePostgreSQLPublication(ctx); err != nil { t.Fatal(err) } f.exec(`ALTER TABLE revisions DISABLE TRIGGER publication_revision_changed`) if err = s.ensurePostgreSQLPublication(ctx); err == nil { t.Fatal("disabled guard accepted") } } }) } } func TestPublicationBackendAutomatic(t *testing.T) { for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} { t.Run(fmt.Sprint(b), func(t *testing.T) { f := newPublicationBackend(t, b) s, d := f.s, f.domain ctx := context.Background() path := "/news_%/st/sp/page" f.page("obj", path, "r1", 1) f.automatic("spot", "sp", "automatic") f.expect(path, "r1") f.revision("obj", "r2", 2) f.expect(path, "r2") f.page("next", "/news_%/st/sp/next", "n1", 1) f.page("hidden", "/newsXX/st/sp/hidden", "h1", 1) nav, err := s.PublicNavigation(ctx, d) if err != nil || len(nav) != 2 { t.Fatal(nav, err) } f.expect("/newsXX/st/sp/hidden", "") f.expect("/u/owner", "") f.automatic("street", "st", "required") f.expect(path, "") f.automatic("street", "st", "inherit") f.expect(path, "r2") if err = s.SetPrincipalState(d, f.actor, "suspended"); err != nil { t.Fatal(err) } f.expect(path, "") if err = s.SetPrincipalState(d, f.actor, "active"); err != nil { t.Fatal(err) } f.expect(path, "r2") g, err := s.CityGovernance(ctx, d, f.actor, "quartier", "q") if err != nil { t.Fatal(err) } private := "authenticated_acl" if _, err = s.ChangeCityGovernance(ctx, d, f.actor, "quartier", "q", CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &private}, false); err != nil { t.Fatal(err) } f.expect(path, "") }) } }