| 1 | package github |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/forge/forgetest" |
| 14 | "amadan.net/rastrillo/rastrillo/db" |
| 15 | "gorm.io/gorm" |
| 16 | "gorm.io/plugin/dbresolver" |
| 17 | ) |
| 18 | |
| 19 | type testOwner struct { |
| 20 | Address string `gorm:"primaryKey"` |
| 21 | Active bool |
| 22 | } |
| 23 | type testAudit struct { |
| 24 | ID int64 |
| 25 | Person, Kind, Record string |
| 26 | } |
| 27 | type rig struct { |
| 28 | s *Store |
| 29 | g *forgetest.GitHub |
| 30 | now time.Time |
| 31 | owner, other Principal |
| 32 | } |
| 33 | |
| 34 | func storeRig(t *testing.T) *rig { |
| 35 | t.Helper() |
| 36 | d, err := db.Open(filepath.Join(t.TempDir(), "github.db"), slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | t.Cleanup(func() { d.Close() }) |
| 41 | if err = d.G.AutoMigrate(&Connection{}, &Pending{}, &testOwner{}, &testAudit{}); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | for _, a := range []string{"owner@example.test", "other@example.test"} { |
| 45 | if err = d.G.Create(&testOwner{a, true}).Error; err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | } |
| 49 | r := &rig{g: forgetest.NewGitHub(t), now: time.Now().UTC(), owner: Principal{"owner@example.test", "first-session"}, other: Principal{"other@example.test", "second-session"}} |
| 50 | r.g.Now = r.now |
| 51 | r.s = &Store{DB: d.G, Root: "test-instance-key", Origin: "https://equipo.example", Enabled: true, Now: func() time.Time { return r.now }} |
| 52 | r.s.API = NewAPI(r.g.Transport(), func() time.Time { return r.now }) |
| 53 | r.s.Write = func(ctx context.Context, fn func(*gorm.DB) error) error { |
| 54 | return d.G.WithContext(ctx).Clauses(dbresolver.Write).Transaction(fn) |
| 55 | } |
| 56 | r.s.Owner = func(tx *gorm.DB, person string) error { |
| 57 | var owner testOwner |
| 58 | if tx.Where("address = ? AND active = ?", person, true).First(&owner).Error != nil { |
| 59 | return ErrAccess |
| 60 | } |
| 61 | return nil |
| 62 | } |
| 63 | r.s.Audit = func(tx *gorm.DB, person, kind, id string) error { |
| 64 | return tx.Create(&testAudit{Person: person, Kind: kind, Record: id}).Error |
| 65 | } |
| 66 | return r |
| 67 | } |
| 68 | func (r *rig) created(t *testing.T) View { |
| 69 | t.Helper() |
| 70 | began, err := r.s.Start(context.Background(), r.owner, r.g.Login, r.g.AccountType) |
| 71 | if err != nil { |
| 72 | t.Fatal("start:", err) |
| 73 | } |
| 74 | view, err := r.s.Convert(context.Background(), r.owner, began.State, r.g.Code) |
| 75 | if err != nil { |
| 76 | t.Fatal("convert:", err) |
| 77 | } |
| 78 | if view.AppID != r.g.AppID { |
| 79 | t.Fatal("converted wrong App") |
| 80 | } |
| 81 | return view |
| 82 | } |
| 83 | func TestForgeConnectionLifecycle(t *testing.T) { |
| 84 | r := storeRig(t) |
| 85 | ctx := context.Background() |
| 86 | view := r.created(t) |
| 87 | var stored Connection |
| 88 | if err := r.s.DB.First(&stored, "id = ?", view.ID).Error; err != nil { |
| 89 | t.Fatal(err) |
| 90 | } |
| 91 | if stored.SealedKey == "" || strings.Contains(stored.SealedKey, r.g.PEM) || stored.ManifestHash != "" { |
| 92 | t.Fatal("key not sealed or callback state not consumed") |
| 93 | } |
| 94 | key, err := openKey(r.s.Root, r.s.Origin, view.ID, r.g.AppID, stored.SealedKey) |
| 95 | if err != nil || key != r.g.PEM { |
| 96 | t.Fatal("sealed wrong key") |
| 97 | } |
| 98 | checked, err := r.s.Check(ctx, r.other, view.ID) |
| 99 | if err != nil || checked.State != "ready" || checked.Count != 2 || !checked.CountKnown { |
| 100 | t.Fatal("other owner check:", err, checked) |
| 101 | } |
| 102 | live, err := r.s.Connect(ctx, r.other, view.ID) |
| 103 | if err != nil || live.State != "live" || !live.Confirmed { |
| 104 | t.Fatal("other owner connect:", err, live) |
| 105 | } |
| 106 | down, err := r.s.Disconnect(ctx, r.owner, view.ID) |
| 107 | if err != nil || down.State != "disconnected" { |
| 108 | t.Fatal("disconnect:", err) |
| 109 | } |
| 110 | if err := r.s.DB.First(&stored, "id = ?", view.ID).Error; err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | if stored.SealedKey != "" || stored.ActiveAccount != nil || stored.Generation <= live.Generation { |
| 114 | t.Fatal("disconnect retained authority") |
| 115 | } |
| 116 | var audits []testAudit |
| 117 | r.s.DB.Find(&audits) |
| 118 | for _, kind := range []string{"forge.start", "forge.convert", "forge.check", "forge.connect", "forge.disconnect"} { |
| 119 | found := false |
| 120 | for _, row := range audits { |
| 121 | found = found || row.Kind == kind |
| 122 | } |
| 123 | if !found { |
| 124 | t.Error("missing audit", kind) |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | func TestForgeStartSinglePending(t *testing.T) { |
| 129 | r := storeRig(t) |
| 130 | old := r.created(t) |
| 131 | next, err := r.s.Start(context.Background(), r.other, "another-org", "Organization") |
| 132 | if err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | if next.Superseded == nil || next.Superseded.ID != old.ID { |
| 136 | t.Fatal("old App cleanup not shown") |
| 137 | } |
| 138 | var record Connection |
| 139 | r.s.DB.First(&record, "id = ?", old.ID) |
| 140 | if record.SealedKey != "" || record.State != "superseded" { |
| 141 | t.Fatal("superseded App key retained") |
| 142 | } |
| 143 | var pending []Pending |
| 144 | r.s.DB.Find(&pending) |
| 145 | if len(pending) != 1 || pending[0].ConnectionID != next.View.ID { |
| 146 | t.Fatal("more than one pending setup") |
| 147 | } |
| 148 | var rows []testAudit |
| 149 | r.s.DB.Where("kind = ?", "forge.supersede").Find(&rows) |
| 150 | if len(rows) != 1 { |
| 151 | t.Fatal("replacement not audited") |
| 152 | } |
| 153 | } |
| 154 | func TestForgeCallbacksBound(t *testing.T) { |
| 155 | for _, change := range []string{"owner", "session", "origin", "expired", "replay", "roster"} { |
| 156 | t.Run(change, func(t *testing.T) { |
| 157 | r := storeRig(t) |
| 158 | ctx := context.Background() |
| 159 | began, err := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType) |
| 160 | if err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | who := r.owner |
| 164 | switch change { |
| 165 | case "owner": |
| 166 | who = r.other |
| 167 | case "session": |
| 168 | who.Session = "new-session" |
| 169 | case "origin": |
| 170 | r.s.Origin = "https://other.example" |
| 171 | case "expired": |
| 172 | r.now = r.now.Add(10 * time.Minute) |
| 173 | case "replay": |
| 174 | if _, err = r.s.Convert(ctx, who, began.State, r.g.Code); err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | case "roster": |
| 178 | r.s.DB.Model(&testOwner{}).Where("address = ?", who.Address).Update("active", false) |
| 179 | } |
| 180 | _, err = r.s.Convert(ctx, who, began.State, r.g.Code) |
| 181 | if err == nil { |
| 182 | t.Fatal("invalid callback accepted") |
| 183 | } |
| 184 | want := 0 |
| 185 | if change == "replay" { |
| 186 | want = 1 |
| 187 | } |
| 188 | if got := r.g.Calls("POST", "/app-manifests/"+r.g.Code+"/conversions"); got != want { |
| 189 | t.Fatal("unbound conversion reached GitHub") |
| 190 | } |
| 191 | }) |
| 192 | } |
| 193 | } |
| 194 | func TestForgeFlagAndOwner(t *testing.T) { |
| 195 | for _, enabled := range []bool{false, true} { |
| 196 | t.Run(map[bool]string{false: "off", true: "member"}[enabled], func(t *testing.T) { |
| 197 | r := storeRig(t) |
| 198 | r.s.Enabled = enabled |
| 199 | who := r.owner |
| 200 | if enabled { |
| 201 | who.Address = "member@example.test" |
| 202 | } |
| 203 | if _, err := r.s.Start(context.Background(), who, r.g.Login, r.g.AccountType); !errors.Is(err, ErrAccess) { |
| 204 | t.Fatal("setup admitted without flag/current owner") |
| 205 | } |
| 206 | var n int64 |
| 207 | r.s.DB.Model(&Connection{}).Count(&n) |
| 208 | if n != 0 { |
| 209 | t.Fatal("refused start wrote a connection") |
| 210 | } |
| 211 | }) |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | func (r *rig) advance(d time.Duration) { r.now = r.now.Add(d); r.g.Now = r.now } |
| 216 | func TestForgePendingAndUninstallDeadlines(t *testing.T) { |
| 217 | ctx := context.Background() |
| 218 | t.Run("pending24h", func(t *testing.T) { |
| 219 | r := storeRig(t) |
| 220 | view := r.created(t) |
| 221 | r.advance(11 * time.Minute) |
| 222 | if _, err := r.s.Check(ctx, r.other, view.ID); err != nil { |
| 223 | t.Fatal("Check again incorrectly needs live redirect:", err) |
| 224 | } |
| 225 | r.advance(24*time.Hour - 11*time.Minute) |
| 226 | got, err := r.s.Check(ctx, r.other, view.ID) |
| 227 | if err == nil || got.State != "expired" { |
| 228 | t.Fatal("pending key did not expire") |
| 229 | } |
| 230 | var c Connection |
| 231 | r.s.DB.First(&c, "id = ?", view.ID) |
| 232 | if c.SealedKey != "" { |
| 233 | t.Fatal("expired key retained") |
| 234 | } |
| 235 | }) |
| 236 | for _, reinstall := range []bool{true, false} { |
| 237 | t.Run(map[bool]string{true: "reinstall", false: "uninstall7d"}[reinstall], func(t *testing.T) { |
| 238 | r := storeRig(t) |
| 239 | view := r.created(t) |
| 240 | if _, err := r.s.Connect(ctx, r.owner, view.ID); err != nil { |
| 241 | t.Fatal(err) |
| 242 | } |
| 243 | r.g.Uninstalled = true |
| 244 | down, err := r.s.Check(ctx, r.other, view.ID) |
| 245 | if err != nil || down.State != "uninstalled" { |
| 246 | t.Fatal("uninstall not detected") |
| 247 | } |
| 248 | var c Connection |
| 249 | r.s.DB.First(&c, "id = ?", view.ID) |
| 250 | first := c.UninstalledAt |
| 251 | if c.SealedKey == "" { |
| 252 | t.Fatal("uninstall immediately erased key") |
| 253 | } |
| 254 | r.advance(3 * 24 * time.Hour) |
| 255 | if _, err = r.s.Check(ctx, r.other, view.ID); err != nil { |
| 256 | t.Fatal(err) |
| 257 | } |
| 258 | r.s.DB.First(&c, "id = ?", view.ID) |
| 259 | if !c.UninstalledAt.Equal(first) { |
| 260 | t.Fatal("refresh extended uninstall retention") |
| 261 | } |
| 262 | if reinstall { |
| 263 | r.g.Uninstalled = false |
| 264 | got, err := r.s.Check(ctx, r.other, view.ID) |
| 265 | if err != nil || got.State != "live" || !got.Confirmed { |
| 266 | t.Fatal("reinstall required another Connect:", err) |
| 267 | } |
| 268 | } else { |
| 269 | r.advance(4 * 24 * time.Hour) |
| 270 | got, err := r.s.Check(ctx, r.other, view.ID) |
| 271 | if err == nil || got.State != "uninstalled_expired" { |
| 272 | t.Fatal("uninstall retention not enforced") |
| 273 | } |
| 274 | r.s.DB.First(&c, "id = ?", view.ID) |
| 275 | if c.SealedKey != "" || c.ActiveAccount != nil { |
| 276 | t.Fatal("uninstall expiry retained key/authority") |
| 277 | } |
| 278 | } |
| 279 | }) |
| 280 | } |
| 281 | } |
| 282 | func TestForgeAlreadyConnectedPreservesPending(t *testing.T) { |
| 283 | r := storeRig(t) |
| 284 | ctx := context.Background() |
| 285 | view := r.created(t) |
| 286 | if _, err := r.s.Connect(ctx, r.owner, view.ID); err != nil { |
| 287 | t.Fatal(err) |
| 288 | } |
| 289 | pending, err := r.s.Start(ctx, r.other, "another-org", "Organization") |
| 290 | if err != nil { |
| 291 | t.Fatal(err) |
| 292 | } |
| 293 | existing, err := r.s.Start(ctx, r.other, r.g.Login, r.g.AccountType) |
| 294 | if err != nil || existing.View.State != "already_connected" { |
| 295 | t.Fatal("duplicate start not refused") |
| 296 | } |
| 297 | var slot Pending |
| 298 | r.s.DB.First(&slot, "id = ?", 1) |
| 299 | if slot.ConnectionID != pending.View.ID { |
| 300 | t.Fatal("duplicate start replaced unrelated pending setup") |
| 301 | } |
| 302 | } |
| 303 | func TestForgeConnectUnknownCount(t *testing.T) { |
| 304 | r := storeRig(t) |
| 305 | ctx := context.Background() |
| 306 | view := r.created(t) |
| 307 | r.g.Count = 1001 |
| 308 | over, err := r.s.Check(ctx, r.other, view.ID) |
| 309 | if err != nil || over.State != "too_many" || over.CountKnown { |
| 310 | t.Fatal("overflow became a known count") |
| 311 | } |
| 312 | if _, err = r.s.Connect(ctx, r.other, view.ID); err == nil { |
| 313 | t.Fatal("Connect accepted unknown count") |
| 314 | } |
| 315 | r.g.Count = 1000 |
| 316 | checked, err := r.s.Check(ctx, r.other, view.ID) |
| 317 | if err != nil || !checked.CountKnown || checked.Count != 1000 { |
| 318 | t.Fatal("recheck did not recover complete count") |
| 319 | } |
| 320 | if _, err = r.s.Connect(ctx, r.other, view.ID); err != nil { |
| 321 | t.Fatal("Connect refused complete inventory:", err) |
| 322 | } |
| 323 | } |
| 324 | func TestForgeInstallationCallback(t *testing.T) { |
| 325 | for _, test := range []string{"ok", "expired", "wrong-session", "replay"} { |
| 326 | t.Run(test, func(t *testing.T) { |
| 327 | r := storeRig(t) |
| 328 | ctx := context.Background() |
| 329 | view := r.created(t) |
| 330 | var c Connection |
| 331 | r.s.DB.First(&c, "id = ?", view.ID) |
| 332 | state := r.s.state(c, "installation") |
| 333 | who := r.owner |
| 334 | switch test { |
| 335 | case "expired": |
| 336 | r.advance(10 * time.Minute) |
| 337 | case "wrong-session": |
| 338 | who.Session = "different" |
| 339 | case "replay": |
| 340 | if _, err := r.s.Install(ctx, who, state); err != nil { |
| 341 | t.Fatal(err) |
| 342 | } |
| 343 | } |
| 344 | got, err := r.s.Install(ctx, who, state) |
| 345 | if test == "ok" { |
| 346 | if err != nil || got.State != "ready" { |
| 347 | t.Fatal("installation callback:", err) |
| 348 | } |
| 349 | return |
| 350 | } |
| 351 | if err == nil { |
| 352 | t.Fatal("invalid installation callback accepted") |
| 353 | } |
| 354 | if test == "expired" { |
| 355 | if got.State != "installation_expired" { |
| 356 | t.Fatal("wrong expiry state") |
| 357 | } |
| 358 | if _, err := r.s.Check(ctx, r.other, view.ID); err != nil { |
| 359 | t.Fatal("installation expiry lost held key") |
| 360 | } |
| 361 | } |
| 362 | }) |
| 363 | } |
| 364 | } |
| 365 | func TestForgeAuditRollback(t *testing.T) { |
| 366 | r := storeRig(t) |
| 367 | ctx := context.Background() |
| 368 | r.s.Audit = func(*gorm.DB, string, string, string) error { return ErrUnavailable } |
| 369 | if _, err := r.s.Start(ctx, r.owner, r.g.Login, r.g.AccountType); err == nil { |
| 370 | t.Fatal("ignored audit failure") |
| 371 | } |
| 372 | var count int64 |
| 373 | if err := r.s.DB.Model(&Connection{}).Count(&count).Error; err != nil { |
| 374 | t.Fatal(err) |
| 375 | } |
| 376 | if count != 0 { |
| 377 | t.Fatal("unaudited connection persisted") |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | func TestForgeSweepWithoutFeatureFlag(t *testing.T) { |
| 382 | r := storeRig(t) |
| 383 | ctx := context.Background() |
| 384 | view := r.created(t) |
| 385 | r.s.Maintain = func(tx *gorm.DB) error { var rows []testOwner; return tx.Find(&rows).Error } |
| 386 | r.advance(24 * time.Hour) |
| 387 | r.s.Enabled = false |
| 388 | if err := r.s.Sweep(ctx); err != nil { |
| 389 | t.Fatal(err) |
| 390 | } |
| 391 | var c Connection |
| 392 | if err := r.s.DB.First(&c, "id = ?", view.ID).Error; err != nil { |
| 393 | t.Fatal(err) |
| 394 | } |
| 395 | if c.SealedKey != "" || c.State != "expired" { |
| 396 | t.Fatal("disabled flag retained expired App key") |
| 397 | } |
| 398 | var count int64 |
| 399 | if err := r.s.DB.Model(&testAudit{}).Where("kind = ?", "forge.expire").Count(&count).Error; err != nil { |
| 400 | t.Fatal(err) |
| 401 | } |
| 402 | if count != 1 { |
| 403 | t.Fatal("expiry audit absent") |
| 404 | } |
| 405 | } |
| 406 | |