|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/steipete/gogcli/internal/config" |
| 12 | + "github.com/steipete/gogcli/internal/googleauth" |
| 13 | + "github.com/steipete/gogcli/internal/outfmt" |
| 14 | + "github.com/steipete/gogcli/internal/ui" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAuthKeepCmd_JSON(t *testing.T) { |
| 18 | + home := t.TempDir() |
| 19 | + t.Setenv("HOME", home) |
| 20 | + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg")) |
| 21 | + |
| 22 | + keyPath := filepath.Join(t.TempDir(), "sa.json") |
| 23 | + if err := os.WriteFile(keyPath, []byte(`{"type":"service_account"}`), 0o600); err != nil { |
| 24 | + t.Fatalf("write key: %v", err) |
| 25 | + } |
| 26 | + |
| 27 | + u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("ui.New: %v", err) |
| 30 | + } |
| 31 | + ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) |
| 32 | + |
| 33 | + cmd := AuthKeepCmd{Email: "a@b.com", Key: keyPath} |
| 34 | + out := captureStdout(t, func() { |
| 35 | + if err := cmd.Run(ctx); err != nil { |
| 36 | + t.Fatalf("AuthKeepCmd: %v", err) |
| 37 | + } |
| 38 | + }) |
| 39 | + var payload struct { |
| 40 | + Stored bool `json:"stored"` |
| 41 | + Email string `json:"email"` |
| 42 | + Path string `json:"path"` |
| 43 | + } |
| 44 | + if err := json.Unmarshal([]byte(out), &payload); err != nil { |
| 45 | + t.Fatalf("decode output: %v", err) |
| 46 | + } |
| 47 | + if !payload.Stored || payload.Email != "a@b.com" || payload.Path == "" { |
| 48 | + t.Fatalf("unexpected payload: %#v", payload) |
| 49 | + } |
| 50 | + if _, err := os.Stat(payload.Path); err != nil { |
| 51 | + t.Fatalf("missing stored key: %v", err) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestAuthManageCmd(t *testing.T) { |
| 56 | + orig := startManageServer |
| 57 | + t.Cleanup(func() { startManageServer = orig }) |
| 58 | + |
| 59 | + var captured googleauth.ManageServerOptions |
| 60 | + startManageServer = func(_ context.Context, opts googleauth.ManageServerOptions) error { |
| 61 | + captured = opts |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + cmd := AuthManageCmd{ServicesCSV: "gmail,calendar", ForceConsent: true} |
| 66 | + if err := cmd.Run(context.Background()); err != nil { |
| 67 | + t.Fatalf("AuthManageCmd: %v", err) |
| 68 | + } |
| 69 | + if !captured.ForceConsent || len(captured.Services) != 2 { |
| 70 | + t.Fatalf("unexpected manage options: %#v", captured) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestAuthServicesCmd_Markdown(t *testing.T) { |
| 75 | + u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) |
| 76 | + if err != nil { |
| 77 | + t.Fatalf("ui.New: %v", err) |
| 78 | + } |
| 79 | + ctx := ui.WithUI(context.Background(), u) |
| 80 | + |
| 81 | + cmd := AuthServicesCmd{Markdown: true} |
| 82 | + out := captureStdout(t, func() { |
| 83 | + if err := cmd.Run(ctx); err != nil { |
| 84 | + t.Fatalf("AuthServicesCmd: %v", err) |
| 85 | + } |
| 86 | + }) |
| 87 | + if !strings.Contains(out, "|") { |
| 88 | + t.Fatalf("expected markdown output, got: %q", out) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestAuthServicesCmd_JSON(t *testing.T) { |
| 93 | + u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("ui.New: %v", err) |
| 96 | + } |
| 97 | + ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) |
| 98 | + |
| 99 | + cmd := AuthServicesCmd{} |
| 100 | + out := captureStdout(t, func() { |
| 101 | + if err := cmd.Run(ctx); err != nil { |
| 102 | + t.Fatalf("AuthServicesCmd: %v", err) |
| 103 | + } |
| 104 | + }) |
| 105 | + if !strings.Contains(out, "\"services\"") { |
| 106 | + t.Fatalf("unexpected json output: %q", out) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestAuthServicesCmd_Table(t *testing.T) { |
| 111 | + u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("ui.New: %v", err) |
| 114 | + } |
| 115 | + ctx := ui.WithUI(context.Background(), u) |
| 116 | + |
| 117 | + cmd := AuthServicesCmd{} |
| 118 | + out := captureStdout(t, func() { |
| 119 | + if err := cmd.Run(ctx); err != nil { |
| 120 | + t.Fatalf("AuthServicesCmd: %v", err) |
| 121 | + } |
| 122 | + }) |
| 123 | + if !strings.Contains(out, "SERVICE") { |
| 124 | + t.Fatalf("unexpected table output: %q", out) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestAuthKeepCmd_Text(t *testing.T) { |
| 129 | + home := t.TempDir() |
| 130 | + t.Setenv("HOME", home) |
| 131 | + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg")) |
| 132 | + |
| 133 | + keyPath := filepath.Join(t.TempDir(), "sa.json") |
| 134 | + if err := os.WriteFile(keyPath, []byte(`{"type":"service_account"}`), 0o600); err != nil { |
| 135 | + t.Fatalf("write key: %v", err) |
| 136 | + } |
| 137 | + |
| 138 | + out := captureStdout(t, func() { |
| 139 | + u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) |
| 140 | + if err != nil { |
| 141 | + t.Fatalf("ui.New: %v", err) |
| 142 | + } |
| 143 | + ctx := ui.WithUI(context.Background(), u) |
| 144 | + |
| 145 | + cmd := AuthKeepCmd{Email: "a@b.com", Key: keyPath} |
| 146 | + if err := cmd.Run(ctx); err != nil { |
| 147 | + t.Fatalf("AuthKeepCmd: %v", err) |
| 148 | + } |
| 149 | + }) |
| 150 | + if !strings.Contains(out, "Keep service account configured") { |
| 151 | + t.Fatalf("unexpected output: %q", out) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestAuthStatusCmd_JSON(t *testing.T) { |
| 156 | + home := t.TempDir() |
| 157 | + t.Setenv("HOME", home) |
| 158 | + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, "xdg")) |
| 159 | + |
| 160 | + u, err := ui.New(ui.Options{Stdout: os.Stdout, Stderr: os.Stderr, Color: "never"}) |
| 161 | + if err != nil { |
| 162 | + t.Fatalf("ui.New: %v", err) |
| 163 | + } |
| 164 | + ctx := outfmt.WithMode(ui.WithUI(context.Background(), u), outfmt.Mode{JSON: true}) |
| 165 | + |
| 166 | + if _, err := config.ConfigPath(); err != nil { |
| 167 | + t.Fatalf("ConfigPath: %v", err) |
| 168 | + } |
| 169 | + |
| 170 | + cmd := AuthStatusCmd{} |
| 171 | + out := captureStdout(t, func() { |
| 172 | + if err := cmd.Run(ctx); err != nil { |
| 173 | + t.Fatalf("AuthStatusCmd: %v", err) |
| 174 | + } |
| 175 | + }) |
| 176 | + if !strings.Contains(out, "\"keyring\"") || !strings.Contains(out, "\"config\"") { |
| 177 | + t.Fatalf("unexpected status output: %q", out) |
| 178 | + } |
| 179 | +} |
0 commit comments