#![allow(clippy::missing_docs_in_private_items)]
use axum::extract::{Query, State};
use axum::Json;
use super::{lock, unlock, LockRequest, UnlockQuery};
struct TempHome(std::path::PathBuf);
impl TempHome {
fn set() -> Self {
let dir =
std::env::temp_dir().join(format!("moadim-handlers-test-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).expect("create temp home");
unsafe {
std::env::set_var("MOADIM_HOME_OVERRIDE", &dir);
}
Self(dir)
}
}
impl Drop for TempHome {
fn drop(&mut self) {
unsafe {
std::env::remove_var("MOADIM_HOME_OVERRIDE");
}
let _ = std::fs::remove_dir_all(&self.0);
}
}
#[tokio::test]
async fn lock_handler_errors_when_set_lock_fails() {
use std::os::unix::fs::PermissionsExt as _;
let _home = TempHome::set();
let config_dir = crate::paths::config_dir();
std::fs::create_dir_all(&config_dir).unwrap();
let mut perms = std::fs::metadata(&config_dir).unwrap().permissions();
perms.set_mode(0o555);
std::fs::set_permissions(&config_dir, perms).unwrap();
let store = crate::routines::new_store();
let result = lock(
State(store),
Json(LockRequest {
scope: "shared".to_string(),
}),
)
.await;
let mut perms = std::fs::metadata(&config_dir).unwrap().permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&config_dir, perms).unwrap();
assert!(
matches!(result, Err(crate::error::AppError::Internal)),
"expected Internal error when lock write fails"
);
}
#[tokio::test]
async fn unlock_handler_errors_when_set_lock_fails() {
use std::os::unix::fs::PermissionsExt as _;
let _home = TempHome::set();
let config_dir = crate::paths::config_dir();
std::fs::create_dir_all(&config_dir).unwrap();
let lock_path = crate::paths::global_lock_path();
std::fs::write(&lock_path, b"").unwrap();
let mut perms = std::fs::metadata(&config_dir).unwrap().permissions();
perms.set_mode(0o555);
std::fs::set_permissions(&config_dir, perms).unwrap();
let store = crate::routines::new_store();
let result = unlock(
State(store),
Query(UnlockQuery {
scope: "shared".to_string(),
}),
)
.await;
let mut perms = std::fs::metadata(&config_dir).unwrap().permissions();
perms.set_mode(0o755);
std::fs::set_permissions(&config_dir, perms).unwrap();
assert!(
matches!(result, Err(crate::error::AppError::Internal)),
"expected Internal error when unlock fails"
);
}