2023-05-22 23:57:08 +00:00
|
|
|
use axum::response::{IntoResponse, Redirect};
|
|
|
|
|
2023-12-18 00:41:04 +00:00
|
|
|
use crate::{AuthSession, MainPage};
|
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-12-18 00:41:04 +00:00
|
|
|
pub async fn handle_slash(auth: AuthSession) -> impl IntoResponse {
|
|
|
|
if let Some(ref user) = auth.user {
|
2023-05-29 21:25:50 +00:00
|
|
|
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.");
|
|
|
|
}
|
2023-12-18 00:41:04 +00:00
|
|
|
MainPage { user: auth.user }
|
2023-05-29 00:55:16 +00:00
|
|
|
}
|
2023-05-31 22:58:03 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2023-06-02 21:15:13 +00:00
|
|
|
mod test {
|
2023-07-19 00:37:24 +00:00
|
|
|
|
|
|
|
use tokio::runtime::Runtime;
|
2023-05-31 22:58:03 +00:00
|
|
|
|
2023-07-21 22:15:47 +00:00
|
|
|
use crate::{get_db_pool, test_utils::server_with_pool};
|
2023-05-31 22:58:03 +00:00
|
|
|
|
2023-07-19 00:37:24 +00:00
|
|
|
#[test]
|
|
|
|
fn slash_is_ok() {
|
2023-07-21 22:15:47 +00:00
|
|
|
let db = get_db_pool();
|
2023-05-31 22:58:03 +00:00
|
|
|
|
2023-07-21 22:15:47 +00:00
|
|
|
let rt = Runtime::new().unwrap();
|
|
|
|
rt.block_on(async {
|
|
|
|
let server = server_with_pool(&db).await;
|
|
|
|
server.get("/").await
|
|
|
|
})
|
|
|
|
.assert_status_ok();
|
2023-05-31 22:58:03 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 00:37:24 +00:00
|
|
|
#[test]
|
|
|
|
fn not_found_is_303() {
|
|
|
|
let rt = Runtime::new().unwrap();
|
2023-05-31 22:58:03 +00:00
|
|
|
|
2023-07-21 22:15:47 +00:00
|
|
|
let db = get_db_pool();
|
2023-05-31 22:58:03 +00:00
|
|
|
assert_eq!(
|
2023-07-21 22:15:47 +00:00
|
|
|
rt.block_on(async {
|
|
|
|
let server = server_with_pool(&db).await;
|
|
|
|
server.get("/no-actual-route").expect_failure().await
|
|
|
|
})
|
2023-07-19 00:37:24 +00:00
|
|
|
.status_code(),
|
2023-05-31 22:58:03 +00:00
|
|
|
303
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|