summaryrefslogtreecommitdiffstats
path: root/src/lib.rs
blob: 2afed5ef90262c436319ec8807afb2f219bcb745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use axum::{http::Uri, response::IntoResponse};
use tower_http::{cors::CorsLayer, trace::TraceLayer};

pub use error::{Error, Result};

pub mod api;
pub mod auth;
pub mod error;
pub mod state;
pub mod utils;

pub fn router() -> axum::Router<state::AppState> {
    axum::Router::new()
        .nest("/api", api::router())
        .merge(auth::router())
        .fallback(fallback)
        // TODO: do this correctly!
        .layer(CorsLayer::permissive())
        .layer(TraceLayer::new_for_http())
}

pub async fn fallback(uri: Uri) -> impl IntoResponse {
    Error::RouteNotFound(uri)
}

#[cfg(test)]
pub(crate) mod tests {
    use crate::state::AppState;

    use super::*;

    use std::sync::Once;

    use axum::{
        body::Body,
        http::{Request, StatusCode},
        Router,
    };
    use sqlx::PgPool;
    use tower::ServiceExt;
    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

    pub type TestResult<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;

    pub const JWT_SECRET: &str = "test-jwt-secret-token";

    static INIT: Once = Once::new();

    pub fn setup_test_env() {
        INIT.call_once(|| {
            tracing_subscriber::registry()
                .with(tracing_subscriber::EnvFilter::from_default_env())
                .with(tracing_subscriber::fmt::layer().with_test_writer())
                .init();

            std::env::set_var("JWT_SECRET", JWT_SECRET);
        });
    }

    #[sqlx::test]
    async fn test_fallback_not_found(pool: PgPool) -> TestResult {
        setup_test_env();

        let router = Router::new().merge(router()).with_state(AppState { pool });

        let request = Request::builder()
            .uri("/does-not-exist")
            .body(Body::empty())?;

        let response = router.oneshot(request).await?;

        assert_eq!(StatusCode::NOT_FOUND, response.status());

        Ok(())
    }
}