1
use axum::{Router, body::Body, http::Request};
2
use tower::ServiceExt;
3

            
4
use crate::common::{create_mock_jwt_auth, create_mock_user, create_test_app_state};
5

            
6
#[supp_macro::shared_runtime_test]
7
1
async fn test_commodity_table_without_auth() {
8
1
    let app_state = create_test_app_state().await;
9
1
    let app = Router::new()
10
1
        .route(
11
1
            "/commodity/list",
12
1
            axum::routing::get(web::pages::commodity::list::commodity_table),
13
1
        )
14
1
        .with_state(app_state);
15

            
16
1
    let response = app
17
1
        .oneshot(
18
1
            Request::builder()
19
1
                .method("GET")
20
1
                .uri("/commodity/list")
21
1
                .body(Body::empty())
22
1
                .unwrap(),
23
1
        )
24
1
        .await
25
        .unwrap();
26

            
27
    // Should fail without authentication - expecting 401 or 500
28
1
    assert!(response.status().is_client_error() || response.status().is_server_error());
29
}
30

            
31
#[supp_macro::shared_runtime_test]
32
1
async fn test_commodity_table_with_mock_auth() {
33
1
    let app_state = create_test_app_state().await;
34
1
    let mock_user = create_mock_user();
35
1
    let jwt_auth = create_mock_jwt_auth(mock_user);
36

            
37
1
    let app = Router::new()
38
1
        .route(
39
1
            "/commodity/list",
40
1
            axum::routing::get(web::pages::commodity::list::commodity_table),
41
1
        )
42
1
        .layer(axum::middleware::from_fn_with_state(
43
1
            app_state.clone(),
44
1
            move |mut req: axum::http::Request<Body>, next: axum::middleware::Next| {
45
1
                let jwt_auth = jwt_auth.clone();
46
1
                async move {
47
1
                    req.extensions_mut().insert(jwt_auth);
48
1
                    next.run(req).await
49
1
                }
50
1
            },
51
        ))
52
1
        .with_state(app_state.clone());
53

            
54
1
    let response = app
55
1
        .oneshot(
56
1
            Request::builder()
57
1
                .method("GET")
58
1
                .uri("/commodity/list")
59
1
                .body(Body::empty())
60
1
                .unwrap(),
61
1
        )
62
1
        .await
63
        .unwrap();
64

            
65
    // This test verifies our ListCommodities macro integration works
66
    // Even if it fails due to DB issues, it should not be a parsing error
67
    // We expect either success (200) or a server error (500) due to missing DB
68
    // but NOT a client error (400) which would indicate API issues
69
1
    assert!(
70
1
        response.status().is_success() || response.status().is_server_error(),
71
        "Expected success or server error, got: {}",
72
        response.status()
73
    );
74

            
75
    // If we get a response, verify it has proper content type (HTML)
76
1
    if response.status().is_success()
77
        && let Some(content_type) = response.headers().get("content-type")
78
    {
79
        let content_type_str = content_type.to_str().unwrap_or("");
80
        // Should be HTML for the table template
81
        assert!(
82
            content_type_str.contains("text/html") || content_type_str.contains("text/"),
83
            "Unexpected content type: {content_type_str}"
84
        );
85
1
    }
86
}
87

            
88
#[supp_macro::shared_runtime_test]
89
1
async fn test_commodity_table_listing_integration() {
90
1
    let app_state = create_test_app_state().await;
91
1
    let mock_user = create_mock_user();
92
1
    let jwt_auth = create_mock_jwt_auth(mock_user);
93

            
94
    // Create a router with both commodity creation and listing endpoints
95
1
    let app = Router::new()
96
1
        .route(
97
1
            "/commodity/create/submit",
98
1
            axum::routing::post(web::pages::commodity::create::commodity_create_submit),
99
1
        )
100
1
        .route(
101
1
            "/commodity/list",
102
1
            axum::routing::get(web::pages::commodity::list::commodity_table),
103
1
        )
104
1
        .layer(axum::middleware::from_fn_with_state(
105
1
            app_state.clone(),
106
3
            move |mut req: axum::http::Request<Body>, next: axum::middleware::Next| {
107
3
                let jwt_auth = jwt_auth.clone();
108
3
                async move {
109
3
                    req.extensions_mut().insert(jwt_auth);
110
3
                    next.run(req).await
111
3
                }
112
3
            },
113
        ))
114
1
        .with_state(app_state.clone());
115

            
116
    // First, create some test commodities
117
1
    let usd_commodity = "symbol=USD&name=US+Dollar";
118
1
    let eur_commodity = "symbol=EUR&name=Euro";
119

            
120
    // Create USD commodity
121
1
    let create_response = app
122
1
        .clone()
123
1
        .oneshot(
124
1
            Request::builder()
125
1
                .method("POST")
126
1
                .uri("/commodity/create/submit")
127
1
                .header("content-type", "application/x-www-form-urlencoded")
128
1
                .body(Body::from(usd_commodity))
129
1
                .unwrap(),
130
1
        )
131
1
        .await
132
        .unwrap();
133

            
134
    // Should succeed or fail gracefully (DB might not be available in test environment)
135
1
    assert!(
136
1
        create_response.status().is_success() || create_response.status().is_server_error(),
137
        "USD commodity creation failed with unexpected status: {}",
138
        create_response.status()
139
    );
140

            
141
    // Create EUR commodity
142
1
    let create_response = app
143
1
        .clone()
144
1
        .oneshot(
145
1
            Request::builder()
146
1
                .method("POST")
147
1
                .uri("/commodity/create/submit")
148
1
                .header("content-type", "application/x-www-form-urlencoded")
149
1
                .body(Body::from(eur_commodity))
150
1
                .unwrap(),
151
1
        )
152
1
        .await
153
        .unwrap();
154

            
155
    // Should succeed or fail gracefully
156
1
    assert!(
157
1
        create_response.status().is_success() || create_response.status().is_server_error(),
158
        "EUR commodity creation failed with unexpected status: {}",
159
        create_response.status()
160
    );
161

            
162
    // Now test the commodity listing
163
1
    let list_response = app
164
1
        .oneshot(
165
1
            Request::builder()
166
1
                .method("GET")
167
1
                .uri("/commodity/list")
168
1
                .body(Body::empty())
169
1
                .unwrap(),
170
1
        )
171
1
        .await
172
        .unwrap();
173

            
174
    // This test verifies the complete ListCommodities integration in the web layer
175
    // It tests that the commodity_table handler correctly calls the ListCommodities command
176
    // and renders the HTML table template with the results
177
1
    assert!(
178
1
        list_response.status().is_success() || list_response.status().is_server_error(),
179
        "Expected success or server error for commodity list, got: {}",
180
        list_response.status()
181
    );
182

            
183
    // Verify response has correct content type for HTML table
184
1
    if list_response.status().is_success()
185
        && let Some(content_type) = list_response.headers().get("content-type")
186
    {
187
        let content_type_str = content_type.to_str().unwrap_or("");
188
        assert!(
189
            content_type_str.contains("text/html") || content_type_str.contains("text/"),
190
            "Expected HTML content type for commodity table, got: {content_type_str}"
191
        );
192
1
    }
193

            
194
    // If we get a successful response, check that it's actually HTML table content
195
1
    if list_response.status().is_success() {
196
        let body = axum::body::to_bytes(list_response.into_body(), usize::MAX)
197
            .await
198
            .unwrap();
199
        let body_str = String::from_utf8(body.to_vec()).unwrap();
200

            
201
        // The response should contain HTML table elements for the commodity list
202
        // This verifies that the ListCommodities command result is properly rendered
203
        assert!(
204
            body_str.contains("<table") || body_str.contains("commodity") || !body_str.is_empty(),
205
            "Expected HTML table content in commodity list response, got empty or non-HTML content"
206
        );
207
1
    }
208
}
209

            
210
#[supp_macro::shared_runtime_test]
211
1
async fn test_get_commodity_account_balance_integration() {
212
1
    let app_state = create_test_app_state().await;
213
1
    let mock_user = create_mock_user();
214
1
    let jwt_auth = create_mock_jwt_auth(mock_user);
215

            
216
1
    let app = Router::new()
217
1
        .route(
218
1
            "/account/list",
219
1
            axum::routing::get(web::pages::account::list::account_table),
220
1
        )
221
1
        .layer(axum::middleware::from_fn_with_state(
222
1
            app_state.clone(),
223
1
            move |mut req: axum::http::Request<Body>, next: axum::middleware::Next| {
224
1
                let jwt_auth = jwt_auth.clone();
225
1
                async move {
226
1
                    req.extensions_mut().insert(jwt_auth);
227
1
                    next.run(req).await
228
1
                }
229
1
            },
230
        ))
231
1
        .with_state(app_state.clone());
232

            
233
1
    let response = app
234
1
        .oneshot(
235
1
            Request::builder()
236
1
                .method("GET")
237
1
                .uri("/account/list")
238
1
                .body(Body::empty())
239
1
                .unwrap(),
240
1
        )
241
1
        .await
242
        .unwrap();
243

            
244
    // This test verifies GetCommodity is working correctly for account balance currency display
245
    // Even with DB connection issues, it should handle the GetCommodity calls gracefully
246
1
    assert!(
247
1
        response.status().is_success() || response.status().is_server_error(),
248
        "Expected success or server error, got: {}",
249
        response.status()
250
    );
251
}