better-auth-api 1.0.0-alpha.5

Plugin implementations for better-auth
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
#![allow(
    clippy::expect_used,
    reason = "Fixture setup and response assertions require successful fallible construction."
)]
use super::*;

#[path = "verification_permissions_tests.rs"]
mod permissions;

#[tokio::test]
async fn cached_json_null_uses_database_fallback_or_rejects_without_consuming_usage() {
    use better_auth_core::store::{MemoryCacheAdapter, SecondaryStorage};

    for fallback_to_database in [false, true] {
        let cache = Arc::new(MemoryCacheAdapter::new());
        let plugin = ApiKeyPlugin::with_config(ApiKeyConfig {
            storage: ApiKeyStorage::SecondaryStorage,
            custom_storage: Some(cache.clone()),
            fallback_to_database,
            defer_updates: false,
            ..Default::default()
        });
        let (ctx, _, session) = create_test_context_with_user().await;
        let (id, key) = create_key_and_get_raw(
            &plugin,
            &ctx,
            session.token.typed().unwrap(),
            serde_json::json!({"name":"null-cache"}),
        )
        .await;
        let config = plugin.resolve_configuration(None).unwrap();
        let hash = ApiKeyPlugin::hash_key(&key);
        let cache_key = format!("api-key:{hash}");
        let id_key = format!("api-key:by-id:{id}");
        let alias = cache.get(&id_key).await.unwrap();
        cache.set(&cache_key, "null", None).await.unwrap();
        let result = plugin
            .verify_api_key(
                &VerifyApiKey {
                    key: &key,
                    config_id: None,
                    permissions: None,
                },
                &ctx,
            )
            .await;
        if fallback_to_database {
            let verified = result.unwrap();
            assert_eq!(verified.id, id);
            assert_eq!(verified.request_count, Some(1.0));
            let restored = storage::get_by_hash(config, &ctx, &hash)
                .await
                .unwrap()
                .unwrap();
            assert_eq!(restored.id, id);
            assert_eq!(restored.request_count, verified.request_count);
            assert_eq!(
                cache.get(&cache_key).await.unwrap(),
                cache.get(&id_key).await.unwrap()
            );
            assert_eq!(
                ctx.database
                    .get_api_key_by_id(&id)
                    .await
                    .unwrap()
                    .unwrap()
                    .request_count,
                verified.request_count
            );
        } else {
            assert!(matches!(
                result,
                Err(ApiKeyVerificationError::Validation(ApiKeyValidationError {
                    code: ApiKeyErrorCode::InvalidApiKey,
                    ..
                }))
            ));
            assert_eq!(
                cache.get(&cache_key).await.unwrap(),
                Some(serde_json::Value::String("null".into()))
            );
            assert_eq!(cache.get(&id_key).await.unwrap(), alias);
            assert!(ctx.database.get_api_key_by_id(&id).await.unwrap().is_none());
        }
    }
}

#[tokio::test]
async fn cached_dynamic_enabled_values_control_verification_before_consuming_usage() {
    use better_auth_core::{
        FieldValue, SchemaValue,
        store::{MemoryCacheAdapter, SecondaryStorage},
    };
    let cache = Arc::new(MemoryCacheAdapter::new());
    let plugin = ApiKeyPlugin::with_config(ApiKeyConfig {
        storage: ApiKeyStorage::SecondaryStorage,
        custom_storage: Some(cache.clone()),
        defer_updates: false,
        rate_limit: RateLimitDefaults {
            enabled: false,
            ..Default::default()
        },
        ..Default::default()
    });
    let (ctx, _, session) = create_test_context_with_user().await;
    let (id, key) = create_key_and_get_raw(
        &plugin,
        &ctx,
        session.token.typed().unwrap(),
        serde_json::json!({"name":"dynamic-enabled"}),
    )
    .await;
    let configuration = plugin.resolve_configuration(None).unwrap();
    let seed = storage::get_by_hash(configuration, &ctx, &ApiKeyPlugin::hash_key(&key))
        .await
        .unwrap()
        .unwrap();
    let cache_key = format!("api-key:{}", seed.key_hash.display_string().unwrap());
    for (value, enabled) in [
        (FieldValue::Undefined, true),
        (FieldValue::Null, true),
        (false.into(), false),
        (true.into(), true),
        (0.0.into(), true),
        (5.25.into(), true),
        ("".into(), true),
        ("0".into(), true),
        (FieldValue::from_json(serde_json::json!({})).unwrap(), true),
        (FieldValue::from_json(serde_json::json!([])).unwrap(), true),
    ] {
        let mut row = seed.clone();
        row.enabled = SchemaValue::from_field(value.clone());
        row.remaining = Some(2.0).into();
        storage::put(cache.clone(), &row, false).await.unwrap();
        let before = cache.get(&cache_key).await.unwrap();
        let result = plugin
            .verify_api_key(
                &VerifyApiKey {
                    key: &key,
                    config_id: None,
                    permissions: None,
                },
                &ctx,
            )
            .await;
        if enabled {
            let verified = result.unwrap();
            assert_eq!(verified.enabled.field_value(), value);
            assert_eq!(verified.remaining, Some(1.0));
        } else {
            assert!(matches!(
                result,
                Err(ApiKeyVerificationError::Validation(ApiKeyValidationError {
                    code: ApiKeyErrorCode::KeyDisabled,
                    ..
                }))
            ));
            assert_eq!(
                cache.get(&cache_key).await.unwrap(),
                before,
                "Disabled keys must not consume quota or change cache state"
            );
        }
        let restored = storage::get_by_hash(configuration, &ctx, seed.key_hash.typed().unwrap())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(restored.enabled.field_value(), value);
        assert_eq!(restored.remaining, Some(if enabled { 1.0 } else { 2.0 }));
        let encoded = cache.get(&cache_key).await.unwrap().unwrap();
        let encoded: serde_json::Value = serde_json::from_str(encoded.as_str().unwrap()).unwrap();
        assert_eq!(encoded.get("enabled"), value.json().unwrap().as_ref());
    }
    assert!(ctx.database.get_api_key_by_id(&id).await.unwrap().is_none());
}

#[tokio::test]
async fn scoped_verification_uses_the_configuration_hashing_and_rejects_other_configs() {
    let plugin = ApiKeyPlugin::builder().build().configuration(ApiKeyConfig {
        config_id: "machines".to_owned(),
        disable_key_hashing: true,
        ..Default::default()
    });
    let (ctx, _, session) = create_test_context_with_user().await;
    let (id, key) = create_key_and_get_raw(
        &plugin,
        &ctx,
        session.token.typed().unwrap(),
        serde_json::json!({ "configId": "machines", "name": "machine" }),
    )
    .await;
    let scoped = VerifyApiKey {
        key: &key,
        config_id: Some("machines"),
        permissions: None,
    };
    let verified = plugin.verify_api_key(&scoped, &ctx).await.unwrap();
    assert_eq!(verified.id, id);
    assert_eq!(verified.config_id, "machines");
    let serialized = serde_json::to_value(verified).unwrap();
    assert!(serialized.get("key").is_none());
    assert!(serialized.get("keyHash").is_none());

    for config_id in [None, Some("default"), Some("unknown")] {
        let error = plugin
            .verify_api_key(
                &VerifyApiKey {
                    config_id,
                    ..scoped
                },
                &ctx,
            )
            .await
            .unwrap_err();
        assert!(matches!(
            error,
            ApiKeyVerificationError::Validation(ApiKeyValidationError {
                code: ApiKeyErrorCode::InvalidApiKey,
                ..
            })
        ));
    }
    assert_eq!(
        ctx.database
            .get_api_key_by_id(&id)
            .await
            .unwrap()
            .unwrap()
            .request_count,
        Some(1.0)
    );
}

#[tokio::test]
async fn unscoped_verification_uses_the_issuing_configuration_limits() {
    let plugin = ApiKeyPlugin::builder().build().configuration(ApiKeyConfig {
        config_id: "machines".to_owned(),
        rate_limit: RateLimitDefaults {
            enabled: false,
            ..Default::default()
        },
        ..Default::default()
    });
    let (ctx, _, session) = create_test_context_with_user().await;
    let (id, key) = create_key_and_get_raw(
        &plugin,
        &ctx,
        session.token.typed().unwrap(),
        serde_json::json!({ "configId": "machines" }),
    )
    .await;
    ctx.database
        .update_api_key(
            &id.clone().into(),
            UpdateApiKey {
                rate_limit_enabled: Some(true),
                rate_limit_max: Some(1.0),
                ..Default::default()
            },
        )
        .await
        .unwrap();

    let input = VerifyApiKey {
        key: &key,
        config_id: None,
        permissions: None,
    };
    for _ in 0..3 {
        assert_eq!(plugin.verify_api_key(&input, &ctx).await.unwrap().id, id);
    }
    let error = plugin
        .verify_api_key(
            &VerifyApiKey {
                config_id: Some("default"),
                ..input
            },
            &ctx,
        )
        .await
        .unwrap_err();
    assert!(matches!(
        error,
        ApiKeyVerificationError::Validation(ApiKeyValidationError {
            code: ApiKeyErrorCode::InvalidApiKey,
            ..
        })
    ));
    assert_eq!(
        ctx.database
            .get_api_key_by_id(&id)
            .await
            .unwrap()
            .unwrap()
            .request_count,
        Some(0.0)
    );
}

#[tokio::test]
async fn permissions_failure_preserves_usage_and_rate_limit_reports_retry_time() {
    let plugin = ApiKeyPlugin::builder().build();
    let (ctx, _, session) = create_test_context_with_user().await;
    let (id, key) = create_key_with_server_fields(
        &plugin,
        &ctx,
        session.token.typed().unwrap(),
        serde_json::json!({}),
        UpdateApiKey {
            remaining: Some(3.0),
            permissions: Some(serde_json::json!({ "nodes": ["read"] }).to_string()),
            rate_limit_max: Some(1.0),
            rate_limit_time_window: Some(60_000.0),
            ..Default::default()
        },
    )
    .await;
    let denied = serde_json::json!({ "nodes": ["delete"] });
    let allowed = serde_json::json!({ "nodes": ["read"] });
    let input = VerifyApiKey {
        key: &key,
        config_id: None,
        permissions: Some(&denied),
    };
    let error = plugin.verify_api_key(&input, &ctx).await.unwrap_err();
    assert!(matches!(
        error,
        ApiKeyVerificationError::Validation(ApiKeyValidationError {
            code: ApiKeyErrorCode::KeyNotFound,
            ..
        })
    ));
    assert_eq!(
        ctx.database
            .get_api_key_by_id(&id)
            .await
            .unwrap()
            .unwrap()
            .remaining,
        Some(3.0)
    );
    let input = VerifyApiKey {
        permissions: Some(&allowed),
        ..input
    };
    assert_eq!(
        plugin.verify_api_key(&input, &ctx).await.unwrap().remaining,
        Some(2.0)
    );
    let error = plugin.verify_api_key(&input, &ctx).await.unwrap_err();
    let ApiKeyVerificationError::Validation(error) = error else {
        panic!("Expected rate limit rejection")
    };
    assert_eq!(error.code, ApiKeyErrorCode::RateLimited);
    let retry = error.details.unwrap().try_again_in;
    assert!(retry > 0.0 && retry <= 60_000.0);
    assert_eq!(
        ctx.database
            .get_api_key_by_id(&id)
            .await
            .unwrap()
            .unwrap()
            .remaining,
        Some(1.0)
    );
}

#[tokio::test]
async fn session_header_selects_a_named_config_without_a_default() {
    let plugin = ApiKeyPlugin::builder()
        .config_id("machines".to_owned())
        .api_key_headers(vec![
            "x-machine-key".to_owned(),
            "x-alternate-key".to_owned(),
        ])
        .enable_session_for_api_keys(true)
        .build();
    let (ctx, user, session) = create_test_context_with_user().await;
    let (_, key) = create_key_and_get_raw(
        &plugin,
        &ctx,
        session.token.typed().unwrap(),
        serde_json::json!({ "configId": "machines" }),
    )
    .await;
    let mut request = create_auth_request(HttpMethod::Get, "/get-session", None, None, None);
    request
        .headers
        .insert("x-alternate-key".to_owned(), key.clone());
    let result = plugin
        .before_request(&request, &ctx)
        .await
        .unwrap()
        .unwrap();
    let BeforeRequestAction::Respond(response) = result else {
        panic!("Expected virtual session")
    };
    assert_eq!(response.status, 200);
    let body = json_body(&response);
    assert_eq!(body["session"]["token"], key);
    assert_eq!(body["session"]["userId"], user.id.typed().unwrap().as_str());
    assert_eq!(body["user"]["emailVerified"], false);
    assert!(body["session"]["createdAt"].is_string());
    assert!(body["session"]["expiresAt"].is_string());
}

#[tokio::test]
async fn session_header_cannot_authenticate_a_different_configuration() {
    let plugin = ApiKeyPlugin::builder()
        .enable_session_for_api_keys(true)
        .build()
        .configuration(ApiKeyConfig {
            config_id: "machines".to_owned(),
            api_key_headers: vec!["x-machine-key".to_owned()],
            enable_session_for_api_keys: true,
            ..Default::default()
        });
    let (ctx, _, session) = create_test_context_with_user().await;
    let (id, key) = create_key_and_get_raw(
        &plugin,
        &ctx,
        session.token.typed().unwrap(),
        serde_json::json!({ "configId": "machines" }),
    )
    .await;
    let mut request = create_auth_request(HttpMethod::Get, "/get-session", None, None, None);
    request.headers.insert("x-api-key".to_owned(), key);
    let error = plugin.before_request(&request, &ctx).await.unwrap_err();
    assert_eq!(error.status_code().unwrap(), 401);
    assert_eq!(
        error.body_value().unwrap().json().unwrap(),
        Some(serde_json::json!({
            "code":"INVALID_API_KEY",
            "message":"Invalid API key."
        }))
    );
    assert_eq!(
        ctx.database
            .get_api_key_by_id(&id)
            .await
            .unwrap()
            .unwrap()
            .request_count,
        Some(0.0)
    );
}

#[tokio::test]
async fn organization_key_verifies_without_emulating_a_user_session() {
    let plugin = ApiKeyPlugin::builder()
        .references(ApiKeyReferences::Organization)
        .enable_session_for_api_keys(true)
        .build();
    let (ctx, user, _) = create_test_context_with_user().await;
    let (key, key_hash) = ApiKeyPlugin::generate_key(&ApiKeyConfig::default(), None);
    let start = better_auth_core::ApiKeyStart::prefix(
        &key,
        ApiKeyConfig::default().starting_characters_length,
    );
    ctx.database
        .create_api_key(better_auth_core::CreateApiKey {
            additional_fields: Default::default(),
            // A colliding user ID must not turn an organization key into a user session.
            reference_id: user.id.clone(),
            config_id: "default".to_owned(),
            key_hash,
            start: Some(start),
            enabled: true.into(),
            name: None.into(),
            prefix: None,
            expires_at: None,
            remaining: None,
            rate_limit_enabled: false,
            rate_limit_time_window: None,
            rate_limit_max: None,
            refill_interval: None,
            refill_amount: None,
            permissions: None.into(),
            metadata: None,
        })
        .await
        .unwrap();
    plugin
        .verify_api_key(
            &VerifyApiKey {
                key: &key,
                config_id: None,
                permissions: None,
            },
            &ctx,
        )
        .await
        .unwrap();
    let mut request = create_auth_request(HttpMethod::Get, "/get-session", None, None, None);
    request.headers.insert("x-api-key".to_owned(), key);
    let error = plugin.before_request(&request, &ctx).await.unwrap_err();
    assert_eq!(error.status_code().unwrap(), 401);
    assert_eq!(
        error.body_value().unwrap().json().unwrap(),
        Some(serde_json::json!({
            "code":"INVALID_REFERENCE_ID_FROM_API_KEY",
            "message":"The reference id from the API key is invalid."
        }))
    );
}

#[tokio::test]
async fn api_key_initialization_rejects_ambiguous_configurations() {
    let (ctx, _, _) = create_test_context_with_user().await;
    for config_id in ["", "default"] {
        let plugin = ApiKeyPlugin::builder().build().configuration(ApiKeyConfig {
            config_id: config_id.to_owned(),
            ..Default::default()
        });
        let mut init =
            better_auth_core::AuthInitContext::new(ctx.config.clone(), ctx.database.clone())
                .expect("fixture context initializes");
        assert!(matches!(
            plugin.on_init(&mut init).await,
            Err(AuthError::Config(_))
        ));
    }
}

#[tokio::test]
async fn verification_preserves_database_failures() {
    let connection = better_auth_seaorm::Database::connect("sqlite::memory:")
        .await
        .unwrap();
    connection.clone().close().await.unwrap();
    let config = Arc::new(crate::plugins::test_helpers::create_test_config());
    let database = Arc::new(better_auth_seaorm::SeaOrmStore::<TestSchema>::new(
        config.clone(),
        connection,
    ));
    let ctx = AuthContext::new(config, database).expect("fixture context initializes");
    let plugin = ApiKeyPlugin::builder().build();
    let error = plugin
        .verify_api_key(
            &VerifyApiKey {
                key: "not-looked-up",
                config_id: None,
                permissions: None,
            },
            &ctx,
        )
        .await
        .unwrap_err();
    assert!(matches!(
        error,
        ApiKeyVerificationError::Internal(AuthError::Database(_))
    ));
}

#[tokio::test]
async fn verified_session_authenticates_a_protected_plugin_route_without_a_database_session() {
    let plugin = ApiKeyPlugin::builder()
        .enable_session_for_api_keys(true)
        .build();
    let (ctx, user, session) = create_test_context_with_user().await;
    let (id, key) = create_key_and_get_raw(
        &plugin,
        &ctx,
        session.token.typed().unwrap(),
        serde_json::json!({}),
    )
    .await;
    let mut request = create_auth_request(HttpMethod::Get, "/api-key/list", None, None, None);
    request.headers.insert("x-api-key".to_owned(), key.clone());
    let action = plugin
        .before_request(&request, &ctx)
        .await
        .unwrap()
        .unwrap();
    let BeforeRequestAction::InjectSession { session } = action else {
        panic!("Expected virtual session")
    };
    assert_eq!(session.token, key);
    request.set_virtual_session(*session);
    let response = plugin.on_request(&request, &ctx).await.unwrap().unwrap();
    assert_eq!(response.status, 200);
    assert_eq!(json_body(&response)["apiKeys"][0]["id"], id);
    assert_eq!(
        ctx.database
            .get_user_sessions(user.id.typed().unwrap())
            .await
            .unwrap()
            .len(),
        1
    );
}

#[tokio::test]
async fn secondary_usage_retains_dynamic_counter_arithmetic_and_strict_rate_limit_flags() {
    use better_auth_core::{FieldValue, SchemaValue, store::MemoryCacheAdapter};
    let cache = Arc::new(MemoryCacheAdapter::new());
    let plugin = ApiKeyPlugin::with_config(ApiKeyConfig {
        storage: ApiKeyStorage::SecondaryStorage,
        custom_storage: Some(cache.clone()),
        defer_updates: false,
        ..Default::default()
    });
    let (ctx, _, session) = create_test_context_with_user().await;
    let (_, raw_key) = create_key_and_get_raw(
        &plugin,
        &ctx,
        session.token.typed().unwrap(),
        serde_json::json!({}),
    )
    .await;
    let config = plugin.resolve_configuration(None).unwrap();
    let seed = storage::get_by_hash(config, &ctx, &ApiKeyPlugin::hash_key(&raw_key))
        .await
        .unwrap()
        .unwrap();
    for (flag, limited) in [
        (FieldValue::Bool(false), false),
        (FieldValue::Null, true),
        (FieldValue::Undefined, true),
        (0.0.into(), true),
    ] {
        let mut key = seed.clone();
        key.rate_limit_enabled = SchemaValue::from_field(flag);
        key.remaining = SchemaValue::from_field("2".into());
        key.request_count = SchemaValue::from_field("1".into());
        key.rate_limit_max = SchemaValue::from_field("0".into());
        key.rate_limit_time_window = Some(1_000_000.0).into();
        key.last_request = Some(chrono::Utc::now().into()).into();
        storage::put(cache.clone(), &key, false).await.unwrap();
        let input = VerifyApiKey {
            key: &raw_key,
            config_id: None,
            permissions: None,
        };
        let result = plugin.verify_api_key(&input, &ctx).await;
        if limited {
            assert!(matches!(
                result,
                Err(ApiKeyVerificationError::Validation(ApiKeyValidationError {
                    code: ApiKeyErrorCode::RateLimited,
                    ..
                }))
            ));
            let stored = storage::get_by_hash(config, &ctx, seed.key_hash.typed().unwrap())
                .await
                .unwrap()
                .unwrap();
            assert_eq!(stored.remaining.field_value(), "2".into());
        } else {
            let verified = result.unwrap();
            assert_eq!(verified.remaining, Some(1.0));
            assert_eq!(verified.request_count.field_value(), "1".into());
        }
    }
    let mut key = seed;
    key.rate_limit_enabled = SchemaValue::from_field(FieldValue::Null);
    key.remaining = SchemaValue::from_field("2".into());
    key.request_count = SchemaValue::from_field("1".into());
    key.rate_limit_max = SchemaValue::from_field("3".into());
    key.rate_limit_time_window = Some(1_000_000.0).into();
    key.last_request = Some(chrono::Utc::now().into()).into();
    storage::put(cache.clone(), &key, false).await.unwrap();
    let verified = plugin
        .verify_api_key(
            &VerifyApiKey {
                key: &raw_key,
                config_id: None,
                permissions: None,
            },
            &ctx,
        )
        .await
        .unwrap();
    assert_eq!(verified.remaining, Some(1.0));
    assert_eq!(verified.request_count.field_value(), "11".into());
}