1
use axum::{
2
    body::Body,
3
    http::{Request, StatusCode, header},
4
};
5
use tower::ServiceExt;
6

            
7
use crate::common::create_test_app_state;
8
use web::route::{create_accounts_router, create_pages_router, create_transactions_router};
9

            
10
#[tokio::test]
11
1
async fn test_protected_page_routes_require_auth() {
12
1
    let app_state = create_test_app_state().await;
13

            
14
    // Test pages router
15
1
    let pages_app = create_pages_router(app_state.clone()).with_state(app_state.clone());
16

            
17
    // Test accounts router
18
1
    let accounts_app = create_accounts_router(app_state.clone()).with_state(app_state.clone());
19

            
20
    // Test transactions router
21
1
    let transactions_app =
22
1
        create_transactions_router(app_state.clone()).with_state(app_state.clone());
23

            
24
    // These routes are in create_api_router, not the page routers
25
1
    let protected_api_routes = vec![
26
1
        ("/commodity/create/submit", "POST"),
27
1
        ("/account/create/submit", "POST"),
28
1
        ("/transaction/create/submit", "POST"),
29
    ];
30

            
31
1
    let api_app = web::route::create_api_router(app_state.clone()).with_state(app_state.clone());
32

            
33
3
    for (route, method) in protected_api_routes {
34
3
        let response = api_app
35
3
            .clone()
36
3
            .oneshot(
37
3
                Request::builder()
38
3
                    .method(method)
39
3
                    .uri(route)
40
3
                    .header(header::CONTENT_TYPE, "application/json")
41
3
                    .body(Body::from("{}"))
42
3
                    .unwrap(),
43
3
            )
44
3
            .await
45
3
            .unwrap();
46

            
47
3
        assert!(
48
3
            response.status() == StatusCode::UNAUTHORIZED
49
                || response.status() == StatusCode::FORBIDDEN
50
                || response.status().is_server_error(),
51
            "Route {} {} should require authentication, got status: {}",
52
            method,
53
            route,
54
            response.status()
55
        );
56
    }
57

            
58
    // Test actual page routes (GET requests)
59
1
    let page_routes = vec![
60
1
        ("/commodity/create", "GET", &pages_app),
61
1
        ("/commodity/list", "GET", &pages_app),
62
1
        ("/account/create", "GET", &accounts_app),
63
1
        ("/account/list", "GET", &accounts_app),
64
1
        ("/transaction/create", "GET", &transactions_app),
65
1
        ("/transaction/list", "GET", &transactions_app),
66
    ];
67

            
68
6
    for (route, method, app) in page_routes {
69
6
        let response = app
70
6
            .clone()
71
6
            .oneshot(
72
6
                Request::builder()
73
6
                    .method(method)
74
6
                    .uri(route)
75
6
                    .body(Body::empty())
76
6
                    .unwrap(),
77
6
            )
78
6
            .await
79
6
            .unwrap();
80
1

            
81
6
        assert!(
82
6
            response.status() == StatusCode::UNAUTHORIZED
83
1
                || response.status() == StatusCode::FORBIDDEN
84
1
                || response.status().is_server_error(),
85
1
            "Route {} {} should require authentication, got status: {}",
86
1
            method,
87
1
            route,
88
1
            response.status()
89
1
        );
90
1
    }
91
1
}