use crate::error::Result;
use crate::storage::KeyStorage;
use std::env;
use std::path::PathBuf;
use tempfile::TempDir;
pub struct TestStorage {
_temp_dir: TempDir,
pub storage_path: PathBuf,
}
impl TestStorage {
pub fn new() -> Result<Self> {
let temp_dir = tempfile::tempdir().map_err(|e| {
crate::error::Error::Storage(format!("Failed to create temp dir: {}", e))
})?;
env::set_var("TAP_HOME", temp_dir.path());
let storage_path = temp_dir.path().join("keys.json");
Ok(Self {
_temp_dir: temp_dir,
storage_path,
})
}
pub fn load(&self) -> Result<KeyStorage> {
KeyStorage::load_from_path(&self.storage_path)
}
pub fn save(&self, storage: &KeyStorage) -> Result<()> {
storage.save_to_path(&self.storage_path)
}
pub fn path(&self) -> &PathBuf {
&self.storage_path
}
pub fn directory(&self) -> PathBuf {
self._temp_dir.path().to_path_buf()
}
}
impl Default for TestStorage {
fn default() -> Self {
Self::new().expect("Failed to create test storage")
}
}
pub fn temp_storage_path() -> PathBuf {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
env::set_var("TAP_HOME", temp_dir.path());
let path = temp_dir.path().join("keys.json");
std::mem::forget(temp_dir);
path
}
pub fn temp_tap_directory() -> PathBuf {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let tap_dir = temp_dir.path().join(".tap");
std::fs::create_dir_all(&tap_dir).expect("Failed to create .tap directory");
env::set_var("TAP_HOME", &tap_dir);
std::mem::forget(temp_dir);
tap_dir
}
pub fn setup_test_environment() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
env::set_var("TAP_TEST_DIR", temp_dir.path());
std::mem::forget(temp_dir);
}
pub fn reset_test_environment() {
env::remove_var("TAP_TEST_DIR");
env::remove_var("TAP_HOME");
}
#[cfg(test)]
mod tests {
use super::*;
use crate::did::KeyType;
use crate::storage::{KeyStorage, StoredKey};
use serial_test::serial;
#[test]
#[serial]
fn test_storage_can_be_created() {
setup_test_environment();
let test_storage = TestStorage::new().unwrap();
assert!(test_storage.storage_path.ends_with("keys.json"));
reset_test_environment();
}
#[test]
#[serial]
fn test_storage_save_and_load() {
setup_test_environment();
let test_storage = TestStorage::new().unwrap();
let stored_key = StoredKey {
did: "did:test:example".to_string(),
label: "test-key".to_string(),
key_type: KeyType::Ed25519,
private_key: "test-private".to_string(),
public_key: "test-public".to_string(),
metadata: std::collections::HashMap::new(),
};
let mut storage = KeyStorage::new();
storage.add_key(stored_key);
test_storage.save(&storage).unwrap();
let loaded_storage = test_storage.load().unwrap();
assert!(loaded_storage.keys.contains_key("did:test:example"));
reset_test_environment();
}
#[test]
#[serial]
fn test_temp_storage_path() {
setup_test_environment();
let path = temp_storage_path();
assert!(path.ends_with("keys.json"));
reset_test_environment();
}
#[test]
#[serial]
fn test_temp_tap_directory() {
setup_test_environment();
let dir = temp_tap_directory();
assert!(dir.ends_with(".tap"));
assert!(dir.exists());
reset_test_environment();
}
}