#![allow(dead_code, unused_imports, unused_qualifications, unreachable_patterns)]
use anyhow::Result;
use super::secret_store::SecretStore;
use super::types::BindingId;
pub fn restore_previous_secret<S>(
secret_store: &S,
id: &BindingId,
previous_secret: Option<&str>,
) -> Result<()>
where
S: SecretStore,
{
match previous_secret {
Some(secret) => secret_store.set(id, secret)?,
None => {
let _ = secret_store.delete(id)?;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::internal::app_adapter::MemorySecretStore;
#[test]
fn restore_previous_secret_with_some() {
let store = MemorySecretStore::new();
let id = BindingId::new("test:default");
store.set(&id, "current-token").expect("set");
restore_previous_secret(&store, &id, Some("previous-token")).expect("restore");
assert_eq!(
store.get(&id).expect("get"),
Some("previous-token".to_string())
);
}
#[test]
fn restore_previous_secret_with_none() {
let store = MemorySecretStore::new();
let id = BindingId::new("test:default");
store.set(&id, "current-token").expect("set");
restore_previous_secret(&store, &id, None).expect("restore");
assert_eq!(store.get(&id).expect("get"), None);
}
#[test]
fn restore_previous_secret_with_none_when_no_existing_value() {
let store = MemorySecretStore::new();
let id = BindingId::new("test:nonexistent");
restore_previous_secret(&store, &id, None).expect("restore");
assert_eq!(store.get(&id).expect("get"), None);
}
#[test]
fn restore_previous_secret_overwrites_existing() {
let store = MemorySecretStore::new();
let id = BindingId::new("test:default");
store.set(&id, "first").expect("set");
restore_previous_secret(&store, &id, Some("second")).expect("restore");
assert_eq!(store.get(&id).expect("get"), Some("second".to_string()));
restore_previous_secret(&store, &id, Some("third")).expect("restore again");
assert_eq!(store.get(&id).expect("get"), Some("third".to_string()));
}
}