garrison 0.9.0

Garrison - 面向 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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! credential 模块测试。

use super::mock::MockCredentialRepository;
use super::*;
use crate::dao::tests::MockDao;

/// `Credential` trait 可作 `Box<dyn Credential>` 使用(对象安全编译验证:若 trait 非对象安全则无法编译)。
#[test]
fn credential_trait_is_object_safe() {
    fn _assert_object_safe(_cred: Box<dyn Credential>) {}
    fn _assert_arc_object_safe(_cred: std::sync::Arc<dyn Credential>) {}
}

/// `CredentialRepository` trait 可作 `Arc<dyn CredentialRepository>` 使用。
#[test]
fn credential_repository_trait_is_object_safe() {
    fn _assert_object_safe(_repo: std::sync::Arc<dyn CredentialRepository>) {}
}

/// `CredentialModel` serde 序列化输出包含全部 8 字段。
#[test]
fn credential_model_serializes_all_8_fields() {
    let model = CredentialModel {
        id: "cred-001".to_string(),
        user_id: "alice".to_string(),
        credential_type: "password".to_string(),
        secret_data: "$argon2id$v=19$...".to_string(),
        label: Some("主密码".to_string()),
        created_at: 1700000000,
        enabled: true,
        priority: 0,
    };
    let json = serde_json::to_string(&model).expect("序列化应成功");
    assert!(
        json.contains("\"id\":\"cred-001\""),
        "JSON 缺少 id 字段: {}",
        json
    );
    assert!(
        json.contains("\"user_id\":\"alice\""),
        "JSON 缺少 user_id 字段: {}",
        json
    );
    assert!(
        json.contains("\"credential_type\":\"password\""),
        "JSON 缺少 credential_type 字段: {}",
        json
    );
    assert!(
        json.contains("\"secret_data\""),
        "JSON 缺少 secret_data 字段: {}",
        json
    );
    assert!(
        json.contains("\"label\":\"主密码\""),
        "JSON 缺少 label 字段: {}",
        json
    );
    assert!(
        json.contains("\"created_at\":1700000000"),
        "JSON 缺少 created_at 字段: {}",
        json
    );
    assert!(
        json.contains("\"enabled\":true"),
        "JSON 缺少 enabled 字段: {}",
        json
    );
    assert!(
        json.contains("\"priority\":0"),
        "JSON 缺少 priority 字段: {}",
        json
    );
}

/// `CredentialModel` serde 反序列化可解析包含全部 8 字段的完整 JSON。
#[test]
fn credential_model_deserializes_from_full_json() {
    let json = r#"{
            "id": "cred-002",
            "user_id": "bob",
            "credential_type": "totp",
            "secret_data": "{\"secret\":\"JBSWY3DPEHPK3PXP\"}",
            "label": null,
            "created_at": 1700000001,
            "enabled": false,
            "priority": 1
        }"#;
    let model: CredentialModel = serde_json::from_str(json).expect("反序列化应成功");
    assert_eq!(model.id, "cred-002");
    assert_eq!(model.user_id, "bob");
    assert_eq!(model.credential_type, "totp");
    assert!(model.secret_data.contains("JBSWY3DPEHPK3PXP"));
    assert!(model.label.is_none(), "label 应为 None");
    assert_eq!(model.created_at, 1700000001);
    assert!(!model.enabled, "enabled 应为 false");
    assert_eq!(model.priority, 1);
}

/// `label` 字段为 `None` 时序列化为 JSON `null`。
#[test]
fn credential_model_label_none_serializes_as_null() {
    let model = CredentialModel {
        id: "cred-003".to_string(),
        user_id: "carol".to_string(),
        credential_type: "password".to_string(),
        secret_data: "hash".to_string(),
        label: None,
        created_at: 0,
        enabled: true,
        priority: 0,
    };
    let json = serde_json::to_string(&model).expect("序列化应成功");
    assert!(
        json.contains("\"label\":null"),
        "label 为 None 时应序列化为 null,实际: {}",
        json
    );
}

/// `CredentialModel` Clone 后字段一致。
#[test]
fn credential_model_clone_preserves_fields() {
    let model = CredentialModel {
        id: "cred-004".to_string(),
        user_id: "dave".to_string(),
        credential_type: "webauthn".to_string(),
        secret_data: "key".to_string(),
        label: Some("YubiKey".to_string()),
        created_at: 1700000002,
        enabled: true,
        priority: 5,
    };
    let cloned = model.clone();
    assert_eq!(cloned.id, model.id);
    assert_eq!(cloned.user_id, model.user_id);
    assert_eq!(cloned.credential_type, model.credential_type);
    assert_eq!(cloned.secret_data, model.secret_data);
    assert_eq!(cloned.label, model.label);
    assert_eq!(cloned.created_at, model.created_at);
    assert_eq!(cloned.enabled, model.enabled);
    assert_eq!(cloned.priority, model.priority);
}

/// 辅助函数:构造测试用 CredentialModel。
///
/// `secret_data` 默认取可通过导入校验门的 PHC 形态串(password 类型写入
/// DaoCredentialRepository 时须通过参数上限门)。
fn make_model(id: &str, user: &str, cred_type: &str, priority: i32) -> CredentialModel {
    CredentialModel {
        id: id.to_string(),
        user_id: user.to_string(),
        credential_type: cred_type.to_string(),
        secret_data: "$argon2id$m=8,t=1,p=1$x".to_string(),
        label: None,
        created_at: 0,
        enabled: true,
        priority,
    }
}

/// `create` + `find_by_user` 正常路径。
#[tokio::test]
async fn repository_create_and_find_by_user() {
    let repo = MockCredentialRepository::default();
    let m1 = make_model("c1", "alice", "password", 0);
    let m2 = make_model("c2", "alice", "totp", 1);
    repo.create(m1.clone()).await.unwrap();
    repo.create(m2.clone()).await.unwrap();

    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found.len(), 2);
    // 按 priority 升序:password(0) 在前,totp(1) 在后
    assert_eq!(found[0].id, "c1");
    assert_eq!(found[1].id, "c2");
}

/// `create` 重复 ID 返回错误。
#[tokio::test]
async fn repository_create_duplicate_returns_error() {
    let repo = MockCredentialRepository::default();
    let m = make_model("c1", "alice", "password", 0);
    repo.create(m).await.unwrap();
    let dup = make_model("c1", "alice", "password", 0);
    let result = repo.create(dup).await;
    assert!(result.is_err(), "重复 create 应返回错误");
}

/// `find_by_user_and_type` 按 credential_type 过滤。
#[tokio::test]
async fn repository_find_by_user_and_type_filters() {
    let repo = MockCredentialRepository::default();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c2", "alice", "totp", 1))
        .await
        .unwrap();
    repo.create(make_model("c3", "alice", "password", 2))
        .await
        .unwrap();

    let passwords = repo
        .find_by_user_and_type("alice", "password")
        .await
        .unwrap();
    assert_eq!(passwords.len(), 2);
    assert!(passwords.iter().all(|c| c.credential_type == "password"));

    let totps = repo.find_by_user_and_type("alice", "totp").await.unwrap();
    assert_eq!(totps.len(), 1);
    assert_eq!(totps[0].id, "c2");
}

/// `update` 覆盖写 + 不存在返回错误。
#[tokio::test]
async fn repository_update_overwrites_and_errors_on_missing() {
    let repo = MockCredentialRepository::default();
    let m = make_model("c1", "alice", "password", 0);
    repo.create(m).await.unwrap();

    let updated = CredentialModel {
        id: "c1".to_string(),
        user_id: "alice".to_string(),
        credential_type: "password".to_string(),
        secret_data: "new-hash".to_string(),
        label: Some("updated".to_string()),
        created_at: 100,
        enabled: false,
        priority: 5,
    };
    repo.update("alice", updated.clone()).await.unwrap();
    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found[0].secret_data, "new-hash");
    assert_eq!(found[0].label, Some("updated".to_string()));
    assert!(!found[0].enabled);
    assert_eq!(found[0].priority, 5);

    let missing = make_model("nonexistent", "alice", "password", 0);
    let result = repo.update("alice", missing).await;
    assert!(result.is_err(), "更新不存在的凭证应返回错误");
}

/// `delete` 删除 + 不存在返回错误。
#[tokio::test]
async fn repository_delete_removes_and_errors_on_missing() {
    let repo = MockCredentialRepository::default();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();

    repo.delete("alice", "c1").await.unwrap();
    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found.len(), 0, "删除后应查不到凭证");

    let result = repo.delete("alice", "c1").await;
    assert!(result.is_err(), "删除不存在的凭证应返回错误");
}

/// 多用户隔离 — 不同用户的凭证互不影响。
#[tokio::test]
async fn repository_multi_user_isolation() {
    let repo = MockCredentialRepository::default();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c2", "bob", "password", 0))
        .await
        .unwrap();

    let alice_creds = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(alice_creds.len(), 1);
    assert_eq!(alice_creds[0].id, "c1");

    let bob_creds = repo.find_by_user("bob", "bob").await.unwrap();
    assert_eq!(bob_creds.len(), 1);
    assert_eq!(bob_creds[0].id, "c2");

    let empty = repo.find_by_user("carol", "carol").await.unwrap();
    assert_eq!(empty.len(), 0);
}

/// `CredentialRepository` 可作 `Arc<dyn CredentialRepository>` 使用。
#[tokio::test]
async fn repository_usable_as_trait_object() {
    let repo: std::sync::Arc<dyn CredentialRepository> =
        std::sync::Arc::new(MockCredentialRepository::default());
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found.len(), 1);
}

/// 辅助函数:构造 DaoCredentialRepository(基于 MockDao)。
fn make_dao_repo() -> DaoCredentialRepository {
    DaoCredentialRepository::new(Arc::new(MockDao::new()))
}

/// `create` + `find_by_user` 正常路径(DAO key = `cred:{user_id}:{cred_id}`)。
#[tokio::test]
async fn dao_repo_create_and_find_by_user() {
    let repo = make_dao_repo();
    let m = make_model("c1", "alice", "password", 0);
    repo.create(m.clone()).await.unwrap();

    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].id, "c1");
    assert_eq!(found[0].user_id, "alice");
    assert_eq!(found[0].credential_type, "password");
}

/// `find_by_user` 未知用户返回空 Vec。
#[tokio::test]
async fn dao_repo_find_by_user_returns_empty_for_unknown() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();

    let found = repo.find_by_user("bob", "bob").await.unwrap();
    assert!(found.is_empty(), "未知用户应返回空 Vec");
}

/// `find_by_user_and_type` 按 `credential_type` 过滤。
#[tokio::test]
async fn dao_repo_find_by_user_and_type_filters() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c2", "alice", "totp", 1))
        .await
        .unwrap();
    repo.create(make_model("c3", "alice", "password", 2))
        .await
        .unwrap();

    let passwords = repo
        .find_by_user_and_type("alice", "password")
        .await
        .unwrap();
    assert_eq!(passwords.len(), 2);
    assert!(passwords.iter().all(|c| c.credential_type == "password"));

    let totps = repo.find_by_user_and_type("alice", "totp").await.unwrap();
    assert_eq!(totps.len(), 1);
    assert_eq!(totps[0].id, "c2");
}

/// `update` 覆盖写 + 不存在返回错误。
#[tokio::test]
async fn dao_repo_update_overwrites_and_errors_on_missing() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();

    let updated = CredentialModel {
        id: "c1".to_string(),
        user_id: "alice".to_string(),
        credential_type: "password".to_string(),
        secret_data: "$argon2id$m=8,t=1,p=1$new".to_string(),
        label: Some("updated".to_string()),
        created_at: 100,
        enabled: false,
        priority: 5,
    };
    repo.update("alice", updated.clone()).await.unwrap();
    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found[0].secret_data, "$argon2id$m=8,t=1,p=1$new");
    assert_eq!(found[0].label, Some("updated".to_string()));
    assert!(!found[0].enabled);
    assert_eq!(found[0].priority, 5);

    let missing = make_model("nonexistent", "alice", "password", 0);
    let result = repo.update("alice", missing).await;
    assert!(result.is_err(), "更新不存在的凭证应返回错误");
}

/// password 类型凭据 `create` 拒绝参数越界的导入哈希(错误显性)。
#[tokio::test]
async fn dao_repo_create_rejects_out_of_range_password_hash() {
    let repo = make_dao_repo();
    let over = CredentialModel {
        id: "c-over".to_string(),
        user_id: "alice".to_string(),
        credential_type: "password".to_string(),
        secret_data: "$argon2id$v=19$m=2097152,t=2,p=1$c2FsdDEyMzQ1Njc4$MDEyMzQ1Njc4OWFiY2RlZg"
            .to_string(),
        label: None,
        created_at: 0,
        enabled: true,
        priority: 0,
    };
    match repo.create(over).await {
        Err(GarrisonError::InvalidParam(msg)) => {
            assert!(
                msg.contains("credential-create")
                    && msg.contains(&crate::i18n::translate_detail(
                        "import-hash-argon2-m-cost",
                        &[("arg0", "2097152"), ("arg1", "1048576")],
                    )),
                "错误应含入口与越界阶段标识,实际: {}",
                msg
            );
        },
        other => panic!(
            "越界导入哈希应被 InvalidParam 拒绝,实际: {:?}",
            other.map(|_| ())
        ),
    }
}

/// password 类型凭据 `update` 同样拒绝越界导入哈希,存量值保持不变。
#[tokio::test]
async fn dao_repo_update_rejects_out_of_range_password_hash() {
    let repo = make_dao_repo();
    let original = "$argon2id$v=19$m=19456,t=2,p=1$c2FsdDEyMzQ1Njc4$MDEyMzQ1Njc4OWFiY2RlZg";
    let mut model = make_model("c-keep", "alice", "password", 0);
    model.secret_data = original.to_string();
    repo.create(model).await.unwrap();

    let over = CredentialModel {
        id: "c-keep".to_string(),
        user_id: "alice".to_string(),
        credential_type: "password".to_string(),
        secret_data: "$argon2id$v=19$m=2097152,t=2,p=1$c2FsdDEyMzQ1Njc4$MDEyMzQ1Njc4OWFiY2RlZg"
            .to_string(),
        label: None,
        created_at: 100,
        enabled: true,
        priority: 0,
    };
    match repo.update("alice", over).await {
        Err(GarrisonError::InvalidParam(msg)) => {
            assert!(
                msg.contains("credential-update")
                    && msg.contains(&crate::i18n::translate_detail(
                        "import-hash-argon2-m-cost",
                        &[("arg0", "2097152"), ("arg1", "1048576")],
                    )),
                "错误应含入口与越界阶段标识,实际: {}",
                msg
            );
        },
        other => panic!(
            "越界导入哈希应被 InvalidParam 拒绝,实际: {:?}",
            other.map(|_| ())
        ),
    }
    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found[0].secret_data, original, "被拒更新不得改动存量哈希");
}

/// 非 password 类型(TOTP seed 等)不受导入门约束——门只约束密码哈希。
#[tokio::test]
async fn dao_repo_create_skips_gate_for_non_password_types() {
    let repo = make_dao_repo();
    let mut seed = make_model("c-totp", "alice", "totp", 0);
    seed.secret_data = "JBSWY3DPEHPK3PXP".to_string();
    repo.create(seed).await.expect("TOTP seed 不应被哈希门拒绝");
}

/// `delete` 删除 + 不存在返回错误。
#[tokio::test]
async fn dao_repo_delete_removes_and_errors_on_missing() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();

    repo.delete("alice", "c1").await.unwrap();
    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert!(found.is_empty(), "删除后应查不到凭证");

    let result = repo.delete("alice", "c1").await;
    assert!(result.is_err(), "删除不存在的凭证应返回错误");
}

/// 多用户隔离 — 不同用户的凭证互不影响(DAO key 含 user_id 前缀)。
#[tokio::test]
async fn dao_repo_multi_user_isolation() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c2", "bob", "password", 0))
        .await
        .unwrap();

    let alice = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(alice.len(), 1);
    assert_eq!(alice[0].id, "c1");

    let bob = repo.find_by_user("bob", "bob").await.unwrap();
    assert_eq!(bob.len(), 1);
    assert_eq!(bob[0].id, "c2");

    let carol = repo.find_by_user("carol", "carol").await.unwrap();
    assert!(carol.is_empty());
}

/// 单用户多凭证类型(password + totp + webauthn 共存)。
#[tokio::test]
async fn dao_repo_multi_credential_types_per_user() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c2", "alice", "totp", 1))
        .await
        .unwrap();
    repo.create(make_model("c3", "alice", "webauthn", 2))
        .await
        .unwrap();

    let all = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(all.len(), 3);
    let types: Vec<&str> = all.iter().map(|c| c.credential_type.as_str()).collect();
    assert!(types.contains(&"password"));
    assert!(types.contains(&"totp"));
    assert!(types.contains(&"webauthn"));
}

/// `find_by_user` 返回含 `enabled=false` 的凭证(trait 层不过滤 enabled,业务层负责)。
#[tokio::test]
async fn dao_repo_find_returns_disabled_credentials() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    let disabled = CredentialModel {
        id: "c2".to_string(),
        user_id: "alice".to_string(),
        credential_type: "totp".to_string(),
        secret_data: "secret".to_string(),
        label: None,
        created_at: 0,
        enabled: false,
        priority: 1,
    };
    repo.create(disabled).await.unwrap();

    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(
        found.len(),
        2,
        "find_by_user 应返回 enabled + disabled 凭证"
    );
    let disabled_cred = found.iter().find(|c| c.id == "c2").unwrap();
    assert!(!disabled_cred.enabled, "disabled 凭证 enabled 应为 false");
}

/// `find_by_user` 按 `priority` 升序返回(trait 契约)。
#[tokio::test]
async fn dao_repo_priority_order_ascending() {
    let repo = make_dao_repo();
    // 故意乱序插入:priority 5, 1, 3
    repo.create(make_model("c1", "alice", "password", 5))
        .await
        .unwrap();
    repo.create(make_model("c2", "alice", "totp", 1))
        .await
        .unwrap();
    repo.create(make_model("c3", "alice", "webauthn", 3))
        .await
        .unwrap();

    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found.len(), 3);
    // 按 priority 升序:1, 3, 5
    assert_eq!(found[0].id, "c2");
    assert_eq!(found[0].priority, 1);
    assert_eq!(found[1].id, "c3");
    assert_eq!(found[1].priority, 3);
    assert_eq!(found[2].id, "c1");
    assert_eq!(found[2].priority, 5);
}

/// `create` 重复 ID 返回错误。
#[tokio::test]
async fn dao_repo_create_duplicate_returns_error() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    let dup = make_model("c1", "alice", "password", 0);
    let result = repo.create(dup).await;
    assert!(result.is_err(), "重复 create 应返回错误");
}

/// `DaoCredentialRepository` 可作 `Arc<dyn CredentialRepository>` 使用。
#[tokio::test]
async fn dao_repo_usable_as_trait_object() {
    let repo: Arc<dyn CredentialRepository> = Arc::new(make_dao_repo());
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found.len(), 1);
}

// IDOR 防护测试:find_by_user / update / delete 在 caller 与目标 owner 不一致时返回
// NotPermission(403);update 不得改变 user_id(防跨用户转移)。覆盖 Mock + DAO 两个实现。

/// 辅助:断言 err 是 NotPermission(IDOR 拒绝)。
fn assert_idor_denied(err: GarrisonError, ctx: &str) {
    assert!(
        matches!(err, GarrisonError::NotPermission(_)),
        "{} 应返回 NotPermission(IDOR 拒绝),实际: {:?}",
        ctx,
        err
    );
}

// IDOR: find_by_user 跨用户查询拒绝

/// IDOR: Mock - alice 调用 find_by_user 查询 bob 的凭证应被拒绝。
#[tokio::test]
async fn mock_find_by_user_denied_when_caller_not_target() {
    let repo = MockCredentialRepository::default();
    repo.create(make_model("c1", "bob", "password", 0))
        .await
        .unwrap();

    let err = repo
        .find_by_user("alice", "bob")
        .await
        .expect_err("alice 查询 bob 凭证应被拒绝");
    assert_idor_denied(err, "mock find_by_user alice→bob");
}

/// IDOR: DAO - alice 调用 find_by_user 查询 bob 的凭证应被拒绝。
#[tokio::test]
async fn dao_find_by_user_denied_when_caller_not_target() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "bob", "password", 0))
        .await
        .unwrap();

    let err = repo
        .find_by_user("alice", "bob")
        .await
        .expect_err("alice 查询 bob 凭证应被拒绝");
    assert_idor_denied(err, "dao find_by_user alice→bob");
}

/// IDOR: 合法 caller 查询自己的凭证应成功(正向用例)。
#[tokio::test]
async fn dao_find_by_user_succeeds_when_caller_is_owner() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c2", "alice", "totp", 1))
        .await
        .unwrap();

    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found.len(), 2, "alice 查询自己的凭证应成功返回 2 条");
}

// IDOR: delete 跨用户删除拒绝

/// IDOR: Mock - alice 尝试删除 bob 的凭证应被拒绝。
#[tokio::test]
async fn mock_delete_denied_when_caller_not_owner() {
    let repo = MockCredentialRepository::default();
    repo.create(make_model("c1", "bob", "password", 0))
        .await
        .unwrap();

    let err = repo
        .delete("alice", "c1")
        .await
        .expect_err("alice 删除 bob 的凭证应被拒绝");
    assert_idor_denied(err, "mock delete alice→bob c1");

    let remaining = repo.find_by_user("bob", "bob").await.unwrap();
    assert_eq!(remaining.len(), 1, "拒绝后 bob 的凭证应仍存在");
    assert_eq!(remaining[0].id, "c1");
}

/// IDOR: DAO - alice 尝试删除 bob 的凭证应被拒绝。
#[tokio::test]
async fn dao_delete_denied_when_caller_not_owner() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "bob", "password", 0))
        .await
        .unwrap();

    let err = repo
        .delete("alice", "c1")
        .await
        .expect_err("alice 删除 bob 的凭证应被拒绝");
    assert_idor_denied(err, "dao delete alice→bob c1");

    let remaining = repo.find_by_user("bob", "bob").await.unwrap();
    assert_eq!(remaining.len(), 1, "拒绝后 bob 的凭证应仍存在");
}

/// IDOR: 合法 caller 删除自己的凭证应成功(正向用例)。
#[tokio::test]
async fn dao_delete_succeeds_when_caller_is_owner() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();

    repo.delete("alice", "c1").await.unwrap();
    let remaining = repo.find_by_user("alice", "alice").await.unwrap();
    assert!(remaining.is_empty(), "alice 删除自己的凭证后应查不到");
}

// IDOR: update 跨用户修改拒绝

/// IDOR: Mock - alice 尝试更新 bob 的凭证应被拒绝。
///
/// forged 使用与原凭证不同的 `secret_data`("attacker-hash"),使事后断言
/// (remaining[0].secret_data 保持 make_model 的值)具备鉴别力。
#[tokio::test]
async fn mock_update_denied_when_caller_not_owner() {
    let repo = MockCredentialRepository::default();
    repo.create(make_model("c1", "bob", "password", 0))
        .await
        .unwrap();

    // alice 伪造一条 user_id=bob 的更新(典型 IDOR 攻击),携带不同的 secret_data
    let forged = CredentialModel {
        id: "c1".to_string(),
        user_id: "bob".to_string(),
        credential_type: "password".to_string(),
        secret_data: "attacker-hash".to_string(),
        label: None,
        created_at: 0,
        enabled: true,
        priority: 0,
    };
    let err = repo
        .update("alice", forged)
        .await
        .expect_err("alice 更新 bob 的凭证应被拒绝");
    assert_idor_denied(err, "mock update alice→bob c1");

    // 验证 bob 的凭证未被修改:secret_data 仍为原值(与 attacker-hash 可区分)
    let remaining = repo.find_by_user("bob", "bob").await.unwrap();
    assert_eq!(
        remaining[0].secret_data, "$argon2id$m=8,t=1,p=1$x",
        "凭证 secret_data 应未被攻击者覆盖(若为 attacker-hash 说明更新意外成功)"
    );
}

/// IDOR: DAO - alice 尝试更新 bob 的凭证应被拒绝。
#[tokio::test]
async fn dao_update_denied_when_caller_not_owner() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "bob", "password", 0))
        .await
        .unwrap();

    let forged = CredentialModel {
        id: "c1".to_string(),
        user_id: "bob".to_string(),
        credential_type: "password".to_string(),
        secret_data: "attacker-hash".to_string(),
        label: None,
        created_at: 0,
        enabled: true,
        priority: 0,
    };
    let err = repo
        .update("alice", forged)
        .await
        .expect_err("alice 更新 bob 的凭证应被拒绝");
    assert_idor_denied(err, "dao update alice→bob c1");

    let remaining = repo.find_by_user("bob", "bob").await.unwrap();
    assert_eq!(
        remaining[0].secret_data, "$argon2id$m=8,t=1,p=1$x",
        "凭证 secret_data 应未被攻击者覆盖"
    );
}

// IDOR: update 禁止 user_id 转移

/// IDOR: Mock - alice 尝试通过 update 把自己的凭证 user_id 改成 bob(跨用户转移)应被拒绝。
#[tokio::test]
async fn mock_update_denied_when_user_id_transferred() {
    let repo = MockCredentialRepository::default();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();

    // alice 把自己的凭证 user_id 改成 bob(凭证转移攻击)
    let transfer = CredentialModel {
        id: "c1".to_string(),
        user_id: "bob".to_string(), // 试图改变 owner
        credential_type: "password".to_string(),
        secret_data: "hash".to_string(),
        label: None,
        created_at: 0,
        enabled: true,
        priority: 0,
    };
    let err = repo
        .update("alice", transfer)
        .await
        .expect_err("alice 转移凭证到 bob 应被拒绝");
    assert_idor_denied(err, "mock update user_id transfer");

    let alice_creds = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(alice_creds.len(), 1, "凭证应仍属于 alice");
    assert_eq!(alice_creds[0].user_id, "alice");
}

/// IDOR: DAO - alice 尝试通过 update 把自己的凭证 user_id 改成 bob 应被拒绝。
#[tokio::test]
async fn dao_update_denied_when_user_id_transferred() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();

    let transfer = CredentialModel {
        id: "c1".to_string(),
        user_id: "bob".to_string(), // 试图改变 owner
        credential_type: "password".to_string(),
        secret_data: "hash".to_string(),
        label: None,
        created_at: 0,
        enabled: true,
        priority: 0,
    };
    let err = repo
        .update("alice", transfer)
        .await
        .expect_err("alice 转移凭证到 bob 应被拒绝");
    assert_idor_denied(err, "dao update user_id transfer");

    let alice_creds = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(alice_creds.len(), 1, "凭证应仍属于 alice");
    assert_eq!(alice_creds[0].user_id, "alice");
}

/// IDOR: 合法 caller 更新自己的凭证(不改 user_id)应成功(正向用例)。
#[tokio::test]
async fn dao_update_succeeds_when_caller_is_owner_and_user_id_unchanged() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();

    let updated = CredentialModel {
        id: "c1".to_string(),
        user_id: "alice".to_string(), // user_id 保持不变
        credential_type: "password".to_string(),
        secret_data: "$argon2id$m=8,t=1,p=1$new".to_string(),
        label: Some("updated".to_string()),
        created_at: 100,
        enabled: false,
        priority: 5,
    };
    repo.update("alice", updated).await.unwrap();

    let found = repo.find_by_user("alice", "alice").await.unwrap();
    assert_eq!(found[0].secret_data, "$argon2id$m=8,t=1,p=1$new");
    assert_eq!(found[0].label, Some("updated".to_string()));
    assert!(!found[0].enabled);
    assert_eq!(found[0].priority, 5);
}

// IDOR: trait object 动态分发也生效

/// IDOR: 通过 `Arc<dyn CredentialRepository>` 调用也应执行 IDOR 校验。
#[tokio::test]
async fn trait_object_enforces_idor_on_delete() {
    let repo: Arc<dyn CredentialRepository> = Arc::new(make_dao_repo());
    repo.create(make_model("c1", "bob", "password", 0))
        .await
        .unwrap();

    let err = repo
        .delete("alice", "c1")
        .await
        .expect_err("trait object: alice 删除 bob 的凭证应被拒绝");
    assert_idor_denied(err, "trait object delete alice→bob");
}

/// IDOR: 通过 `Arc<dyn CredentialRepository>` 调用 find_by_user 也应执行校验。
#[tokio::test]
async fn trait_object_enforces_idor_on_find_by_user() {
    let repo: Arc<dyn CredentialRepository> = Arc::new(make_dao_repo());
    repo.create(make_model("c1", "bob", "password", 0))
        .await
        .unwrap();

    let err = repo
        .find_by_user("alice", "bob")
        .await
        .expect_err("trait object: alice 查询 bob 凭证应被拒绝");
    assert_idor_denied(err, "trait object find_by_user alice→bob");
}

// IDOR: find_by_user_and_type / create 覆盖缺口

/// IDOR: `find_by_user_and_type` 严格按 user_id 隔离,不泄露他人凭证。
///
/// 签名无 `caller_login_id`(认证上下文内部受信接口,见 trait 文档「安全语义」),
/// caller-vs-owner 拒绝路径不存在;此处验证可测的安全性质——查询结果严格限定在给定
/// user_id 内(alice/bob 各自的 password 凭证互不可见)。
#[tokio::test]
async fn mock_find_by_user_and_type_isolated_per_user() {
    let repo = MockCredentialRepository::default();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c2", "bob", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c3", "bob", "totp", 1))
        .await
        .unwrap();

    // bob 的 password 查询只返回 bob 的凭证(不泄露 alice)
    let bob_passwords = repo.find_by_user_and_type("bob", "password").await.unwrap();
    assert_eq!(
        bob_passwords.len(),
        1,
        "bob 的 password 查询应只含 bob 的凭证"
    );
    assert_eq!(bob_passwords[0].id, "c2");
    assert!(
        bob_passwords.iter().all(|c| c.user_id == "bob"),
        "find_by_user_and_type 不得返回其他用户的凭证(跨用户泄露)"
    );

    let alice_passwords = repo
        .find_by_user_and_type("alice", "password")
        .await
        .unwrap();
    assert_eq!(alice_passwords.len(), 1);
    assert_eq!(alice_passwords[0].id, "c1");
}

/// IDOR: DAO 实现 — `find_by_user_and_type` 按 user_id 隔离(同上,DAO 层)。
#[tokio::test]
async fn dao_find_by_user_and_type_isolated_per_user() {
    let repo = make_dao_repo();
    repo.create(make_model("c1", "alice", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c2", "bob", "password", 0))
        .await
        .unwrap();
    repo.create(make_model("c3", "bob", "totp", 1))
        .await
        .unwrap();

    let bob_passwords = repo.find_by_user_and_type("bob", "password").await.unwrap();
    assert_eq!(
        bob_passwords.len(),
        1,
        "DAO: bob 的 password 查询应只含 bob 的凭证"
    );
    assert_eq!(bob_passwords[0].id, "c2");

    // carol 无凭证 → 空结果,不报错也不泄露他人凭证
    let carol = repo
        .find_by_user_and_type("carol", "password")
        .await
        .unwrap();
    assert!(carol.is_empty(), "无凭证用户应返回空 Vec");
}

/// IDOR: `create` 是受信边界 API——仓库层不做 caller 校验(设计文档化测试)。
///
/// `create(credential)` 签名无 caller 参数,IDOR 边界在调用方:`CredentialModel.user_id`
/// 必须由服务端从认证会话构造,绝不接受外部输入的 user_id 直传(否则即跨用户创建/提权);
/// 仓库层以 `set_if_absent` 保证 key(`cred:{user_id}:{cred_id}`)唯一性。本测试固化该行为。
#[tokio::test]
async fn dao_create_is_trusted_boundary_stores_model_as_given() {
    let repo = make_dao_repo();
    // 服务端代码以 bob 的身份构造 model(合法场景:注册流程)
    repo.create(make_model("c9", "bob", "password", 0))
        .await
        .unwrap();

    // 仅 owner(bob)可通过 find_by_user 查到该凭证;alice 查询 bob 被 IDOR 拒绝
    let bob_creds = repo.find_by_user("bob", "bob").await.unwrap();
    assert_eq!(bob_creds.len(), 1);
    assert_eq!(bob_creds[0].id, "c9");

    let err = repo
        .find_by_user("alice", "bob")
        .await
        .expect_err("alice 查询 bob 凭证应被拒绝");
    assert_idor_denied(err, "create trusted boundary: alice→bob find_by_user");
}

/// Argon2Hasher::default() 参数锁定:m=19456 KiB / t=2 / p=1(OWASP 建议最低档);
/// 默认策略无声漂移(降参数、换算法)会被本测试阻断。
#[test]
fn argon2_default_policy_is_owasp_baseline() {
    // 字段为模块私有,此处仅锁定默认构造行为的可观测 PHC 输出
    let hasher = crate::account::credential::password::Argon2Hasher::default();
    let hash = hasher.hash("policy-lock-probe").unwrap();
    assert!(
        hash.starts_with("$argon2id$v=19$m=19456,t=2,p=1$"),
        "默认策略应为 Argon2id m=19456/t=2/p=1,实际: {}",
        &hash[..hash.len().min(48)]
    );
}

/// hash 输出 PHC 字符串锁定:算法标识 Argon2id + 版本 v=19 + 默认参数。
#[test]
fn argon2_hash_phc_prefix_locks_algorithm_and_params() {
    let hasher = crate::account::credential::password::Argon2Hasher::default();
    let hash = hasher.hash("correct horse battery staple").unwrap();
    assert!(
        hash.starts_with("$argon2id$v=19$m=19456,t=2,p=1$"),
        "PHC 前缀应为 $argon2id$v=19$m=19456,t=2,p=1$,实际: {}",
        &hash[..hash.len().min(48)]
    );
}