what2watch/src/generic_handlers.rs

55 lines
1.2 KiB
Rust
Raw Normal View History

2023-05-22 23:57:08 +00:00
use axum::response::{IntoResponse, Redirect};
use crate::{AuthContext, 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 {
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 {
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 {
tracing::debug!("Not logged in.");
}
MainPage {
user: auth.current_user,
2023-05-29 00:55:16 +00:00
}
}
#[cfg(test)]
2023-06-02 21:15:13 +00:00
mod test {
use tokio::runtime::Runtime;
use crate::{get_db_pool, test_utils::server_with_pool};
#[test]
fn slash_is_ok() {
let db = get_db_pool();
let rt = Runtime::new().unwrap();
rt.block_on(async {
let server = server_with_pool(&db).await;
server.get("/").await
})
.assert_status_ok();
}
#[test]
fn not_found_is_303() {
let rt = Runtime::new().unwrap();
let db = get_db_pool();
assert_eq!(
rt.block_on(async {
let server = server_with_pool(&db).await;
server.get("/no-actual-route").expect_failure().await
})
.status_code(),
303
);
}
}