oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "errors"
6 "testing"
7
8 "amadan.net/oficina/equipo/internal/wire"
9 "gorm.io/gorm"
10)
11
12func TestProjectAutonomyAuthority(t *testing.T) {
13 r, fail := rosterRig(t)
14 r.task("tkautonomy", "Autonomy", hkOwner)
15 audits := 0
16 r.s.authority.Audit = func(tx *gorm.DB, actor, kind, detail string) error {
17 if actor != hkOwner || kind != "project.autonomy" || detail == "" {
18 t.Fatalf("wrong audit: %s %s %s", actor, kind, detail)
19 }
20 audits++
21 return nil
22 }
23 read := func() wire.ProjectAutonomy {
24 var got wire.ProjectAutonomy
25 r.must(r.tx(func(tx *txn) error { var err error; got, err = tx.projectAutonomy("p1"); return err }))
26 return got
27 }
28 if got := read(); got.AutoMergeCI || got.PublicPreviews {
29 t.Fatal("authority enabled by default")
30 }
31 r.must(r.s.SetProjectAutonomy(r.ctx, hkOwner, "p1", true, true, 0))
32 if got := read(); !got.AutoMergeCI || !got.PublicPreviews || got.Revision != 1 {
33 t.Fatalf("grant missing: %+v", got)
34 }
35 if audits != 1 {
36 t.Fatal("not audited")
37 }
38 if !errors.Is(r.s.SetProjectAutonomy(r.ctx, hkOwner, "p1", false, false, 0), ErrCardConflict) {
39 t.Fatal("stale write accepted")
40 }
41 r.exec("UPDATE members SET role='member' WHERE address=?", hkOwner)
42 if r.s.SetProjectAutonomy(r.ctx, hkOwner, "p1", true, true, 1) == nil {
43 t.Fatal("member changed authority")
44 }
45 if got := read(); got.AutoMergeCI || got.PublicPreviews {
46 t.Fatal("revoked owner still grants authority")
47 }
48 r.exec("UPDATE members SET role='owner' WHERE address=?", hkOwner)
49 *fail = true
50 if r.tx(func(tx *txn) error { _, err := tx.projectAutonomy("p1"); return err }) == nil {
51 t.Fatal("roster error granted authority")
52 }
53 if r.s.SetProjectAutonomy(r.ctx, hkOwner, "p1", false, false, 1) == nil {
54 t.Fatal("roster error allowed write")
55 }
56 *fail = false
57 r.s.authority.CurrentPerson = func(_ context.Context, _ *gorm.DB) error { return ErrForbidden }
58 if r.s.SetProjectAutonomy(r.ctx, hkOwner, "p1", false, false, 1) == nil {
59 t.Fatal("revoked session allowed write")
60 }
61}
62
63func TestProjectAutonomyAuditRollback(t *testing.T) {
64 r, _ := rosterRig(t)
65 r.task("tkautonomy", "Autonomy", hkOwner)
66 r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return errors.New("audit unavailable") }
67 if r.s.SetProjectAutonomy(r.ctx, hkOwner, "p1", true, true, 0) == nil {
68 t.Fatal("audit failure accepted")
69 }
70 r.must(r.tx(func(tx *txn) error {
71 got, err := tx.projectAutonomy("p1")
72 if got.AutoMergeCI || got.PublicPreviews || got.Revision != 0 {
73 t.Fatalf("failed audit committed: %+v", got)
74 }
75 return err
76 }))
77}
78
79func TestProjectAutonomyInactiveAndCorruptFailClosed(t *testing.T) {
80 r, _ := rosterRig(t)
81 r.task("tkautonomy", "Autonomy", hkOwner)
82 r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return nil }
83 r.must(r.s.SetProjectAutonomy(r.ctx, hkOwner, "p1", true, true, 0))
84 r.exec("UPDATE chat_projects SET state='archived' WHERE id='p1'")
85 r.must(r.tx(func(tx *txn) error {
86 got, err := tx.projectAutonomy("p1")
87 if got.AutoMergeCI || got.PublicPreviews {
88 t.Fatal("inactive project retained authority")
89 }
90 return err
91 }))
92 if r.s.SetProjectAutonomy(r.ctx, hkOwner, "p1", true, true, 1) == nil {
93 t.Fatal("inactive project accepted write")
94 }
95 r.exec("UPDATE chat_projects SET state='active', autonomy='invalid' WHERE id='p1'")
96 if r.tx(func(tx *txn) error { _, err := tx.projectAutonomy("p1"); return err }) == nil {
97 t.Fatal("corrupt settings ignored")
98 }
99 r.exec("DROP TABLE chat_projects")
100 if r.tx(func(tx *txn) error { _, err := tx.projectAutonomy("p1"); return err }) == nil {
101 t.Fatal("lookup error ignored")
102 }
103}
104
105func TestProjectAutonomyRefreshesOpenMergeCards(t *testing.T) {
106 r, _ := rosterRig(t)
107 k := r.task("tkautonomy", "Autonomy", hkOwner)
108 r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return nil }
109 r.must(r.s.db.G.Create(&cardRecord{ID: "merge-autonomy", Person: hkOwner, ProjectID: k.ProjectID, TaskID: k.ID, Kind: wire.CardMerge, State: wire.CardOpen, Revision: 1}).Error)
110 r.must(r.s.SetProjectAutonomy(r.ctx, hkOwner, k.ProjectID, true, false, 0))
111 var c cardRecord
112 r.must(r.s.db.G.First(&c, "id = ?", "merge-autonomy").Error)
113 if c.Revision != 2 {
114 t.Fatalf("stale merge card revision: %d", c.Revision)
115 }
116}
117