raisfast 0.2.23

The last backend you'll ever need. Rust-powered headless CMS with built-in blog, ecommerce, wallet, payment and 4 plugin engines.
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
use super::*;
use raisfast::DbDriver;

async fn setup() -> (axum::Router, String, raisfast::db::Pool) {
    let (app, state) = test_app().await;
    let (int_id, id) = create_admin(&state.pool).await;
    let tok = make_token(&id, int_id, raisfast::models::user::UserRole::Admin);
    (app, tok, state.pool)
}

#[tokio::test]
async fn create_token_success() {
    let (mut app, tok, _) = setup().await;
    let (status, body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "CI/CD", "scopes": ["read", "write"]}),
            &tok,
        ),
    )
    .await;
    assert_eq!(status, StatusCode::CREATED);
    let data = &body["data"];
    assert!(data["token"].as_str().unwrap().starts_with("rblog_"));
    assert_eq!(data["name"].as_str().unwrap(), "CI/CD");
}

#[tokio::test]
async fn list_tokens() {
    let (mut app, tok, _) = setup().await;
    let (_, create_body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "My Token", "scopes": ["read"]}),
            &tok,
        ),
    )
    .await;

    let (status, body) = send(&mut app, get_auth("/api/v1/tokens", &tok)).await;
    assert!(status.is_success());
    let tokens = body["data"].as_array().unwrap();
    assert_eq!(tokens.len(), 1);
    assert_eq!(tokens[0]["name"].as_str().unwrap(), "My Token");
    assert!(tokens[0]["token"].is_null());
    assert_eq!(
        tokens[0]["token_prefix"].as_str().unwrap(),
        create_body["data"]["token_prefix"].as_str().unwrap()
    );
}

#[tokio::test]
async fn delete_token() {
    let (mut app, tok, _) = setup().await;
    let (_, create_body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "To Delete", "scopes": ["read"]}),
            &tok,
        ),
    )
    .await;
    let id = create_body["data"]["id"].as_str().unwrap();

    let (status, _) = send(&mut app, delete_auth(&format!("/api/v1/tokens/{id}"), &tok)).await;
    assert!(status.is_success());

    let (_, body) = send(&mut app, get_auth("/api/v1/tokens", &tok)).await;
    assert!(body["data"].as_array().unwrap().is_empty());
}

#[tokio::test]
async fn delete_token_not_found() {
    let (mut app, tok, _) = setup().await;
    let (status, _) = send(&mut app, delete_auth("/api/v1/tokens/9999999999999", &tok)).await;
    assert_eq!(status, StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn create_token_requires_auth() {
    let (mut app, _, _) = setup().await;
    let (status, _) = send(
        &mut app,
        post_json("/api/v1/tokens", json!({"name": "x", "scopes": ["read"]})),
    )
    .await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn api_token_authenticates_as_user() {
    let (mut app, tok, _) = setup().await;
    let (_, create_body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Test Auth", "scopes": ["read", "write"]}),
            &tok,
        ),
    )
    .await;
    let api_token = create_body["data"]["token"].as_str().unwrap();

    let (status, body) = send(&mut app, get_auth("/api/v1/users/me", api_token)).await;
    assert!(
        status.is_success(),
        "api token auth failed: {status} {body:?}"
    );
    assert_eq!(body["data"]["username"].as_str().unwrap(), "testadmin");
}

#[tokio::test]
async fn api_token_admin_scope() {
    let (mut app, tok, _) = setup().await;
    let (_, create_body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Admin Token", "scopes": ["admin"]}),
            &tok,
        ),
    )
    .await;
    let api_token = create_body["data"]["token"].as_str().unwrap();

    let (status, body) = send(&mut app, get_auth("/api/v1/admin/stats", api_token)).await;
    assert!(status.is_success(), "admin scope failed: {status} {body:?}");
}

#[tokio::test]
async fn api_token_read_scope_cannot_create_post() {
    let (mut app, tok, _) = setup().await;
    let (_, create_body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Read Only", "scopes": ["read"]}),
            &tok,
        ),
    )
    .await;
    let api_token = create_body["data"]["token"].as_str().unwrap();

    let (status, _) = send(
        &mut app,
        post_json_auth(
            "/api/v1/posts",
            json!({"title": "t", "content": "c", "slug": "s"}),
            api_token,
        ),
    )
    .await;
    assert_eq!(status, StatusCode::FORBIDDEN);
}

#[tokio::test]
async fn invalid_api_token_rejected() {
    let (mut app, _, _) = setup().await;
    let (status, _) = send(
        &mut app,
        get_auth("/api/v1/users/me", "rblog_invalid_token_here"),
    )
    .await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn delete_token_non_owner_forbidden() {
    let (mut app, tok, pool) = setup().await;
    let (_, create_body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Owned", "scopes": ["read"]}),
            &tok,
        ),
    )
    .await;
    let id = create_body["data"]["id"].as_str().unwrap();

    let reader_hash = raisfast::services::auth::hash_password("ReaderPass123!").unwrap();
    let sql = "INSERT INTO users (username, role, status, registered_via) VALUES ('tokenreader', 'reader', 'active', 'email') RETURNING id";
    let reader_int_id: i64 = sqlx::query_scalar(sql).fetch_one(&pool).await.unwrap();
    let cred_data = serde_json::json!({"password_hash": reader_hash}).to_string();
    let cred_id = raisfast::utils::id::new_id();
    let cred_now = raisfast::utils::tz::now_utc();
    let cred_sql = format!(
        "INSERT INTO user_credentials (id, user_id, auth_type, identifier, credential_data, verified, created_at, updated_at) VALUES ({}, {}, 'email', {}, {}, 1, {}, {})",
        raisfast::db::Driver::ph(1),
        raisfast::db::Driver::ph(2),
        raisfast::db::Driver::ph(3),
        raisfast::db::Driver::ph(4),
        raisfast::db::Driver::ph(5),
        raisfast::db::Driver::ph(6)
    );
    sqlx::query(&cred_sql)
        .bind(cred_id)
        .bind(reader_int_id)
        .bind("reader-token@test.com")
        .bind(&cred_data)
        .bind(cred_now)
        .bind(cred_now)
        .execute(&pool)
        .await
        .unwrap();
    let reader_tok = make_token(
        &reader_int_id.to_string(),
        reader_int_id,
        raisfast::models::user::UserRole::Reader,
    );

    let (status, _) = send(
        &mut app,
        delete_auth(&format!("/api/v1/tokens/{id}"), &reader_tok),
    )
    .await;
    assert_eq!(status, StatusCode::FORBIDDEN);
}

#[tokio::test]
async fn admin_can_delete_other_users_token() {
    let (mut app, tok, pool) = setup().await;
    let reader_hash = raisfast::services::auth::hash_password("ReaderPass123!").unwrap();
    let sql = "INSERT INTO users (username, role, status, registered_via) VALUES ('readeradmindel', 'reader', 'active', 'email') RETURNING id";
    let reader_int_id: i64 = sqlx::query_scalar(sql).fetch_one(&pool).await.unwrap();
    let cred_data = serde_json::json!({"password_hash": reader_hash}).to_string();
    let cred_id = raisfast::utils::id::new_id();
    let cred_now = raisfast::utils::tz::now_utc();
    let cred_sql = format!(
        "INSERT INTO user_credentials (id, user_id, auth_type, identifier, credential_data, verified, created_at, updated_at) VALUES ({}, {}, 'email', {}, {}, 1, {}, {})",
        raisfast::db::Driver::ph(1),
        raisfast::db::Driver::ph(2),
        raisfast::db::Driver::ph(3),
        raisfast::db::Driver::ph(4),
        raisfast::db::Driver::ph(5),
        raisfast::db::Driver::ph(6)
    );
    sqlx::query(&cred_sql)
        .bind(cred_id)
        .bind(reader_int_id)
        .bind("reader-admin-del@test.com")
        .bind(&cred_data)
        .bind(cred_now)
        .bind(cred_now)
        .execute(&pool)
        .await
        .unwrap();
    let reader_tok = make_token(
        &reader_int_id.to_string(),
        reader_int_id,
        raisfast::models::user::UserRole::Reader,
    );

    let (_, create_body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Reader Token", "scopes": ["read"]}),
            &reader_tok,
        ),
    )
    .await;
    let id = create_body["data"]["id"].as_str().unwrap();

    let (status, _) = send(&mut app, delete_auth(&format!("/api/v1/tokens/{id}"), &tok)).await;
    assert!(status.is_success());
}

#[tokio::test]
async fn create_token_validation_empty_name() {
    let (mut app, tok, _) = setup().await;
    let (status, body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "", "scopes": ["read"]}),
            &tok,
        ),
    )
    .await;
    assert!(
        status == StatusCode::BAD_REQUEST || status == StatusCode::UNPROCESSABLE_ENTITY,
        "expected 400 or 422, got {status}: {body:?}"
    );
}

#[tokio::test]
async fn create_token_validation_empty_scopes() {
    let (mut app, tok, _) = setup().await;
    let (status, _body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Test", "scopes": []}),
            &tok,
        ),
    )
    .await;
    assert_eq!(status, StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn create_token_validation_invalid_scope() {
    let (mut app, tok, _) = setup().await;
    let (status, _) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Test", "scopes": ["superuser"]}),
            &tok,
        ),
    )
    .await;
    assert_eq!(status, StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn create_token_validation_missing_body() {
    let (mut app, tok, _) = setup().await;
    let (status, _) = send(
        &mut app,
        Request::builder()
            .method("POST")
            .uri("/api/v1/tokens")
            .header(header::AUTHORIZATION, format!("Bearer {tok}"))
            .header(header::CONTENT_TYPE, "application/json")
            .body(Body::from("{}"))
            .unwrap(),
    )
    .await;
    assert!(
        status == StatusCode::BAD_REQUEST || status == StatusCode::UNPROCESSABLE_ENTITY,
        "expected 400 or 422, got {status}"
    );
}

#[tokio::test]
async fn token_deleted_cannot_authenticate() {
    let (mut app, tok, _) = setup().await;
    let (_, create_body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Then Delete", "scopes": ["read", "write"]}),
            &tok,
        ),
    )
    .await;
    let api_token = create_body["data"]["token"].as_str().unwrap();
    let id = create_body["data"]["id"].as_str().unwrap();

    send(&mut app, delete_auth(&format!("/api/v1/tokens/{id}"), &tok)).await;

    let (status, _) = send(&mut app, get_auth("/api/v1/users/me", api_token)).await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn multiple_tokens_per_user() {
    let (mut app, tok, _) = setup().await;
    send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "First", "scopes": ["read"]}),
            &tok,
        ),
    )
    .await;
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Second", "scopes": ["write"]}),
            &tok,
        ),
    )
    .await;
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Third", "scopes": ["admin"]}),
            &tok,
        ),
    )
    .await;

    let (_, body) = send(&mut app, get_auth("/api/v1/tokens", &tok)).await;
    let tokens = body["data"].as_array().unwrap();
    assert_eq!(tokens.len(), 3);
    assert_eq!(tokens[0]["name"].as_str().unwrap(), "Third");
    assert_eq!(tokens[1]["name"].as_str().unwrap(), "Second");
    assert_eq!(tokens[2]["name"].as_str().unwrap(), "First");
}

#[tokio::test]
async fn list_tokens_sanitized_fields() {
    let (mut app, tok, _) = setup().await;
    send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Safe", "scopes": ["read"]}),
            &tok,
        ),
    )
    .await;

    let (_, body) = send(&mut app, get_auth("/api/v1/tokens", &tok)).await;
    let token = &body["data"][0];
    let obj = token.as_object().unwrap();

    assert!(obj.contains_key("id"));
    assert!(obj.contains_key("name"));
    assert!(obj.contains_key("token_prefix"));
    assert!(obj.contains_key("scopes"));
    assert!(obj.contains_key("created_at"));
    assert!(!obj.contains_key("token"), "full token must not appear");
    assert!(!obj.contains_key("token_hash"), "hash must not appear");
    assert!(!obj.contains_key("user_id"), "user_id must not appear");
}

#[tokio::test]
async fn token_with_expires_at() {
    let (mut app, tok, _) = setup().await;
    let (status, body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Expiring", "scopes": ["read"], "expires_at": "2099-12-31T00:00:00+00:00"}),
            &tok,
        ),
    )
    .await;
    assert_eq!(status, StatusCode::CREATED);
    assert_eq!(
        body["data"]["expires_at"].as_str().unwrap(),
        "2099-12-31T00:00:00Z"
    );
}

#[tokio::test]
async fn token_prefix_is_first_8_chars() {
    let (mut app, tok, _) = setup().await;
    let (_, body) = send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Prefix", "scopes": ["read"]}),
            &tok,
        ),
    )
    .await;
    let full_token = body["data"]["token"].as_str().unwrap();
    let prefix = body["data"]["token_prefix"].as_str().unwrap();
    assert_eq!(prefix, &full_token[..8]);
    assert!(prefix.starts_with("rblog_"));
}

#[tokio::test]
async fn list_requires_auth() {
    let (mut app, _, _) = setup().await;
    let (status, _) = send(&mut app, get_req("/api/v1/tokens")).await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn delete_requires_auth() {
    let (mut app, _, _) = setup().await;
    let (status, _) = send(
        &mut app,
        Request::builder()
            .method("DELETE")
            .uri("/api/v1/tokens/some-id")
            .body(Body::empty())
            .unwrap(),
    )
    .await;
    assert_eq!(status, StatusCode::UNAUTHORIZED);
}

#[tokio::test]
async fn each_user_sees_only_own_tokens() {
    let (mut app, tok, pool) = setup().await;

    let reader_hash = raisfast::services::auth::hash_password("ReaderPass123!").unwrap();
    let sql = "INSERT INTO users (username, role, status, registered_via) VALUES ('isolationreader', 'reader', 'active', 'email') RETURNING id";
    let reader_int_id: i64 = sqlx::query_scalar(sql).fetch_one(&pool).await.unwrap();
    let cred_data = serde_json::json!({"password_hash": reader_hash}).to_string();
    let cred_id = raisfast::utils::id::new_id();
    let cred_now = raisfast::utils::tz::now_utc();
    let cred_sql = format!(
        "INSERT INTO user_credentials (id, user_id, auth_type, identifier, credential_data, verified, created_at, updated_at) VALUES ({}, {}, 'email', {}, {}, 1, {}, {})",
        raisfast::db::Driver::ph(1),
        raisfast::db::Driver::ph(2),
        raisfast::db::Driver::ph(3),
        raisfast::db::Driver::ph(4),
        raisfast::db::Driver::ph(5),
        raisfast::db::Driver::ph(6)
    );
    sqlx::query(&cred_sql)
        .bind(cred_id)
        .bind(reader_int_id)
        .bind("isolation@test.com")
        .bind(&cred_data)
        .bind(cred_now)
        .bind(cred_now)
        .execute(&pool)
        .await
        .unwrap();
    let reader_tok = make_token(
        &reader_int_id.to_string(),
        reader_int_id,
        raisfast::models::user::UserRole::Reader,
    );

    send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Admin Token", "scopes": ["admin"]}),
            &tok,
        ),
    )
    .await;
    send(
        &mut app,
        post_json_auth(
            "/api/v1/tokens",
            json!({"name": "Reader Token", "scopes": ["read"]}),
            &reader_tok,
        ),
    )
    .await;

    let (_, admin_body) = send(&mut app, get_auth("/api/v1/tokens", &tok)).await;
    assert_eq!(admin_body["data"].as_array().unwrap().len(), 1);
    assert_eq!(
        admin_body["data"][0]["name"].as_str().unwrap(),
        "Admin Token"
    );

    let (_, reader_body) = send(&mut app, get_auth("/api/v1/tokens", &reader_tok)).await;
    assert_eq!(reader_body["data"].as_array().unwrap().len(), 1);
    assert_eq!(
        reader_body["data"][0]["name"].as_str().unwrap(),
        "Reader Token"
    );
}