test loading AppSession

This commit is contained in:
jfmonty2 committed 2026-11-18 10:14:36 -05:00
1 parent 0798124811
commit 5999aba0a5
2 files changed
+44 -14

No files matched your search

+19 -14
View File
@@ -18,10 +18,6 @@ impl AppSession {
AppSession::Empty AppSession::Empty
} }
} }
pub fn unlock(&mut self, passphrase: &str) -> Result<AppSession, CreddyError> {
todo!()
}
} }
impl Global for AppSession {} impl Global for AppSession {}
@@ -29,15 +25,24 @@ impl Global for AppSession {}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::assert_matches;
use testresult::TestResult;
use crate::test_data::TestEnv;
// #[test] #[test]
// fn test_new() { fn test_load_empty() -> TestResult {
// let passphrase = "correct horse battery staple"; let env = TestEnv::new()?;
// let salt = AppSession::salt().expect("salt generation failed."); let session = AppSession::load(&env.config);
// let session = AppSession::new(passphrase, &salt).expect("password KDF failed."); assert_matches!(session, AppSession::Empty);
// match session { Ok(())
// AppSession::Unlocked { .. } => (), }
// _ => panic!("AppSession::new() should return an unlocked session"),
// } #[test]
// } fn test_load_locked() -> TestResult {
let env = TestEnv::new()?;
let _ = env.db()?; // creates the database in the temp dir
let session = AppSession::load(&env.config);
assert_matches!(session, AppSession::Locked);
Ok(())
}
} }
+25
View File
@@ -0,0 +1,25 @@
use tempfile::TempDir;
use crate::config::AppConfig;
use crate::db::Db;
use crate::error::CreddyError;
pub const TEST_PASSPHRASE: &'static str = "correct horse battery staple";
pub struct TestEnv {
// we have to keep this around or the tempdir will be deleted as soon as it's dropped
_dir: TempDir,
pub config: AppConfig,
}
impl TestEnv {
pub fn new() -> Result<TestEnv, CreddyError> {
let dir = tempfile::tempdir()?;
let config = AppConfig::load_from(dir.path().to_path_buf())?;
Ok(TestEnv { _dir: dir, config })
}
pub fn db(&self) -> Result<Db, CreddyError> {
Db::connect(&self.config, TEST_PASSPHRASE)
}
}