-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_handle_registry.cairo
261 lines (217 loc) · 10.5 KB
/
test_handle_registry.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
use core::option::OptionTrait;
use core::starknet::SyscallResultTrait;
use core::result::ResultTrait;
use core::traits::{TryInto, Into};
use starknet::{ContractAddress, get_block_timestamp};
use snforge_std::{
declare, start_cheat_caller_address, stop_cheat_caller_address, start_cheat_block_timestamp,
stop_cheat_block_timestamp, spy_events, EventSpyAssertionsTrait, ContractClassTrait,
DeclareResultTrait
};
use coloniz::interfaces::IHandleRegistry::{
IHandleRegistryDispatcher, IHandleRegistryDispatcherTrait
};
use coloniz::interfaces::IHandle::{IHandleDispatcher, IHandleDispatcherTrait};
use coloniz::namespaces::handle_registry::HandleRegistry::{Event as LinkedEvent, HandleLinked};
use coloniz::namespaces::handle_registry::HandleRegistry::{Event as UnlinkedEvent, HandleUnlinked};
const ADMIN_ADDRESS: felt252 = 'ADMIN';
const USER_ONE: felt252 = 'BOB';
const USER_TWO: felt252 = 'JOHN';
const TEST_LOCAL_NAME: felt252 = 'user';
const PROFILE_ADDRESS: felt252 = 'PROFILE';
const HANDLE_ADDRESS: felt252 = 'HANDLE';
const PROFILE_ID: u256 = 1234;
const HANDLE_ID: u256 = 1234;
fn __setup__() -> (ContractAddress, ContractAddress) {
// deploy handle contract
let handle_class_hash = declare("Handles").unwrap().contract_class();
let mut calldata: Array<felt252> = array![ADMIN_ADDRESS];
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_ADDRESS];
let (handle_registry_contract_address, _) = handle_registry_class_hash
.deploy(@calldata)
.unwrap_syscall();
return (handle_registry_contract_address, handle_contract_address);
}
// *************************************************************************
// TEST
// *************************************************************************
#[test]
#[should_panic(expected: ('coloniz: handle does not exist!',))]
fn test_cannot_resolve_if_handle_does_not_exist() {
let (handle_registry_contract_address, _) = __setup__();
let dispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_contract_address
};
dispatcher.resolve(1234);
}
#[test]
fn test_resolve() {
let (handle_registry_contract_address, handle_contract_address) = __setup__();
// Initialize Dispatchers
let handle_registry_dispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_contract_address
};
let handle_dispatcher = IHandleDispatcher { contract_address: handle_contract_address };
// Mint Handle to USER_ONE
start_cheat_caller_address(handle_contract_address, USER_ONE.try_into().unwrap());
let token_id = handle_dispatcher.mint_handle(TEST_LOCAL_NAME, USER_ONE.try_into().unwrap());
// Link handle to USER_ONE
handle_registry_dispatcher.link(token_id, USER_ONE.try_into().unwrap());
assert(
handle_registry_dispatcher.resolve(token_id) == USER_ONE.try_into().unwrap(),
'INCORRECT PROFILE ID'
);
stop_cheat_caller_address(handle_contract_address);
}
#[test]
fn test_link() {
let (handle_registry_address, handle_contract_address) = __setup__();
let registryDispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_address
};
let handleDispatcher = IHandleDispatcher { contract_address: handle_contract_address };
// mint handle
start_cheat_caller_address(handle_contract_address, USER_ONE.try_into().unwrap());
let handle_id = handleDispatcher.mint_handle(TEST_LOCAL_NAME, USER_ONE.try_into().unwrap());
// link token to profile
start_cheat_caller_address(handle_registry_address, USER_ONE.try_into().unwrap());
registryDispatcher.link(handle_id, USER_ONE.try_into().unwrap());
// check profile was linked
let retrieved_handle = registryDispatcher.get_handle(USER_ONE.try_into().unwrap());
assert(retrieved_handle == handle_id, 'linking failed');
stop_cheat_caller_address(handle_registry_address);
}
#[test]
#[should_panic(expected: ('coloniz: profile is not owner!',))]
fn test_linking_fails_if_profile_address_is_not_owner() {
let (handle_registry_address, handle_contract_address) = __setup__();
let registryDispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_address
};
let handleDispatcher = IHandleDispatcher { contract_address: handle_contract_address };
// mint handle
start_cheat_caller_address(handle_contract_address, USER_TWO.try_into().unwrap());
let handle_id = handleDispatcher.mint_handle(TEST_LOCAL_NAME, USER_TWO.try_into().unwrap());
stop_cheat_caller_address(handle_contract_address);
// link token to profile
start_cheat_caller_address(handle_registry_address, USER_ONE.try_into().unwrap());
registryDispatcher.link(handle_id, USER_ONE.try_into().unwrap());
stop_cheat_caller_address(handle_registry_address);
}
#[test]
#[should_panic(expected: ('coloniz: handle already linked!',))]
fn test_does_not_link_twice_for_same_handle() {
let (handle_registry_address, handle_contract_address) = __setup__();
let registryDispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_address
};
let handleDispatcher = IHandleDispatcher { contract_address: handle_contract_address };
// mint handle
start_cheat_caller_address(handle_contract_address, USER_ONE.try_into().unwrap());
let handle_id = handleDispatcher.mint_handle(TEST_LOCAL_NAME, USER_ONE.try_into().unwrap());
// link token to profile
start_cheat_caller_address(handle_registry_address, USER_ONE.try_into().unwrap());
registryDispatcher.link(handle_id, USER_ONE.try_into().unwrap());
// try linking again
registryDispatcher.link(handle_id, USER_ONE.try_into().unwrap());
stop_cheat_caller_address(handle_registry_address);
}
#[test]
fn test_unlink() {
let (handle_registry_address, handle_contract_address) = __setup__();
let registryDispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_address
};
let handleDispatcher = IHandleDispatcher { contract_address: handle_contract_address };
// mint handle
start_cheat_caller_address(handle_contract_address, USER_ONE.try_into().unwrap());
let handle_id = handleDispatcher.mint_handle(TEST_LOCAL_NAME, USER_ONE.try_into().unwrap());
stop_cheat_caller_address(handle_contract_address);
// link token to profile
start_cheat_caller_address(handle_registry_address, USER_ONE.try_into().unwrap());
registryDispatcher.link(handle_id, USER_ONE.try_into().unwrap());
// call unlink
registryDispatcher.unlink(handle_id, USER_ONE.try_into().unwrap());
// check it unlinks successfully
let retrieved_handle = registryDispatcher.get_handle(USER_ONE.try_into().unwrap());
assert(retrieved_handle == 0, 'unlinking failed');
stop_cheat_caller_address(handle_registry_address);
}
#[test]
#[should_panic(expected: ('coloniz: caller is not owner!',))]
fn test_unlink_fails_if_caller_is_not_owner() {
let (handle_registry_address, handle_contract_address) = __setup__();
let registryDispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_address
};
let handleDispatcher = IHandleDispatcher { contract_address: handle_contract_address };
// mint handle
start_cheat_caller_address(handle_contract_address, USER_ONE.try_into().unwrap());
let handle_id = handleDispatcher.mint_handle(TEST_LOCAL_NAME, USER_ONE.try_into().unwrap());
stop_cheat_caller_address(handle_contract_address);
// link token to profile
start_cheat_caller_address(handle_registry_address, USER_TWO.try_into().unwrap());
registryDispatcher.link(handle_id, USER_ONE.try_into().unwrap());
// call unlink
registryDispatcher.unlink(handle_id, USER_ONE.try_into().unwrap());
stop_cheat_caller_address(handle_registry_address);
}
#[test]
fn test_linked_event_emission() {
let (handle_registry_address, handle_contract_address) = __setup__();
let registryDispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_address
};
let handleDispatcher = IHandleDispatcher { contract_address: handle_contract_address };
let mut spy = spy_events();
// mint handle
start_cheat_caller_address(handle_contract_address, USER_ONE.try_into().unwrap());
let handle_id = handleDispatcher.mint_handle(TEST_LOCAL_NAME, USER_ONE.try_into().unwrap());
stop_cheat_caller_address(handle_contract_address);
// link token to profile
start_cheat_caller_address(handle_registry_address, USER_ONE.try_into().unwrap());
start_cheat_block_timestamp(handle_registry_address, 40);
registryDispatcher.link(handle_id, USER_ONE.try_into().unwrap());
let expected_event = LinkedEvent::Linked(
HandleLinked {
handle_id: handle_id,
profile_address: USER_ONE.try_into().unwrap(),
caller: USER_ONE.try_into().unwrap(),
timestamp: 40
}
);
spy.assert_emitted(@array![(handle_registry_address, expected_event)]);
stop_cheat_block_timestamp(handle_registry_address);
stop_cheat_caller_address(handle_registry_address);
}
#[test]
fn test_unlinked_event_emission() {
let (handle_registry_address, handle_contract_address) = __setup__();
let registryDispatcher = IHandleRegistryDispatcher {
contract_address: handle_registry_address
};
let handleDispatcher = IHandleDispatcher { contract_address: handle_contract_address };
let mut spy = spy_events();
// mint handle
start_cheat_caller_address(handle_contract_address, USER_ONE.try_into().unwrap());
let handle_id = handleDispatcher.mint_handle(TEST_LOCAL_NAME, USER_ONE.try_into().unwrap());
stop_cheat_caller_address(handle_contract_address);
// link token to profile
start_cheat_caller_address(handle_registry_address, USER_ONE.try_into().unwrap());
registryDispatcher.link(handle_id, USER_ONE.try_into().unwrap());
// call unlink
registryDispatcher.unlink(handle_id, USER_ONE.try_into().unwrap());
let expected_event = UnlinkedEvent::Unlinked(
HandleUnlinked {
handle_id: handle_id,
profile_address: USER_ONE.try_into().unwrap(),
caller: USER_ONE.try_into().unwrap(),
timestamp: get_block_timestamp()
}
);
spy.assert_emitted(@array![(handle_registry_address, expected_event)]);
stop_cheat_caller_address(handle_registry_address);
}