-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest.py
More file actions
360 lines (309 loc) · 14.4 KB
/
test.py
File metadata and controls
360 lines (309 loc) · 14.4 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
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
import time
import unittest
import requests
import re
import pytest
from alttester import *
class TestConfig:
EMAIL = "unity-sdk@mailslurp.net"
PASSPORT_ID="email|67480492219c150aceeb1f37"
WALLET_ADDRESS = "0x547044ea95f03651139081241c99ffedbefdc5e8"
ANDROID_PACKAGE = "com.immutable.ImmutableSample"
IOS_BUNDLE_ID = "com.immutable.Immutable-Sample-GameSDK"
class UnityTest(unittest.TestCase):
altdriver = None
@classmethod
def setUpClass(cls):
cls.altdriver = AltDriver()
@classmethod
def tearDownClass(cls):
if cls.altdriver:
cls.altdriver.stop()
def get_altdriver(self):
return self.__class__.altdriver
def start_altdriver(self):
self.__class__.altdriver = AltDriver()
def stop_altdriver(self):
if self.__class__.altdriver:
self.__class__.altdriver.stop()
def wait_for_output(
self,
output_obj,
predicate,
*,
timeout_seconds: float = 15.0,
poll_seconds: float = 0.25,
) -> str:
"""
Poll the `Output` UI element until `predicate(text)` is True.
UI actions often update `Output` asynchronously (especially in CI),
so reading immediately after `.tap()` can be flaky.
"""
deadline = time.time() + float(timeout_seconds)
last_text = ""
while time.time() < deadline:
try:
last_text = output_obj.get_text()
if predicate(last_text):
return last_text
except Exception:
# App/UI might be mid-transition; retry.
pass
time.sleep(float(poll_seconds))
return last_text
@pytest.mark.skip(reason="Base test should not be executed directly")
def test_0_other_functions(self):
# Show set call timeout scene
self.altdriver.find_object(By.NAME, "CallTimeout").tap()
self.altdriver.wait_for_current_scene_to_be("SetCallTimeout")
milliseconds = self.altdriver.wait_for_object(By.NAME, "MsInput")
milliseconds.set_text("600000")
self.altdriver.find_object(By.NAME, "SetButton").tap()
output = self.altdriver.find_object(By.NAME, "Output")
text = output.get_text()
print(f"CallTimeout output: {text}")
self.assertEqual("Set call timeout to: 600000ms", text)
# Go back to authenticated scene
self.altdriver.find_object(By.NAME, "CancelButton").tap()
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")
@pytest.mark.skip(reason="Base test should not be executed directly")
def test_1_passport_functions(self):
output = self.altdriver.find_object(By.NAME, "Output")
# Get access token
prev = output.get_text()
self.altdriver.find_object(By.NAME, "GetAccessTokenBtn").tap()
text = self.wait_for_output(
output,
lambda t: len(t) > 50 and (t != prev or prev == ""),
timeout_seconds=20,
)
self.assertTrue(len(text) > 50, f"Access token output too short. Actual output: '{text}'")
# Get ID token
prev = output.get_text()
self.altdriver.find_object(By.NAME, "GetIdTokenBtn").tap()
text = self.wait_for_output(
output,
lambda t: len(t) > 50 and (t != prev or prev == ""),
timeout_seconds=20,
)
self.assertTrue(len(text) > 50, f"ID token output too short. Actual output: '{text}'")
# Get email
self.altdriver.find_object(By.NAME, "GetEmail").tap()
text = self.wait_for_output(
output,
lambda t: t == TestConfig.EMAIL,
timeout_seconds=10,
)
print(f"GetEmail output: {text}")
self.assertEqual(TestConfig.EMAIL, text)
# Get Passport ID
self.altdriver.find_object(By.NAME, "GetPassportId").tap()
text = self.wait_for_output(
output,
lambda t: t == TestConfig.PASSPORT_ID,
timeout_seconds=10,
)
print(f"GetPassportId output: {text}")
self.assertEqual(TestConfig.PASSPORT_ID, text)
# Get linked addresses
self.altdriver.find_object(By.NAME, "GetLinkedAddresses").tap()
text = self.wait_for_output(
output,
lambda t: t == "No linked addresses",
timeout_seconds=10,
)
print(f"GetLinkedAddresses output: {text}")
self.assertEqual("No linked addresses", text)
@pytest.mark.skip(reason="Base test should not be executed directly")
def test_2_imx_functions(self):
output = self.altdriver.find_object(By.NAME, "Output")
# Connect to IMX
self.altdriver.find_object(By.NAME, "ConnectBtn").tap()
time.sleep(5)
text = output.get_text()
print(f"ConnectBtn output: {text}")
self.assertEqual("Connected to IMX", text)
# Is registered off-chain
self.altdriver.wait_for_object(By.NAME, "IsRegisteredOffchainBtn").tap()
time.sleep(1)
text = output.get_text()
print(f"IsRegisteredOffchainBtn output: {text}")
self.assertEqual("Registered", text)
# Register off-chain
# Wait up to 3 times for "Passport account already registered" to appear
attempts = 0
while attempts < 3:
self.altdriver.find_object(By.NAME, "RegisterOffchainBtn").tap()
text = output.get_text()
print(f"RegisterOffchainBtn output: {text}")
self.assertEqual("Registering off-chain...", text)
time.sleep(20)
output_text = output.get_text()
# Accept either success message or 409 error (account already registered)
if "Successfully registered" in output_text or ("409" in output_text and "USER_REGISTRATION_ERROR" in output_text):
break
attempts += 1
# Assert that registration completed (either success or 409 error for already registered)
output_text = output.get_text()
self.assertTrue(
"Successfully registered" in output_text or ("409" in output_text and "USER_REGISTRATION_ERROR" in output_text),
f"Expected 'Successfully registered' or '409 (USER_REGISTRATION_ERROR)' not found. Actual output: '{output_text}'"
)
# Get address
self.altdriver.find_object(By.NAME, "GetAddressBtn").tap()
text = output.get_text()
print(f"GetAddressBtn output: {text}")
self.assertEqual(TestConfig.WALLET_ADDRESS, text)
# Show NFT transfer scene
self.altdriver.find_object(By.NAME, "NftTransferBtn").tap()
self.altdriver.wait_for_current_scene_to_be("ImxNftTransfer")
# Get all NFTs the user owns
collection = "0x3765d19d5bc39b60718e43b4b12b30e87d383181"
api_url = f"https://api.sandbox.immutable.com/v1/assets?collection={collection}&user={TestConfig.WALLET_ADDRESS}&page_size=3"
token_ids = []
try:
# Make the API request
response = requests.get(api_url)
# Raise an exception if the request was unsuccessful
response.raise_for_status()
# Parse the JSON response
data = response.json()
# Extract the token_ids
token_ids = [item['token_id'] for item in data['result']]
# Check that there's enough NFTs to test transfer
if len(token_ids) < 3:
raise SystemExit(f"Not enough NFTs to test transfer")
except requests.exceptions.HTTPError as err:
raise SystemExit(f"HTTP error occurred: {err}")
except Exception as err:
raise SystemExit(f"An error occurred: {err}")
# Single transfer
tokenId = self.altdriver.wait_for_object(By.NAME, "TokenId1")
tokenId.set_text(token_ids[0])
tokenAddress = self.altdriver.wait_for_object(By.NAME, "TokenAddress1")
tokenAddress.set_text(collection)
receiver = self.altdriver.wait_for_object(By.NAME, "Receiver1")
receiver.set_text("0x0000000000000000000000000000000000000000")
self.altdriver.find_object(By.NAME, "TransferButton").tap()
time.sleep(30)
output = self.altdriver.find_object(By.NAME, "Output")
text = output.get_text()
print(f"Single transfer output: {text}")
self.assertTrue(text.startswith("NFT transferred successfully"))
# Batch transfer
tokenId = self.altdriver.wait_for_object(By.NAME, "TokenId1")
tokenId.set_text(token_ids[1])
tokenAddress = self.altdriver.wait_for_object(By.NAME, "TokenAddress1")
tokenAddress.set_text(collection)
receiver = self.altdriver.wait_for_object(By.NAME, "Receiver1")
receiver.set_text("0x0000000000000000000000000000000000000000")
tokenId = self.altdriver.wait_for_object(By.NAME, "TokenId2")
tokenId.set_text(token_ids[2])
tokenAddress = self.altdriver.wait_for_object(By.NAME, "TokenAddress2")
tokenAddress.set_text(collection)
receiver = self.altdriver.wait_for_object(By.NAME, "Receiver2")
receiver.set_text("0x0000000000000000000000000000000000000000")
self.altdriver.find_object(By.NAME, "TransferButton").tap()
time.sleep(30)
output = self.altdriver.find_object(By.NAME, "Output")
text = output.get_text()
print(f"Batch transfer output: {text}")
self.assertEqual("Successfully transferred 2 NFTs.", text)
# Go back to authenticated scene
self.altdriver.find_object(By.NAME, "CancelButton").tap()
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")
@pytest.mark.skip(reason="Base test should not be executed directly")
def test_3_zkevm_functions(self):
output = self.altdriver.find_object(By.NAME, "Output")
# Connect to zkEVM
self.altdriver.find_object(By.NAME, "ConnectEvmBtn").tap()
text = self.wait_for_output(
output,
lambda t: t == "Connected to EVM",
timeout_seconds=30,
)
print(f"ConnectEvmBtn output: {text}")
self.assertEqual("Connected to EVM", text)
# Initiliase wallet and get address
self.altdriver.wait_for_object(By.NAME, "RequestAccountsBtn").tap()
text = self.wait_for_output(
output,
lambda t: t == TestConfig.WALLET_ADDRESS,
timeout_seconds=30,
)
print(f"RequestAccountsBtn output: {text}")
self.assertEqual(TestConfig.WALLET_ADDRESS, text)
# Show get balance scene
self.altdriver.find_object(By.NAME, "GetBalanceBtn").tap()
self.altdriver.wait_for_current_scene_to_be("ZkEvmGetBalance")
# Get balance of account
address = self.altdriver.wait_for_object(By.NAME, "AddressInput")
address.set_text(TestConfig.WALLET_ADDRESS)
self.altdriver.find_object(By.NAME, "GetBalanceButton").tap()
time.sleep(2)
output = self.altdriver.find_object(By.NAME, "Output")
text = output.get_text()
print(f"Get balance output: {text}")
self.assertRegex(text, r"Balance:\nHex: 0x[0-9a-fA-F]+\nDec: \d+")
# Go back to authenticated scene
self.altdriver.find_object(By.NAME, "CancelButton").tap()
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")
# Show send transaction scene
self.altdriver.find_object(By.NAME, "SendTransactionBtn").tap()
self.altdriver.wait_for_current_scene_to_be("ZkEvmSendTransaction")
output = self.altdriver.find_object(By.NAME, "Output")
# Send transaction with confirmation
to = self.altdriver.wait_for_object(By.NAME, "ToInput")
to.set_text("0xb237501b35dfdcad274299236a141425469ab9ba")
amount = self.altdriver.wait_for_object(By.NAME, "ValueInput")
amount.set_text("0")
data = self.altdriver.wait_for_object(By.NAME, "DataInput")
data.set_text("0x1e957f1e")
self.altdriver.find_object(By.NAME, "SendButton").tap()
time.sleep(15)
text = output.get_text()
print(f"Send transaction with confirmation output: {text}")
self.assertTrue(text.startswith("Transaction hash"))
self.assertTrue(text.endswith("Status: Success"))
time.sleep(20)
# Send transaction without confirmation and get transaction receipt
self.altdriver.wait_for_object(By.NAME, "WithConfirmationToggle").tap()
self.altdriver.find_object(By.NAME, "SendButton").tap()
time.sleep(20)
text = output.get_text()
print(f"Send transaction without confirmation and get transaction receipt output: {text}")
self.assertTrue(text.startswith("Transaction hash"))
self.assertTrue(text.endswith("Status: Success"))
time.sleep(20)
# Send transaction without confirmation and don't get transaction receipt
self.altdriver.wait_for_object(By.NAME, "GetTransactionReceiptToggle").tap()
self.altdriver.find_object(By.NAME, "SendButton").tap()
time.sleep(15)
text = output.get_text()
print(f"Send transaction without confirmation and don't get transaction receipt output: {text}")
self.assertTrue(text.startswith("Transaction hash"))
# Grab the transaction hash
match = re.search(r"0x[0-9a-fA-F]+", output.get_text())
transactionHash = ""
if match:
transactionHash = match.group()
else:
raise SystemExit(f"Could not find transaction hash")
# Go back to authenticated scene
self.altdriver.find_object(By.NAME, "CancelButton").tap()
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")
# Show get transaction receipt scene
self.altdriver.find_object(By.NAME, "GetTransactionReceiptBtn").tap()
self.altdriver.wait_for_current_scene_to_be("ZkEvmGetTransactionReceipt")
# Get transaction receipt
hash = self.altdriver.wait_for_object(By.NAME, "HashInput")
hash.set_text(transactionHash)
self.altdriver.find_object(By.NAME, "GetReceiptButton").tap()
time.sleep(2)
output = self.altdriver.find_object(By.NAME, "Output")
text = output.get_text()
print(f"Get transaction receipt output: {text}")
self.assertEqual("Status: Success", text)
# Go back to authenticated scene
self.altdriver.find_object(By.NAME, "CancelButton").tap()
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")