@@ -0,0 +1,334 @@ |
| | 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| | 2 | + |
| | 3 | +package lifecycle_test |
| | 4 | + |
| | 5 | +import ( |
| | 6 | + "context" |
| | 7 | + "errors" |
| | 8 | + "os" |
| | 9 | + "path/filepath" |
| | 10 | + "testing" |
| | 11 | + "time" |
| | 12 | + |
| | 13 | + "github.com/jackc/pgx/v5/pgtype" |
| | 14 | + |
| | 15 | + "github.com/tenseleyFlow/shithub/internal/auth/audit" |
| | 16 | + "github.com/tenseleyFlow/shithub/internal/auth/throttle" |
| | 17 | + "github.com/tenseleyFlow/shithub/internal/infra/storage" |
| | 18 | + "github.com/tenseleyFlow/shithub/internal/repos" |
| | 19 | + "github.com/tenseleyFlow/shithub/internal/repos/lifecycle" |
| | 20 | + reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc" |
| | 21 | + "github.com/tenseleyFlow/shithub/internal/testing/dbtest" |
| | 22 | + usersdb "github.com/tenseleyFlow/shithub/internal/users/sqlc" |
| | 23 | +) |
| | 24 | + |
| | 25 | +const fixtureHash = "$argon2id$v=19$m=16384,t=1,p=1$" + |
| | 26 | + "AAAAAAAAAAAAAAAA$" + |
| | 27 | + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| | 28 | + |
| | 29 | +type env struct { |
| | 30 | + deps lifecycle.Deps |
| | 31 | + rdeps repos.Deps |
| | 32 | + |
| | 33 | + alice usersdb.User |
| | 34 | + bob usersdb.User |
| | 35 | + |
| | 36 | + repoID int64 |
| | 37 | + originalFS string |
| | 38 | +} |
| | 39 | + |
| | 40 | +func setup(t *testing.T) *env { |
| | 41 | + t.Helper() |
| | 42 | + pool := dbtest.NewTestDB(t) |
| | 43 | + root := t.TempDir() |
| | 44 | + rfs, err := storage.NewRepoFS(root) |
| | 45 | + if err != nil { |
| | 46 | + t.Fatalf("NewRepoFS: %v", err) |
| | 47 | + } |
| | 48 | + uq := usersdb.New() |
| | 49 | + mk := func(name string) usersdb.User { |
| | 50 | + u, err := uq.CreateUser(context.Background(), pool, usersdb.CreateUserParams{ |
| | 51 | + Username: name, DisplayName: name, PasswordHash: fixtureHash, |
| | 52 | + }) |
| | 53 | + if err != nil { |
| | 54 | + t.Fatalf("CreateUser %s: %v", name, err) |
| | 55 | + } |
| | 56 | + em, err := uq.CreateUserEmail(context.Background(), pool, usersdb.CreateUserEmailParams{ |
| | 57 | + UserID: u.ID, Email: name + "@example.com", IsPrimary: true, Verified: true, |
| | 58 | + }) |
| | 59 | + if err != nil { |
| | 60 | + t.Fatalf("CreateUserEmail %s: %v", name, err) |
| | 61 | + } |
| | 62 | + _ = uq.LinkUserPrimaryEmail(context.Background(), pool, usersdb.LinkUserPrimaryEmailParams{ |
| | 63 | + ID: u.ID, PrimaryEmailID: pgtype.Int8{Int64: em.ID, Valid: true}, |
| | 64 | + }) |
| | 65 | + return u |
| | 66 | + } |
| | 67 | + alice := mk("alice") |
| | 68 | + bob := mk("bob") |
| | 69 | + |
| | 70 | + rdeps := repos.Deps{ |
| | 71 | + Pool: pool, RepoFS: rfs, Audit: audit.NewRecorder(), Limiter: throttle.NewLimiter(), |
| | 72 | + } |
| | 73 | + res, err := repos.Create(context.Background(), rdeps, repos.Params{ |
| | 74 | + OwnerUserID: alice.ID, OwnerUsername: alice.Username, |
| | 75 | + Name: "demo", Visibility: "public", InitReadme: true, |
| | 76 | + }) |
| | 77 | + if err != nil { |
| | 78 | + t.Fatalf("repos.Create: %v", err) |
| | 79 | + } |
| | 80 | + |
| | 81 | + return &env{ |
| | 82 | + deps: lifecycle.Deps{Pool: pool, RepoFS: rfs, Audit: audit.NewRecorder()}, |
| | 83 | + rdeps: rdeps, |
| | 84 | + alice: alice, bob: bob, |
| | 85 | + repoID: res.Repo.ID, |
| | 86 | + originalFS: res.DiskPath, |
| | 87 | + } |
| | 88 | +} |
| | 89 | + |
| | 90 | +func TestRename_HappyPath(t *testing.T) { |
| | 91 | + t.Parallel() |
| | 92 | + env := setup(t) |
| | 93 | + if err := lifecycle.Rename(context.Background(), env.deps, lifecycle.RenameParams{ |
| | 94 | + ActorUserID: env.alice.ID, |
| | 95 | + RepoID: env.repoID, |
| | 96 | + OwnerUserID: env.alice.ID, |
| | 97 | + OwnerName: "alice", |
| | 98 | + OldName: "demo", |
| | 99 | + NewName: "renamed", |
| | 100 | + }); err != nil { |
| | 101 | + t.Fatalf("Rename: %v", err) |
| | 102 | + } |
| | 103 | + rq := reposdb.New() |
| | 104 | + repo, err := rq.GetRepoByID(context.Background(), env.deps.Pool, env.repoID) |
| | 105 | + if err != nil { |
| | 106 | + t.Fatalf("GetRepoByID: %v", err) |
| | 107 | + } |
| | 108 | + if repo.Name != "renamed" { |
| | 109 | + t.Errorf("name = %q, want renamed", repo.Name) |
| | 110 | + } |
| | 111 | + // Redirect row exists. |
| | 112 | + rid, err := rq.LookupRedirectByUserOwner(context.Background(), env.deps.Pool, reposdb.LookupRedirectByUserOwnerParams{ |
| | 113 | + OldOwnerUserID: pgtype.Int8{Int64: env.alice.ID, Valid: true}, |
| | 114 | + OldName: "demo", |
| | 115 | + }) |
| | 116 | + if err != nil || rid != env.repoID { |
| | 117 | + t.Errorf("LookupRedirect: id=%d err=%v", rid, err) |
| | 118 | + } |
| | 119 | + // FS dir moved. |
| | 120 | + newPath := filepath.Join(filepath.Dir(env.originalFS), "renamed.git") |
| | 121 | + if _, err := os.Stat(newPath); err != nil { |
| | 122 | + t.Errorf("new path missing: %v", err) |
| | 123 | + } |
| | 124 | +} |
| | 125 | + |
| | 126 | +func TestRename_RejectsSameName(t *testing.T) { |
| | 127 | + t.Parallel() |
| | 128 | + env := setup(t) |
| | 129 | + err := lifecycle.Rename(context.Background(), env.deps, lifecycle.RenameParams{ |
| | 130 | + RepoID: env.repoID, OwnerUserID: env.alice.ID, OwnerName: "alice", |
| | 131 | + OldName: "demo", NewName: "demo", |
| | 132 | + }) |
| | 133 | + if !errors.Is(err, lifecycle.ErrSameName) { |
| | 134 | + t.Errorf("err = %v, want ErrSameName", err) |
| | 135 | + } |
| | 136 | +} |
| | 137 | + |
| | 138 | +func TestRename_RateLimit(t *testing.T) { |
| | 139 | + t.Parallel() |
| | 140 | + env := setup(t) |
| | 141 | + for i := 0; i < 5; i++ { |
| | 142 | + newName := []string{"a1", "a2", "a3", "a4", "a5"}[i] |
| | 143 | + oldName := "demo" |
| | 144 | + if i > 0 { |
| | 145 | + oldName = []string{"a1", "a2", "a3", "a4"}[i-1] |
| | 146 | + } |
| | 147 | + if err := lifecycle.Rename(context.Background(), env.deps, lifecycle.RenameParams{ |
| | 148 | + RepoID: env.repoID, OwnerUserID: env.alice.ID, OwnerName: "alice", |
| | 149 | + OldName: oldName, NewName: newName, |
| | 150 | + }); err != nil { |
| | 151 | + t.Fatalf("rename %d: %v", i, err) |
| | 152 | + } |
| | 153 | + } |
| | 154 | + err := lifecycle.Rename(context.Background(), env.deps, lifecycle.RenameParams{ |
| | 155 | + RepoID: env.repoID, OwnerUserID: env.alice.ID, OwnerName: "alice", |
| | 156 | + OldName: "a5", NewName: "a6", |
| | 157 | + }) |
| | 158 | + if !errors.Is(err, lifecycle.ErrRenameRateLimited) { |
| | 159 | + t.Errorf("6th rename: err=%v, want ErrRenameRateLimited", err) |
| | 160 | + } |
| | 161 | +} |
| | 162 | + |
| | 163 | +func TestArchiveUnarchive(t *testing.T) { |
| | 164 | + t.Parallel() |
| | 165 | + env := setup(t) |
| | 166 | + if err := lifecycle.Archive(context.Background(), env.deps, env.alice.ID, env.repoID); err != nil { |
| | 167 | + t.Fatalf("Archive: %v", err) |
| | 168 | + } |
| | 169 | + if err := lifecycle.Archive(context.Background(), env.deps, env.alice.ID, env.repoID); !errors.Is(err, lifecycle.ErrAlreadyArchived) { |
| | 170 | + t.Errorf("double archive: err=%v, want ErrAlreadyArchived", err) |
| | 171 | + } |
| | 172 | + if err := lifecycle.Unarchive(context.Background(), env.deps, env.alice.ID, env.repoID); err != nil { |
| | 173 | + t.Fatalf("Unarchive: %v", err) |
| | 174 | + } |
| | 175 | + if err := lifecycle.Unarchive(context.Background(), env.deps, env.alice.ID, env.repoID); !errors.Is(err, lifecycle.ErrNotArchived) { |
| | 176 | + t.Errorf("double unarchive: err=%v, want ErrNotArchived", err) |
| | 177 | + } |
| | 178 | +} |
| | 179 | + |
| | 180 | +func TestSetVisibility(t *testing.T) { |
| | 181 | + t.Parallel() |
| | 182 | + env := setup(t) |
| | 183 | + if err := lifecycle.SetVisibility(context.Background(), env.deps, env.alice.ID, env.repoID, "private"); err != nil { |
| | 184 | + t.Fatalf("SetVisibility: %v", err) |
| | 185 | + } |
| | 186 | + rq := reposdb.New() |
| | 187 | + repo, _ := rq.GetRepoByID(context.Background(), env.deps.Pool, env.repoID) |
| | 188 | + if string(repo.Visibility) != "private" { |
| | 189 | + t.Errorf("Visibility = %q, want private", repo.Visibility) |
| | 190 | + } |
| | 191 | + if err := lifecycle.SetVisibility(context.Background(), env.deps, env.alice.ID, env.repoID, "bogus"); !errors.Is(err, lifecycle.ErrInvalidVisibility) { |
| | 192 | + t.Errorf("invalid: err=%v, want ErrInvalidVisibility", err) |
| | 193 | + } |
| | 194 | +} |
| | 195 | + |
| | 196 | +func TestSoftDeleteAndRestore(t *testing.T) { |
| | 197 | + t.Parallel() |
| | 198 | + env := setup(t) |
| | 199 | + if err := lifecycle.SoftDelete(context.Background(), env.deps, env.alice.ID, env.repoID); err != nil { |
| | 200 | + t.Fatalf("SoftDelete: %v", err) |
| | 201 | + } |
| | 202 | + if err := lifecycle.SoftDelete(context.Background(), env.deps, env.alice.ID, env.repoID); !errors.Is(err, lifecycle.ErrAlreadyDeleted) { |
| | 203 | + t.Errorf("double soft-delete: err=%v", err) |
| | 204 | + } |
| | 205 | + if err := lifecycle.Restore(context.Background(), env.deps, env.alice.ID, env.repoID); err != nil { |
| | 206 | + t.Fatalf("Restore: %v", err) |
| | 207 | + } |
| | 208 | + if err := lifecycle.Restore(context.Background(), env.deps, env.alice.ID, env.repoID); !errors.Is(err, lifecycle.ErrNotDeleted) { |
| | 209 | + t.Errorf("double restore: err=%v", err) |
| | 210 | + } |
| | 211 | +} |
| | 212 | + |
| | 213 | +func TestRestore_PastGraceRefuses(t *testing.T) { |
| | 214 | + t.Parallel() |
| | 215 | + env := setup(t) |
| | 216 | + // Pin Now to 8 days from now after a soft-delete to simulate |
| | 217 | + // past-grace state. |
| | 218 | + if err := lifecycle.SoftDelete(context.Background(), env.deps, env.alice.ID, env.repoID); err != nil { |
| | 219 | + t.Fatal(err) |
| | 220 | + } |
| | 221 | + env.deps.Now = func() time.Time { return time.Now().Add(8 * 24 * time.Hour) } |
| | 222 | + if err := lifecycle.Restore(context.Background(), env.deps, env.alice.ID, env.repoID); !errors.Is(err, lifecycle.ErrPastGrace) { |
| | 223 | + t.Errorf("past-grace restore: err=%v, want ErrPastGrace", err) |
| | 224 | + } |
| | 225 | +} |
| | 226 | + |
| | 227 | +func TestTransfer_AcceptHappyPath(t *testing.T) { |
| | 228 | + t.Parallel() |
| | 229 | + env := setup(t) |
| | 230 | + id, err := lifecycle.RequestTransfer(context.Background(), env.deps, lifecycle.TransferRequestParams{ |
| | 231 | + ActorUserID: env.alice.ID, RepoID: env.repoID, |
| | 232 | + FromUserID: env.alice.ID, ToPrincipalKind: "user", ToPrincipalID: env.bob.ID, |
| | 233 | + }) |
| | 234 | + if err != nil { |
| | 235 | + t.Fatalf("RequestTransfer: %v", err) |
| | 236 | + } |
| | 237 | + if err := lifecycle.AcceptTransfer(context.Background(), env.deps, env.bob.ID, id); err != nil { |
| | 238 | + t.Fatalf("AcceptTransfer: %v", err) |
| | 239 | + } |
| | 240 | + rq := reposdb.New() |
| | 241 | + repo, _ := rq.GetRepoByID(context.Background(), env.deps.Pool, env.repoID) |
| | 242 | + if !repo.OwnerUserID.Valid || repo.OwnerUserID.Int64 != env.bob.ID { |
| | 243 | + t.Errorf("owner_user_id = %v, want %d", repo.OwnerUserID, env.bob.ID) |
| | 244 | + } |
| | 245 | + // Redirect from alice/demo → repo_id should resolve. |
| | 246 | + rid, err := rq.LookupRedirectByUserOwner(context.Background(), env.deps.Pool, reposdb.LookupRedirectByUserOwnerParams{ |
| | 247 | + OldOwnerUserID: pgtype.Int8{Int64: env.alice.ID, Valid: true}, |
| | 248 | + OldName: "demo", |
| | 249 | + }) |
| | 250 | + if err != nil || rid != env.repoID { |
| | 251 | + t.Errorf("redirect: id=%d err=%v", rid, err) |
| | 252 | + } |
| | 253 | +} |
| | 254 | + |
| | 255 | +func TestTransfer_DeclineLeavesOwnerUnchanged(t *testing.T) { |
| | 256 | + t.Parallel() |
| | 257 | + env := setup(t) |
| | 258 | + id, _ := lifecycle.RequestTransfer(context.Background(), env.deps, lifecycle.TransferRequestParams{ |
| | 259 | + ActorUserID: env.alice.ID, RepoID: env.repoID, |
| | 260 | + FromUserID: env.alice.ID, ToPrincipalKind: "user", ToPrincipalID: env.bob.ID, |
| | 261 | + }) |
| | 262 | + if err := lifecycle.DeclineTransfer(context.Background(), env.deps, env.bob.ID, id); err != nil { |
| | 263 | + t.Fatalf("DeclineTransfer: %v", err) |
| | 264 | + } |
| | 265 | + rq := reposdb.New() |
| | 266 | + repo, _ := rq.GetRepoByID(context.Background(), env.deps.Pool, env.repoID) |
| | 267 | + if !repo.OwnerUserID.Valid || repo.OwnerUserID.Int64 != env.alice.ID { |
| | 268 | + t.Errorf("owner changed despite decline: %v", repo.OwnerUserID) |
| | 269 | + } |
| | 270 | +} |
| | 271 | + |
| | 272 | +func TestTransfer_CancelByOwner(t *testing.T) { |
| | 273 | + t.Parallel() |
| | 274 | + env := setup(t) |
| | 275 | + id, _ := lifecycle.RequestTransfer(context.Background(), env.deps, lifecycle.TransferRequestParams{ |
| | 276 | + ActorUserID: env.alice.ID, RepoID: env.repoID, |
| | 277 | + FromUserID: env.alice.ID, ToPrincipalKind: "user", ToPrincipalID: env.bob.ID, |
| | 278 | + }) |
| | 279 | + if err := lifecycle.CancelTransfer(context.Background(), env.deps, env.alice.ID, id); err != nil { |
| | 280 | + t.Fatalf("CancelTransfer: %v", err) |
| | 281 | + } |
| | 282 | + // Bob's accept now fails. |
| | 283 | + if err := lifecycle.AcceptTransfer(context.Background(), env.deps, env.bob.ID, id); !errors.Is(err, lifecycle.ErrTransferTerminal) { |
| | 284 | + t.Errorf("accept-after-cancel: err=%v, want ErrTransferTerminal", err) |
| | 285 | + } |
| | 286 | +} |
| | 287 | + |
| | 288 | +func TestTransfer_ExpireSweepFlipsPending(t *testing.T) { |
| | 289 | + t.Parallel() |
| | 290 | + env := setup(t) |
| | 291 | + _, _ = lifecycle.RequestTransfer(context.Background(), env.deps, lifecycle.TransferRequestParams{ |
| | 292 | + ActorUserID: env.alice.ID, RepoID: env.repoID, |
| | 293 | + FromUserID: env.alice.ID, ToPrincipalKind: "user", ToPrincipalID: env.bob.ID, |
| | 294 | + }) |
| | 295 | + // Force the row past its expires_at by SQL update so we don't need |
| | 296 | + // to advance lifecycle.now() through the call path. |
| | 297 | + _, err := env.deps.Pool.Exec(context.Background(), |
| | 298 | + `UPDATE repo_transfer_requests SET expires_at = now() - interval '1 minute' WHERE repo_id = $1`, env.repoID) |
| | 299 | + if err != nil { |
| | 300 | + t.Fatalf("force expiry: %v", err) |
| | 301 | + } |
| | 302 | + n, err := lifecycle.ExpirePending(context.Background(), env.deps) |
| | 303 | + if err != nil { |
| | 304 | + t.Fatalf("ExpirePending: %v", err) |
| | 305 | + } |
| | 306 | + if n != 1 { |
| | 307 | + t.Errorf("expired count = %d, want 1", n) |
| | 308 | + } |
| | 309 | +} |
| | 310 | + |
| | 311 | +func TestHardDelete_PastGraceCascades(t *testing.T) { |
| | 312 | + t.Parallel() |
| | 313 | + env := setup(t) |
| | 314 | + if err := lifecycle.SoftDelete(context.Background(), env.deps, env.alice.ID, env.repoID); err != nil { |
| | 315 | + t.Fatal(err) |
| | 316 | + } |
| | 317 | + // Force deleted_at past grace and pin Now likewise. |
| | 318 | + _, _ = env.deps.Pool.Exec(context.Background(), |
| | 319 | + `UPDATE repos SET deleted_at = now() - interval '8 days' WHERE id = $1`, env.repoID) |
| | 320 | + env.deps.Now = func() time.Time { return time.Now() } |
| | 321 | + |
| | 322 | + if err := lifecycle.HardDelete(context.Background(), env.deps, 0, env.repoID); err != nil { |
| | 323 | + t.Fatalf("HardDelete: %v", err) |
| | 324 | + } |
| | 325 | + // Repo row gone. |
| | 326 | + rq := reposdb.New() |
| | 327 | + if _, err := rq.GetRepoByID(context.Background(), env.deps.Pool, env.repoID); err == nil { |
| | 328 | + t.Errorf("repo row still present after hard-delete") |
| | 329 | + } |
| | 330 | + // FS dir gone. |
| | 331 | + if _, err := os.Stat(env.originalFS); !os.IsNotExist(err) { |
| | 332 | + t.Errorf("FS path still present: err=%v", err) |
| | 333 | + } |
| | 334 | +} |