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
3
async fn test_protected_page_routes_require_auth() {
12
2
    let app_state = create_test_app_state().await;
13

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

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

            
20
    // Test transactions router
21
2
    let transactions_app =
22
2
        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
2
    let protected_api_routes = vec![
26
2
        ("/commodity/create/submit", "POST"),
27
2
        ("/account/create/submit", "POST"),
28
2
        ("/transaction/create/submit", "POST"),
29
    ];
30

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

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

            
47
6
        assert!(
48
6
            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
2
    let page_routes = vec![
60
2
        ("/commodity/create", "GET", &pages_app),
61
2
        ("/commodity/list", "GET", &pages_app),
62
2
        ("/account/create", "GET", &accounts_app),
63
2
        ("/account/list", "GET", &accounts_app),
64
2
        ("/transaction/create", "GET", &transactions_app),
65
2
        ("/transaction/list", "GET", &transactions_app),
66
    ];
67

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

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