forked from Liquifact/Liquifact-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_allowlist_tests.rs
More file actions
921 lines (786 loc) · 27.6 KB
/
Copy pathtest_allowlist_tests.rs
File metadata and controls
921 lines (786 loc) · 27.6 KB
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
use super::{
AllowlistEnabledChanged, DataKey, EscrowError, InvestorAllowlistChanged, LiquifactEscrow,
LiquifactEscrowClient,
};
use soroban_sdk::Vec as SorobanVec;
use soroban_sdk::{symbol_short, testutils::Address as _, Address, Env, Error, Event, InvokeError};
use std::fmt::Debug;
fn deploy(env: &Env) -> LiquifactEscrowClient<'_> {
let id = env.register(LiquifactEscrow, ());
LiquifactEscrowClient::new(env, &id)
}
fn init(env: &Env, client: &LiquifactEscrowClient) -> (Address, Address) {
let admin = Address::generate(env);
let sme = Address::generate(env);
let token = Address::generate(env);
let treasury = Address::generate(env);
client.init(
&admin,
&soroban_sdk::String::from_str(env, "ALINV001"),
&sme,
&10_000i128,
&800i64,
&0u64,
&token,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
(admin, sme)
}
// --- defaults ---
#[test]
fn test_allowlist_disabled_by_default() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
assert!(!client.is_allowlist_active());
}
#[test]
fn test_is_allowlisted_false_by_default() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let stranger = Address::generate(&env);
assert!(!client.is_investor_allowlisted(&stranger));
}
// --- enable / disable ---
#[test]
fn test_enable_and_disable_allowlist() {
use soroban_sdk::testutils::Events as _;
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let invoice_id = client.get_escrow().invoice_id;
let contract_id = client.address.clone();
client.set_allowlist_active(&true);
let enabled_events = env.events().all();
env.as_contract(&contract_id, || {
assert!(
env.storage()
.instance()
.get::<DataKey, bool>(&DataKey::AllowlistActive)
== Some(true)
);
});
client.set_allowlist_active(&false);
let disabled_events = env.events().all();
env.as_contract(&contract_id, || {
assert!(
env.storage()
.instance()
.get::<DataKey, bool>(&DataKey::AllowlistActive)
== Some(false)
);
});
assert_eq!(
enabled_events,
std::vec![AllowlistEnabledChanged {
name: symbol_short!("al_ena"),
invoice_id: invoice_id.clone(),
active: 1,
}
.to_xdr(&env, &contract_id)]
);
assert_eq!(
disabled_events,
std::vec![AllowlistEnabledChanged {
name: symbol_short!("al_ena"),
invoice_id,
active: 0,
}
.to_xdr(&env, &contract_id)]
);
}
#[test]
#[should_panic]
fn test_enable_allowlist_requires_admin_auth() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
env.mock_auths(&[]);
client.set_allowlist_active(&true);
}
#[test]
#[should_panic]
fn test_disable_allowlist_requires_admin_auth() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
client.set_allowlist_active(&true);
env.mock_auths(&[]);
client.set_allowlist_active(&false);
}
// --- add / remove ---
#[test]
fn test_add_and_remove_from_allowlist() {
use soroban_sdk::testutils::Events as _;
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let invoice_id = client.get_escrow().invoice_id;
let contract_id = client.address.clone();
let investor = Address::generate(&env);
client.set_investor_allowlisted(&investor, &true);
let added_events = env.events().all();
env.as_contract(&contract_id, || {
assert!(
env.storage()
.persistent()
.get::<DataKey, bool>(&DataKey::InvestorAllowlisted(investor.clone()))
== Some(true)
);
});
client.set_investor_allowlisted(&investor, &false);
let removed_events = env.events().all();
env.as_contract(&contract_id, || {
assert!(
env.storage()
.persistent()
.get::<DataKey, bool>(&DataKey::InvestorAllowlisted(investor.clone()))
== Some(false)
);
});
assert_eq!(
added_events,
std::vec![InvestorAllowlistChanged {
name: symbol_short!("al_set"),
invoice_id: invoice_id.clone(),
investor: investor.clone(),
allowed: 1,
}
.to_xdr(&env, &contract_id)]
);
assert_eq!(
removed_events,
std::vec![InvestorAllowlistChanged {
name: symbol_short!("al_set"),
invoice_id,
investor: investor.clone(),
allowed: 0,
}
.to_xdr(&env, &contract_id)]
);
}
#[test]
#[should_panic]
fn test_add_to_allowlist_requires_admin_auth() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
env.mock_auths(&[]);
client.set_investor_allowlisted(&investor, &true);
}
#[test]
#[should_panic]
fn test_remove_from_allowlist_requires_admin_auth() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
client.set_investor_allowlisted(&investor, &true);
env.mock_auths(&[]);
client.set_investor_allowlisted(&investor, &false);
}
#[test]
fn test_remove_non_existent_address_is_noop() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let stranger = Address::generate(&env);
// Should not panic.
client.set_investor_allowlisted(&stranger, &false);
assert!(!client.is_investor_allowlisted(&stranger));
}
// --- fund gating ---
#[test]
fn test_fund_allowed_when_allowlist_disabled() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
// Allowlist off — anyone can fund.
let escrow = client.fund(&investor, &5_000i128);
assert_eq!(escrow.funded_amount, 5_000i128);
}
#[test]
fn test_fund_with_commitment_allowed_when_allowlist_disabled() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
// Allowlist off — anyone can fund with commitment.
let escrow = client.fund_with_commitment(&investor, &5_000i128, &0u64);
assert_eq!(escrow.funded_amount, 5_000i128);
}
#[test]
fn test_fund_allowed_when_on_allowlist() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &true);
let escrow = client.fund(&investor, &5_000i128);
assert_eq!(escrow.funded_amount, 5_000i128);
}
#[test]
#[should_panic]
fn test_fund_blocked_when_not_on_allowlist() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.fund(&investor, &1_000i128);
}
#[test]
#[should_panic]
fn test_fund_with_commitment_blocked_when_not_on_allowlist() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.fund_with_commitment(&investor, &1_000i128, &0u64);
}
#[test]
fn test_fund_with_commitment_allowed_when_on_allowlist() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &true);
let escrow = client.fund_with_commitment(&investor, &5_000i128, &0u64);
assert_eq!(escrow.funded_amount, 5_000i128);
}
#[test]
fn test_fund_allowed_after_disable_even_without_entry() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_allowlist_active(&false);
// Gate is off — investor not in list but can still fund.
let escrow = client.fund(&investor, &3_000i128);
assert_eq!(escrow.funded_amount, 3_000i128);
}
#[test]
fn test_entries_persist_across_disable_reenable() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &true);
client.set_allowlist_active(&false);
// Entry still there even while disabled.
assert!(client.is_investor_allowlisted(&investor));
// Re-enable — investor can still fund without re-adding.
client.set_allowlist_active(&true);
let escrow = client.fund(&investor, &2_000i128);
assert_eq!(escrow.funded_amount, 2_000i128);
}
#[test]
#[should_panic]
fn test_removed_investor_blocked_after_reenable() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &true);
client.set_investor_allowlisted(&investor, &false);
client.fund(&investor, &1_000i128);
}
#[test]
fn test_multiple_investors_independent_allowlist_entries() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let a = Address::generate(&env);
let b = Address::generate(&env);
let c = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&a, &true);
client.set_investor_allowlisted(&b, &true);
assert!(client.is_investor_allowlisted(&a));
assert!(client.is_investor_allowlisted(&b));
assert!(!client.is_investor_allowlisted(&c));
client.fund(&a, &3_000i128);
client.fund(&b, &3_000i128);
let blocked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
client.fund(&c, &1_000i128);
}));
assert!(blocked.is_err());
}
#[test]
fn test_batch_add_and_remove_from_allowlist() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let a = Address::generate(&env);
let b = Address::generate(&env);
let c = Address::generate(&env);
let mut v: SorobanVec<Address> = SorobanVec::new(&env);
v.push_back(a.clone());
v.push_back(b.clone());
v.push_back(c.clone());
client.set_investors_allowlisted(&v, &true);
assert!(client.is_investor_allowlisted(&a));
assert!(client.is_investor_allowlisted(&b));
assert!(client.is_investor_allowlisted(&c));
client.set_investors_allowlisted(&v, &false);
assert!(!client.is_investor_allowlisted(&a));
assert!(!client.is_investor_allowlisted(&b));
assert!(!client.is_investor_allowlisted(&c));
}
#[test]
#[should_panic]
fn test_batch_rejects_empty_vector() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let v: SorobanVec<Address> = SorobanVec::new(&env);
client.set_investors_allowlisted(&v, &true);
}
#[test]
#[should_panic]
fn test_batch_rejects_too_large_vector() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let mut v: SorobanVec<Address> = SorobanVec::new(&env);
let cap = super::MAX_INVESTOR_ALLOWLIST_BATCH as usize;
for _ in 0..(cap + 1) {
v.push_back(Address::generate(&env));
}
client.set_investors_allowlisted(&v, &true);
}
#[test]
#[should_panic]
fn test_batch_requires_admin_auth() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init(&env, &client);
let a = Address::generate(&env);
let mut v: SorobanVec<Address> = SorobanVec::new(&env);
v.push_back(a.clone());
env.mock_auths(&[]);
client.set_investors_allowlisted(&v, &true);
}
// =============================================================================
// Funding-gate enforcement matrix
//
// The tests above cover setter behaviour and basic should_panic guards.
// This section adds **typed-error** assertions for every gate combination so
// that:
// - Both `fund` and `fund_with_commitment` are verified to return the exact
// error code `InvestorNotAllowlisted` (104) when the gate blocks them.
// - Successful paths return the correct funded_amount.
// - Revocation mid-funding is validated: a prior contribution does not
// exempt an investor from the gate on their next deposit.
// - Disabling the allowlist after enabling it lets any address fund,
// regardless of whether they have an entry.
// =============================================================================
/// Assert that a `try_*` client call returns the expected typed contract error.
///
/// Works for both `Err(Ok(Error))` (normal SDK encoding) and
/// `Err(Err(InvokeError::Contract(code)))` (raw host encoding) to stay
/// resilient across Soroban SDK minor version differences.
fn assert_contract_error_gate<T, E>(
result: Result<Result<T, E>, Result<Error, InvokeError>>,
expected: EscrowError,
) where
T: Debug,
E: Debug,
{
let code = expected as u32;
match result {
Err(Ok(err)) => assert_eq!(err, Error::from_contract_error(code)),
Err(Err(InvokeError::Contract(c))) => assert_eq!(c, code),
other => panic!("expected ContractError({code}), got {other:?}"),
}
}
// ---------------------------------------------------------------------------
// Helper: initialise an escrow backed by a fresh address-stub token.
// Returns (admin, sme). The caller has already called `env.mock_all_auths()`.
// ---------------------------------------------------------------------------
fn init_gate(env: &Env, client: &LiquifactEscrowClient) -> (Address, Address) {
let admin = Address::generate(env);
let sme = Address::generate(env);
let token = Address::generate(env);
let treasury = Address::generate(env);
client.init(
&admin,
&soroban_sdk::String::from_str(env, "GATEINV01"),
&sme,
&100_000i128,
&800i64,
&0u64,
&token,
&None,
&treasury,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None,
&None::<i64>,
);
(admin, sme)
}
// ---------------------------------------------------------------------------
// Gate matrix: fund
// ---------------------------------------------------------------------------
/// Gate inactive, no entry → fund succeeds (gate bypassed).
#[test]
fn gate_fund_allowlist_inactive_no_entry_succeeds() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
// Allowlist is off by default — no entry required.
let escrow = client.fund(&investor, &1_000i128);
assert_eq!(escrow.funded_amount, 1_000i128);
}
/// Gate inactive, entry explicitly set to false → fund still succeeds (gate bypassed).
#[test]
fn gate_fund_allowlist_inactive_entry_false_succeeds() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
// Write an explicit `false` entry while gate is off — should have no effect.
client.set_investor_allowlisted(&investor, &false);
let escrow = client.fund(&investor, &2_000i128);
assert_eq!(escrow.funded_amount, 2_000i128);
}
/// Gate active, investor allowlisted → fund succeeds.
#[test]
fn gate_fund_allowlist_active_investor_allowed_succeeds() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &true);
let escrow = client.fund(&investor, &5_000i128);
assert_eq!(escrow.funded_amount, 5_000i128);
}
/// Gate active, investor NOT allowlisted (absent entry) → fund returns `InvestorNotAllowlisted`.
#[test]
fn gate_fund_allowlist_active_investor_absent_returns_typed_error() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
// No call to set_investor_allowlisted — entry is absent → default-to-deny.
assert_contract_error_gate(
client.try_fund(&investor, &1_000i128),
EscrowError::InvestorNotAllowlisted,
);
}
/// Gate active, investor explicitly set to false → fund returns `InvestorNotAllowlisted`.
#[test]
fn gate_fund_allowlist_active_investor_denied_returns_typed_error() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &false);
assert_contract_error_gate(
client.try_fund(&investor, &1_000i128),
EscrowError::InvestorNotAllowlisted,
);
}
// ---------------------------------------------------------------------------
// Gate matrix: fund_with_commitment
// ---------------------------------------------------------------------------
/// Gate inactive, no entry → fund_with_commitment succeeds.
#[test]
fn gate_fwc_allowlist_inactive_no_entry_succeeds() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
let escrow = client.fund_with_commitment(&investor, &3_000i128, &0u64);
assert_eq!(escrow.funded_amount, 3_000i128);
}
/// Gate active, investor allowlisted → fund_with_commitment succeeds.
#[test]
fn gate_fwc_allowlist_active_investor_allowed_succeeds() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &true);
let escrow = client.fund_with_commitment(&investor, &4_000i128, &0u64);
assert_eq!(escrow.funded_amount, 4_000i128);
}
/// Gate active, investor NOT allowlisted (absent) → fund_with_commitment returns
/// `InvestorNotAllowlisted`.
#[test]
fn gate_fwc_allowlist_active_investor_absent_returns_typed_error() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
assert_contract_error_gate(
client.try_fund_with_commitment(&investor, &1_000i128, &0u64),
EscrowError::InvestorNotAllowlisted,
);
}
/// Gate active, investor explicitly denied → fund_with_commitment returns
/// `InvestorNotAllowlisted`.
#[test]
fn gate_fwc_allowlist_active_investor_denied_returns_typed_error() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &false);
assert_contract_error_gate(
client.try_fund_with_commitment(&investor, &1_000i128, &0u64),
EscrowError::InvestorNotAllowlisted,
);
}
// ---------------------------------------------------------------------------
// Toggle mid-funding
// ---------------------------------------------------------------------------
/// Disabling the gate after enabling it allows a previously-blocked investor
/// to fund without any allowlist entry.
#[test]
fn gate_disable_mid_funding_unblocks_any_investor() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
client.set_allowlist_active(&true);
// Confirm investor is blocked while gate is active.
assert_contract_error_gate(
client.try_fund(&investor, &500i128),
EscrowError::InvestorNotAllowlisted,
);
// Disable gate — investor should now succeed without an entry.
client.set_allowlist_active(&false);
let escrow = client.fund(&investor, &500i128);
assert_eq!(escrow.funded_amount, 500i128);
}
/// Re-enabling the gate after disabling it blocks investors without entries again.
#[test]
fn gate_reenable_after_disable_blocks_unenrolled_investor() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
// Gate off → fund succeeds.
let escrow = client.fund(&investor, &1_000i128);
assert_eq!(escrow.funded_amount, 1_000i128);
// Enable gate without allowlisting this investor.
client.set_allowlist_active(&true);
// Second deposit blocked — prior contribution does not exempt the investor.
assert_contract_error_gate(
client.try_fund(&investor, &500i128),
EscrowError::InvestorNotAllowlisted,
);
}
// ---------------------------------------------------------------------------
// Revocation mid-funding
// ---------------------------------------------------------------------------
/// Security invariant: revoking an investor mid-funding prevents their next
/// deposit even though they have an existing contribution.
///
/// A prior contribution does NOT grant a bypass — the gate checks the current
/// allowlist status on every call, not historical access.
#[test]
fn gate_revoke_mid_funding_blocks_next_deposit_fund() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
// Step 1: allowlist active, investor allowed — first deposit succeeds.
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &true);
let after_first = client.fund(&investor, &3_000i128);
assert_eq!(after_first.funded_amount, 3_000i128);
assert_eq!(client.get_contribution(&investor), 3_000i128);
// Step 2: admin revokes the investor.
client.set_investor_allowlisted(&investor, &false);
assert!(!client.is_investor_allowlisted(&investor));
// Step 3: second deposit must be rejected with InvestorNotAllowlisted.
assert_contract_error_gate(
client.try_fund(&investor, &1_000i128),
EscrowError::InvestorNotAllowlisted,
);
// Contribution must not have changed.
assert_eq!(client.get_contribution(&investor), 3_000i128);
}
/// Same revocation invariant for `fund_with_commitment`. Because
/// `fund_with_commitment` is first-deposit-only, this test verifies that
/// a revoked investor is blocked before they can make even their first tiered
/// deposit after the gate is re-enabled.
#[test]
fn gate_revoke_before_first_fwc_deposit_blocks_it() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let investor = Address::generate(&env);
// Allowlist investor, then revoke before they deposit.
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&investor, &true);
client.set_investor_allowlisted(&investor, &false);
assert_contract_error_gate(
client.try_fund_with_commitment(&investor, &5_000i128, &0u64),
EscrowError::InvestorNotAllowlisted,
);
assert_eq!(client.get_contribution(&investor), 0i128);
}
// ---------------------------------------------------------------------------
// Multiple-investor independence
// ---------------------------------------------------------------------------
/// Allowlisting investor A does not affect investor B. Both are independently
/// gated, and investor C (never added) is rejected.
#[test]
fn gate_multiple_investors_independent_gating() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let a = Address::generate(&env);
let b = Address::generate(&env);
let c = Address::generate(&env);
client.set_allowlist_active(&true);
client.set_investor_allowlisted(&a, &true);
client.set_investor_allowlisted(&b, &true);
// c is never added.
// A and B succeed.
let after_a = client.fund(&a, &2_000i128);
assert_eq!(after_a.funded_amount, 2_000i128);
let after_b = client.fund(&b, &3_000i128);
assert_eq!(after_b.funded_amount, 5_000i128);
// C is rejected.
assert_contract_error_gate(
client.try_fund(&c, &1_000i128),
EscrowError::InvestorNotAllowlisted,
);
// Contributions of A and B are unchanged after C's rejection.
assert_eq!(client.get_contribution(&a), 2_000i128);
assert_eq!(client.get_contribution(&b), 3_000i128);
assert_eq!(client.get_contribution(&c), 0i128);
}
// ---------------------------------------------------------------------------
// Batch allowlist + gate interaction
// ---------------------------------------------------------------------------
/// Batch-allowlisting investors and then enabling the gate lets every batch
/// member fund; addresses outside the batch are rejected.
#[test]
fn gate_batch_allowlist_then_gate_active_correct_access() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let a = Address::generate(&env);
let b = Address::generate(&env);
let outsider = Address::generate(&env);
let mut batch: SorobanVec<Address> = SorobanVec::new(&env);
batch.push_back(a.clone());
batch.push_back(b.clone());
client.set_allowlist_active(&true);
client.set_investors_allowlisted(&batch, &true);
// Batch members succeed.
client.fund(&a, &1_000i128);
client.fund(&b, &1_000i128);
// Outsider is rejected.
assert_contract_error_gate(
client.try_fund(&outsider, &1_000i128),
EscrowError::InvestorNotAllowlisted,
);
}
/// Batch-revoking investors while the gate is active immediately blocks them.
#[test]
fn gate_batch_revoke_blocks_all_revoked_members() {
let env = Env::default();
env.mock_all_auths();
let client = deploy(&env);
init_gate(&env, &client);
let a = Address::generate(&env);
let b = Address::generate(&env);
let mut batch: SorobanVec<Address> = SorobanVec::new(&env);
batch.push_back(a.clone());
batch.push_back(b.clone());
client.set_allowlist_active(&true);
client.set_investors_allowlisted(&batch, &true);
// Fund once successfully.
client.fund(&a, &1_000i128);
client.fund(&b, &1_000i128);
// Batch-revoke both.
client.set_investors_allowlisted(&batch, &false);
// Both are now blocked.
assert_contract_error_gate(
client.try_fund(&a, &500i128),
EscrowError::InvestorNotAllowlisted,
);
assert_contract_error_gate(
client.try_fund(&b, &500i128),
EscrowError::InvestorNotAllowlisted,
);
// Contributions remain at the pre-revocation values.
assert_eq!(client.get_contribution(&a), 1_000i128);
assert_eq!(client.get_contribution(&b), 1_000i128);
}