forked from Lilly-Protocol/lily-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.rs
More file actions
190 lines (153 loc) · 5.85 KB
/
Copy pathtest.rs
File metadata and controls
190 lines (153 loc) · 5.85 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
#![allow(clippy::unwrap_used, clippy::expect_used)]
#![cfg(test)]
use soroban_sdk::symbol_short;
use soroban_sdk::testutils::{Address as _, MockAuth, MockAuthInvoke};
use soroban_sdk::{Address, Env, IntoVal};
use super::{WalletBinding, WalletContract, WalletContractClient};
use lily_common::PROTOCOL_VERSION;
use lily_test_support::{test_address, test_env};
use soroban_sdk::testutils::Events;
use soroban_sdk::{Address, TryIntoVal};
#[test]
fn returns_protocol_version() {
let env = test_env();
let contract_id = env.register(WalletContract, ());
let client = WalletContractClient::new(&env, &contract_id);
assert_eq!(client.version(), PROTOCOL_VERSION);
}
#[test]
fn binds_wallet_and_updates_policy() {
let env = test_env();
let admin = test_address(&env);
let agent = test_address(&env);
let wallet = test_address(&env);
let contract_id = env.register(WalletContract, (admin.clone(),));
let client = WalletContractClient::new(&env, &contract_id);
client.initialize(&admin);
client.bind_wallet(&agent, &wallet, &symbol_short!("USDC"), &1_000_i128);
let binding = client.get_binding(&agent);
assert_eq!(
binding,
WalletBinding {
wallet: wallet.clone(),
settlement_asset: symbol_short!("USDC"),
spend_limit: 1_000,
enabled: true,
revision: 0,
}
);
client.update_spend_limit(&agent, &2_500_i128);
client.set_enabled(&agent, &false);
let updated = client.get_binding(&agent);
assert_eq!(updated.spend_limit, 2_500);
assert!(!updated.enabled);
assert_eq!(updated.revision, 2);
}
#[test]
#[should_panic]
fn rejects_double_binding_while_active() {
let env = test_env();
let admin = test_address(&env);
let agent = test_address(&env);
let wallet = test_address(&env);
let contract_id = env.register(WalletContract, (admin.clone(),));
let client = WalletContractClient::new(&env, &contract_id);
client.initialize(&admin);
client.bind_wallet(&agent, &wallet, &symbol_short!("USDC"), &100_i128);
client.bind_wallet(&agent, &wallet, &symbol_short!("USDC"), &100_i128);
}
#[test]
#[should_panic]
fn rejects_binding_when_any_binding_exists() {
let env = test_env();
let admin = test_address(&env);
let agent = test_address(&env);
let wallet = test_address(&env);
let wallet2 = test_address(&env);
let contract_id = env.register(WalletContract, ());
let client = WalletContractClient::new(&env, &contract_id);
client.initialize(&admin);
client.bind_wallet(&agent, &wallet, &symbol_short!("USDC"), &100_i128);
client.set_enabled(&agent, &false);
// bind_wallet must fail even when the existing binding is disabled.
client.bind_wallet(&agent, &wallet2, &symbol_short!("USDC"), &200_i128);
}
#[test]
fn rebinds_disabled_binding_explicitly() {
let env = test_env();
let admin = test_address(&env);
let agent = test_address(&env);
let wallet = test_address(&env);
let new_wallet = test_address(&env);
let contract_id = env.register(WalletContract, ());
let client = WalletContractClient::new(&env, &contract_id);
client.initialize(&admin);
client.bind_wallet(&agent, &wallet, &symbol_short!("USDC"), &100_i128);
client.set_enabled(&agent, &false);
client.rebind_wallet(&agent, &new_wallet, &symbol_short!("XLM"), &500_i128);
let binding = client.get_binding(&agent);
assert_eq!(binding.wallet, new_wallet);
assert_eq!(binding.settlement_asset, symbol_short!("XLM"));
assert_eq!(binding.spend_limit, 500);
assert!(binding.enabled);
assert_eq!(binding.revision, 0);
}
#[test]
fn rebinds_enabled_binding_explicitly() {
let env = test_env();
let admin = test_address(&env);
let agent = test_address(&env);
let wallet = test_address(&env);
let new_wallet = test_address(&env);
let contract_id = env.register(WalletContract, ());
let client = WalletContractClient::new(&env, &contract_id);
client.initialize(&admin);
client.bind_wallet(&agent, &wallet, &symbol_short!("USDC"), &100_i128);
client.update_spend_limit(&agent, &200_i128);
assert_eq!(client.get_binding(&agent).revision, 1);
client.rebind_wallet(&agent, &new_wallet, &symbol_short!("USDC"), &1_000_i128);
let binding = client.get_binding(&agent);
assert_eq!(binding.wallet, new_wallet);
assert_eq!(binding.spend_limit, 1_000);
assert!(binding.enabled);
assert_eq!(binding.revision, 0);
}
#[test]
#[should_panic]
fn rejects_rebind_without_existing_binding() {
let env = test_env();
let admin = test_address(&env);
let agent = test_address(&env);
let wallet = test_address(&env);
let contract_id = env.register(WalletContract, ());
let client = WalletContractClient::new(&env, &contract_id);
client.initialize(&admin);
client.rebind_wallet(&agent, &wallet, &symbol_short!("USDC"), &100_i128);
}
#[test]
#[should_panic]
fn rejects_zero_spend_limit() {
let env = test_env();
let admin = test_address(&env);
let agent = test_address(&env);
let wallet = test_address(&env);
let contract_id = env.register(WalletContract, (admin.clone(),));
let client = WalletContractClient::new(&env, &contract_id);
client.initialize(&admin);
client.bind_wallet(&agent, &wallet, &symbol_short!("USDC"), &0_i128);
}
#[test]
fn admin_can_deactivate_wallet_binding() {
let env = test_env();
let admin = test_address(&env);
let agent = test_address(&env);
let wallet = test_address(&env);
let contract_id = env.register(WalletContract, ());
let client = WalletContractClient::new(&env, &contract_id);
client.initialize(&admin);
client.bind_wallet(&agent, &wallet, &symbol_short!("USDC"), &1_000_i128);
client.admin_deactivate(&agent);
let binding = client.get_binding(&agent);
assert!(!binding.enabled);
assert_eq!(binding.revision, 1);
}