@@ -0,0 +1,232 @@ |
| | 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| | 2 | + |
| | 3 | +package repos_test |
| | 4 | + |
| | 5 | +import ( |
| | 6 | + "context" |
| | 7 | + "errors" |
| | 8 | + "io" |
| | 9 | + "log/slog" |
| | 10 | + "os/exec" |
| | 11 | + "path/filepath" |
| | 12 | + "strings" |
| | 13 | + "testing" |
| | 14 | + "time" |
| | 15 | + |
| | 16 | + "github.com/jackc/pgx/v5/pgtype" |
| | 17 | + "github.com/jackc/pgx/v5/pgxpool" |
| | 18 | + |
| | 19 | + "github.com/tenseleyFlow/shithub/internal/auth/audit" |
| | 20 | + "github.com/tenseleyFlow/shithub/internal/auth/throttle" |
| | 21 | + "github.com/tenseleyFlow/shithub/internal/infra/storage" |
| | 22 | + "github.com/tenseleyFlow/shithub/internal/repos" |
| | 23 | + "github.com/tenseleyFlow/shithub/internal/testing/dbtest" |
| | 24 | + usersdb "github.com/tenseleyFlow/shithub/internal/users/sqlc" |
| | 25 | +) |
| | 26 | + |
| | 27 | +// gitCmd wraps exec.Command with the single G204 suppression for this |
| | 28 | +// file — every git invocation runs against a t.TempDir path. |
| | 29 | +func gitCmd(args ...string) *exec.Cmd { |
| | 30 | + //nolint:gosec // G204 false positive: callers feed t.TempDir paths and fixed flags. |
| | 31 | + return exec.Command("git", args...) |
| | 32 | +} |
| | 33 | + |
| | 34 | +// fixtureHash is a static PHC test fixture (zero salt, zero key) — not a credential. |
| | 35 | +const fixtureHash = "$argon2id$v=19$m=16384,t=1,p=1$" + |
| | 36 | + "AAAAAAAAAAAAAAAA$" + |
| | 37 | + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| | 38 | + |
| | 39 | +// setupCreateEnv constructs Deps + a verified-email user against a |
| | 40 | +// fresh test DB. |
| | 41 | +func setupCreateEnv(t *testing.T) (*pgxpool.Pool, repos.Deps, int64, string, string) { |
| | 42 | + t.Helper() |
| | 43 | + pool := dbtest.NewTestDB(t) |
| | 44 | + |
| | 45 | + root := t.TempDir() |
| | 46 | + rfs, err := storage.NewRepoFS(root) |
| | 47 | + if err != nil { |
| | 48 | + t.Fatalf("NewRepoFS: %v", err) |
| | 49 | + } |
| | 50 | + |
| | 51 | + deps := repos.Deps{ |
| | 52 | + Pool: pool, |
| | 53 | + RepoFS: rfs, |
| | 54 | + Audit: audit.NewRecorder(), |
| | 55 | + Limiter: throttle.NewLimiter(), |
| | 56 | + Logger: slog.New(slog.NewTextHandler(io.Discard, nil)), |
| | 57 | + } |
| | 58 | + |
| | 59 | + uq := usersdb.New() |
| | 60 | + user, err := uq.CreateUser(context.Background(), pool, usersdb.CreateUserParams{ |
| | 61 | + Username: "alice", DisplayName: "Alice Anderson", PasswordHash: fixtureHash, |
| | 62 | + }) |
| | 63 | + if err != nil { |
| | 64 | + t.Fatalf("CreateUser: %v", err) |
| | 65 | + } |
| | 66 | + em, err := uq.CreateUserEmail(context.Background(), pool, usersdb.CreateUserEmailParams{ |
| | 67 | + UserID: user.ID, Email: "alice@example.com", IsPrimary: true, Verified: true, |
| | 68 | + }) |
| | 69 | + if err != nil { |
| | 70 | + t.Fatalf("CreateUserEmail: %v", err) |
| | 71 | + } |
| | 72 | + if err := uq.LinkUserPrimaryEmail(context.Background(), pool, usersdb.LinkUserPrimaryEmailParams{ |
| | 73 | + ID: user.ID, PrimaryEmailID: pgtype.Int8{Int64: em.ID, Valid: true}, |
| | 74 | + }); err != nil { |
| | 75 | + t.Fatalf("LinkUserPrimaryEmail: %v", err) |
| | 76 | + } |
| | 77 | + return pool, deps, user.ID, user.Username, root |
| | 78 | +} |
| | 79 | + |
| | 80 | +func TestCreate_EmptyRepo(t *testing.T) { |
| | 81 | + t.Parallel() |
| | 82 | + _, deps, uid, uname, root := setupCreateEnv(t) |
| | 83 | + res, err := repos.Create(context.Background(), deps, repos.Params{ |
| | 84 | + OwnerUserID: uid, |
| | 85 | + OwnerUsername: uname, |
| | 86 | + Name: "empty-repo", |
| | 87 | + Visibility: "public", |
| | 88 | + }) |
| | 89 | + if err != nil { |
| | 90 | + t.Fatalf("Create: %v", err) |
| | 91 | + } |
| | 92 | + if res.InitialCommitOID != "" { |
| | 93 | + t.Errorf("expected no initial commit; got %q", res.InitialCommitOID) |
| | 94 | + } |
| | 95 | + if !strings.HasPrefix(res.DiskPath, root) { |
| | 96 | + t.Errorf("DiskPath %q not under root %q", res.DiskPath, root) |
| | 97 | + } |
| | 98 | + |
| | 99 | + // HEAD must be a symbolic ref to refs/heads/trunk (unborn branch). |
| | 100 | + out, err := gitCmd("-C", res.DiskPath, "symbolic-ref", "HEAD").CombinedOutput() |
| | 101 | + if err != nil { |
| | 102 | + t.Fatalf("symbolic-ref: %v\n%s", err, out) |
| | 103 | + } |
| | 104 | + if got := strings.TrimSpace(string(out)); got != "refs/heads/trunk" { |
| | 105 | + t.Fatalf("HEAD = %q, want refs/heads/trunk", got) |
| | 106 | + } |
| | 107 | + |
| | 108 | + // Zero commits. |
| | 109 | + out, _ = gitCmd("-C", res.DiskPath, "rev-list", "--all", "--count").CombinedOutput() |
| | 110 | + if got := strings.TrimSpace(string(out)); got != "0" { |
| | 111 | + t.Fatalf("rev-list count = %q, want 0", got) |
| | 112 | + } |
| | 113 | +} |
| | 114 | + |
| | 115 | +func TestCreate_WithReadmeLicenseGitignore(t *testing.T) { |
| | 116 | + t.Parallel() |
| | 117 | + _, deps, uid, uname, _ := setupCreateEnv(t) |
| | 118 | + res, err := repos.Create(context.Background(), deps, repos.Params{ |
| | 119 | + OwnerUserID: uid, |
| | 120 | + OwnerUsername: uname, |
| | 121 | + Name: "init-repo", |
| | 122 | + Description: "hello world", |
| | 123 | + Visibility: "public", |
| | 124 | + InitReadme: true, |
| | 125 | + LicenseKey: "MIT", |
| | 126 | + GitignoreKey: "Go", |
| | 127 | + InitialCommitWhen: time.Date(2026, 5, 7, 12, 0, 0, 0, time.UTC), |
| | 128 | + }) |
| | 129 | + if err != nil { |
| | 130 | + t.Fatalf("Create: %v", err) |
| | 131 | + } |
| | 132 | + if res.InitialCommitOID == "" { |
| | 133 | + t.Fatal("expected an initial commit") |
| | 134 | + } |
| | 135 | + |
| | 136 | + // Single commit, three files, expected names. |
| | 137 | + out, _ := gitCmd("-C", res.DiskPath, "rev-list", "--count", "trunk").CombinedOutput() |
| | 138 | + if got := strings.TrimSpace(string(out)); got != "1" { |
| | 139 | + t.Fatalf("rev-list count = %q, want 1", got) |
| | 140 | + } |
| | 141 | + out, _ = gitCmd("-C", res.DiskPath, "ls-tree", "--name-only", "trunk").CombinedOutput() |
| | 142 | + got := strings.TrimSpace(string(out)) |
| | 143 | + for _, want := range []string{"README.md", "LICENSE", ".gitignore"} { |
| | 144 | + if !strings.Contains(got, want) { |
| | 145 | + t.Errorf("missing %q in tree: %q", want, got) |
| | 146 | + } |
| | 147 | + } |
| | 148 | + // Author identity is alice's verified primary email. |
| | 149 | + out, _ = gitCmd("-C", res.DiskPath, "log", "-1", "--format=%an <%ae>", "trunk").CombinedOutput() |
| | 150 | + if want := "Alice Anderson <alice@example.com>"; strings.TrimSpace(string(out)) != want { |
| | 151 | + t.Errorf("author = %q, want %q", strings.TrimSpace(string(out)), want) |
| | 152 | + } |
| | 153 | + // LICENSE has the year substituted. |
| | 154 | + out, _ = gitCmd("-C", res.DiskPath, "show", "trunk:LICENSE").CombinedOutput() |
| | 155 | + if !strings.Contains(string(out), "2026") { |
| | 156 | + t.Errorf("LICENSE missing year 2026; got first 200 chars: %s", string(out)[:200]) |
| | 157 | + } |
| | 158 | +} |
| | 159 | + |
| | 160 | +func TestCreate_RejectsDuplicate(t *testing.T) { |
| | 161 | + t.Parallel() |
| | 162 | + _, deps, uid, uname, _ := setupCreateEnv(t) |
| | 163 | + if _, err := repos.Create(context.Background(), deps, repos.Params{ |
| | 164 | + OwnerUserID: uid, OwnerUsername: uname, Name: "dup", Visibility: "public", |
| | 165 | + }); err != nil { |
| | 166 | + t.Fatalf("first create: %v", err) |
| | 167 | + } |
| | 168 | + _, err := repos.Create(context.Background(), deps, repos.Params{ |
| | 169 | + OwnerUserID: uid, OwnerUsername: uname, Name: "dup", Visibility: "public", |
| | 170 | + }) |
| | 171 | + if !errors.Is(err, repos.ErrTaken) { |
| | 172 | + t.Fatalf("second create: err = %v, want ErrTaken", err) |
| | 173 | + } |
| | 174 | +} |
| | 175 | + |
| | 176 | +func TestCreate_RejectsReservedName(t *testing.T) { |
| | 177 | + t.Parallel() |
| | 178 | + _, deps, uid, uname, _ := setupCreateEnv(t) |
| | 179 | + _, err := repos.Create(context.Background(), deps, repos.Params{ |
| | 180 | + OwnerUserID: uid, OwnerUsername: uname, Name: "head", Visibility: "public", |
| | 181 | + }) |
| | 182 | + if !errors.Is(err, repos.ErrReservedName) { |
| | 183 | + t.Fatalf("err = %v, want ErrReservedName", err) |
| | 184 | + } |
| | 185 | +} |
| | 186 | + |
| | 187 | +func TestCreate_RefusesWithoutVerifiedEmail(t *testing.T) { |
| | 188 | + t.Parallel() |
| | 189 | + pool := dbtest.NewTestDB(t) |
| | 190 | + root := t.TempDir() |
| | 191 | + rfs, _ := storage.NewRepoFS(root) |
| | 192 | + deps := repos.Deps{ |
| | 193 | + Pool: pool, RepoFS: rfs, |
| | 194 | + Audit: audit.NewRecorder(), |
| | 195 | + Limiter: throttle.NewLimiter(), |
| | 196 | + } |
| | 197 | + uq := usersdb.New() |
| | 198 | + user, err := uq.CreateUser(context.Background(), pool, usersdb.CreateUserParams{ |
| | 199 | + Username: "bob", DisplayName: "Bob", PasswordHash: fixtureHash, |
| | 200 | + }) |
| | 201 | + if err != nil { |
| | 202 | + t.Fatalf("CreateUser: %v", err) |
| | 203 | + } |
| | 204 | + // User exists but has NO verified primary email. |
| | 205 | + _, err = repos.Create(context.Background(), deps, repos.Params{ |
| | 206 | + OwnerUserID: user.ID, OwnerUsername: user.Username, |
| | 207 | + Name: "needs-email", Visibility: "public", |
| | 208 | + InitReadme: true, |
| | 209 | + }) |
| | 210 | + if !errors.Is(err, repos.ErrNoVerifiedEmail) { |
| | 211 | + t.Fatalf("err = %v, want ErrNoVerifiedEmail", err) |
| | 212 | + } |
| | 213 | +} |
| | 214 | + |
| | 215 | +func TestCreate_PrivateVisibilityPersists(t *testing.T) { |
| | 216 | + t.Parallel() |
| | 217 | + _, deps, uid, uname, _ := setupCreateEnv(t) |
| | 218 | + res, err := repos.Create(context.Background(), deps, repos.Params{ |
| | 219 | + OwnerUserID: uid, OwnerUsername: uname, |
| | 220 | + Name: "secret", Visibility: "private", |
| | 221 | + }) |
| | 222 | + if err != nil { |
| | 223 | + t.Fatalf("Create: %v", err) |
| | 224 | + } |
| | 225 | + if string(res.Repo.Visibility) != "private" { |
| | 226 | + t.Errorf("Visibility = %q, want private", res.Repo.Visibility) |
| | 227 | + } |
| | 228 | + // Sharded path layout sanity check (shard is first 2 chars of OWNER). |
| | 229 | + if want := filepath.Join("al", "alice", "secret.git"); !strings.HasSuffix(res.DiskPath, want) { |
| | 230 | + t.Errorf("DiskPath %q does not end with %q", res.DiskPath, want) |
| | 231 | + } |
| | 232 | +} |