atlassian-cli 0.9.1

Unified CLI for Atlassian Cloud products
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
mod common;

use common::Sandbox;
use tempfile::tempdir;

#[test]
fn test_cli_version() {
    let output = Sandbox::new()
        .cli()
        .args(["--version"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("atlassian-cli"));
    // Check version matches Cargo.toml (works with any version)
    const EXPECTED_VERSION: &str = env!("CARGO_PKG_VERSION");
    assert!(
        stdout.contains(EXPECTED_VERSION),
        "CLI should output version {}, got: {}",
        EXPECTED_VERSION,
        stdout
    );
}

#[test]
fn test_cli_help() {
    let output = Sandbox::new()
        .cli()
        .args(["--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Usage:"));
    assert!(stdout.contains("jira"));
    assert!(stdout.contains("confluence"));
    assert!(stdout.contains("bitbucket"));
    // Verify alias is shown in help (visible_alias shows as [alias: bb])
    assert!(stdout.contains("bb") || stdout.contains("[alias"));
}

#[test]
fn test_jira_help() {
    let output = Sandbox::new()
        .cli()
        .args(["jira", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Jira commands"));
    assert!(stdout.contains("search"));
    assert!(stdout.contains("create"));
}

#[test]
fn test_confluence_help() {
    let output = Sandbox::new()
        .cli()
        .args(["confluence", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Confluence commands"));
}

#[test]
fn test_auth_help() {
    let output = Sandbox::new()
        .cli()
        .args(["auth", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Authentication commands"));
}

/// Regression: `--token` carries a clap `env` annotation, and clap prints the
/// variable's value in `--help` unless told not to. Anyone with the token
/// exported leaked it by asking for help, CI logs included.
#[test]
fn test_auth_login_help_hides_the_token_env_value() {
    let output = Sandbox::new()
        .cli()
        .args(["auth", "login", "--help"])
        .env("ATLASSIAN_API_TOKEN", "super-secret-token-value")
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        !stdout.contains("super-secret-token-value"),
        "auth login --help leaked the token value. Got: {stdout}"
    );
    // The variable is still advertised, just without its value.
    assert!(
        stdout.contains("ATLASSIAN_API_TOKEN"),
        "auth login --help should still name the env var. Got: {stdout}"
    );
}

#[test]
fn test_output_format_flag() {
    let output = Sandbox::new()
        .cli()
        .args(["--format", "json", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
}

#[test]
fn test_invalid_command() {
    let output = Sandbox::new()
        .cli()
        .args(["nonexistent"])
        .output()
        .expect("Failed to execute command");

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("unrecognized subcommand") || stderr.contains("error:"));
}

#[test]
fn test_bb_alias_works() {
    // Test that 'bb' alias executes the same as 'bitbucket'
    let bb_output = Sandbox::new()
        .cli()
        .args(["bb", "--help"])
        .output()
        .expect("Failed to execute bb alias");

    let bitbucket_output = Sandbox::new()
        .cli()
        .args(["bitbucket", "--help"])
        .output()
        .expect("Failed to execute bitbucket command");

    assert!(bb_output.status.success());
    assert!(bitbucket_output.status.success());

    // Both should produce similar help output for bitbucket subcommands
    let bb_help = String::from_utf8_lossy(&bb_output.stdout);
    let bitbucket_help = String::from_utf8_lossy(&bitbucket_output.stdout);

    // Verify both show Bitbucket subcommands
    assert!(bb_help.contains("repo") || bb_help.contains("Repo"));
    assert!(bb_help.contains("pipeline") || bb_help.contains("Pipeline"));
    assert!(bitbucket_help.contains("repo") || bitbucket_help.contains("Repo"));
    assert!(bitbucket_help.contains("pipeline") || bitbucket_help.contains("Pipeline"));
}

#[test]
fn test_bitbucket_alias_backwards_compatible() {
    // Ensure 'bitbucket' and 'bb' behave identically (backward compatibility)
    let bitbucket_output = Sandbox::new()
        .cli()
        .args(["bitbucket", "whoami"])
        .env("ATLASSIAN_CLI_PROFILE", "nonexistent")
        .output()
        .expect("Failed to execute bitbucket whoami");

    let bb_output = Sandbox::new()
        .cli()
        .args(["bb", "whoami"])
        .env("ATLASSIAN_CLI_PROFILE", "nonexistent")
        .output()
        .expect("Failed to execute bb whoami");

    // Both should fail in the same way (no parsing errors, identical behavior)
    assert_eq!(
        bitbucket_output.status.success(),
        bb_output.status.success(),
        "Both commands should have identical exit status"
    );

    let bitbucket_stderr = String::from_utf8_lossy(&bitbucket_output.stderr);
    let bb_stderr = String::from_utf8_lossy(&bb_output.stderr);

    // Both should produce similar error messages (profile/auth related)
    assert_eq!(
        bitbucket_stderr, bb_stderr,
        "Both commands should produce identical error messages"
    );
}

/// Regression test: Bitbucket-only profiles (no base_url) should not fail with "missing base_url" error.
/// This tests that profile resolution is properly split between Bitbucket and Jira/Confluence commands.
#[test]
fn test_bitbucket_only_profile_no_base_url_error() {
    // Create a temp config with a Bitbucket-only profile (no base_url)
    let temp_dir = tempdir().expect("Failed to create temp dir");
    let config_path = temp_dir.path().join("config.yaml");

    let config_content = r#"
default_profile: bbonly
profiles:
  bbonly:
    email: test@example.com
    workspace: myworkspace
"#;

    std::fs::write(&config_path, config_content).expect("Failed to write config");

    // Run a bitbucket command with the custom config and a fake token via env var
    // The command will fail at the API call (no real credentials), but should NOT fail
    // at profile resolution with "missing base_url" error
    let output = Sandbox::new()
        .cli()
        .args([
            "--config",
            config_path.to_str().unwrap(),
            "bitbucket",
            "repo",
            "list",
        ])
        .env("ATLASSIAN_CLI_BITBUCKET_TOKEN_BBONLY", "fake-token")
        .output()
        .expect("Failed to execute command");

    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should NOT fail with "missing base_url" - that was the bug
    assert!(
        !stderr.contains("missing a base_url"),
        "Bitbucket-only profile should not require base_url. Got: {stderr}"
    );

    // Expected: fails at API level (401 or connection error), not profile resolution
    // The command will fail, but for the right reason (no valid credentials/API error)
}

/// Test that auth login help shows bearer flag and deprecation notice.
#[test]
fn test_auth_login_help_shows_bearer_flag() {
    let output = Sandbox::new()
        .cli()
        .args(["auth", "login", "--help"])
        .output()
        .expect("Failed to execute auth login help");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should show --bearer flag
    assert!(
        stdout.contains("--bearer"),
        "auth login help should mention --bearer flag. Got:\n{stdout}"
    );
    // Should mention deprecation
    assert!(
        stdout.contains("deprecated") || stdout.contains("App passwords"),
        "auth login help should mention app password deprecation. Got:\n{stdout}"
    );
    // Should show both basic and bearer examples
    assert!(
        stdout.contains("access token"),
        "auth login help should mention access tokens. Got:\n{stdout}"
    );
}

/// Test that bearer-only profile (no email) works for bitbucket commands.
#[test]
fn test_bitbucket_bearer_profile_no_email_required() {
    let temp_dir = tempdir().expect("Failed to create temp dir");
    let config_path = temp_dir.path().join("config.yaml");

    // Bearer profile: no email, has bitbucket_token_type: bearer
    let config_content = r#"
default_profile: ci
profiles:
  ci:
    workspace: myworkspace
    bitbucket_token_type: bearer
"#;

    std::fs::write(&config_path, config_content).expect("Failed to write config");

    let output = Sandbox::new()
        .cli()
        .args([
            "--config",
            config_path.to_str().unwrap(),
            "bitbucket",
            "repo",
            "list",
        ])
        .env("ATLASSIAN_CLI_BITBUCKET_TOKEN_CI", "fake-bearer-token")
        .output()
        .expect("Failed to execute command");

    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should NOT fail with "missing email" error
    assert!(
        !stderr.contains("missing an email"),
        "Bearer profile should not require email. Got: {stderr}"
    );

    // Expected: fails at API level (401 or connection error), not profile resolution
}

/// Test that Jira commands still require base_url (ensure we didn't break existing behavior).
#[test]
fn test_jira_still_requires_base_url() {
    let temp_dir = tempdir().expect("Failed to create temp dir");
    let config_path = temp_dir.path().join("config.yaml");

    // Profile with only email (no base_url) - should fail for Jira
    let config_content = r#"
default_profile: nobaseurl
profiles:
  nobaseurl:
    email: test@example.com
"#;

    std::fs::write(&config_path, config_content).expect("Failed to write config");

    let output = Sandbox::new()
        .cli()
        .args([
            "--config",
            config_path.to_str().unwrap(),
            "jira",
            "issue",
            "search",
        ])
        .env("ATLASSIAN_CLI_TOKEN_NOBASEURL", "fake-token")
        .output()
        .expect("Failed to execute command");

    let stderr = String::from_utf8_lossy(&output.stderr);

    // Should fail with "missing base_url" for Jira commands
    assert!(
        stderr.contains("missing a base_url") || stderr.contains("base-url"),
        "Jira commands should require base_url. Got: {stderr}"
    );
}

#[test]
fn test_jira_issue_create_help_mentions_custom_fields() {
    let output = Sandbox::new()
        .cli()
        .args(["jira", "issue", "create", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(
        stdout.contains("KEY=JSON_VALUE"),
        "expected --field value_name in help, got: {stdout}"
    );
    assert!(
        stdout.contains("jira fields list"),
        "expected fields-discovery hint in help, got: {stdout}"
    );
}

/// An invalid `ArgGroup` only panics when clap builds the command at runtime, so
/// exercising the parser is the only way to catch it. This covers the
/// `jira attachment download` group added for issue #93.
#[test]
fn test_jira_attachment_help_lists_subcommands() {
    let output = Sandbox::new()
        .cli()
        .args(["jira", "attachment", "--help"])
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success());
    let stdout = String::from_utf8_lossy(&output.stdout);
    for sub in ["list", "get", "download", "upload", "delete"] {
        assert!(
            stdout.contains(sub),
            "attachment help should list `{sub}`, got:\n{stdout}"
        );
    }
}

#[test]
fn test_jira_attachment_download_requires_a_source() {
    let output = Sandbox::new()
        .cli()
        .args(["jira", "attachment", "download"])
        .output()
        .expect("Failed to execute command");

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("required"),
        "expected a required-argument error, got: {stderr}"
    );
}

#[test]
fn test_jira_attachment_download_rejects_id_with_issue() {
    let output = Sandbox::new()
        .cli()
        .args([
            "jira",
            "attachment",
            "download",
            "10001",
            "--issue",
            "TEST-1",
        ])
        .output()
        .expect("Failed to execute command");

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cannot be used with"),
        "expected a conflict error, got: {stderr}"
    );
}

/// `--dir` is bulk-mode only. Without an explicit conflict, clap treats the
/// `requires = "issue"` as satisfied by the ArgGroup and silently ignores it.
#[test]
fn test_jira_attachment_download_rejects_dir_without_issue() {
    let output = Sandbox::new()
        .cli()
        .args(["jira", "attachment", "download", "10001", "--dir", "./out"])
        .output()
        .expect("Failed to execute command");

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cannot be used with"),
        "expected a conflict error, got: {stderr}"
    );
}

#[test]
fn test_jira_attachment_download_rejects_output_with_issue() {
    let output = Sandbox::new()
        .cli()
        .args([
            "jira",
            "attachment",
            "download",
            "--issue",
            "TEST-1",
            "--output",
            "f.bin",
        ])
        .output()
        .expect("Failed to execute command");

    assert!(!output.status.success());
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("cannot be used with"),
        "expected a conflict error, got: {stderr}"
    );
}

/// `--format` has always been global, so users and most of the published
/// examples write `--profile` after the subcommand too. It used to be rejected,
/// which broke a large share of the documented commands and every example
/// script in `docs/examples/`.
#[test]
fn test_profile_and_config_are_accepted_after_the_subcommand() {
    let temp_dir = tempdir().expect("Failed to create temp dir");
    let config_path = temp_dir.path().join("config.yaml");
    std::fs::write(
        &config_path,
        "default_profile: t\nprofiles:\n  t:\n    email: a@b.c\n    base_url: http://127.0.0.1:1\n",
    )
    .expect("Failed to write config");

    // Deeply nested subcommands too, not just the first level.
    for args in [
        vec!["jira", "issue", "get", "T-1"],
        vec!["confluence", "attachment", "list", "123"],
        vec!["jira", "attachment", "download", "10001", "--output", "-"],
    ] {
        let output = Sandbox::new()
            .cli()
            .args(&args)
            .args(["--profile", "t", "--config", config_path.to_str().unwrap()])
            .env("ATLASSIAN_CLI_TOKEN_T", "x")
            .output()
            .expect("Failed to execute command");

        // Exit code 2 is a clap usage error. Anything else means the flags were
        // accepted and the command failed at connect, which is the point.
        assert_ne!(
            output.status.code(),
            Some(2),
            "trailing --profile/--config rejected for {args:?}: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }
}

/// `auth login` used to be rejected by the argument parser when run bare, even
/// though the CLI's own errors ("Run `atlassian-cli auth login` first") and the
/// published guides both tell users to do exactly that. Missing values are now
/// prompted for, and without a terminal it stays a hard error rather than
/// hanging on stdin.
#[test]
fn test_auth_login_without_flags_is_not_a_parse_error() {
    let output = Sandbox::new()
        .cli()
        .args(["auth", "login"])
        .stdin(std::process::Stdio::null())
        .output()
        .expect("Failed to execute command");

    assert_ne!(
        output.status.code(),
        Some(2),
        "bare `auth login` should reach the command, not fail argument parsing"
    );
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("--profile"),
        "should name the flag to pass when it cannot prompt, got: {stderr}"
    );
    assert!(
        !output.status.success(),
        "it must still fail without a terminal rather than proceeding"
    );
}