use std::{fs::OpenOptions, path::PathBuf};
use pretty_assertions::assert_eq;
use rho_providers::credentials::{CredentialResult, FileCredentialStore};
use tempfile::TempDir;
use super::*;
use crate::app::mcp_server::tokens::{Rotation, TokenSet};
struct ContendedStore {
store: FileCredentialStore,
lock_path: PathBuf,
}
impl ContendedStore {
fn assert_transaction_locked(&self) {
let contender = OpenOptions::new()
.read(true)
.write(true)
.open(&self.lock_path)
.unwrap();
assert_eq!(
fs2::FileExt::try_lock_exclusive(&contender)
.err()
.and_then(|error| error.raw_os_error()),
fs2::lock_contended_error().raw_os_error(),
"another token writer must be excluded for the whole transaction"
);
}
}
impl CredentialStore for ContendedStore {
fn get_secret(&self, account: &str) -> CredentialResult<Option<String>> {
self.assert_transaction_locked();
self.store.get_secret(account)
}
fn set_secret(&self, account: &str, secret: &str) -> CredentialResult<()> {
self.assert_transaction_locked();
self.store.set_secret(account, secret)
}
fn delete_secret(&self, account: &str) -> CredentialResult<bool> {
self.store.delete_secret(account)
}
}
#[test]
fn token_updates_exclude_other_writers_from_reload_through_save() {
let root = TempDir::new().unwrap();
let lock_path = root.path().join("tokens.lock");
let store = ContendedStore {
store: FileCredentialStore::with_rho_home(root.path()).unwrap(),
lock_path,
};
drop(lock_account_file(&store.lock_path, GUARDED).unwrap());
let rotation = || Rotation {
access: None,
workspaces: vec![root.path().to_owned()],
};
let old = update_at(&store, &store.lock_path, |tokens: &mut TokenSet| {
tokens.rotate("compromised", rotation())
})
.unwrap();
update_at(&store, &store.lock_path, |tokens: &mut TokenSet| {
store.assert_transaction_locked();
assert!(tokens.revoke("compromised"));
Ok(())
})
.unwrap();
let new = update_at(&store, &store.lock_path, |tokens: &mut TokenSet| {
store.assert_transaction_locked();
assert!(tokens.authenticate(&old).is_none());
tokens.rotate("other", rotation())
})
.unwrap();
let saved = TokenSet::load(&store.store).unwrap();
assert_eq!(
(
saved.authenticate(&old).is_some(),
saved.authenticate(&new).map(|token| token.name.as_str()),
),
(false, Some("other"))
);
drop(lock_account_file(&store.lock_path, GUARDED).unwrap());
}