Files
imeai-aKode/akode-rs/login/src/auth/provider_api_key_tests.rs

144 lines
4.8 KiB
Rust

use std::sync::Arc;
use akode_config::types::AuthCredentialsStoreMode;
use akode_keyring_store::tests::MockKeyringStore;
use akode_model_provider_info::ProviderApiKeyLocation;
use pretty_assertions::assert_eq;
use tempfile::TempDir;
use super::*;
const SECRET: &str = "sk-provider-secret-0123456789";
const ACCOUNT: &str = "model-provider:imeai";
fn failing_keyring() -> MockKeyringStore {
let keyring = MockKeyringStore::default();
keyring.set_error(
ACCOUNT,
keyring::Error::NoStorageAccess(std::io::Error::other("no secret service").into()),
);
keyring
}
#[test]
fn auto_mode_prefers_the_keyring() {
let home = TempDir::new().unwrap();
let keyring = MockKeyringStore::default();
let store = configured_provider_api_key_store_with_keyring(
home.path(),
AuthCredentialsStoreMode::Auto,
Arc::new(keyring.clone()),
);
let location = store.save("imeai", SECRET).unwrap();
assert!(matches!(location, ProviderApiKeyLocation::Keyring { .. }));
assert_eq!(keyring.saved_value(ACCOUNT).as_deref(), Some(SECRET));
assert!(!home.path().join(PROVIDER_API_KEYS_FILE).exists());
assert_eq!(store.load("imeai").unwrap().as_deref(), Some(SECRET));
}
#[test]
fn auto_mode_falls_back_to_a_private_file_without_a_keyring() {
let home = TempDir::new().unwrap();
let store = configured_provider_api_key_store_with_keyring(
home.path(),
AuthCredentialsStoreMode::Auto,
Arc::new(failing_keyring()),
);
let location = store.save("imeai", SECRET).unwrap();
let path = home.path().join(PROVIDER_API_KEYS_FILE);
assert_eq!(
location,
ProviderApiKeyLocation::File {
path: path.display().to_string()
}
);
assert_eq!(store.load("imeai").unwrap().as_deref(), Some(SECRET));
assert_eq!(store.locate("imeai").unwrap(), Some(location.clone()));
// The displayed location never includes the key.
assert!(!location.to_string().contains(SECRET));
assert!(store.delete("imeai").unwrap());
assert_eq!(store.load("imeai").unwrap(), None);
assert!(!path.exists());
}
#[cfg(unix)]
#[test]
fn file_store_is_readable_only_by_the_owner() {
use std::os::unix::fs::PermissionsExt;
let home = TempDir::new().unwrap();
let store = ProviderApiKeyFileStore::new(home.path());
store.save("imeai", SECRET).unwrap();
store.save("other", "second-secret").unwrap();
let mode = std::fs::metadata(store.path())
.unwrap()
.permissions()
.mode()
& 0o777;
assert_eq!(mode, 0o600);
assert_eq!(
store.load("other").unwrap().as_deref(),
Some("second-secret")
);
assert_eq!(store.load("imeai").unwrap().as_deref(), Some(SECRET));
}
#[test]
fn keyring_mode_never_writes_a_file() {
let home = TempDir::new().unwrap();
let store = configured_provider_api_key_store_with_keyring(
home.path(),
AuthCredentialsStoreMode::Keyring,
Arc::new(failing_keyring()),
);
let error = store.save("imeai", SECRET).unwrap_err();
assert!(!error.contains(SECRET), "{error}");
assert!(!home.path().join(PROVIDER_API_KEYS_FILE).exists());
}
#[test]
fn file_mode_ignores_the_keyring() {
let home = TempDir::new().unwrap();
let keyring = MockKeyringStore::default();
let store = configured_provider_api_key_store_with_keyring(
home.path(),
AuthCredentialsStoreMode::File,
Arc::new(keyring.clone()),
);
store.save("imeai", SECRET).unwrap();
assert!(!keyring.contains(ACCOUNT));
assert!(home.path().join(PROVIDER_API_KEYS_FILE).exists());
}
#[test]
fn corrupt_file_errors_and_debug_never_contain_keys() {
let home = TempDir::new().unwrap();
let path = home.path().join(PROVIDER_API_KEYS_FILE);
std::fs::write(&path, format!("{{\"imeai\": \"{SECRET}\", broken")).unwrap();
let store = ProviderApiKeyFileStore::new(home.path());
let error = store.load("imeai").unwrap_err();
assert!(!error.contains(SECRET), "{error}");
let auto = configured_provider_api_key_store_with_keyring(
home.path(),
AuthCredentialsStoreMode::Auto,
Arc::new(MockKeyringStore::default()),
);
assert!(!format!("{auto:?}").contains(SECRET));
}
#[test]
fn ephemeral_mode_keeps_keys_in_memory() {
let home = TempDir::new().unwrap();
let store = configured_provider_api_key_store_with_keyring(
home.path(),
AuthCredentialsStoreMode::Ephemeral,
Arc::new(MockKeyringStore::default()),
);
assert_eq!(
store.save("imeai", SECRET).unwrap(),
ProviderApiKeyLocation::Memory
);
assert_eq!(store.load("imeai").unwrap().as_deref(), Some(SECRET));
assert!(!home.path().join(PROVIDER_API_KEYS_FILE).exists());
assert!(!format!("{store:?}").contains(SECRET));
}