2023-05-22 23:57:08 +00:00
|
|
|
use axum::response::{IntoResponse, Redirect};
|
|
|
|
|
2023-05-29 21:25:50 +00:00
|
|
|
use crate::{templates::Index, AuthContext};
|
2023-05-29 00:55:16 +00:00
|
|
|
|
2023-05-22 23:57:08 +00:00
|
|
|
pub async fn handle_slash_redir() -> impl IntoResponse {
|
2023-05-31 22:58:03 +00:00
|
|
|
Redirect::to("/")
|
2023-05-22 23:57:08 +00:00
|
|
|
}
|
|
|
|
|
2023-05-29 00:55:16 +00:00
|
|
|
pub async fn handle_slash(auth: AuthContext) -> impl IntoResponse {
|
2023-05-29 21:25:50 +00:00
|
|
|
if let Some(ref user) = auth.current_user {
|
|
|
|
let name = &user.username;
|
|
|
|
tracing::debug!("Logged in as: {name}");
|
2023-05-29 00:55:16 +00:00
|
|
|
} else {
|
2023-05-29 21:25:50 +00:00
|
|
|
tracing::debug!("Not logged in.");
|
|
|
|
}
|
|
|
|
Index {
|
|
|
|
user: auth.current_user,
|
2023-05-29 00:55:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 22:58:03 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use axum_test::TestServer;
|
|
|
|
|
|
|
|
use crate::db;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn slash_is_ok() {
|
|
|
|
let pool = db::get_pool().await;
|
|
|
|
let secret = [0u8; 64];
|
|
|
|
let app = crate::app(pool.clone(), &secret).await.into_make_service();
|
|
|
|
|
|
|
|
let server = TestServer::new(app).unwrap();
|
|
|
|
|
|
|
|
server.get("/").await.assert_status_ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn not_found_is_303() {
|
|
|
|
let pool = db::get_pool().await;
|
|
|
|
let secret = [0u8; 64];
|
|
|
|
let app = crate::app(pool, &secret).await.into_make_service();
|
|
|
|
|
|
|
|
let server = TestServer::new(app).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
server
|
|
|
|
.get("/no-actual-route")
|
|
|
|
.expect_failure()
|
|
|
|
.await
|
|
|
|
.status_code(),
|
|
|
|
303
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|