Lines
100 %
Functions
Branches
use axum::{extract::State, http::StatusCode, response::IntoResponse};
use axum_extra::extract::CookieJar;
use redis::Client;
use std::sync::Arc;
use tokio::sync::Mutex;
use uuid::Uuid;
use web::{AppState, Config, User, pages};
fn create_test_app_state() -> Arc<AppState> {
let conf = Config {
site_url: "http://localhost:8080".to_string(),
redis_url: "redis://127.0.0.1:6379".to_string(),
client_origin: "http://localhost:3000".to_string(),
access_token_private_key: "test_private_key".to_string(),
access_token_public_key: "test_public_key".to_string(),
refresh_token_private_key: "test_refresh_private".to_string(),
refresh_token_public_key: "test_refresh_public".to_string(),
access_token_expires_in: "15m".to_string(),
access_token_max_age: 900,
refresh_token_expires_in: "60m".to_string(),
refresh_token_max_age: 3600,
};
let redis_client = Client::open("redis://127.0.0.1:6379").unwrap();
Arc::new(AppState {
conf,
redis_client,
frac: 42,
user: Mutex::new(Some(User {
id: Uuid::new_v4(),
name: "Test User".to_string(),
email: "test@example.com".to_string(),
password: "hashed".to_string(),
role: "user".to_string(),
photo: "".to_string(),
verified: true,
database: "testdb".to_string(),
created_at: Some(chrono::Utc::now()),
updated_at: Some(chrono::Utc::now()),
})),
})
}
#[tokio::test]
async fn test_index_handler_logged_out() {
let app_state = create_test_app_state();
let cookie_jar = CookieJar::new();
let response = pages::index(State(app_state), cookie_jar).await;
let response = response.into_response();
assert_eq!(response.status(), StatusCode::OK);
async fn test_index_handler_returns_correct_fraction() {
// Verify the fraction value is passed correctly
assert_eq!(app_state.frac, 42);
let _response = pages::index(State(app_state), cookie_jar).await;
// Template should receive fraction: 42
async fn test_index_handler_user_data() {
// Verify user data is accessible
let user_guard = app_state.user.lock().await;
assert!(user_guard.is_some());
assert_eq!(user_guard.as_ref().unwrap().name, "Test User");
drop(user_guard);
async fn test_file_table_handler() {
// This will likely fail due to S3 dependencies, but we can test the structure
let result = pages::file_table(State(app_state)).await;
// Should return either Ok or Err, both are valid outcomes for this test
match result {
Ok(response) => {
Err(status) => {
// Expected to fail in test environment due to S3 dependencies
assert!(matches!(
status,
StatusCode::INTERNAL_SERVER_ERROR | StatusCode::SERVICE_UNAVAILABLE
));
async fn test_register_handler() {
let response = pages::register().await;
async fn test_login_handler() {
let response = pages::login().await;
async fn test_app_state_fraction_value() {
// Verify the test value in AppState
async fn test_app_state_user_value() {
let user = user_guard.as_ref().unwrap();
assert_eq!(user.name, "Test User");
assert_eq!(user.email, "test@example.com");
assert_eq!(user.database, "testdb");
assert!(user.verified);