axonml-server 0.6.2

REST API server for AxonML Machine Learning Framework
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Data Analysis API — Integration Tests
//!
//! Tests for the data analysis, Kaggle integration, and built-in dataset API
//! endpoints on the AxonML server. Covers authentication enforcement (401 on
//! unauthenticated requests), 404 handling for nonexistent datasets, Kaggle
//! credential validation, search, and downloaded-list queries, as well as
//! built-in dataset listing, searching, source enumeration, info lookup, and
//! prepare operations. Uses the `require_server!` macro to skip gracefully
//! when the server or admin DB is unavailable.
//!
//! # File
//! `crates/axonml-server/tests/api_data.rs`
//!
//! # Author
//! Andrew Jewell Sr. — AutomataNexus LLC
//! ORCID: 0009-0005-2158-7060
//!
//! # Updated
//! April 16, 2026 11:15 PM EST
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

mod common;

use common::*;
use serde_json::Value;

// =============================================================================
// Test Helpers
// =============================================================================

/// Skip test if server not running
macro_rules! require_server {
    () => {
        if !is_server_running().await {
            eprintln!("SKIP: server not running at {}", TEST_API_URL);
            return;
        }
        let _c = test_client();
        if login_as_admin(&_c).await.is_err() {
            eprintln!("SKIP: admin login failed (run AxonML_DB_Init.sh)");
            return;
        }
    };
}

// =============================================================================
// Data Analysis Endpoint Tests
// =============================================================================

#[tokio::test]
async fn test_analyze_dataset_requires_auth() {
    require_server!();

    let client = test_client();
    let response = client
        .post(format!("{}/api/data/test-id/analyze", TEST_API_URL))
        .send()
        .await
        .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        401,
        "Should require authentication"
    );
}

#[tokio::test]
async fn test_analyze_dataset_not_found() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    let response = auth_post(
        &client,
        "/api/data/nonexistent-dataset-id/analyze",
        &token,
        serde_json::json!({}),
    )
    .await
    .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        404,
        "Should return 404 for nonexistent dataset"
    );
}

#[tokio::test]
async fn test_preview_dataset_requires_auth() {
    require_server!();

    let client = test_client();
    let response = client
        .post(format!("{}/api/data/test-id/preview", TEST_API_URL))
        .send()
        .await
        .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        401,
        "Should require authentication"
    );
}

#[tokio::test]
async fn test_validate_dataset_requires_auth() {
    require_server!();

    let client = test_client();
    let response = client
        .post(format!("{}/api/data/test-id/validate", TEST_API_URL))
        .send()
        .await
        .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        401,
        "Should require authentication"
    );
}

#[tokio::test]
async fn test_generate_config_requires_auth() {
    require_server!();

    let client = test_client();
    let response = client
        .post(format!("{}/api/data/test-id/generate-config", TEST_API_URL))
        .json(&serde_json::json!({}))
        .send()
        .await
        .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        401,
        "Should require authentication"
    );
}

// =============================================================================
// Kaggle Integration Endpoint Tests
// =============================================================================

#[tokio::test]
async fn test_kaggle_status_endpoint() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    let response = auth_get(&client, "/api/kaggle/status", &token)
        .await
        .expect("Request failed");

    // Should return status (configured or not)
    assert!(
        response.status().is_success(),
        "Kaggle status should return success, got {}",
        response.status()
    );

    let body: Value = response.json().await.expect("Failed to parse JSON");
    assert!(
        body.get("configured").is_some(),
        "Response should have 'configured' field"
    );
}

#[tokio::test]
async fn test_kaggle_status_requires_auth() {
    require_server!();

    let client = test_client();
    let response = client
        .get(format!("{}/api/kaggle/status", TEST_API_URL))
        .send()
        .await
        .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        401,
        "Should require authentication"
    );
}

#[tokio::test]
async fn test_kaggle_search_requires_auth() {
    require_server!();

    let client = test_client();
    let response = client
        .get(format!("{}/api/kaggle/search?query=mnist", TEST_API_URL))
        .send()
        .await
        .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        401,
        "Should require authentication"
    );
}

#[tokio::test]
async fn test_kaggle_search_without_credentials() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    // First check if Kaggle is configured
    let status_resp = auth_get(&client, "/api/kaggle/status", &token)
        .await
        .expect("Request failed");

    let status: Value = status_resp.json().await.expect("Failed to parse JSON");
    let configured = status
        .get("configured")
        .and_then(|v| v.as_bool())
        .unwrap_or(false);

    let response = auth_get(&client, "/api/kaggle/search?query=mnist&limit=5", &token)
        .await
        .expect("Request failed");

    if configured {
        // If configured, search should work
        assert!(
            response.status().is_success() || response.status().as_u16() == 500,
            "Search should succeed or return server error if API fails"
        );
    } else {
        // If not configured, should return error
        assert!(
            response.status().is_client_error() || response.status().is_server_error(),
            "Should return error when not configured"
        );
    }
}

#[tokio::test]
async fn test_kaggle_downloaded_list() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    let response = auth_get(&client, "/api/kaggle/downloaded", &token)
        .await
        .expect("Request failed");

    // Should return list (possibly empty)
    assert!(
        response.status().is_success(),
        "Downloaded list should return success, got {}",
        response.status()
    );
}

#[tokio::test]
async fn test_kaggle_save_credentials_validation() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    // Test with empty credentials (should fail validation)
    let response = auth_post(
        &client,
        "/api/kaggle/credentials",
        &token,
        serde_json::json!({
            "username": "",
            "key": ""
        }),
    )
    .await
    .expect("Request failed");

    // Should reject empty credentials
    assert!(
        response.status().is_client_error() || response.status().is_server_error(),
        "Should reject empty credentials"
    );
}

// =============================================================================
// Built-in Datasets Endpoint Tests
// =============================================================================

#[tokio::test]
async fn test_list_builtin_datasets() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    let response = match auth_get(&client, "/api/builtin-datasets", &token).await {
        Ok(resp) => resp,
        Err(e) => {
            eprintln!("Note: Request failed (network issue): {} - skipping", e);
            return;
        }
    };

    assert!(
        response.status().is_success(),
        "List builtin datasets should succeed, got {}",
        response.status()
    );

    let body: Value = response.json().await.expect("Failed to parse JSON");
    assert!(body.is_array(), "Should return array of datasets");
}

#[tokio::test]
async fn test_list_builtin_datasets_requires_auth() {
    require_server!();

    let client = test_client();
    let response = client
        .get(format!("{}/api/builtin-datasets", TEST_API_URL))
        .send()
        .await
        .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        401,
        "Should require authentication"
    );
}

#[tokio::test]
async fn test_search_builtin_datasets() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    let response = auth_get(&client, "/api/builtin-datasets/search?query=mnist", &token)
        .await
        .expect("Request failed");

    assert!(
        response.status().is_success(),
        "Search builtin datasets should succeed, got {}",
        response.status()
    );
}

#[tokio::test]
async fn test_list_dataset_sources() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    let response = auth_get(&client, "/api/builtin-datasets/sources", &token)
        .await
        .expect("Request failed");

    assert!(
        response.status().is_success(),
        "List sources should succeed, got {}",
        response.status()
    );

    let body: Value = response.json().await.expect("Failed to parse JSON");
    assert!(body.is_array(), "Should return array of sources");
}

#[tokio::test]
async fn test_get_builtin_dataset_info_not_found() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    let response = auth_get(&client, "/api/builtin-datasets/nonexistent-id", &token)
        .await
        .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        404,
        "Should return 404 for nonexistent dataset"
    );
}

#[tokio::test]
async fn test_prepare_builtin_dataset_not_found() {
    require_server!();

    let client = test_client();
    let token = login_as_admin(&client).await.expect("Login failed");

    let response = auth_post(
        &client,
        "/api/builtin-datasets/nonexistent-id/prepare",
        &token,
        serde_json::json!({}),
    )
    .await
    .expect("Request failed");

    assert_eq!(
        response.status().as_u16(),
        404,
        "Should return 404 for nonexistent dataset"
    );
}