use super::*;
use tempfile::tempdir;
#[test]
fn accepts_existing_account_binding() {
let directory = tempdir().unwrap();
let root = directory.path();
SessionStore::remember_account_at(root, "test-session", 123).unwrap();
SessionStore::remember_account_at(root, "test-session", 123).unwrap();
assert_eq!(
SessionStore::known_account(root, "test-session").unwrap(),
Some(123),
);
}
#[test]
fn rejects_account_identity_changes() {
let directory = tempdir().unwrap();
let root = directory.path();
SessionStore::remember_account_at(root, "test-session", 123).unwrap();
assert!(matches!(
SessionStore::remember_account_at(root, "test-session", 456,),
Err(ClientError::AccountIdentityMismatch)
));
assert_eq!(
SessionStore::known_account(root, "test-session").unwrap(),
Some(123),
);
}
#[test]
fn accepts_rotated_session_for_same_account() {
let directory = tempdir().unwrap();
let root = directory.path();
SessionStore::remember_account_at(root, "old-session", 123).unwrap();
SessionStore::remember_account_at(root, "new-session", 123).unwrap();
for token in ["old-session", "new-session"] {
assert_eq!(SessionStore::known_account(root, token).unwrap(), Some(123),);
}
}
#[test]
fn rejects_zero_account_identity() {
let directory = tempdir().unwrap();
assert!(matches!(
SessionStore::remember_account_at(directory.path(), "test-session", 0,),
Err(ClientError::AccountIdentityUnavailable)
));
}
#[test]
fn rejects_corrupted_account_binding() {
let directory = tempdir().unwrap();
let root = directory.path();
let path = SessionStore::account_path(root, "test-session");
cache::store(&path, "invalid-account").unwrap();
assert!(matches!(
SessionStore::known_account(root, "test-session"),
Err(ClientError::AccountIdentityUnavailable)
));
assert!(matches!(
SessionStore::remember_account_at(root, "test-session", 123,),
Err(ClientError::AccountIdentityUnavailable)
));
assert_eq!(fs::read_to_string(path).unwrap(), "invalid-account",);
}
#[test]
fn rejects_invalid_session_binding() {
let directory = tempdir().unwrap();
assert!(matches!(
SessionStore::remember_account_at(directory.path(), "invalid;session", 123,),
Err(ClientError::InvalidSession)
));
}
#[test]
fn override_bypasses_credential_store() {
for source in [SessionSource::Environment, SessionSource::Dotenv] {
let resolved = resolve_with(Some(("test-session".to_owned(), source)), || {
panic!("Credential store must not be accessed")
})
.unwrap();
assert_eq!(resolved, Some(("test-session".to_owned(), source)),);
}
}
#[test]
fn resolves_stored_session_without_override() {
let resolved = resolve_with(None, || Ok(Some("stored-session".to_owned()))).unwrap();
assert_eq!(
resolved,
Some(("stored-session".to_owned(), SessionSource::CredentialStore,)),
);
}
#[test]
fn resolves_missing_session() {
let resolved = resolve_with(None, || Ok(None)).unwrap();
assert!(resolved.is_none());
}
#[test]
fn rejects_invalid_override_without_fallback() {
assert!(matches!(
resolve_with(
Some(("invalid;session".to_owned(), SessionSource::Environment,)),
|| panic!("Must not fall back to another account"),
),
Err(ClientError::InvalidSession)
));
}
#[test]
fn propagates_credential_store_failure() {
assert!(matches!(
resolve_with(None, || Err(ClientError::CredentialStore),),
Err(ClientError::CredentialStore)
));
}
#[test]
fn rejects_invalid_stored_session() {
assert!(matches!(
resolve_with(None, || Ok(Some("invalid session".to_owned())),),
Err(ClientError::InvalidSession)
));
}