mod support;
use support::{FakeGithub, fixture_token, run, runner_manager_against};
fn document(access: &str) -> String {
format!(
r#"{{"access_token":"{access}","refresh_token":"{}",
"access_expires_at":"2026-09-06T20:00:00Z",
"refresh_expires_at":"2027-03-05T12:00:00Z"}}"#,
format_args!("{}{}", "ghr_", "adoptRefreshCanary000000000000")
)
}
const INVALID_ARGUMENT: i32 = 9;
const NOT_AUTHENTICATED: i32 = 3;
#[test]
fn an_adopted_credential_is_one_auth_status_reports_as_authenticated() {
let data_dir = tempfile::tempdir().expect("a temporary directory");
let github = FakeGithub::start();
github.with_installation(11, "acme", "Organization", "selected", &["acme/repo"]);
let adopted = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command
.args(["daemon", "adopt-credential", "--start-at", "boot"])
.write_stdin(document(&fixture_token()));
command
});
assert_eq!(
adopted.code,
0,
"the handover must store:\n{}",
adopted.both()
);
assert!(
adopted.stdout.contains("machine-scoped store"),
"the report names the store, and nothing about the value: {}",
adopted.stdout
);
let status = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command.args(["auth", "status"]);
command
});
assert_eq!(status.code, 0, "signed in afterwards:\n{}", status.both());
}
#[test]
fn adopting_records_no_start_mode() {
let data_dir = tempfile::tempdir().expect("a temporary directory");
let github = FakeGithub::start();
let adopted = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command
.args(["daemon", "adopt-credential", "--start-at", "login"])
.write_stdin(document(&fixture_token()));
command
});
assert_eq!(adopted.code, 0, "{}", adopted.both());
let status = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command.args(["auth", "status"]);
command
});
assert_eq!(
status.code,
NOT_AUTHENTICATED,
"the recorded start mode moved:\n{}",
status.both()
);
}
#[test]
fn a_refused_document_leaves_nothing_stored() {
let data_dir = tempfile::tempdir().expect("a temporary directory");
let github = FakeGithub::start();
for input in [String::new(), "not a credential document".to_string()] {
let adopted = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command
.args(["daemon", "adopt-credential", "--start-at", "boot"])
.write_stdin(input);
command
});
assert_eq!(adopted.code, INVALID_ARGUMENT, "{}", adopted.both());
}
let status = run({
let mut command = runner_manager_against(data_dir.path(), &github);
command.args(["auth", "status"]);
command
});
assert_eq!(status.code, NOT_AUTHENTICATED, "{}", status.both());
}