quorum-cli 0.2.1

Quorum CLI: the quorum binary.
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
//! Phase 1B Stage 4 — non-interactive auth integration tests.
//!
//! Drives the `quorum` binary against a `mockito` Lippa server so we
//! exercise the full env-var → login → keyring round-trip without
//! touching real Lippa. The keyring is the `--no-keyring` file
//! fallback; we redirect its base dir (`APPDATA` on Windows,
//! `XDG_CONFIG_HOME` on Linux) into a tempdir so the tests are
//! hermetic against the host's OS keychain.
//!
//! ACs covered: 88, 89, 90, 91, 92, 127, 129.

use std::fs;
use std::path::Path;

use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::TempDir;

fn quorum() -> Command {
    Command::cargo_bin("quorum").expect("quorum binary built")
}

fn init_repo() -> TempDir {
    let td = TempDir::new().unwrap();
    git2::Repository::init(td.path()).unwrap();
    td
}

/// Build a command rooted at `repo` with both the storage-base-dir
/// envs pointed into `cfg_dir` so the `--no-keyring` file fallback is
/// hermetic. Inherited `QUORUM_LIPPA_*` env vars are cleared so each
/// test runs from a known-empty base.
fn quorum_in(repo: &Path, cfg_dir: &Path) -> Command {
    let mut c = quorum();
    c.current_dir(repo)
        .env("APPDATA", cfg_dir) // Windows config dir
        .env("XDG_CONFIG_HOME", cfg_dir) // Linux config dir
        .env_remove("QUORUM_LIPPA_SESSION")
        .env_remove("QUORUM_LIPPA_EMAIL")
        .env_remove("QUORUM_LIPPA_PASSWORD");
    c
}

#[test]
fn login_non_interactive_missing_email_exits_2() {
    // AC 89: missing required env var → exit 2 with named var.
    let td = init_repo();
    let cfg = TempDir::new().unwrap();
    let url = "http://127.0.0.1:1".to_string(); // unreachable; never contacted
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "login",
            "--non-interactive",
            "--no-keyring",
            "--url",
        ])
        .arg(&url)
        .env("QUORUM_LIPPA_PASSWORD", "ignored")
        // QUORUM_LIPPA_EMAIL deliberately unset
        .assert()
        .failure()
        .code(predicate::eq(2))
        .stderr(predicate::str::contains(
            "missing required env var: QUORUM_LIPPA_EMAIL",
        ));
}

#[test]
fn login_non_interactive_missing_password_exits_2() {
    // AC 89.
    let td = init_repo();
    let cfg = TempDir::new().unwrap();
    let url = "http://127.0.0.1:1".to_string();
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "login",
            "--non-interactive",
            "--no-keyring",
            "--url",
        ])
        .arg(&url)
        .env("QUORUM_LIPPA_EMAIL", "x@example.com")
        // QUORUM_LIPPA_PASSWORD deliberately unset
        .assert()
        .failure()
        .code(predicate::eq(2))
        .stderr(predicate::str::contains(
            "missing required env var: QUORUM_LIPPA_PASSWORD",
        ));
}

#[test]
fn login_non_interactive_empty_password_treated_as_missing() {
    // Defensive: empty-string env var still counts as missing.
    let td = init_repo();
    let cfg = TempDir::new().unwrap();
    let url = "http://127.0.0.1:1".to_string();
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "login",
            "--non-interactive",
            "--no-keyring",
            "--url",
        ])
        .arg(&url)
        .env("QUORUM_LIPPA_EMAIL", "x@example.com")
        .env("QUORUM_LIPPA_PASSWORD", "")
        .assert()
        .failure()
        .code(predicate::eq(2));
}

#[test]
fn login_non_interactive_happy_path_writes_session_file() {
    // AC 88 + AC 90: env-var capture → login → cookie persisted.
    // Also implicitly verifies AC 90 (TTY check skipped) since this
    // test process is non-TTY and the call must succeed regardless.
    let mut server = mockito::Server::new();
    let mock = server
        .mock("POST", "/api/v1/auth/login")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_header(
            "set-cookie",
            "session=preflight-token-xyz; Path=/; HttpOnly",
        )
        .with_body(r#"{"ok":true}"#)
        .create();

    let td = init_repo();
    let cfg = TempDir::new().unwrap();
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "login",
            "--non-interactive",
            "--no-keyring",
            "--url",
        ])
        .arg(server.url())
        .env("QUORUM_LIPPA_EMAIL", "alice@example.com")
        .env("QUORUM_LIPPA_PASSWORD", "hunter2-very-secret")
        .assert()
        .success();
    mock.assert();

    // Session file present at <cfg>/quorum/sessions/<host>.session.
    let host = url::Url::parse(&server.url())
        .unwrap()
        .host_str()
        .unwrap()
        .to_string();
    let port = url::Url::parse(&server.url()).unwrap().port().unwrap();
    let file_root = cfg.path().join("quorum").join("sessions");
    let files: Vec<_> = fs::read_dir(&file_root).unwrap().collect();
    assert!(
        !files.is_empty(),
        "session file written under {:?}",
        file_root
    );
    let body = fs::read_to_string(files[0].as_ref().unwrap().path()).unwrap();
    assert!(
        body.contains("preflight-token-xyz"),
        "session file should hold the cookie value; got {body:?}"
    );
    // The password value must NOT appear in any artifact.
    assert!(!body.contains("hunter2"));
    let _ = host;
    let _ = port;
}

#[test]
fn show_session_non_tty_without_y_exits_2() {
    // AC 92.
    let td = init_repo();
    let cfg = TempDir::new().unwrap();
    // Need a stored session first; spin a mock login.
    let mut server = mockito::Server::new();
    server
        .mock("POST", "/api/v1/auth/login")
        .with_status(200)
        .with_header("set-cookie", "session=visible-cookie-value; Path=/")
        .with_body("{}")
        .create();
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "login",
            "--non-interactive",
            "--no-keyring",
            "--url",
        ])
        .arg(server.url())
        .env("QUORUM_LIPPA_EMAIL", "x@example.com")
        .env("QUORUM_LIPPA_PASSWORD", "p")
        .assert()
        .success();

    // Now try --show-session without -y; non-TTY context (test).
    quorum_in(td.path(), cfg.path())
        .args(["auth", "status", "--show-session", "--no-keyring", "--url"])
        .arg(server.url())
        .assert()
        .failure()
        .code(predicate::eq(2))
        .stderr(predicate::str::contains("non-TTY"));
}

#[test]
fn show_session_with_y_prints_cookie_value_to_stdout() {
    // AC 92.
    let td = init_repo();
    let cfg = TempDir::new().unwrap();
    let mut server = mockito::Server::new();
    server
        .mock("POST", "/api/v1/auth/login")
        .with_status(200)
        .with_header("set-cookie", "session=visible-cookie-value-123; Path=/")
        .with_body("{}")
        .create();
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "login",
            "--non-interactive",
            "--no-keyring",
            "--url",
        ])
        .arg(server.url())
        .env("QUORUM_LIPPA_EMAIL", "x@example.com")
        .env("QUORUM_LIPPA_PASSWORD", "p")
        .assert()
        .success();

    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "status",
            "--show-session",
            "-y",
            "--no-keyring",
            "--url",
        ])
        .arg(server.url())
        .assert()
        .success()
        .stdout(predicate::str::contains("visible-cookie-value-123"));
}

#[test]
fn show_session_no_auth_stored_exits_2() {
    let td = init_repo();
    let cfg = TempDir::new().unwrap();
    let url = "http://127.0.0.1:1".to_string();
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "status",
            "--show-session",
            "-y",
            "--no-keyring",
            "--url",
        ])
        .arg(&url)
        .assert()
        .failure()
        .code(predicate::eq(2))
        .stderr(predicate::str::contains("no session stored"));
}

#[test]
fn session_env_var_precedence_note_suppressed_under_hook_mode() {
    // AC 91 + P31: when BOTH the env var AND a keyring entry exist,
    // the env var wins. The precedence note appears only in
    // interactive review; suppressed under --hook-mode=*.
    let td = init_repo();
    let cfg = TempDir::new().unwrap();

    // Spin up mockito so config has a valid URL.
    let mut server = mockito::Server::new();
    // Login endpoint to populate the keyring entry.
    server
        .mock("POST", "/api/v1/auth/login")
        .with_status(200)
        .with_header("set-cookie", "session=keyring-cookie; Path=/")
        .with_body("{}")
        .create();
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "login",
            "--non-interactive",
            "--no-keyring",
            "--url",
        ])
        .arg(server.url())
        .env("QUORUM_LIPPA_EMAIL", "x@example.com")
        .env("QUORUM_LIPPA_PASSWORD", "p")
        .assert()
        .success();

    // Link the repo to a project so review can proceed to the auth
    // step (otherwise NotLinked fires before SESSION resolution).
    quorum_in(td.path(), cfg.path())
        .args(["link", "--project", "p_x", "--url"])
        .arg(server.url())
        .assert()
        .success();

    // Mock /sessions so review tries to fire — we expect failure
    // afterward (it's fine; we just want to assert on stderr shape
    // BEFORE the network failure point).
    server
        .mock("POST", "/api/v1/consensus/sessions")
        .with_status(401)
        .create();

    // Under --hook-mode=pre-commit: no precedence note, even though
    // both auth sources are present.
    quorum_in(td.path(), cfg.path())
        .args(["review", "--hook-mode=pre-commit", "--no-keyring"])
        .env("QUORUM_LIPPA_SESSION", "env-cookie-value")
        // Stage staged content so the bundle actually has something
        // to review (otherwise it short-circuits with "no staged
        // changes — nothing to review"). Easy: skip; the auth
        // resolution still runs and we can grep stderr for the note.
        .assert()
        // Exit may be anything; we only assert stderr shape:
        .stderr(predicate::str::contains("env var takes precedence").not());
}

#[test]
fn session_env_var_precedence_note_emitted_in_interactive_review() {
    // AC 91 / P31: interactive review surfaces the note.
    let td = init_repo();
    let cfg = TempDir::new().unwrap();

    let mut server = mockito::Server::new();
    server
        .mock("POST", "/api/v1/auth/login")
        .with_status(200)
        .with_header("set-cookie", "session=keyring-cookie; Path=/")
        .with_body("{}")
        .create();
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "login",
            "--non-interactive",
            "--no-keyring",
            "--url",
        ])
        .arg(server.url())
        .env("QUORUM_LIPPA_EMAIL", "x@example.com")
        .env("QUORUM_LIPPA_PASSWORD", "p")
        .assert()
        .success();
    quorum_in(td.path(), cfg.path())
        .args(["link", "--project", "p_x", "--url"])
        .arg(server.url())
        .assert()
        .success();
    server
        .mock("POST", "/api/v1/consensus/sessions")
        .with_status(401)
        .create();

    quorum_in(td.path(), cfg.path())
        .args(["review", "--no-keyring"])
        .env("QUORUM_LIPPA_SESSION", "env-cookie-value")
        .assert()
        .stderr(predicate::str::contains("env var takes precedence"));
}

#[test]
fn tracing_at_trace_level_does_not_leak_session_env() {
    // AC 127: env var values never appear in `tracing` output at any
    // RUST_LOG level. We invoke `quorum auth status --show-session
    // -y` (which prints the cookie to stdout intentionally) under
    // `RUST_LOG=trace` and assert the SECRET literal never appears
    // in stderr (where tracing writes by default).
    let td = init_repo();
    let cfg = TempDir::new().unwrap();
    let mut server = mockito::Server::new();
    server
        .mock("POST", "/api/v1/auth/login")
        .with_status(200)
        .with_header("set-cookie", "session=TRACE-LEAK-CANARY-XYZ; Path=/")
        .with_body("{}")
        .create();
    quorum_in(td.path(), cfg.path())
        .args([
            "auth",
            "login",
            "--non-interactive",
            "--no-keyring",
            "--url",
        ])
        .arg(server.url())
        .env("QUORUM_LIPPA_EMAIL", "x@example.com")
        .env("QUORUM_LIPPA_PASSWORD", "PASSWORD-LEAK-CANARY-XYZ")
        .env("RUST_LOG", "trace")
        .assert()
        .success()
        // The canaries must not appear in stderr (tracing target).
        .stderr(predicate::str::contains("TRACE-LEAK-CANARY-XYZ").not())
        .stderr(predicate::str::contains("PASSWORD-LEAK-CANARY-XYZ").not());
}