|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "slices" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestListBootstrapRepoNamesPaginate(t *testing.T) { |
| 13 | + originalRunGH := runBootstrapGHContext |
| 14 | + t.Cleanup(func() { |
| 15 | + runBootstrapGHContext = originalRunGH |
| 16 | + }) |
| 17 | + |
| 18 | + calls := []string{} |
| 19 | + runBootstrapGHContext = func(_ context.Context, _ string, args ...string) ([]byte, error) { |
| 20 | + calls = append(calls, strings.Join(args, " ")) |
| 21 | + if strings.Contains(args[1], "/variables") { |
| 22 | + return []byte("ALPHA\nOMEGA\n"), nil |
| 23 | + } |
| 24 | + return []byte("FIRST\nSECOND\n"), nil |
| 25 | + } |
| 26 | + |
| 27 | + variables, err := listBootstrapRepoVariableNames(context.Background(), "octo/platform-ops") |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("listBootstrapRepoVariableNames returned error: %v", err) |
| 30 | + } |
| 31 | + if !slices.Equal(variables, []string{"ALPHA", "OMEGA"}) { |
| 32 | + t.Fatalf("unexpected variables: %#v", variables) |
| 33 | + } |
| 34 | + |
| 35 | + secrets, err := listBootstrapRepoSecretNames(context.Background(), "octo/platform-ops") |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("listBootstrapRepoSecretNames returned error: %v", err) |
| 38 | + } |
| 39 | + if !slices.Equal(secrets, []string{"FIRST", "SECOND"}) { |
| 40 | + t.Fatalf("unexpected secrets: %#v", secrets) |
| 41 | + } |
| 42 | + |
| 43 | + if len(calls) != 2 { |
| 44 | + t.Fatalf("expected 2 gh api calls, got %d", len(calls)) |
| 45 | + } |
| 46 | + for _, call := range calls { |
| 47 | + if !strings.Contains(call, "--paginate") { |
| 48 | + t.Fatalf("expected paginated gh api call, got %q", call) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestRunBootstrapRepoVariableAction(t *testing.T) { |
| 54 | + originalUpsertVariable := bootstrapUpsertVariable |
| 55 | + t.Cleanup(func() { |
| 56 | + bootstrapUpsertVariable = originalUpsertVariable |
| 57 | + }) |
| 58 | + |
| 59 | + t.Setenv(bootstrapRepositoryVariableEnvName("MY_VAR"), "configured") |
| 60 | + var gotName, gotValue string |
| 61 | + bootstrapUpsertVariable = func(_ context.Context, _ string, name, value string) error { |
| 62 | + gotName = name |
| 63 | + gotValue = value |
| 64 | + return nil |
| 65 | + } |
| 66 | + |
| 67 | + applied, err := runBootstrapRepoVariableAction(context.Background(), "octo/platform-ops", repositoryPackageBootstrapAction{ |
| 68 | + Name: "MY_VAR", |
| 69 | + }, &bootstrapProfileExistingState{variables: map[string]struct{}{}, secrets: map[string]struct{}{}}) |
| 70 | + if err != nil { |
| 71 | + t.Fatalf("runBootstrapRepoVariableAction returned error: %v", err) |
| 72 | + } |
| 73 | + if !applied { |
| 74 | + t.Fatal("expected variable action to apply") |
| 75 | + } |
| 76 | + if gotName != "MY_VAR" || gotValue != "configured" { |
| 77 | + t.Fatalf("unexpected variable write: %s=%s", gotName, gotValue) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestRunBootstrapRepoSecretAction(t *testing.T) { |
| 82 | + originalSetSecret := bootstrapSetSecret |
| 83 | + t.Cleanup(func() { |
| 84 | + bootstrapSetSecret = originalSetSecret |
| 85 | + }) |
| 86 | + |
| 87 | + t.Setenv(bootstrapRepositorySecretEnvName("MY_SECRET"), "top-secret") |
| 88 | + var gotName, gotValue string |
| 89 | + bootstrapSetSecret = func(_ context.Context, _ string, name, value string) error { |
| 90 | + gotName = name |
| 91 | + gotValue = value |
| 92 | + return nil |
| 93 | + } |
| 94 | + |
| 95 | + applied, err := runBootstrapRepoSecretAction(context.Background(), "octo/platform-ops", repositoryPackageBootstrapAction{ |
| 96 | + Name: "MY_SECRET", |
| 97 | + }, &bootstrapProfileExistingState{variables: map[string]struct{}{}, secrets: map[string]struct{}{}}) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("runBootstrapRepoSecretAction returned error: %v", err) |
| 100 | + } |
| 101 | + if !applied { |
| 102 | + t.Fatal("expected secret action to apply") |
| 103 | + } |
| 104 | + if gotName != "MY_SECRET" || gotValue != "top-secret" { |
| 105 | + t.Fatalf("unexpected secret write: %s=%s", gotName, gotValue) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestRunBootstrapCopilotAuthAction(t *testing.T) { |
| 110 | + t.Run("skips actions token auth", func(t *testing.T) { |
| 111 | + applied, err := runBootstrapCopilotAuthAction(context.Background(), "octo/platform-ops", repositoryPackageBootstrapAction{ |
| 112 | + Secret: "COPILOT_TOKEN", |
| 113 | + }, &bootstrapProfileExistingState{variables: map[string]struct{}{}, secrets: map[string]struct{}{}}, true) |
| 114 | + if err != nil { |
| 115 | + t.Fatalf("runBootstrapCopilotAuthAction returned error: %v", err) |
| 116 | + } |
| 117 | + if applied { |
| 118 | + t.Fatal("expected action to skip when Actions token auth is enabled") |
| 119 | + } |
| 120 | + }) |
| 121 | + |
| 122 | + t.Run("stores valid pat", func(t *testing.T) { |
| 123 | + originalSetSecret := bootstrapSetSecret |
| 124 | + t.Cleanup(func() { |
| 125 | + bootstrapSetSecret = originalSetSecret |
| 126 | + }) |
| 127 | + |
| 128 | + t.Setenv("COPILOT_TOKEN", "github_pat_abc123xyz") |
| 129 | + var wrote string |
| 130 | + bootstrapSetSecret = func(_ context.Context, _ string, name, value string) error { |
| 131 | + wrote = name + "=" + value |
| 132 | + return nil |
| 133 | + } |
| 134 | + |
| 135 | + applied, err := runBootstrapCopilotAuthAction(context.Background(), "octo/platform-ops", repositoryPackageBootstrapAction{ |
| 136 | + Secret: "COPILOT_TOKEN", |
| 137 | + }, &bootstrapProfileExistingState{variables: map[string]struct{}{}, secrets: map[string]struct{}{}}, false) |
| 138 | + if err != nil { |
| 139 | + t.Fatalf("runBootstrapCopilotAuthAction returned error: %v", err) |
| 140 | + } |
| 141 | + if !applied { |
| 142 | + t.Fatal("expected action to apply") |
| 143 | + } |
| 144 | + if wrote != "COPILOT_TOKEN=github_pat_abc123xyz" { |
| 145 | + t.Fatalf("unexpected secret write: %s", wrote) |
| 146 | + } |
| 147 | + }) |
| 148 | +} |
| 149 | + |
| 150 | +func TestBootstrapRepoMutationHelpers_RejectInvalidRepo(t *testing.T) { |
| 151 | + if err := upsertBootstrapRepoVariable("not-a-repo", "NAME", "value"); err == nil { |
| 152 | + t.Fatal("expected invalid repo slug error for variable upsert") |
| 153 | + } |
| 154 | + if err := setBootstrapRepoSecret("not-a-repo", "NAME", "value"); err == nil { |
| 155 | + t.Fatal("expected invalid repo slug error for secret set") |
| 156 | + } |
| 157 | +} |
0 commit comments