@@ -0,0 +1,255 @@ |
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package variables_test |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/jackc/pgx/v5/pgtype" |
| 12 | + |
| 13 | + "github.com/tenseleyFlow/shithub/internal/actions/variables" |
| 14 | + orgsdb "github.com/tenseleyFlow/shithub/internal/orgs/sqlc" |
| 15 | + reposdb "github.com/tenseleyFlow/shithub/internal/repos/sqlc" |
| 16 | + "github.com/tenseleyFlow/shithub/internal/testing/dbtest" |
| 17 | + usersdb "github.com/tenseleyFlow/shithub/internal/users/sqlc" |
| 18 | +) |
| 19 | + |
| 20 | +const fixtureHash = "$argon2id$v=19$m=16384,t=1,p=1$" + |
| 21 | + "AAAAAAAAAAAAAAAA$" + |
| 22 | + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" |
| 23 | + |
| 24 | +type fx struct { |
| 25 | + deps variables.Deps |
| 26 | + repoID int64 |
| 27 | + orgID int64 |
| 28 | + userID int64 |
| 29 | +} |
| 30 | + |
| 31 | +func setup(t *testing.T) fx { |
| 32 | + t.Helper() |
| 33 | + pool := dbtest.NewTestDB(t) |
| 34 | + ctx := context.Background() |
| 35 | + |
| 36 | + user, err := usersdb.New().CreateUser(ctx, pool, usersdb.CreateUserParams{ |
| 37 | + Username: "alice", DisplayName: "Alice", PasswordHash: fixtureHash, |
| 38 | + }) |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("CreateUser: %v", err) |
| 41 | + } |
| 42 | + repo, err := reposdb.New().CreateRepo(ctx, pool, reposdb.CreateRepoParams{ |
| 43 | + OwnerUserID: pgtype.Int8{Int64: user.ID, Valid: true}, |
| 44 | + Name: "demo", |
| 45 | + DefaultBranch: "trunk", |
| 46 | + Visibility: reposdb.RepoVisibilityPublic, |
| 47 | + }) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("CreateRepo: %v", err) |
| 50 | + } |
| 51 | + org, err := orgsdb.New().CreateOrg(ctx, pool, orgsdb.CreateOrgParams{ |
| 52 | + Slug: "acme", |
| 53 | + DisplayName: "Acme", |
| 54 | + Description: "", |
| 55 | + BillingEmail: "ops@example.com", |
| 56 | + CreatedByUserID: pgtype.Int8{Int64: user.ID, Valid: true}, |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("CreateOrg: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + return fx{ |
| 63 | + deps: variables.Deps{Pool: pool}, |
| 64 | + repoID: repo.ID, |
| 65 | + orgID: org.ID, |
| 66 | + userID: user.ID, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestSetGet_RoundTripRepoScope(t *testing.T) { |
| 71 | + f := setup(t) |
| 72 | + ctx := context.Background() |
| 73 | + scope := variables.RepoScope(f.repoID) |
| 74 | + if err := f.deps.Set(ctx, scope, "IMAGE_TAG", "2026.05", f.userID); err != nil { |
| 75 | + t.Fatalf("Set: %v", err) |
| 76 | + } |
| 77 | + got, err := f.deps.Get(ctx, scope, "IMAGE_TAG") |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("Get: %v", err) |
| 80 | + } |
| 81 | + if got.Value != "2026.05" { |
| 82 | + t.Errorf("got %q want 2026.05", got.Value) |
| 83 | + } |
| 84 | + if got.CreatedByUserID != f.userID { |
| 85 | + t.Errorf("CreatedByUserID = %d, want %d", got.CreatedByUserID, f.userID) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestSet_AllowsEmptyValue(t *testing.T) { |
| 90 | + f := setup(t) |
| 91 | + ctx := context.Background() |
| 92 | + scope := variables.RepoScope(f.repoID) |
| 93 | + if err := f.deps.Set(ctx, scope, "EMPTY", "", f.userID); err != nil { |
| 94 | + t.Fatalf("Set empty value: %v", err) |
| 95 | + } |
| 96 | + got, err := f.deps.Get(ctx, scope, "EMPTY") |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("Get: %v", err) |
| 99 | + } |
| 100 | + if got.Value != "" { |
| 101 | + t.Errorf("got %q want empty string", got.Value) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func TestSet_OverwriteOnSameName(t *testing.T) { |
| 106 | + f := setup(t) |
| 107 | + ctx := context.Background() |
| 108 | + scope := variables.RepoScope(f.repoID) |
| 109 | + if err := f.deps.Set(ctx, scope, "TARGET_ENV", "staging", f.userID); err != nil { |
| 110 | + t.Fatalf("Set staging: %v", err) |
| 111 | + } |
| 112 | + if err := f.deps.Set(ctx, scope, "TARGET_ENV", "production", f.userID); err != nil { |
| 113 | + t.Fatalf("Set production: %v", err) |
| 114 | + } |
| 115 | + got, err := f.deps.Get(ctx, scope, "TARGET_ENV") |
| 116 | + if err != nil { |
| 117 | + t.Fatalf("Get: %v", err) |
| 118 | + } |
| 119 | + if got.Value != "production" { |
| 120 | + t.Errorf("got %q want production", got.Value) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestSet_InvalidNameRejected(t *testing.T) { |
| 125 | + f := setup(t) |
| 126 | + ctx := context.Background() |
| 127 | + scope := variables.RepoScope(f.repoID) |
| 128 | + for _, name := range []string{"", "1leading-digit", "has space", "has-dash", "cafe!"} { |
| 129 | + if err := f.deps.Set(ctx, scope, name, "v", f.userID); !errors.Is(err, variables.ErrInvalidName) { |
| 130 | + t.Errorf("Set name=%q: expected ErrInvalidName, got %v", name, err) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestSet_ValueTooLongRejected(t *testing.T) { |
| 136 | + f := setup(t) |
| 137 | + ctx := context.Background() |
| 138 | + scope := variables.RepoScope(f.repoID) |
| 139 | + value := strings.Repeat("x", variables.MaxValueChars+1) |
| 140 | + if err := f.deps.Set(ctx, scope, "TOO_BIG", value, f.userID); !errors.Is(err, variables.ErrValueTooLong) { |
| 141 | + t.Errorf("Set long value: expected ErrValueTooLong, got %v", err) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +func TestSet_InvalidScopeRejected(t *testing.T) { |
| 146 | + f := setup(t) |
| 147 | + ctx := context.Background() |
| 148 | + for _, sc := range []variables.Scope{ |
| 149 | + {}, |
| 150 | + {RepoID: 1, OrgID: 2}, |
| 151 | + } { |
| 152 | + if err := f.deps.Set(ctx, sc, "K", "v", 0); !errors.Is(err, variables.ErrInvalidScope) { |
| 153 | + t.Errorf("Set scope=%+v: expected ErrInvalidScope, got %v", sc, err) |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func TestList_ReturnsValuesSortedAndMetadata(t *testing.T) { |
| 159 | + f := setup(t) |
| 160 | + ctx := context.Background() |
| 161 | + scope := variables.RepoScope(f.repoID) |
| 162 | + for _, item := range []struct { |
| 163 | + name string |
| 164 | + value string |
| 165 | + }{ |
| 166 | + {"BBB", "two"}, |
| 167 | + {"AAA", "one"}, |
| 168 | + {"ccc", "three"}, |
| 169 | + } { |
| 170 | + if err := f.deps.Set(ctx, scope, item.name, item.value, f.userID); err != nil { |
| 171 | + t.Fatalf("Set %s: %v", item.name, err) |
| 172 | + } |
| 173 | + } |
| 174 | + got, err := f.deps.List(ctx, scope) |
| 175 | + if err != nil { |
| 176 | + t.Fatalf("List: %v", err) |
| 177 | + } |
| 178 | + if len(got) != 3 { |
| 179 | + t.Fatalf("got %d variables, want 3", len(got)) |
| 180 | + } |
| 181 | + wantNames := []string{"AAA", "BBB", "ccc"} |
| 182 | + wantValues := []string{"one", "two", "three"} |
| 183 | + for i := range wantNames { |
| 184 | + if got[i].Name != wantNames[i] || got[i].Value != wantValues[i] { |
| 185 | + t.Errorf("got[%d] = %s/%q, want %s/%q", i, got[i].Name, got[i].Value, wantNames[i], wantValues[i]) |
| 186 | + } |
| 187 | + if got[i].CreatedByUserID != f.userID { |
| 188 | + t.Errorf("got[%d].CreatedByUserID = %d, want %d", i, got[i].CreatedByUserID, f.userID) |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +func TestDelete_RemovesRow(t *testing.T) { |
| 194 | + f := setup(t) |
| 195 | + ctx := context.Background() |
| 196 | + scope := variables.RepoScope(f.repoID) |
| 197 | + if err := f.deps.Set(ctx, scope, "TOKEN", "v", f.userID); err != nil { |
| 198 | + t.Fatalf("Set: %v", err) |
| 199 | + } |
| 200 | + if err := f.deps.Delete(ctx, scope, "TOKEN"); err != nil { |
| 201 | + t.Fatalf("Delete: %v", err) |
| 202 | + } |
| 203 | + if _, err := f.deps.Get(ctx, scope, "TOKEN"); !errors.Is(err, variables.ErrNotFound) { |
| 204 | + t.Errorf("Get after Delete: expected ErrNotFound, got %v", err) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +func TestDelete_MissingIsIdempotent(t *testing.T) { |
| 209 | + f := setup(t) |
| 210 | + ctx := context.Background() |
| 211 | + scope := variables.RepoScope(f.repoID) |
| 212 | + if err := f.deps.Delete(ctx, scope, "NEVER_EXISTED"); err != nil { |
| 213 | + t.Errorf("Delete missing: expected nil, got %v", err) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +func TestGet_CitextNameIsCaseInsensitive(t *testing.T) { |
| 218 | + f := setup(t) |
| 219 | + ctx := context.Background() |
| 220 | + scope := variables.RepoScope(f.repoID) |
| 221 | + if err := f.deps.Set(ctx, scope, "IMAGE_TAG", "v1", f.userID); err != nil { |
| 222 | + t.Fatalf("Set: %v", err) |
| 223 | + } |
| 224 | + got, err := f.deps.Get(ctx, scope, "image_tag") |
| 225 | + if err != nil { |
| 226 | + t.Fatalf("Get lowercased: %v", err) |
| 227 | + } |
| 228 | + if got.Value != "v1" { |
| 229 | + t.Errorf("got %q want v1", got.Value) |
| 230 | + } |
| 231 | +} |
| 232 | + |
| 233 | +func TestOrgAndRepoScopesAreIsolated(t *testing.T) { |
| 234 | + f := setup(t) |
| 235 | + ctx := context.Background() |
| 236 | + repoScope := variables.RepoScope(f.repoID) |
| 237 | + orgScope := variables.OrgScope(f.orgID) |
| 238 | + if err := f.deps.Set(ctx, repoScope, "IMAGE_TAG", "repo", f.userID); err != nil { |
| 239 | + t.Fatalf("Set repo: %v", err) |
| 240 | + } |
| 241 | + if err := f.deps.Set(ctx, orgScope, "IMAGE_TAG", "org", f.userID); err != nil { |
| 242 | + t.Fatalf("Set org: %v", err) |
| 243 | + } |
| 244 | + repoVar, err := f.deps.Get(ctx, repoScope, "IMAGE_TAG") |
| 245 | + if err != nil { |
| 246 | + t.Fatalf("Get repo: %v", err) |
| 247 | + } |
| 248 | + orgVar, err := f.deps.Get(ctx, orgScope, "IMAGE_TAG") |
| 249 | + if err != nil { |
| 250 | + t.Fatalf("Get org: %v", err) |
| 251 | + } |
| 252 | + if repoVar.Value != "repo" || orgVar.Value != "org" { |
| 253 | + t.Fatalf("scope bleed: repo=%q org=%q", repoVar.Value, orgVar.Value) |
| 254 | + } |
| 255 | +} |