-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_follownft.cairo
372 lines (339 loc) · 16.3 KB
/
test_follownft.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
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
// *************************************************************************
// FOLLOW NFT TEST
// *************************************************************************
use core::option::OptionTrait;
use core::result::ResultTrait;
use core::traits::TryInto;
use core::poseidon::PoseidonTrait;
use core::hash::{HashStateTrait, HashStateExTrait};
use starknet::{ContractAddress, get_block_timestamp};
use snforge_std::{
declare, start_cheat_caller_address, stop_cheat_caller_address, spy_events,
EventSpyAssertionsTrait, ContractClassTrait, DeclareResultTrait, start_cheat_block_timestamp,
stop_cheat_block_timestamp
};
use coloniz::interfaces::IFollowNFT::{IFollowNFTDispatcher, IFollowNFTDispatcherTrait};
use coloniz::follownft::follownft::Follow::{Event as FollowEvent, Followed};
use coloniz::follownft::follownft::Follow::{Event as UnfollowEvent, Unfollowed};
use coloniz::follownft::follownft::Follow::{Event as FollowerBlockedEvent, FollowerBlocked};
use coloniz::follownft::follownft::Follow::{Event as FollowerUnblockedEvent, FollowerUnblocked};
use coloniz::base::constants::types::FollowData;
use coloniz::interfaces::IERC721::{IERC721Dispatcher, IERC721DispatcherTrait};
const HUB_ADDRESS: felt252 = 24205;
const ADMIN: felt252 = 13245;
const FOLLOWED_ADDRESS: felt252 = 1234;
const FOLLOWER1: felt252 = 53453;
const FOLLOWER2: felt252 = 24252;
const FOLLOWER3: felt252 = 24552;
const FOLLOWER4: felt252 = 24262;
fn __setup__() -> ContractAddress {
let follow_nft_contract = declare("Follow").unwrap().contract_class();
let mut follow_nft_constructor_calldata = array![HUB_ADDRESS, FOLLOWED_ADDRESS, 10, 0, ADMIN];
let (follow_nft_contract_address, _) = follow_nft_contract
.deploy(@follow_nft_constructor_calldata)
.unwrap();
return (follow_nft_contract_address);
}
// *************************************************************************
// TEST
// *************************************************************************
#[test]
fn test_follower_count_on_init_is_zero() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
let follower_count = dispatcher.get_follower_count();
assert(follower_count == 0, 'invalid_follower_count');
}
#[test]
#[should_panic(expected: ('coloniz: caller is not Hub!',))]
fn test_cannot_call_follow_if_not_hub() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, FOLLOWER2.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
#[should_panic(expected: ('coloniz: caller is not Hub!',))]
fn test_cannot_call_unfollow_if_not_hub() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, FOLLOWER2.try_into().unwrap());
dispatcher.unfollow(FOLLOWER1.try_into().unwrap());
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
#[should_panic(expected: ('coloniz: already following!',))]
fn test_cannot_follow_if_already_following() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
// follow
dispatcher.follow(FOLLOWER1.try_into().unwrap());
// try to follow again
dispatcher.follow(FOLLOWER1.try_into().unwrap());
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_follow() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let follower_profile_address = dispatcher.get_follower_profile_address(follow_id);
let expected_follow_id: u256 = PoseidonTrait::new()
.update_with(follower_profile_address)
.finalize()
.try_into()
.unwrap();
assert(follow_id == expected_follow_id, 'invalid follow ID');
assert(
follower_profile_address == FOLLOWER1.try_into().unwrap(), 'invalid follower
profile'
);
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_follower_count() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
dispatcher.follow(FOLLOWER2.try_into().unwrap());
dispatcher.follow(FOLLOWER3.try_into().unwrap());
dispatcher.follow(FOLLOWER4.try_into().unwrap());
let follower_count = dispatcher.get_follower_count();
assert(follower_count == 4, 'invalid follower count');
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_is_following() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
let is_following_should_be_true = dispatcher.is_following(FOLLOWER1.try_into().unwrap());
let is_following_should_be_false = dispatcher.is_following(FOLLOWER2.try_into().unwrap());
assert(is_following_should_be_true == true, 'invalid result');
assert(is_following_should_be_false == false, 'invalid result');
}
#[test]
fn test_follow_data() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
start_cheat_block_timestamp(follow_nft_contract_address, 100);
dispatcher.follow(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let follow_data = dispatcher.get_follow_data(follow_id);
let data = FollowData {
followed_profile_address: FOLLOWED_ADDRESS.try_into().unwrap(),
follower_profile_address: FOLLOWER1.try_into().unwrap(),
follow_timestamp: 100,
block_status: false
};
assert(
follow_data.followed_profile_address == data.followed_profile_address,
'invalid followed profile'
);
assert(
follow_data.follower_profile_address == data.follower_profile_address,
'invalid follower profile'
);
assert(follow_data.follow_timestamp == data.follow_timestamp, 'invalid follow timestamp');
assert(follow_data.block_status == data.block_status, 'invalid block status');
stop_cheat_caller_address(follow_nft_contract_address);
stop_cheat_block_timestamp(follow_nft_contract_address);
}
#[test]
fn test_unfollow() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
dispatcher.follow(FOLLOWER2.try_into().unwrap());
dispatcher.unfollow(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let follower_count = dispatcher.get_follower_count();
assert(follow_id == 0, 'unfollow operation failed');
assert(follower_count == 1, 'invalid follower count');
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
#[should_panic(expected: ('coloniz: user not following!',))]
fn test_cannot_unfollow_if_not_following() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.unfollow(FOLLOWER1.try_into().unwrap());
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_process_block() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
dispatcher.process_block(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let follow_data = dispatcher.get_follow_data(follow_id);
assert(follow_data.block_status == true, 'block operation failed');
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_process_unblock() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
dispatcher.process_block(FOLLOWER1.try_into().unwrap());
dispatcher.process_unblock(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let follow_data = dispatcher.get_follow_data(follow_id);
assert(follow_data.block_status == false, 'unblock operation failed');
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_metadata() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
let nft_name = dispatcher.name();
let nft_symbol = dispatcher.symbol();
assert(nft_name == "Coloniz Followers | #10", 'invalid name');
assert(nft_symbol == "CLZ:FOLLOWERS", 'invalid symbol');
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_is_blocked() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
dispatcher.process_block(FOLLOWER1.try_into().unwrap());
assert(
dispatcher.is_blocked(FOLLOWER1.try_into().unwrap()) == true,
'incorrect value for is_blocked'
);
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_follow_mints_nft() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
let _erc721Dispatcher = IERC721Dispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let follower_profile_address = dispatcher.get_follower_profile_address(follow_id);
assert(
_erc721Dispatcher.owner_of(follow_id) == follower_profile_address,
'Follow did not mint
NFT'
);
}
#[test]
#[should_panic(expected: ('ERC721: invalid token ID',))]
fn test_unfollow_burns_nft() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
let _erc721Dispatcher = IERC721Dispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let follower_profile_address = dispatcher.get_follower_profile_address(follow_id);
assert(
_erc721Dispatcher.owner_of(follow_id) == follower_profile_address,
'Follow did not mint
NFT'
);
dispatcher.unfollow(FOLLOWER1.try_into().unwrap());
_erc721Dispatcher.owner_of(follow_id);
}
#[test]
fn test_followed_event() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
let mut spy = spy_events();
dispatcher.follow(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let follower_profile_address = dispatcher.get_follower_profile_address(follow_id);
let expected_event = FollowEvent::Followed(
Followed {
followed_address: FOLLOWED_ADDRESS.try_into().unwrap(),
follower_address: follower_profile_address,
follow_id: follow_id,
timestamp: get_block_timestamp()
}
);
spy.assert_emitted(@array![(follow_nft_contract_address, expected_event)]);
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_unfollowed_event() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
let mut spy = spy_events();
dispatcher.follow(FOLLOWER1.try_into().unwrap());
dispatcher.follow(FOLLOWER2.try_into().unwrap());
dispatcher.unfollow(FOLLOWER1.try_into().unwrap());
let unfollower_profile_address: ContractAddress = FOLLOWER1.try_into().unwrap();
let follow_id: u256 = PoseidonTrait::new()
.update_with(unfollower_profile_address)
.finalize()
.try_into()
.unwrap();
let expected_event = UnfollowEvent::Unfollowed(
Unfollowed {
unfollowed_address: FOLLOWED_ADDRESS.try_into().unwrap(),
unfollower_address: unfollower_profile_address,
follow_id: follow_id,
timestamp: get_block_timestamp()
}
);
spy.assert_emitted(@array![(follow_nft_contract_address, expected_event)]);
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_block_event() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
let mut spy = spy_events();
dispatcher.process_block(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let expected_event = FollowerBlockedEvent::FollowerBlocked(
FollowerBlocked {
followed_address: FOLLOWED_ADDRESS.try_into().unwrap(),
blocked_follower: FOLLOWER1.try_into().unwrap(),
follow_id: follow_id,
timestamp: get_block_timestamp()
}
);
spy.assert_emitted(@array![(follow_nft_contract_address, expected_event)]);
stop_cheat_caller_address(follow_nft_contract_address);
}
#[test]
fn test_unblock_event() {
let follow_nft_contract_address = __setup__();
let dispatcher = IFollowNFTDispatcher { contract_address: follow_nft_contract_address };
start_cheat_caller_address(follow_nft_contract_address, HUB_ADDRESS.try_into().unwrap());
dispatcher.follow(FOLLOWER1.try_into().unwrap());
dispatcher.process_block(FOLLOWER1.try_into().unwrap());
let mut spy = spy_events();
dispatcher.process_unblock(FOLLOWER1.try_into().unwrap());
let follow_id = dispatcher.get_follow_id(FOLLOWER1.try_into().unwrap());
let expected_event = FollowerUnblockedEvent::FollowerUnblocked(
FollowerUnblocked {
followed_address: FOLLOWED_ADDRESS.try_into().unwrap(),
unblocked_follower: FOLLOWER1.try_into().unwrap(),
follow_id: follow_id,
timestamp: get_block_timestamp()
}
);
spy.assert_emitted(@array![(follow_nft_contract_address, expected_event)]);
stop_cheat_caller_address(follow_nft_contract_address);
}