diff --git a/cmd/root.go b/cmd/root.go index 1893916b..0904c094 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -205,6 +205,13 @@ var testCmd = &cobra.Command{ return err } + if cfg.APIKey == "" { + return fmt.Errorf("DD_API_KEY is required (set via environment variable or run 'pup auth login')") + } + if cfg.AppKey == "" { + return fmt.Errorf("DD_APP_KEY is required (set via environment variable or run 'pup auth login')") + } + fmt.Println("Configuration is valid:") fmt.Printf(" Site: %s\n", cfg.Site) fmt.Printf(" API Key: %s...%s\n", cfg.APIKey[:8], cfg.APIKey[len(cfg.APIKey)-4:]) diff --git a/cmd/root_test.go b/cmd/root_test.go index c171347d..9c853333 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -9,6 +9,8 @@ import ( "errors" "strings" "testing" + + "github.com/DataDog/pup/pkg/config" ) func TestRootCmd_SilenceUsage(t *testing.T) { @@ -185,6 +187,71 @@ func TestFormatAPIError(t *testing.T) { } } +func TestTestCmd_MissingKeys(t *testing.T) { + tests := []struct { + name string + apiKey string + appKey string + wantErr string + }{ + { + name: "missing API key", + apiKey: "", + appKey: "test-app-key-value", + wantErr: "DD_API_KEY is required", + }, + { + name: "missing App key", + apiKey: "test-api-key-value", + appKey: "", + wantErr: "DD_APP_KEY is required", + }, + { + name: "both keys missing", + apiKey: "", + appKey: "", + wantErr: "DD_API_KEY is required", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + origCfg := cfg + defer func() { cfg = origCfg }() + + cfg = &config.Config{ + APIKey: tt.apiKey, + AppKey: tt.appKey, + Site: "datadoghq.com", + } + + err := testCmd.RunE(testCmd, []string{}) + if err == nil { + t.Fatal("expected error, got nil") + } + if !strings.Contains(err.Error(), tt.wantErr) { + t.Errorf("error = %q, want containing %q", err.Error(), tt.wantErr) + } + }) + } +} + +func TestTestCmd_ValidKeys(t *testing.T) { + origCfg := cfg + defer func() { cfg = origCfg }() + + cfg = &config.Config{ + APIKey: "abcdefgh12345678", + AppKey: "ijklmnop87654321", + Site: "datadoghq.com", + } + + err := testCmd.RunE(testCmd, []string{}) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } +} + func TestFormatAPIError_AllStatusCodes(t *testing.T) { // Test that all documented status codes get special handling statusTests := []struct {