|
6 | 6 | package cmd |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "bytes" |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "strings" |
9 | 13 | "testing" |
| 14 | + |
| 15 | + "github.com/DataDog/datadog-api-client-go/v2/api/datadog" |
| 16 | + "github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" |
| 17 | + "github.com/DataDog/pup/pkg/client" |
| 18 | + "github.com/DataDog/pup/pkg/config" |
10 | 19 | ) |
11 | 20 |
|
12 | 21 | func TestAPIKeysCmd(t *testing.T) { |
@@ -139,3 +148,250 @@ func TestAPIKeysCmd_ParentChild(t *testing.T) { |
139 | 148 | } |
140 | 149 | } |
141 | 150 | } |
| 151 | + |
| 152 | +// Helper function to create a test client with mock data |
| 153 | +func setupTestClient(t *testing.T) func() { |
| 154 | + t.Helper() |
| 155 | + |
| 156 | + // Save original values |
| 157 | + origClient := ddClient |
| 158 | + origCfg := cfg |
| 159 | + origFactory := clientFactory |
| 160 | + |
| 161 | + // Create test config |
| 162 | + cfg = &config.Config{ |
| 163 | + Site: "datadoghq.com", |
| 164 | + APIKey: "test-api-key-12345678", |
| 165 | + AppKey: "test-app-key-12345678", |
| 166 | + AutoApprove: false, |
| 167 | + } |
| 168 | + |
| 169 | + // Mock the client factory to return an error immediately |
| 170 | + clientFactory = func(c *config.Config) (*client.Client, error) { |
| 171 | + return nil, fmt.Errorf("mock client: no real API connection in tests") |
| 172 | + } |
| 173 | + |
| 174 | + ddClient = nil |
| 175 | + |
| 176 | + // Return cleanup function |
| 177 | + return func() { |
| 178 | + ddClient = origClient |
| 179 | + cfg = origCfg |
| 180 | + clientFactory = origFactory |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// Helper to capture output |
| 185 | +func captureOutput(t *testing.T, f func()) string { |
| 186 | + t.Helper() |
| 187 | + var buf bytes.Buffer |
| 188 | + origWriter := outputWriter |
| 189 | + outputWriter = &buf |
| 190 | + defer func() { outputWriter = origWriter }() |
| 191 | + f() |
| 192 | + return buf.String() |
| 193 | +} |
| 194 | + |
| 195 | +func TestRunAPIKeysList(t *testing.T) { |
| 196 | + cleanup := setupTestClient(t) |
| 197 | + defer cleanup() |
| 198 | + |
| 199 | + tests := []struct { |
| 200 | + name string |
| 201 | + wantErr bool |
| 202 | + }{ |
| 203 | + { |
| 204 | + name: "requires valid client", |
| 205 | + wantErr: true, // Will fail without real API credentials |
| 206 | + }, |
| 207 | + } |
| 208 | + |
| 209 | + for _, tt := range tests { |
| 210 | + t.Run(tt.name, func(t *testing.T) { |
| 211 | + var buf bytes.Buffer |
| 212 | + outputWriter = &buf |
| 213 | + defer func() { outputWriter = os.Stdout }() |
| 214 | + |
| 215 | + err := runAPIKeysList(apiKeysListCmd, []string{}) |
| 216 | + |
| 217 | + if (err != nil) != tt.wantErr { |
| 218 | + t.Errorf("runAPIKeysList() error = %v, wantErr %v", err, tt.wantErr) |
| 219 | + } |
| 220 | + }) |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +func TestRunAPIKeysGet(t *testing.T) { |
| 225 | + cleanup := setupTestClient(t) |
| 226 | + defer cleanup() |
| 227 | + |
| 228 | + tests := []struct { |
| 229 | + name string |
| 230 | + args []string |
| 231 | + wantErr bool |
| 232 | + }{ |
| 233 | + { |
| 234 | + name: "with valid key ID", |
| 235 | + args: []string{"test-key-id"}, |
| 236 | + wantErr: true, // Will fail without real API |
| 237 | + }, |
| 238 | + { |
| 239 | + name: "requires key ID", |
| 240 | + args: []string{}, |
| 241 | + wantErr: true, |
| 242 | + }, |
| 243 | + } |
| 244 | + |
| 245 | + for _, tt := range tests { |
| 246 | + t.Run(tt.name, func(t *testing.T) { |
| 247 | + var buf bytes.Buffer |
| 248 | + outputWriter = &buf |
| 249 | + defer func() { outputWriter = os.Stdout }() |
| 250 | + |
| 251 | + // For empty args, we test the command validation |
| 252 | + if len(tt.args) == 0 { |
| 253 | + // cobra.ExactArgs(1) will catch this |
| 254 | + return |
| 255 | + } |
| 256 | + |
| 257 | + err := runAPIKeysGet(apiKeysGetCmd, tt.args) |
| 258 | + |
| 259 | + if (err != nil) != tt.wantErr { |
| 260 | + t.Errorf("runAPIKeysGet() error = %v, wantErr %v", err, tt.wantErr) |
| 261 | + } |
| 262 | + }) |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +func TestRunAPIKeysCreate(t *testing.T) { |
| 267 | + cleanup := setupTestClient(t) |
| 268 | + defer cleanup() |
| 269 | + |
| 270 | + tests := []struct { |
| 271 | + name string |
| 272 | + keyName string |
| 273 | + wantErr bool |
| 274 | + }{ |
| 275 | + { |
| 276 | + name: "with valid name", |
| 277 | + keyName: "test-key", |
| 278 | + wantErr: true, // Will fail without real API |
| 279 | + }, |
| 280 | + } |
| 281 | + |
| 282 | + for _, tt := range tests { |
| 283 | + t.Run(tt.name, func(t *testing.T) { |
| 284 | + apiKeyName = tt.keyName |
| 285 | + |
| 286 | + var buf bytes.Buffer |
| 287 | + outputWriter = &buf |
| 288 | + defer func() { outputWriter = os.Stdout }() |
| 289 | + |
| 290 | + err := runAPIKeysCreate(apiKeysCreateCmd, []string{}) |
| 291 | + |
| 292 | + if (err != nil) != tt.wantErr { |
| 293 | + t.Errorf("runAPIKeysCreate() error = %v, wantErr %v", err, tt.wantErr) |
| 294 | + } |
| 295 | + }) |
| 296 | + } |
| 297 | +} |
| 298 | + |
| 299 | +func TestRunAPIKeysDelete_AutoApprove(t *testing.T) { |
| 300 | + cleanup := setupTestClient(t) |
| 301 | + defer cleanup() |
| 302 | + |
| 303 | + // Set auto-approve |
| 304 | + cfg.AutoApprove = true |
| 305 | + |
| 306 | + tests := []struct { |
| 307 | + name string |
| 308 | + args []string |
| 309 | + wantErr bool |
| 310 | + }{ |
| 311 | + { |
| 312 | + name: "with auto-approve", |
| 313 | + args: []string{"test-key-id"}, |
| 314 | + wantErr: true, // Will fail without real API |
| 315 | + }, |
| 316 | + } |
| 317 | + |
| 318 | + for _, tt := range tests { |
| 319 | + t.Run(tt.name, func(t *testing.T) { |
| 320 | + var buf bytes.Buffer |
| 321 | + outputWriter = &buf |
| 322 | + defer func() { outputWriter = os.Stdout }() |
| 323 | + |
| 324 | + err := runAPIKeysDelete(apiKeysDeleteCmd, tt.args) |
| 325 | + |
| 326 | + if (err != nil) != tt.wantErr { |
| 327 | + t.Errorf("runAPIKeysDelete() error = %v, wantErr %v", err, tt.wantErr) |
| 328 | + } |
| 329 | + }) |
| 330 | + } |
| 331 | +} |
| 332 | + |
| 333 | +func TestRunAPIKeysDelete_WithConfirmation(t *testing.T) { |
| 334 | + cleanup := setupTestClient(t) |
| 335 | + defer cleanup() |
| 336 | + |
| 337 | + // Disable auto-approve |
| 338 | + cfg.AutoApprove = false |
| 339 | + |
| 340 | + tests := []struct { |
| 341 | + name string |
| 342 | + args []string |
| 343 | + input string |
| 344 | + wantErr bool |
| 345 | + }{ |
| 346 | + { |
| 347 | + name: "fails on client creation (mock)", |
| 348 | + args: []string{"test-key-id"}, |
| 349 | + input: "no\n", |
| 350 | + wantErr: true, // getClient() called before confirmation |
| 351 | + }, |
| 352 | + { |
| 353 | + name: "fails on client creation with yes (mock)", |
| 354 | + args: []string{"test-key-id"}, |
| 355 | + input: "yes\n", |
| 356 | + wantErr: true, // getClient() called before confirmation |
| 357 | + }, |
| 358 | + } |
| 359 | + |
| 360 | + for _, tt := range tests { |
| 361 | + t.Run(tt.name, func(t *testing.T) { |
| 362 | + var buf bytes.Buffer |
| 363 | + outputWriter = &buf |
| 364 | + defer func() { outputWriter = os.Stdout }() |
| 365 | + |
| 366 | + // Simulate input |
| 367 | + inputReader = strings.NewReader(tt.input) |
| 368 | + defer func() { inputReader = os.Stdin }() |
| 369 | + |
| 370 | + err := runAPIKeysDelete(apiKeysDeleteCmd, tt.args) |
| 371 | + |
| 372 | + if (err != nil) != tt.wantErr { |
| 373 | + t.Errorf("runAPIKeysDelete() error = %v, wantErr %v", err, tt.wantErr) |
| 374 | + } |
| 375 | + }) |
| 376 | + } |
| 377 | +} |
| 378 | + |
| 379 | +func TestAPIKeyFormatter(t *testing.T) { |
| 380 | + // Test that we can format API key responses |
| 381 | + testKey := datadogV2.APIKeyResponse{ |
| 382 | + Data: &datadogV2.FullAPIKey{ |
| 383 | + Id: datadog.PtrString("test-key-id"), |
| 384 | + Attributes: &datadogV2.FullAPIKeyAttributes{ |
| 385 | + Name: datadog.PtrString("Test Key"), |
| 386 | + }, |
| 387 | + }, |
| 388 | + } |
| 389 | + |
| 390 | + if testKey.Data == nil { |
| 391 | + t.Error("Test key data is nil") |
| 392 | + } |
| 393 | + |
| 394 | + if testKey.Data.Id == nil || *testKey.Data.Id != "test-key-id" { |
| 395 | + t.Error("Test key ID not set correctly") |
| 396 | + } |
| 397 | +} |
0 commit comments