|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + psauth "github.com/planetscale/cli/internal/auth" |
| 9 | + "github.com/planetscale/cli/internal/cmdutil" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAuthLoginHelpOmitsOAuthSecrets(t *testing.T) { |
| 14 | + assertHelpOmitsOAuthSecrets(t, LoginCmd(&cmdutil.Helper{})) |
| 15 | +} |
| 16 | + |
| 17 | +func TestAuthLogoutHelpOmitsOAuthSecrets(t *testing.T) { |
| 18 | + assertHelpOmitsOAuthSecrets(t, LogoutCmd(&cmdutil.Helper{})) |
| 19 | +} |
| 20 | + |
| 21 | +func TestAuthCheckHasNoOAuthClientFlags(t *testing.T) { |
| 22 | + cmd := CheckCmd(&cmdutil.Helper{}) |
| 23 | + if cmd.Flags().Lookup("client-id") != nil { |
| 24 | + t.Fatal("auth check must not define --client-id") |
| 25 | + } |
| 26 | + if cmd.Flags().Lookup("client-secret") != nil { |
| 27 | + t.Fatal("auth check must not define --client-secret") |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func assertHelpOmitsOAuthSecrets(t *testing.T, cmd *cobra.Command) { |
| 32 | + t.Helper() |
| 33 | + |
| 34 | + var out bytes.Buffer |
| 35 | + cmd.SetOut(&out) |
| 36 | + cmd.SetErr(&out) |
| 37 | + cmd.SetArgs([]string{"--help"}) |
| 38 | + if err := cmd.Execute(); err != nil { |
| 39 | + t.Fatalf("help: %v", err) |
| 40 | + } |
| 41 | + |
| 42 | + help := out.String() |
| 43 | + if strings.Contains(help, psauth.OAuthClientID) { |
| 44 | + t.Fatalf("help text must not include OAuth client ID; got:\n%s", help) |
| 45 | + } |
| 46 | + if strings.Contains(help, psauth.OAuthClientSecret) { |
| 47 | + t.Fatalf("help text must not include OAuth client secret; got:\n%s", help) |
| 48 | + } |
| 49 | + if !strings.Contains(help, "--client-id") { |
| 50 | + t.Fatalf("help text should still document --client-id; got:\n%s", help) |
| 51 | + } |
| 52 | + if !strings.Contains(help, "--client-secret") { |
| 53 | + t.Fatalf("help text should still document --client-secret; got:\n%s", help) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestResolveOAuthClientDefaults(t *testing.T) { |
| 58 | + id, secret := resolveOAuthClient("", "") |
| 59 | + if id != psauth.OAuthClientID { |
| 60 | + t.Fatalf("client ID = %q, want built-in default", id) |
| 61 | + } |
| 62 | + if secret != psauth.OAuthClientSecret { |
| 63 | + t.Fatalf("client secret = %q, want built-in default", secret) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestResolveOAuthClientOverrides(t *testing.T) { |
| 68 | + id, secret := resolveOAuthClient("custom-id", "custom-secret") |
| 69 | + if id != "custom-id" { |
| 70 | + t.Fatalf("client ID = %q, want custom-id", id) |
| 71 | + } |
| 72 | + if secret != "custom-secret" { |
| 73 | + t.Fatalf("client secret = %q, want custom-secret", secret) |
| 74 | + } |
| 75 | +} |
0 commit comments