-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtest_profile.cairo
194 lines (164 loc) · 7.27 KB
/
test_profile.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
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, spy_events,
EventSpyAssertionsTrait, ContractClassTrait, DeclareResultTrait
};
use coloniz::interfaces::IColonizNFT::{IColonizNFTDispatcher, IColonizNFTDispatcherTrait};
use coloniz::profile::profile::ProfileComponent::{Event as ProfileEvent, CreatedProfile};
use coloniz::interfaces::IProfile::{IProfileDispatcher, IProfileDispatcherTrait};
use coloniz::base::constants::types::{
ProfileVariants, AccessoryVariants, FaceVariants, ClothVariants, BackgroundVariants,
BodyVariants, ToolVariants
};
const HUB_ADDRESS: felt252 = 'HUB';
const USER: felt252 = 'USER1';
// *************************************************************************
// SETUP
// *************************************************************************
fn __setup__() -> (ContractAddress, ContractAddress, felt252, ContractAddress) {
// deploy NFT
let nft_contract = declare("ColonizNFT").unwrap().contract_class();
let mut calldata: Array<felt252> = array![USER];
let (nft_contract_address, _) = nft_contract.deploy(@calldata).unwrap_syscall();
// deploy registry
let registry_class_hash = declare("Registry").unwrap().contract_class();
let (registry_contract_address, _) = registry_class_hash.deploy(@array![]).unwrap_syscall();
// declare account
let account_class_hash = declare("AccountV3").unwrap().contract_class();
// declare follownft
let follow_nft_classhash = declare("Follow").unwrap().contract_class();
// deploy profile
let profile_contract = declare("ColonizProfile").unwrap().contract_class();
let mut coloniz_profile_constructor_calldata = array![
nft_contract_address.into(), HUB_ADDRESS, (*follow_nft_classhash.class_hash).into()
];
let (profile_contract_address, _) = profile_contract
.deploy(@coloniz_profile_constructor_calldata)
.unwrap();
return (
nft_contract_address,
registry_contract_address,
(*account_class_hash.class_hash).into(),
profile_contract_address
);
}
// *************************************************************************
// TESTS
// *************************************************************************
#[test]
fn test_profile_creation() {
let (
nft_contract_address,
registry_contract_address,
account_class_hash,
profile_contract_address
) =
__setup__();
let colonizNFTDispatcher = IColonizNFTDispatcher { contract_address: nft_contract_address };
let profileDispatcher = IProfileDispatcher { contract_address: profile_contract_address };
let profile_variant = ProfileVariants {
body: BodyVariants::BODY1,
tool: ToolVariants::TOOL1,
background: BackgroundVariants::BACKGROUND1,
cloth: ClothVariants::CLOTH1,
face: FaceVariants::FACE1,
accessory: AccessoryVariants::ACCESSORY1
};
//user 1 create profile
start_cheat_caller_address(profile_contract_address, USER.try_into().unwrap());
start_cheat_caller_address(nft_contract_address, USER.try_into().unwrap());
let profile_address = profileDispatcher
.create_profile(registry_contract_address, account_class_hash, 2456, profile_variant);
// test a new coloniz nft is minted
let last_minted_id = colonizNFTDispatcher.get_last_minted_id();
let token_id = colonizNFTDispatcher.get_user_token_id(USER.try_into().unwrap());
assert(last_minted_id == 1.try_into().unwrap(), 'invalid ID');
assert(token_id == 1.try_into().unwrap(), 'invalid ID');
// test profile creation was successful
let profile = profileDispatcher.get_profile(profile_address);
assert(profile.profile_address == profile_address, 'invalid profile address');
assert(profile.profile_owner == USER.try_into().unwrap(), 'invalid profile address');
// test follow nft contract is deployed
assert(profile.follow_nft != 0.try_into().unwrap(), 'follow nft not deployed');
stop_cheat_caller_address(profile_contract_address);
stop_cheat_caller_address(nft_contract_address);
}
#[test]
fn test_profile_metadata() {
let (
nft_contract_address,
registry_contract_address,
account_class_hash,
profile_contract_address
) =
__setup__();
let profileDispatcher = IProfileDispatcher { contract_address: profile_contract_address };
let profile_variant = ProfileVariants {
body: BodyVariants::BODY1,
tool: ToolVariants::TOOL1,
background: BackgroundVariants::BACKGROUND1,
cloth: ClothVariants::CLOTH1,
face: FaceVariants::FACE1,
accessory: AccessoryVariants::ACCESSORY1
};
//user 1 create profile
start_cheat_caller_address(profile_contract_address, USER.try_into().unwrap());
start_cheat_caller_address(nft_contract_address, USER.try_into().unwrap());
let profile_address = profileDispatcher
.create_profile(registry_contract_address, account_class_hash, 2456, profile_variant);
profileDispatcher
.set_profile_metadata_uri(
profile_address.try_into().unwrap(),
"ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZyjaYXr4gQ/"
);
// test profile URI
let profile_uri = profileDispatcher.get_profile_metadata(profile_address.try_into().unwrap());
assert(
profile_uri == "ipfs://QmSkDCsS32eLpcymxtn1cEn7Rc5hfefLBgfvZyjaYXr4gQ/",
'invalid profile URI'
);
stop_cheat_caller_address(profile_contract_address);
stop_cheat_caller_address(nft_contract_address);
}
#[test]
fn test_profile_creation_event() {
let (
nft_contract_address,
registry_contract_address,
account_class_hash,
profile_contract_address
) =
__setup__();
let colonizNFTDispatcher = IColonizNFTDispatcher { contract_address: nft_contract_address };
let profileDispatcher = IProfileDispatcher { contract_address: profile_contract_address };
let mut spy = spy_events();
let profile_variant = ProfileVariants {
body: BodyVariants::BODY1,
tool: ToolVariants::TOOL1,
background: BackgroundVariants::BACKGROUND1,
cloth: ClothVariants::CLOTH1,
face: FaceVariants::FACE1,
accessory: AccessoryVariants::ACCESSORY1
};
//user 1 create profile
start_cheat_caller_address(profile_contract_address, USER.try_into().unwrap());
start_cheat_caller_address(nft_contract_address, USER.try_into().unwrap());
let profile_address = profileDispatcher
.create_profile(registry_contract_address, account_class_hash, 2456, profile_variant);
let token_id = colonizNFTDispatcher.get_user_token_id(USER.try_into().unwrap());
let expected_event = ProfileEvent::CreatedProfile(
CreatedProfile {
owner: USER.try_into().unwrap(),
profile_address,
token_id,
timestamp: get_block_timestamp()
}
);
spy.assert_emitted(@array![(profile_contract_address, expected_event)]);
stop_cheat_caller_address(profile_contract_address);
stop_cheat_caller_address(nft_contract_address);
}