dogy_backend_api/middleware/
mod.rs1pub mod auth;
2pub mod log;
3
4#[cfg(test)]
5mod test {
6 use crate::config::load_config;
7 use crate::AppState;
8 use axum::Router;
9 use sqlx::postgres::PgPoolOptions;
10 use std::sync::Arc;
11 use testcontainers::ContainerAsync;
12 use testcontainers_modules::postgres::Postgres;
13 use testcontainers_modules::testcontainers::{runners::AsyncRunner, ImageExt};
14
15 #[cfg(test)]
16 pub fn setup_test_router() -> Router {
17 dotenv::from_filename(".env.test").unwrap();
18 let _ = load_config();
19
20 Router::new().route(
21 "/",
22 axum::routing::get(|| async { "Middleware test succeeded" }),
23 )
24 }
25
26 #[cfg(test)]
27 pub async fn setup_test_db() -> (AppState, ContainerAsync<Postgres>) {
28 let container_instance = Postgres::default()
29 .with_init_sql(
30 "CREATE ROLE web_backend_public WITH LOGIN PASSWORD 'testpassword';
31 CREATE DATABASE \"web-backend\";
32 "
33 .to_string()
34 .into_bytes(),
35 )
36 .with_name("sheape/postgis-uuidv7")
37 .with_tag("1.0.0")
38 .start()
39 .await
40 .unwrap();
41
42 let host_port = container_instance.get_host_port_ipv4(5432).await.unwrap();
43
44 let pool = PgPoolOptions::new()
45 .max_connections(1)
46 .connect(&format!(
47 "postgres://web_backend_public:testpassword@localhost:{}/web-backend",
48 host_port
49 ))
50 .await
51 .expect("Failed to create PgPool.");
52
53 let admin_pool = PgPoolOptions::new()
54 .max_connections(1)
55 .connect(&format!(
56 "postgres://postgres:postgres@localhost:{}/web-backend",
57 host_port
58 ))
59 .await
60 .expect("Failed to create PgPool.");
61
62 sqlx::migrate!("./migrations")
63 .run(&admin_pool)
64 .await
65 .unwrap();
66
67 (AppState { db: Arc::new(pool) }, container_instance)
68 }
69}