1
use axum::{http::StatusCode, response::IntoResponse};
2
use axum_extra::extract::CookieJar;
3
use redis::Client;
4
use std::sync::Arc;
5
use tokio::sync::Mutex;
6
use uuid::Uuid;
7
use web::{AppState, Config, User, pages};
8

            
9
3
fn create_test_app_state() -> Arc<AppState> {
10
3
    let conf = Config {
11
3
        site_url: "http://localhost:8080".to_string(),
12
3
        redis_url: "redis://127.0.0.1:6379".to_string(),
13
3
        access_token_max_age: 900,
14
3
        refresh_token_max_age: 3600,
15
3
        ssh: web::config::SshConnectInfo {
16
3
            hostname: "ssh.example.invalid".to_string(),
17
3
            port: 2222,
18
3
            host_fingerprint: "(test)".to_string(),
19
3
        },
20
3
    };
21

            
22
3
    let redis_client = Client::open("redis://127.0.0.1:6379").unwrap();
23

            
24
3
    Arc::new(AppState {
25
3
        conf,
26
3
        redis_client,
27
3
        user: Mutex::new(Some(User {
28
3
            id: Uuid::new_v4(),
29
3
            name: "Test User".to_string(),
30
3
            email: "test@example.com".to_string(),
31
3
            role: "user".to_string(),
32
3
            photo: String::new(),
33
3
            verified: true,
34
3
            created_at: Some(chrono::Utc::now()),
35
3
            updated_at: Some(chrono::Utc::now()),
36
3
        })),
37
3
    })
38
3
}
39

            
40
#[supp_macro::shared_runtime_test]
41
1
async fn test_index_handler_logged_out() {
42
1
    let _app_state = create_test_app_state();
43
1
    let cookie_jar = CookieJar::new();
44

            
45
1
    let response = pages::index(cookie_jar).await;
46
1
    let response = response.into_response();
47

            
48
1
    assert_eq!(response.status(), StatusCode::OK);
49
}
50

            
51
#[supp_macro::shared_runtime_test]
52
1
async fn test_index_handler_renders() {
53
1
    let cookie_jar = CookieJar::new();
54

            
55
1
    let response = pages::index(cookie_jar).await.into_response();
56
1
    assert_eq!(response.status(), StatusCode::OK);
57
}
58

            
59
#[supp_macro::shared_runtime_test]
60
1
async fn test_index_handler_user_data() {
61
1
    let app_state = create_test_app_state();
62
1
    let cookie_jar = CookieJar::new();
63

            
64
    // Verify user data is accessible
65
1
    let user_guard = app_state.user.lock().await;
66
1
    assert!(user_guard.is_some());
67
1
    assert_eq!(user_guard.as_ref().unwrap().name, "Test User");
68
1
    drop(user_guard);
69

            
70
1
    let _response = pages::index(cookie_jar).await;
71
}
72

            
73
#[supp_macro::shared_runtime_test]
74
1
async fn test_register_handler() {
75
1
    let response = pages::register().await;
76
1
    let response = response.into_response();
77

            
78
1
    assert_eq!(response.status(), StatusCode::OK);
79
}
80

            
81
#[supp_macro::shared_runtime_test]
82
1
async fn test_login_handler() {
83
1
    let response = pages::login().await;
84
1
    let response = response.into_response();
85

            
86
1
    assert_eq!(response.status(), StatusCode::OK);
87
}
88

            
89
#[supp_macro::shared_runtime_test]
90
1
async fn test_app_state_user_value() {
91
1
    let app_state = create_test_app_state();
92

            
93
1
    let user_guard = app_state.user.lock().await;
94
1
    assert!(user_guard.is_some());
95

            
96
1
    let user = user_guard.as_ref().unwrap();
97
1
    assert_eq!(user.name, "Test User");
98
1
    assert_eq!(user.email, "test@example.com");
99
1
    assert!(user.verified);
100
}