-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_hub.cairo
296 lines (250 loc) · 11.2 KB
/
test_hub.cairo
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
// *************************************************************************
// HUB TEST
// *************************************************************************
use core::option::OptionTrait;
use core::starknet::SyscallResultTrait;
use core::result::ResultTrait;
use core::traits::{TryInto, Into};
use starknet::{ContractAddress};
use snforge_std::{
declare, DeclareResultTrait, ContractClassTrait, start_cheat_caller_address,
stop_cheat_caller_address
};
use coloniz::interfaces::IHub::{IHubDispatcher, IHubDispatcherTrait};
use coloniz::interfaces::IHandle::{IHandleDispatcher, IHandleDispatcherTrait};
use coloniz::interfaces::IHandleRegistry::{
IHandleRegistryDispatcher, IHandleRegistryDispatcherTrait
};
use coloniz::base::constants::types::{
ProfileVariants, AccessoryVariants, FaceVariants, ClothVariants, BackgroundVariants,
BodyVariants, ToolVariants
};
const ADMIN: felt252 = 13245;
const ADDRESS1: felt252 = 1234;
const ADDRESS2: felt252 = 53453;
const ADDRESS3: felt252 = 24252;
const ADDRESS4: felt252 = 24552;
const TEST_LOCAL_NAME: felt252 = 'user';
const OWNER: felt252 = 2357;
// *************************************************************************
// SETUP
// *************************************************************************
fn __setup__() -> (ContractAddress, ContractAddress, ContractAddress, ContractAddress, u256) {
// deploy NFT
let nft_class_hash = declare("ColonizNFT").unwrap().contract_class();
let mut calldata: Array<felt252> = array![ADMIN];
let (nft_contract_address, _) = nft_class_hash.deploy(@calldata).unwrap_syscall();
// deploy handle contract
let handle_class_hash = declare("Handles").unwrap().contract_class();
let mut calldata: Array<felt252> = array![ADMIN];
let (handle_contract_address, _) = handle_class_hash.deploy(@calldata).unwrap_syscall();
// deploy handle registry contract
let handle_registry_class_hash = declare("HandleRegistry").unwrap().contract_class();
let mut calldata: Array<felt252> = array![handle_contract_address.into(), ADMIN];
let (handle_registry_contract_address, _) = handle_registry_class_hash
.deploy(@calldata)
.unwrap_syscall();
// deploy tokenbound registry
let registry_class_hash = declare("Registry").unwrap().contract_class();
let (registry_contract_address, _) = registry_class_hash.deploy(@array![]).unwrap_syscall();
// declare tokenbound account
let account_class_hash = declare("AccountV3").unwrap().contract_class();
// declare follownft
let follow_nft_classhash = declare("Follow").unwrap().contract_class();
// declare community_nft
let community_nft_classhash = declare("CommunityNFT").unwrap().contract_class();
// declare collectnft
// let collect_nft_classhash = declare("CollectNFT").unwrap().contract_class();
// deploy hub contract
let hub_class_hash = declare("ColonizHub").unwrap().contract_class();
let mut calldata: Array<felt252> = array![
nft_contract_address.into(),
handle_contract_address.into(),
handle_registry_contract_address.into(),
(*follow_nft_classhash.class_hash).into(),
(*community_nft_classhash.class_hash).into(),
// (*collect_nft_classhash.class_hash).into(),
OWNER
];
let (hub_contract_address, _) = hub_class_hash.deploy(@calldata).unwrap_syscall();
// create profiles
let dispatcher = IHubDispatcher { contract_address: hub_contract_address };
start_cheat_caller_address(hub_contract_address, ADDRESS1.try_into().unwrap());
let profile_variant = ProfileVariants {
body: BodyVariants::BODY1,
tool: ToolVariants::TOOL1,
background: BackgroundVariants::BACKGROUND1,
cloth: ClothVariants::CLOTH1,
face: FaceVariants::FACE1,
accessory: AccessoryVariants::ACCESSORY1
};
let user_one_profile_address = dispatcher
.create_profile(
registry_contract_address,
(*account_class_hash.class_hash).into(),
2478,
profile_variant
);
stop_cheat_caller_address(hub_contract_address);
start_cheat_caller_address(hub_contract_address, ADDRESS2.try_into().unwrap());
let user_two_profile_address = dispatcher
.create_profile(
registry_contract_address,
(*account_class_hash.class_hash).into(),
2478,
profile_variant
);
stop_cheat_caller_address(hub_contract_address);
start_cheat_caller_address(hub_contract_address, ADDRESS3.try_into().unwrap());
let user_three_profile_address = dispatcher
.create_profile(
registry_contract_address,
(*account_class_hash.class_hash).into(),
2478,
profile_variant
);
stop_cheat_caller_address(hub_contract_address);
// mint and link handle for user_one
let handleDispatcher = IHandleDispatcher { contract_address: handle_contract_address };
let handleRegistryDispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_contract_address
};
let minted_handle_id = handleDispatcher.mint_handle(TEST_LOCAL_NAME, user_one_profile_address);
start_cheat_caller_address(handle_registry_contract_address, user_one_profile_address);
handleRegistryDispatcher.link(minted_handle_id, user_one_profile_address);
stop_cheat_caller_address(handle_registry_contract_address);
return (
hub_contract_address,
user_one_profile_address,
user_two_profile_address,
user_three_profile_address,
minted_handle_id
);
}
// *************************************************************************
// TEST
// *************************************************************************
#[test]
fn test_hub_following() {
let (
hub_contract_address,
user_one_profile_address,
user_two_profile_address,
user_three_profile_address,
_
) =
__setup__();
let dispatcher = IHubDispatcher { contract_address: hub_contract_address };
start_cheat_caller_address(hub_contract_address, user_one_profile_address);
let profiles_to_follow: Array<ContractAddress> = array![
user_two_profile_address, user_three_profile_address
];
dispatcher.follow(profiles_to_follow);
let follow_status_1 = dispatcher
.is_following(user_two_profile_address, user_one_profile_address);
let follow_status_2 = dispatcher
.is_following(user_three_profile_address, user_one_profile_address);
assert(follow_status_1 == true, 'invalid follow status');
assert(follow_status_2 == true, 'invalid follow status');
stop_cheat_caller_address(hub_contract_address);
}
#[test]
#[should_panic(expected: ('coloniz: invalid profile_addr!',))]
fn test_hub_following_fails_if_any_profile_is_invalid() {
let (hub_contract_address, user_one_profile_address, _, user_three_profile_address, _) =
__setup__();
let dispatcher = IHubDispatcher { contract_address: hub_contract_address };
start_cheat_caller_address(hub_contract_address, user_one_profile_address);
let profiles_to_follow: Array<ContractAddress> = array![
ADDRESS4.try_into().unwrap(), user_three_profile_address
];
dispatcher.follow(profiles_to_follow);
stop_cheat_caller_address(hub_contract_address);
}
#[test]
#[should_panic(expected: ('coloniz: self_follow forbidden',))]
fn test_hub_following_fails_if_profile_is_self_following() {
let (hub_contract_address, user_one_profile_address, _, _, _) = __setup__();
let dispatcher = IHubDispatcher { contract_address: hub_contract_address };
start_cheat_caller_address(hub_contract_address, user_one_profile_address);
let profiles_to_follow: Array<ContractAddress> = array![user_one_profile_address];
dispatcher.follow(profiles_to_follow);
stop_cheat_caller_address(hub_contract_address);
}
#[test]
fn test_hub_unfollowing() {
let (
hub_contract_address,
user_one_profile_address,
user_two_profile_address,
user_three_profile_address,
_
) =
__setup__();
let dispatcher = IHubDispatcher { contract_address: hub_contract_address };
// first follow the profiles
start_cheat_caller_address(hub_contract_address, user_one_profile_address);
let profiles_to_follow: Array<ContractAddress> = array![
user_two_profile_address, user_three_profile_address
];
dispatcher.follow(profiles_to_follow);
// then unfollow them
let profiles_to_unfollow: Array<ContractAddress> = array![
user_two_profile_address, user_three_profile_address
];
dispatcher.unfollow(profiles_to_unfollow);
stop_cheat_caller_address(hub_contract_address);
// check following status
let follow_status_1 = dispatcher
.is_following(user_two_profile_address, user_one_profile_address);
let follow_status_2 = dispatcher
.is_following(user_three_profile_address, user_one_profile_address);
assert(follow_status_1 == false, 'invalid follow status');
assert(follow_status_2 == false, 'invalid follow status');
}
#[test]
fn test_set_block_status() {
let (
hub_contract_address,
user_one_profile_address,
user_two_profile_address,
user_three_profile_address,
_
) =
__setup__();
let dispatcher = IHubDispatcher { contract_address: hub_contract_address };
// follow action
start_cheat_caller_address(hub_contract_address, user_one_profile_address);
dispatcher.follow(array![user_two_profile_address, user_three_profile_address]);
stop_cheat_caller_address(hub_contract_address);
// block action
start_cheat_caller_address(hub_contract_address, user_two_profile_address);
let profiles_to_block: Array<ContractAddress> = array![user_one_profile_address];
dispatcher.set_block_status(profiles_to_block, true);
// check block status
let block_status_1 = dispatcher.is_blocked(user_two_profile_address, user_one_profile_address);
assert(block_status_1 == true, 'invalid block status');
stop_cheat_caller_address(hub_contract_address);
// unblock action
start_cheat_caller_address(hub_contract_address, user_two_profile_address);
let profiles_to_unblock: Array<ContractAddress> = array![user_one_profile_address];
dispatcher.set_block_status(profiles_to_unblock, false);
// check block status
let block_status_2 = dispatcher.is_blocked(user_two_profile_address, user_one_profile_address);
assert(block_status_2 == false, 'invalid block status');
stop_cheat_caller_address(hub_contract_address);
}
#[test]
fn test_get_handle_id() {
let (hub_contract_address, user_one_profile_address, _, _, minted_handle_id) = __setup__();
let dispatcher = IHubDispatcher { contract_address: hub_contract_address };
let handle_id = dispatcher.get_handle_id(user_one_profile_address);
assert(handle_id == minted_handle_id, 'invalid handle id');
}
#[test]
fn test_get_handle() {
let (hub_contract_address, _, _, _, minted_handle_id) = __setup__();
let dispatcher = IHubDispatcher { contract_address: hub_contract_address };
let handle = dispatcher.get_handle(minted_handle_id);
assert(handle == "user.clz", 'invalid handle id');
}