1
use chrono::Utc;
2
use redis::Client;
3
use std::sync::Arc;
4
use uuid::Uuid;
5
use web::{AppState, Config, jwt_auth::JWTAuthMiddleware, model::User};
6

            
7
/// Create a test app state with mock configuration
8
89
pub async fn create_test_app_state() -> Arc<AppState> {
9
89
    let config = Config {
10
89
        site_url: "http://localhost:3000".to_string(),
11
89
        redis_url: "redis://localhost:6379".to_string(),
12
89
        client_origin: "http://localhost:3000".to_string(),
13
89
        access_token_private_key: "dGVzdF9wcml2YXRlX2tleQ==".to_string(), // base64("test_private_key")
14
89
        access_token_public_key: "dGVzdF9wdWJsaWNfa2V5".to_string(), // base64("test_public_key")
15
89
        access_token_expires_in: "15m".to_string(),
16
89
        access_token_max_age: 15,
17
89
        refresh_token_private_key: "cmVmcmVzaF9wcml2YXRlX2tleQ==".to_string(), // base64("refresh_private_key")
18
89
        refresh_token_public_key: "cmVmcmVzaF9wdWJsaWNfa2V5".to_string(), // base64("refresh_public_key")
19
89
        refresh_token_expires_in: "60m".to_string(),
20
89
        refresh_token_max_age: 60,
21
89
    };
22

            
23
89
    let redis_client = Client::open("redis://localhost:6379").unwrap_or_else(|_| {
24
        Client::open("redis://127.0.0.1:6379")
25
            .unwrap_or_else(|_| panic!("Failed to connect to Redis for testing"))
26
    });
27

            
28
89
    AppState::new(config, redis_client)
29
89
}
30

            
31
/// Create a mock user for testing
32
54
pub fn create_mock_user() -> User {
33
54
    User {
34
54
        id: Uuid::new_v4(),
35
54
        name: "Test User".to_string(),
36
54
        email: "test@example.com".to_string(),
37
54
        password: "hashed_password".to_string(),
38
54
        role: "user".to_string(),
39
54
        photo: "photo.jpg".to_string(),
40
54
        verified: true,
41
54
        database: "test_db".to_string(),
42
54
        created_at: Some(Utc::now()),
43
54
        updated_at: Some(Utc::now()),
44
54
    }
45
54
}
46

            
47
/// Create mock JWT auth middleware for testing
48
54
pub fn create_mock_jwt_auth(user: User) -> JWTAuthMiddleware {
49
54
    JWTAuthMiddleware {
50
54
        user,
51
54
        access_token_uuid: Uuid::new_v4(),
52
54
    }
53
54
}