forked from TevaLabs/Xelma-Blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialization.rs
More file actions
162 lines (123 loc) · 5.01 KB
/
Copy pathinitialization.rs
File metadata and controls
162 lines (123 loc) · 5.01 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
//! Tests for contract initialization and token minting.
use crate::contract::{VirtualTokenContract, VirtualTokenContractClient};
use crate::errors::ContractError;
use soroban_sdk::{testutils::Address as _, Address, Env};
#[test]
fn test_mint_initial() {
// Create a test environment
let env = Env::default();
// Register our contract in the test environment
// This deploys the contract to the test blockchain and returns its unique ID
// Think of it as: installing your app on a test phone before you can use it
// The () means we're not passing any initialization arguments
let contract_id = env.register(VirtualTokenContract, ());
// Create a client to interact with the contract
let client = VirtualTokenContractClient::new(&env, &contract_id);
// Generate a random test user address
let user = Address::generate(&env);
// Mock the authorization (in tests, we need to simulate user approval)
env.mock_all_auths();
// Call mint_initial for the user
let balance = client.mint_initial(&user);
// Verify the user received 1000 vXLM
assert_eq!(balance, 1000_0000000);
// Verify we can query the balance
let queried_balance = client.balance(&user);
assert_eq!(queried_balance, 1000_0000000);
}
#[test]
fn test_mint_initial_only_once() {
let env = Env::default();
let contract_id = env.register(VirtualTokenContract, ());
let client = VirtualTokenContractClient::new(&env, &contract_id);
let user = Address::generate(&env);
env.mock_all_auths();
// First mint
let first_mint = client.mint_initial(&user);
assert_eq!(first_mint, 1000_0000000);
// Try to mint again - should return existing balance, not mint more
let second_mint = client.mint_initial(&user);
assert_eq!(second_mint, 1000_0000000);
// Balance should still be 1000, not 2000
let balance = client.balance(&user);
assert_eq!(balance, 1000_0000000);
}
#[test]
fn test_balance_for_new_user() {
let env = Env::default();
let contract_id = env.register(VirtualTokenContract, ());
let client = VirtualTokenContractClient::new(&env, &contract_id);
let user = Address::generate(&env);
// Query balance for a user who never minted
let balance = client.balance(&user);
// Should return 0
assert_eq!(balance, 0);
}
#[test]
fn test_initialize() {
let env = Env::default();
let contract_id = env.register(VirtualTokenContract, ());
let client = VirtualTokenContractClient::new(&env, &contract_id);
// Generate an admin and oracle address
let admin = Address::generate(&env);
let oracle = Address::generate(&env);
env.mock_all_auths();
// Initialize the contract
client.initialize(&admin, &oracle);
// Verify admin and oracle are set
let stored_admin = client.get_admin();
let stored_oracle = client.get_oracle();
assert_eq!(stored_admin, Some(admin));
assert_eq!(stored_oracle, Some(oracle));
// Schema version must be set deterministically at initialization.
assert_eq!(client.get_schema_version(), 2u32);
}
#[test]
fn test_initialize_twice_fails() {
let env = Env::default();
let contract_id = env.register(VirtualTokenContract, ());
let client = VirtualTokenContractClient::new(&env, &contract_id);
let admin = Address::generate(&env);
let oracle = Address::generate(&env);
env.mock_all_auths();
// Initialize once
client.initialize(&admin, &oracle);
// Try to initialize again - should return error
let result = client.try_initialize(&admin, &oracle);
assert_eq!(result, Err(Ok(ContractError::AlreadyInitialized)));
}
#[test]
fn test_initialize_fails_without_admin_auth() {
let env = Env::default();
let contract_id = env.register(VirtualTokenContract, ());
let client = VirtualTokenContractClient::new(&env, &contract_id);
let admin = Address::generate(&env);
let oracle = Address::generate(&env);
// We do NOT call env.mock_all_auths() here
// So admin.require_auth() should fail
let result = client.try_initialize(&admin, &oracle);
assert!(result.is_err());
}
#[test]
fn test_mint_initial_fails_without_user_auth() {
let env = Env::default();
let contract_id = env.register(VirtualTokenContract, ());
let client = VirtualTokenContractClient::new(&env, &contract_id);
let user = Address::generate(&env);
// We do NOT call env.mock_all_auths() here
// So user.require_auth() should fail
let result = client.try_mint_initial(&user);
assert!(result.is_err());
}
#[test]
fn test_initialize_fails_identical_addresses() {
let env = Env::default();
let contract_id = env.register(VirtualTokenContract, ());
let client = VirtualTokenContractClient::new(&env, &contract_id);
// Generate a single address
let admin_and_oracle = Address::generate(&env);
env.mock_all_auths();
// Try to initialize using the same address for both
let result = client.try_initialize(&admin_and_oracle, &admin_and_oracle);
assert_eq!(result, Err(Ok(ContractError::AdminIsOracle)));
}