use axum_test::{TestServer, TestServerConfig};
use lazy_static::lazy_static;
#[cfg(feature = "with-db")]
use sea_orm::DatabaseConnection;
use crate::{
app::{AppContext, Hooks},
boot::{self, BootResult},
environment::Environment,
Result,
};
lazy_static! {
pub static ref CLEANUP_USER_MODEL: Vec<(&'static str, &'static str)> = vec![
(
r"([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})",
"PID"
),
(r"password: (.*{60}),", "password: \"PASSWORD\","),
(r"([A-Za-z0-9-_]*\.[A-Za-z0-9-_]*\.[A-Za-z0-9-_]*)","TOKEN")
];
pub static ref CLEANUP_DATE: Vec<(&'static str, &'static str)> =
vec![
(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?\+\d{2}:\d{2}", "DATE"), (r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d+", "DATE"),
(r"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})", "DATE")
];
pub static ref CLEANUP_MODEL: Vec<(&'static str, &'static str)> = vec![(r"id: \d+,", "id: ID")];
pub static ref CLEANUP_MAIL: Vec<(&'static str, &'static str)> = vec![
(r"[0-9A-Za-z]+{40}", "IDENTIFIER"),
(r"\w+, \d{1,2} \w+ \d{4} \d{2}:\d{2}:\d{2} [+-]\d{4}", "DATE"),
(r"([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})","RANDOM_ID"),
(r"([0-9a-fA-F]{8}-[0-9a-fA-F]{4})-[0-9a-fA-F]{4}-.*[0-9a-fA-F]{2}", "RANDOM_ID")
];
}
#[must_use]
pub fn cleanup_user_model() -> Vec<(&'static str, &'static str)> {
let mut combined_filters = CLEANUP_USER_MODEL.to_vec();
combined_filters.extend(CLEANUP_DATE.iter().copied());
combined_filters.extend(CLEANUP_MODEL.iter().copied());
combined_filters
}
#[must_use]
pub fn cleanup_email() -> Vec<(&'static str, &'static str)> {
let mut combined_filters = CLEANUP_MAIL.to_vec();
combined_filters.extend(CLEANUP_DATE.iter().copied());
combined_filters
}
pub async fn boot_test<H: Hooks>() -> Result<BootResult> {
H::boot(boot::StartMode::ServerOnly, &Environment::Test).await
}
#[cfg(feature = "with-db")]
pub async fn seed<H: Hooks>(db: &DatabaseConnection) -> Result<()> {
let path = std::path::Path::new("src/fixtures");
H::seed(db, path).await
}
#[allow(clippy::future_not_send)]
#[allow(clippy::future_not_send)]
pub async fn request<H: Hooks, F, Fut>(callback: F)
where
F: FnOnce(TestServer, AppContext) -> Fut,
Fut: std::future::Future<Output = ()>,
{
let boot = boot_test::<H>().await.unwrap();
let config = TestServerConfig {
default_content_type: Some("application/json".to_string()),
..Default::default()
};
let server = TestServer::new_with_config(boot.router.unwrap(), config).unwrap();
callback(server, boot.app_context.clone()).await;
}