| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/release" |
| 10 | "github.com/carlosframework/platform/internal/updatewords" |
| 11 | ) |
| 12 | |
| 13 | func runAuthorize(t *testing.T, answer string, args ...string) string { |
| 14 | t.Helper() |
| 15 | var out bytes.Buffer |
| 16 | prevOut, prevIn := stdout, authorizeInput |
| 17 | stdout, authorizeInput = &out, strings.NewReader(answer) |
| 18 | t.Cleanup(func() { stdout, authorizeInput = prevOut, prevIn }) |
| 19 | runReleasesAuthorize(args) |
| 20 | return out.String() |
| 21 | } |
| 22 | |
| 23 | func TestReleasesAuthorizeAsksThenSigns(t *testing.T) { |
| 24 | s, pub := directDigestStore(t) // plan 1's helper: km v1 shipped and promoted to stable |
| 25 | ctx := context.Background() |
| 26 | if err := s.Delete(ctx, release.PromotionRecordKey("bab", "km", "v1")); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | out := runAuthorize(t, "no\n", "--account-id", "bab", "km", "v1") |
| 30 | if !strings.Contains(out, "linux-amd64") || !strings.Contains(out, updatewords.Text("settings.authorize.aborted")) { |
| 31 | t.Fatalf("answering no:\n%s", out) |
| 32 | } |
| 33 | if _, err := release.ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1"); err == nil { |
| 34 | t.Fatal("answering no wrote a record") |
| 35 | } |
| 36 | runAuthorize(t, "yes\n", "--account-id", "bab", "--app", "km", "v1") |
| 37 | if rec, err := release.ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1"); err != nil || !rec.Authorized { |
| 38 | t.Fatalf("answering yes: %+v, %v", rec, err) |
| 39 | } |
| 40 | out = runAuthorize(t, "", "--account-id", "bab", "--yes", "km", "v1") |
| 41 | if !strings.Contains(out, "already") { |
| 42 | t.Fatalf("a second authorize:\n%s", out) |
| 43 | } |
| 44 | } |
| 45 | |