lumina-framework 1.0.0

A beautiful, fast, and elegant web framework for Rust
Documentation
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
use crate::core::application::AppState;
use crate::database::connection::DatabasePool;
use axum::{
    body::Body,
    http::{header, HeaderName, HeaderValue, Request, StatusCode},
    response::Response,
    Router,
};
use serde::Serialize;
use serde_json::Value;
use std::sync::Arc;
use tower::util::ServiceExt;
use uuid::Uuid;

/// Trait helper agar assert_status menerima `StatusCode` maupun `u16`
pub trait IntoStatusCode {
    fn into_status_code(self) -> StatusCode;
}

impl IntoStatusCode for StatusCode {
    fn into_status_code(self) -> StatusCode {
        self
    }
}

impl IntoStatusCode for u16 {
    fn into_status_code(self) -> StatusCode {
        StatusCode::from_u16(self).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
    }
}

/// Harness pengujian in-memory Lumina Framework
pub struct TestApp {
    pub router: Router,
    pub state: AppState,
}

impl TestApp {
    /// Buat instance TestApp baru dengan database in-memory yang unik.
    pub async fn new() -> Self {
        let id = Uuid::new_v4();
        let db_url = format!("sqlite:file:{}?mode=memory&cache=shared", id);
        let pool = DatabasePool::connect(&db_url)
            .await
            .expect("Failed to connect to test database");

        crate::database::migration::run_migrations(&pool.pool, pool.kind)
            .await
            .expect("Failed to run test migrations");

        let config = Arc::new(crate::core::config::ConfigManager::new());
        let lang = Arc::new(crate::core::i18n::LangManager::new("en".to_string()));
        let view = crate::core::view::ViewEngine::new(Some(lang.clone()));
        let cache = Arc::new(crate::core::cache::CacheManager::new());
        let events = Arc::new(crate::core::event::EventDispatcher::new());
        let telescope = Arc::new(crate::core::telescope::TelescopeManager::new());
        let echo = Arc::new(crate::core::echo::EchoManager::new());
        let registry = Arc::new(crate::core::queue::JobRegistry::new());
        let pool_arc = Arc::new(pool);
        let queue = Arc::new(crate::core::queue::QueueManager::new_with_pool(pool_arc.clone()));
        let storage = Arc::new(crate::core::storage::Storage::new("local"));
        let mail = Arc::new(crate::core::mail::Mail::new(
            Arc::new(crate::core::mail::LogDriver),
            view.clone(),
        ));
        let socialite = Arc::new(crate::core::auth::socialite::SocialiteManager::new());

        let state = AppState {
            db: Some(pool_arc),
            view,
            db_error: None,
            csrf_config: crate::core::security::csrf::config(),
            storage,
            config,
            queue,
            cache,
            mail,
            registry,
            events,
            telescope,
            lang,
            echo,
            socialite,
            sandbox: Arc::new(crate::core::sandbox::LuminaPluginManager::new()),
        };

        let session_store = crate::core::session::store::LuminaSessionStore::Memory(
            tower_sessions::MemoryStore::default(),
        );

        let app = crate::core::application::Application::new();
        let router = app.build_router(state.clone(), session_store);

        Self { router, state }
    }

    /// Buat request builder fluen untuk kustomisasi method, header, body, dan token
    pub fn req(&self, method: &str, uri: &str) -> TestRequestBuilder {
        TestRequestBuilder::new(self.router.clone(), method, uri)
    }

    pub fn get_req(&self, uri: &str) -> TestRequestBuilder {
        self.req("GET", uri)
    }

    pub fn post_req(&self, uri: &str) -> TestRequestBuilder {
        self.req("POST", uri)
    }

    pub fn put_req(&self, uri: &str) -> TestRequestBuilder {
        self.req("PUT", uri)
    }

    pub fn patch_req(&self, uri: &str) -> TestRequestBuilder {
        self.req("PATCH", uri)
    }

    pub fn delete_req(&self, uri: &str) -> TestRequestBuilder {
        self.req("DELETE", uri)
    }

    /// Shorthand request langsung (Backward Compatible)
    pub async fn get(&self, uri: &str) -> TestResponse {
        self.get_req(uri).send().await
    }

    pub async fn post(&self, uri: &str, body: Value) -> TestResponse {
        self.post_req(uri).with_json(body).send().await
    }

    pub async fn put(&self, uri: &str, body: Value) -> TestResponse {
        self.put_req(uri).with_json(body).send().await
    }

    pub async fn delete(&self, uri: &str) -> TestResponse {
        self.delete_req(uri).send().await
    }

    // =========================================================================
    // 🗄️ Fluent Database Testing Assertions
    // =========================================================================

    /// Pastikan data tertentu ada di dalam tabel database.
    ///
    /// Contoh:
    /// ```rust,ignore
    /// app.assert_database_has("contacts", json!({ "name": "Budi Santoso", "is_active": 1 })).await;
    /// ```
    pub async fn assert_database_has(&self, table: &str, criteria: Value) -> &Self {
        let pool = self.state.db().get_pool();
        let obj = criteria.as_object().expect("Criteria must be a JSON object");

        let mut query = format!("SELECT COUNT(*) as count FROM {} WHERE ", table);
        let mut clauses = Vec::new();
        let mut values = Vec::new();

        for (col, val) in obj {
            clauses.push(format!("{} = ?", col));
            values.push(val.clone());
        }
        query.push_str(&clauses.join(" AND "));

        let mut q = sqlx::query_scalar::<_, i64>(&query);
        for val in values {
            match val {
                Value::Number(n) if n.is_i64() => q = q.bind(n.as_i64().unwrap()),
                Value::Number(n) if n.is_f64() => q = q.bind(n.as_f64().unwrap()),
                Value::Bool(b) => q = q.bind(b),
                Value::String(s) => q = q.bind(s),
                _ => q = q.bind(val.to_string()),
            }
        }

        let count = q.fetch_one(pool).await.unwrap_or(0);
        assert!(
            count > 0,
            "Failed asserting that a row in [{}] matches criteria: {:?}",
            table,
            criteria
        );
        self
    }

    /// Pastikan data tertentu TIDAK ada di dalam tabel database.
    pub async fn assert_database_missing(&self, table: &str, criteria: Value) -> &Self {
        let pool = self.state.db().get_pool();
        let obj = criteria.as_object().expect("Criteria must be a JSON object");

        let mut query = format!("SELECT COUNT(*) as count FROM {} WHERE ", table);
        let mut clauses = Vec::new();
        let mut values = Vec::new();

        for (col, val) in obj {
            clauses.push(format!("{} = ?", col));
            values.push(val.clone());
        }
        query.push_str(&clauses.join(" AND "));

        let mut q = sqlx::query_scalar::<_, i64>(&query);
        for val in values {
            match val {
                Value::Number(n) if n.is_i64() => q = q.bind(n.as_i64().unwrap()),
                Value::Number(n) if n.is_f64() => q = q.bind(n.as_f64().unwrap()),
                Value::Bool(b) => q = q.bind(b),
                Value::String(s) => q = q.bind(s),
                _ => q = q.bind(val.to_string()),
            }
        }

        let count = q.fetch_one(pool).await.unwrap_or(0);
        assert_eq!(
            count,
            0,
            "Failed asserting that table [{}] does NOT contain criteria: {:?}",
            table,
            criteria
        );
        self
    }

    /// Pastikan jumlah total baris pada tabel sesuai dengan ekspektasi.
    pub async fn assert_database_count(&self, table: &str, expected_count: usize) -> &Self {
        let pool = self.state.db().get_pool();
        let query = format!("SELECT COUNT(*) as count FROM {}", table);
        let count = sqlx::query_scalar::<_, i64>(&query)
            .fetch_one(pool)
            .await
            .unwrap_or(0) as usize;

        assert_eq!(
            count,
            expected_count,
            "Failed asserting that table [{}] has count [{}]. Actual count was [{}].",
            table,
            expected_count,
            count
        );
        self
    }
}

/// Fluent HTTP Request Builder untuk pengujian
pub struct TestRequestBuilder {
    router: Router,
    method: String,
    uri: String,
    headers: Vec<(String, String)>,
    body: Vec<u8>,
    user: Option<crate::core::auth::AuthUser>,
}

impl TestRequestBuilder {
    pub fn new(router: Router, method: &str, uri: &str) -> Self {
        Self {
            router,
            method: method.to_uppercase(),
            uri: uri.to_string(),
            headers: Vec::new(),
            body: Vec::new(),
            user: None,
        }
    }

    /// Tambahkan header HTTP kustom
    pub fn with_header(mut self, key: &str, value: &str) -> Self {
        self.headers.push((key.to_string(), value.to_string()));
        self
    }

    /// Tambahkan Bearer token ke header Authorization
    pub fn with_token(mut self, token: &str) -> Self {
        self.headers.push((
            "Authorization".to_string(),
            format!("Bearer {}", token.trim_start_matches("Bearer ").trim()),
        ));
        self
    }

    /// Simulasikan user yang sedang login via AuthUser extension
    pub fn acting_as(mut self, user: crate::core::auth::AuthUser) -> Self {
        self.user = Some(user);
        self
    }

    /// Kirim payload JSON
    pub fn with_json<T: Serialize>(mut self, body: T) -> Self {
        self.headers.push((
            header::CONTENT_TYPE.to_string(),
            "application/json".to_string(),
        ));
        self.headers.push((
            header::ACCEPT.to_string(),
            "application/json".to_string(),
        ));
        self.body = serde_json::to_vec(&body).unwrap_or_default();
        self
    }

    /// Kirim payload URL-encoded form data
    pub fn with_form(mut self, pairs: &[(&str, &str)]) -> Self {
        self.headers.push((
            header::CONTENT_TYPE.to_string(),
            "application/x-www-form-urlencoded".to_string(),
        ));
        let form_str = serde_urlencoded::to_string(pairs).unwrap_or_default();
        self.body = form_str.into_bytes();
        self
    }

    /// Eksekusi request dan kembalikan TestResponse fluen
    pub async fn send(self) -> TestResponse {
        let mut builder = Request::builder()
            .uri(&self.uri)
            .method(self.method.as_str());

        for (k, v) in self.headers {
            if let (Ok(name), Ok(val)) = (HeaderName::from_bytes(k.as_bytes()), HeaderValue::from_str(&v)) {
                builder = builder.header(name, val);
            }
        }

        let mut req = builder.body(Body::from(self.body)).unwrap();

        if let Some(user) = self.user {
            req.extensions_mut().insert(user);
        }

        let response = self.router.clone().oneshot(req).await.unwrap();
        TestResponse::new(response).await
    }
}

/// Representasi Response pengujian dengan 20+ fluent assertions
pub struct TestResponse {
    pub response: Response,
    pub body_bytes: Vec<u8>,
}

impl TestResponse {
    pub async fn new(response: Response) -> Self {
        use axum::body::to_bytes;
        let (parts, body) = response.into_parts();
        let bytes = to_bytes(body, usize::MAX).await.unwrap().to_vec();
        let response = Response::from_parts(parts, Body::from(bytes.clone()));

        Self {
            response,
            body_bytes: bytes,
        }
    }

    // =========================================================================
    // 🚦 HTTP Status Assertions
    // =========================================================================

    pub fn status(&self) -> StatusCode {
        self.response.status()
    }

    pub fn assert_status<T: IntoStatusCode>(&self, expected: T) -> &Self {
        let expected_code = expected.into_status_code();
        assert_eq!(
            self.response.status(),
            expected_code,
            "HTTP Status mismatch. Expected {}, got {}. Body: {}",
            expected_code,
            self.response.status(),
            self.text()
        );
        self
    }

    pub fn assert_ok(&self) -> &Self {
        self.assert_status(StatusCode::OK)
    }

    pub fn assert_created(&self) -> &Self {
        self.assert_status(StatusCode::CREATED)
    }

    pub fn assert_accepted(&self) -> &Self {
        self.assert_status(StatusCode::ACCEPTED)
    }

    pub fn assert_no_content(&self) -> &Self {
        self.assert_status(StatusCode::NO_CONTENT)
    }

    pub fn assert_bad_request(&self) -> &Self {
        self.assert_status(StatusCode::BAD_REQUEST)
    }

    pub fn assert_unauthorized(&self) -> &Self {
        self.assert_status(StatusCode::UNAUTHORIZED)
    }

    pub fn assert_forbidden(&self) -> &Self {
        self.assert_status(StatusCode::FORBIDDEN)
    }

    pub fn assert_not_found(&self) -> &Self {
        self.assert_status(StatusCode::NOT_FOUND)
    }

    pub fn assert_unprocessable(&self) -> &Self {
        self.assert_status(StatusCode::UNPROCESSABLE_ENTITY)
    }

    pub fn assert_server_error(&self) -> &Self {
        self.assert_status(StatusCode::INTERNAL_SERVER_ERROR)
    }

    // =========================================================================
    // 📦 JSON Structure & Content Assertions
    // =========================================================================

    pub fn json(&self) -> Value {
        serde_json::from_slice(&self.body_bytes).unwrap_or_else(|e| {
            panic!(
                "Failed to parse response body as JSON: {}. Body was: '{}'",
                e,
                self.text()
            )
        })
    }

    /// Evaluasi nilai JSON pada path tertentu menggunakan dot notation (misal: "data.user.name" atau "items[0].id")
    pub fn json_path(&self, path: &str) -> Option<Value> {
        let root = self.json();
        get_json_path(&root, path)
    }

    pub fn assert_json_path(&self, path: &str, expected: impl Into<Value>) -> &Self {
        let expected_val = expected.into();
        let actual_val = self.json_path(path);
        assert_eq!(
            actual_val.as_ref(),
            Some(&expected_val),
            "JSON path '{}' mismatch. Expected: {:?}, Got: {:?}",
            path,
            expected_val,
            actual_val
        );
        self
    }

    pub fn assert_json_has(&self, path: &str) -> &Self {
        assert!(
            self.json_path(path).is_some(),
            "Expected JSON path '{}' to exist, but not found in: {:?}",
            path,
            self.json()
        );
        self
    }

    pub fn assert_json_missing(&self, path: &str) -> &Self {
        assert!(
            self.json_path(path).is_none(),
            "Expected JSON path '{}' to NOT exist, but found: {:?}",
            path,
            self.json_path(path)
        );
        self
    }

    pub fn assert_json_count(&self, path: &str, expected_count: usize) -> &Self {
        let val = self.json_path(path).unwrap_or_else(|| {
            panic!("JSON path '{}' not found", path);
        });

        match val {
            Value::Array(arr) => assert_eq!(
                arr.len(),
                expected_count,
                "JSON array at path '{}' has length {}, expected {}",
                path,
                arr.len(),
                expected_count
            ),
            Value::Object(map) => assert_eq!(
                map.len(),
                expected_count,
                "JSON object at path '{}' has {} keys, expected {}",
                path,
                map.len(),
                expected_count
            ),
            _ => panic!("JSON path '{}' is neither an Array nor an Object", path),
        }
        self
    }

    // =========================================================================
    // 📝 Text, Headers, & Redirection Assertions
    // =========================================================================

    pub fn text(&self) -> String {
        String::from_utf8_lossy(&self.body_bytes).to_string()
    }

    pub fn assert_see(&self, text: &str) -> &Self {
        let body = self.text();
        assert!(
            body.contains(text),
            "Expected text '{}' was not found in response body: '{}'",
            text,
            body
        );
        self
    }

    pub fn assert_dont_see(&self, text: &str) -> &Self {
        let body = self.text();
        assert!(
            !body.contains(text),
            "Unexpected text '{}' was found in response body",
            text
        );
        self
    }

    pub fn assert_header(&self, key: &str, expected: &str) -> &Self {
        let actual = self
            .response
            .headers()
            .get(key)
            .and_then(|v| v.to_str().ok());
        assert_eq!(
            actual,
            Some(expected),
            "Header '{}' mismatch. Expected '{:?}', got '{:?}'",
            key,
            expected,
            actual
        );
        self
    }

    pub fn assert_header_missing(&self, key: &str) -> &Self {
        assert!(
            !self.response.headers().contains_key(key),
            "Header '{}' unexpectedly present in response",
            key
        );
        self
    }

    pub fn assert_redirect(&self, expected_location: &str) -> &Self {
        let status = self.response.status();
        assert!(
            status.is_redirection(),
            "Expected redirect status (3xx), got: {}",
            status
        );

        let location = self
            .response
            .headers()
            .get(header::LOCATION)
            .and_then(|v| v.to_str().ok())
            .unwrap_or_default();

        assert!(
            location.ends_with(expected_location) || location == expected_location,
            "Expected redirect to '{}', but redirected to '{}'",
            expected_location,
            location
        );
        self
    }
}

/// Helper parsing dot notation untuk traversal JSON Value
fn get_json_path(root: &Value, path: &str) -> Option<Value> {
    let mut current = root;
    for part in path.split('.') {
        if let Some(bracket_idx) = part.find('[') {
            let key = &part[..bracket_idx];
            let rest = &part[bracket_idx..];
            if !key.is_empty() {
                current = current.get(key)?;
            }
            for num_str in rest.split(']').filter_map(|s| s.strip_prefix('[')) {
                let idx: usize = num_str.parse().ok()?;
                current = current.get(idx)?;
            }
        } else {
            current = current.get(part)?;
        }
    }
    Some(current.clone())
}