#![cfg(target_os = "linux")]
use std::sync::{Arc, Mutex};
use pretty_assertions::assert_eq;
use rho_protocol::{
methods::SessionNew, Client, ClientEnvironment, CredentialStoreKind, CredentialStoreStatus,
ErrorCode, JobState, SessionId, SessionNewParams, SessionSetup, StoreSelectedBy,
};
use rho_providers::credentials::CredentialStore;
use rho_tui_pty::{Key, PtyHarness, PtySize, RhoLaunchPlan, WaitTimeout};
use super::{
auth::{rpc_error, store_status, Fixture, FIRST_KEY, KEY_ACCOUNT},
sessions::DEADLINE,
};
const STORE_VARIABLE: &str = "RHO_CREDENTIAL_STORE";
fn headless_environment(fixture: &Fixture) -> ClientEnvironment {
fixture
.sandbox
.client_environment()
.into_iter()
.filter(|(name, _)| name != STORE_VARIABLE)
.chain([(
"SSH_CONNECTION".to_owned(),
"127.0.0.1 1 127.0.0.1 2".to_owned(),
)])
.collect()
}
async fn open_session(fixture: &Fixture, client: &Client) -> Result<SessionId, Option<ErrorCode>> {
client
.request::<SessionNew>(&SessionNewParams {
cwd: fixture.sandbox.workspace.clone(),
environment: headless_environment(fixture),
agent: None,
setup: SessionSetup::default(),
parent: None,
grant: None,
worktree: false,
})
.await
.map(|opened| opened.session)
.map_err(|error| rpc_error(error).error_code())
}
fn file_store_from_config() -> CredentialStoreStatus {
CredentialStoreStatus {
store: CredentialStoreKind::File,
selected_by: StoreSelectedBy::Config,
unreachable: None,
}
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn rho_login_from_a_headless_shell_signs_in_the_daemons_next_session() {
let valid = Arc::new(Mutex::new(FIRST_KEY.to_owned()));
let fixture = Fixture::without_daemon(&valid);
let plan = RhoLaunchPlan {
binary: env!("CARGO_BIN_EXE_rho").into(),
size: PtySize {
rows: 28,
cols: 200,
},
args: vec!["login".into(), "mock".into()],
env: headless_environment(&fixture).into_iter().collect(),
cwd: fixture.sandbox.workspace.clone(),
};
let mut login = PtyHarness::spawn_named(&plan, "rho_login_headless_session").unwrap();
let mut at_terminal = |step: &mut dyn FnMut(&mut PtyHarness)| {
tokio::task::block_in_place(|| step(&mut login));
};
at_terminal(&mut |login| {
login
.wait_for_text(
"input hidden",
WaitTimeout::secs(DEADLINE.as_secs(), "key prompt"),
)
.unwrap();
login.type_text(FIRST_KEY).unwrap();
login.inject_key(&Key::Enter).unwrap();
login
.wait_for_text(
"(default 2)",
WaitTimeout::secs(DEADLINE.as_secs(), "store prompt"),
)
.unwrap();
});
let client = fixture.sandbox.connect().await;
let before = (
open_session(&fixture, &client).await.err(),
fixture.saved_key(),
);
let mut exit = None;
at_terminal(&mut |login| {
login.inject_key(&Key::Enter).unwrap();
exit = Some(
login
.wait_for_exit(WaitTimeout::secs(DEADLINE.as_secs(), "rho login exits"))
.unwrap(),
);
});
let session = open_session(&fixture, &client).await;
let turn = match &session {
Ok(session) => Some(
fixture
.prompt_from(&client, session, headless_environment(&fixture))
.await,
),
Err(_) => None,
};
assert_eq!(
(
before,
exit,
store_status(&client).await,
fixture.saved_key(),
session.map(drop),
turn,
),
(
(Some(Some(ErrorCode::SignInRequired)), None),
Some(0),
file_store_from_config(),
Some(FIRST_KEY.into()),
Ok(()),
Some(Ok(JobState::Completed)),
),
"log:\n{}",
fixture.sandbox.isolated.daemon_log()
);
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_store_chosen_outside_the_daemon_is_the_one_its_next_session_reads() {
let valid = Arc::new(Mutex::new(FIRST_KEY.to_owned()));
let fixture = Fixture::start_with(&valid, |command| {
command.env_remove(STORE_VARIABLE);
})
.await;
fixture.store.set_secret(KEY_ACCOUNT, FIRST_KEY).unwrap();
let client = fixture.sandbox.connect().await;
let before = open_session(&fixture, &client).await.err();
let chosen = fixture
.sandbox
.rho("fixed")
.env_remove(STORE_VARIABLE)
.args(["credential-store", "set", "file"])
.status()
.unwrap();
let session = open_session(&fixture, &client)
.await
.expect("the daemon reads the file store");
assert_eq!(
(
before,
chosen.success(),
store_status(&client).await,
fixture
.prompt_from(&client, &session, headless_environment(&fixture))
.await,
),
(
Some(Some(ErrorCode::SignInRequired)),
true,
file_store_from_config(),
Ok(JobState::Completed),
),
"log:\n{}",
fixture.sandbox.isolated.daemon_log()
);
}