what2watch/src/generic_handlers.rs

68 lines
1.6 KiB
Rust

use axum::response::{IntoResponse, Redirect};
use crate::{AuthContext, MainPage};
pub async fn handle_slash_redir() -> impl IntoResponse {
Redirect::to("/")
}
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}");
} else {
tracing::debug!("Not logged in.");
}
MainPage {
user: auth.current_user,
}
}
#[cfg(test)]
mod test {
use std::future::IntoFuture;
use axum_test::TestServer;
use tokio::runtime::Runtime;
use crate::db;
#[test]
fn slash_is_ok() {
let pool = db::get_db_pool();
let secret = [0u8; 64];
let rt = Runtime::new().unwrap();
let app = rt
.block_on(crate::app(pool.clone(), &secret))
.into_make_service();
let server = TestServer::new(app).unwrap();
rt.block_on(server.get("/").into_future())
.assert_status_ok();
}
#[test]
fn not_found_is_303() {
let pool = db::get_db_pool();
let secret = [0u8; 64];
let rt = Runtime::new().unwrap();
let app = rt
.block_on(crate::app(pool.clone(), &secret))
.into_make_service();
let server = TestServer::new(app).unwrap();
assert_eq!(
rt.block_on(
server
.get("/no-actual-route")
.expect_failure()
.into_future()
)
.status_code(),
303
);
}
}