Skip to content

Commit 7af6f2d

Browse files
test: add pkce login test for windows
1 parent 3d09e8b commit 7af6f2d

File tree

3 files changed

+83
-70
lines changed

3 files changed

+83
-70
lines changed

sample/Tests/test/test.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TestConfig:
1212
WALLET_ADDRESS = "0x547044ea95f03651139081241c99ffedbefdc5e8"
1313
ANDROID_PACKAGE = "com.immutable.ImmutableSample"
1414
IOS_BUNDLE_ID = "com.immutable.Immutable-Sample-GameSDK"
15-
15+
1616
class UnityTest(unittest.TestCase):
1717

1818
altdriver = None
@@ -23,7 +23,18 @@ def setUpClass(cls):
2323

2424
@classmethod
2525
def tearDownClass(cls):
26-
cls.altdriver.stop()
26+
if cls.altdriver:
27+
cls.altdriver.stop()
28+
29+
def get_altdriver(self):
30+
return self.__class__.altdriver
31+
32+
def start_altdriver(self):
33+
self.__class__.altdriver = AltDriver()
34+
35+
def stop_altdriver(self):
36+
if self.__class__.altdriver:
37+
self.__class__.altdriver.stop()
2738

2839
@pytest.mark.skip(reason="Base test should not be executed directly")
2940
def test_0_other_functions(self):
@@ -266,7 +277,7 @@ def test_3_zkevm_functions(self):
266277
transactionHash = match.group()
267278
else:
268279
raise SystemExit(f"Could not find transaction hash")
269-
280+
270281
# Go back to authenticated scene
271282
self.altdriver.find_object(By.NAME, "CancelButton").tap()
272283
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")

sample/Tests/test/test_windows.py

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,50 @@
77

88
class WindowsTest(UnityTest):
99

10-
altdriver = None
11-
1210
@classmethod
1311
def setUpClass(cls):
1412
open_sample_app()
15-
cls.altdriver = AltDriver()
13+
time.sleep(5) # Give time for the app to open
14+
super().setUpClass()
1615

1716
@classmethod
1817
def tearDownClass(cls):
19-
cls.altdriver.stop()
20-
stop_sample_app()
18+
super().tearDownClass()
19+
stop_sample_app()
2120

22-
def test_1_device_code_login(self):
23-
# Select use device code auth
24-
self.altdriver.find_object(By.NAME, "DeviceCodeAuth").tap()
21+
def restart_app_and_altdriver(self):
22+
self.stop_altdriver()
23+
stop_sample_app()
24+
open_sample_app()
25+
time.sleep(5) # Give time for the app to open
26+
self.start_altdriver()
27+
28+
def select_auth_type(self, use_pkce: bool):
29+
auth_type = "PKCE" if use_pkce else "DeviceCodeAuth"
30+
self.get_altdriver().find_object(By.NAME, auth_type).tap()
31+
32+
def login(self, use_pkce: bool):
33+
self.select_auth_type(use_pkce)
2534

2635
# Wait for unauthenticated screen
27-
self.altdriver.wait_for_current_scene_to_be("UnauthenticatedScene")
36+
self.get_altdriver().wait_for_current_scene_to_be("UnauthenticatedScene")
2837

2938
for attempt in range(2):
3039
try:
3140
# Check app state
32-
login_button = self.altdriver.find_object(By.NAME, "LoginBtn")
41+
login_button = self.get_altdriver().find_object(By.NAME, "LoginBtn")
3342
print("Found login button, app is in the correct state")
3443

3544
# Login
3645
print("Logging in...")
3746
launch_browser()
3847
bring_sample_app_to_foreground()
3948
login_button.tap()
40-
login()
49+
login(use_pkce)
4150
bring_sample_app_to_foreground()
4251

4352
# Wait for authenticated screen
44-
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")
53+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene")
4554
stop_browser()
4655
print("Logged in")
4756
return
@@ -54,31 +63,38 @@ def test_1_device_code_login(self):
5463
# Relogin (optional: only if the button is present)
5564
print("Try reset the app and log out once...")
5665
try:
57-
self.altdriver.wait_for_object(By.NAME, "ReloginBtn").tap()
66+
self.get_altdriver().wait_for_object(By.NAME, "ReloginBtn").tap()
5867
except Exception as e:
5968
print("ReloginBtn not found, skipping relogin step. User may already be in AuthenticatedScene.")
6069

6170
# Wait for authenticated screen
62-
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")
71+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene")
6372
print("Re-logged in")
6473

6574
# Logout
6675
print("Logging out...")
6776
launch_browser()
6877
bring_sample_app_to_foreground()
69-
self.altdriver.find_object(By.NAME, "LogoutBtn").tap()
78+
self.get_altdriver().find_object(By.NAME, "LogoutBtn").tap()
7079
time.sleep(5)
7180
bring_sample_app_to_foreground()
72-
81+
7382
# Wait for unauthenticated screen
74-
self.altdriver.wait_for_current_scene_to_be("UnauthenticatedScene")
83+
self.get_altdriver().wait_for_current_scene_to_be("UnauthenticatedScene")
7584
stop_browser()
7685
print("Logged out and successfully reset app")
7786

7887
time.sleep(5)
7988
else:
8089
raise SystemExit(f"Failed to reset app {err}")
8190

91+
def test_1a_pkce_login(self):
92+
self.login(True)
93+
94+
def test_1b_device_code_login(self):
95+
self.restart_app_and_altdriver()
96+
self.login(False)
97+
8298
def test_2_other_functions(self):
8399
self.test_0_other_functions()
84100

@@ -92,111 +108,95 @@ def test_5_zkevm_functions(self):
92108
self.test_3_zkevm_functions()
93109

94110
def test_6_relogin(self):
95-
# Close and reopen app
96-
stop_sample_app()
97-
open_sample_app()
98-
99-
# Restart AltTester
100-
self.altdriver.stop()
101-
self.altdriver = AltDriver()
102-
time.sleep(5)
111+
self.restart_app_and_altdriver()
103112

104113
# Select use device code auth
105-
self.altdriver.find_object(By.NAME, "DeviceCodeAuth").tap()
114+
self.select_auth_type(use_pkce=False)
106115

107116
# Relogin
108117
print("Re-logging in...")
109-
self.altdriver.wait_for_object(By.NAME, "ReloginBtn").tap()
118+
self.get_altdriver().wait_for_object(By.NAME, "ReloginBtn").tap()
110119

111120
# Wait for authenticated screen
112-
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")
121+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene")
113122
print("Re-logged in")
114123

115124
# Get access token
116-
self.altdriver.find_object(By.NAME, "GetAccessTokenBtn").tap()
117-
output = self.altdriver.find_object(By.NAME, "Output")
125+
self.get_altdriver().find_object(By.NAME, "GetAccessTokenBtn").tap()
126+
output = self.get_altdriver().find_object(By.NAME, "Output")
118127
self.assertTrue(len(output.get_text()) > 50)
119128

120129
# Click Connect to IMX button
121-
self.altdriver.find_object(By.NAME, "ConnectBtn").tap()
130+
self.get_altdriver().find_object(By.NAME, "ConnectBtn").tap()
122131
self.assertEqual("Connected to IMX", output.get_text())
123132

124-
self.altdriver.stop()
125-
126133
def test_7_reconnect_device_code_connect_imx(self):
127-
# Close and reopen app
128-
stop_sample_app()
129-
open_sample_app()
134+
self.restart_app_and_altdriver()
130135

131-
# Restart AltTester
132-
self.altdriver.stop()
133-
self.altdriver = AltDriver()
134-
time.sleep(5)
135-
136-
# Select use device code auth
137-
self.altdriver.find_object(By.NAME, "DeviceCodeAuth").tap()
136+
use_pkce = False
137+
self.select_auth_type(use_pkce)
138138

139139
# Reconnect
140140
print("Reconnecting...")
141-
self.altdriver.wait_for_object(By.NAME, "ReconnectBtn").tap()
141+
self.get_altdriver().wait_for_object(By.NAME, "ReconnectBtn").tap()
142142

143143
# Wait for authenticated screen
144-
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")
144+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene")
145145
print("Reconnected")
146146

147147
# Get access token
148-
self.altdriver.find_object(By.NAME, "GetAccessTokenBtn").tap()
149-
output = self.altdriver.find_object(By.NAME, "Output")
148+
self.get_altdriver().find_object(By.NAME, "GetAccessTokenBtn").tap()
149+
output = self.get_altdriver().find_object(By.NAME, "Output")
150150
self.assertTrue(len(output.get_text()) > 50)
151151

152152
# Get address without having to click Connect to IMX button
153-
self.altdriver.find_object(By.NAME, "GetAddressBtn").tap()
153+
self.get_altdriver().find_object(By.NAME, "GetAddressBtn").tap()
154154
self.assertEqual(TestConfig.WALLET_ADDRESS, output.get_text())
155155

156156
# Logout
157157
print("Logging out...")
158158
launch_browser()
159159
bring_sample_app_to_foreground()
160-
self.altdriver.find_object(By.NAME, "LogoutBtn").tap()
160+
self.get_altdriver().find_object(By.NAME, "LogoutBtn").tap()
161161
time.sleep(5)
162162
bring_sample_app_to_foreground()
163163

164164
# Wait for authenticated screen
165-
self.altdriver.wait_for_current_scene_to_be("UnauthenticatedScene")
165+
self.get_altdriver().wait_for_current_scene_to_be("UnauthenticatedScene")
166166
stop_browser()
167167
print("Logged out")
168168

169169
# Connect IMX
170170
print("Logging in and connecting to IMX...")
171171
launch_browser()
172172
bring_sample_app_to_foreground()
173-
self.altdriver.wait_for_object(By.NAME, "ConnectBtn").tap()
174-
login()
173+
self.get_altdriver().wait_for_object(By.NAME, "ConnectBtn").tap()
174+
login(use_pkce)
175175
bring_sample_app_to_foreground()
176-
176+
177177
# Wait for authenticated screen
178-
self.altdriver.wait_for_current_scene_to_be("AuthenticatedScene")
178+
self.get_altdriver().wait_for_current_scene_to_be("AuthenticatedScene")
179179
print("Logged in and connected to IMX")
180180
stop_browser()
181181

182182
# Get access token
183-
self.altdriver.find_object(By.NAME, "GetAccessTokenBtn").tap()
184-
output = self.altdriver.find_object(By.NAME, "Output")
183+
self.get_altdriver().find_object(By.NAME, "GetAccessTokenBtn").tap()
184+
output = self.get_altdriver().find_object(By.NAME, "Output")
185185
self.assertTrue(len(output.get_text()) > 50)
186186

187187
# Get address without having to click Connect to IMX button
188-
self.altdriver.find_object(By.NAME, "GetAddressBtn").tap()
188+
self.get_altdriver().find_object(By.NAME, "GetAddressBtn").tap()
189189
self.assertEqual(TestConfig.WALLET_ADDRESS, output.get_text())
190190

191191
# Logout
192192
launch_browser()
193193
bring_sample_app_to_foreground()
194194
print("Logging out...")
195-
self.altdriver.find_object(By.NAME, "LogoutBtn").tap()
195+
self.get_altdriver().find_object(By.NAME, "LogoutBtn").tap()
196196
time.sleep(5)
197197
bring_sample_app_to_foreground()
198-
198+
199199
# Wait for authenticated screen
200-
self.altdriver.wait_for_current_scene_to_be("UnauthenticatedScene")
200+
self.get_altdriver().wait_for_current_scene_to_be("UnauthenticatedScene")
201201
stop_browser()
202202
print("Logged out")

sample/Tests/test/test_windows_helpers.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def get_product_name():
3939
# If regex fails, return default
4040
return "SampleApp"
4141

42-
def login():
42+
def login(use_pkce: bool):
4343
print("Connect to Chrome")
4444
# Set up Chrome options to connect to the existing Chrome instance
4545
chrome_options = Options()
@@ -62,13 +62,14 @@ def login():
6262

6363
print("Switch to the new window")
6464
driver.switch_to.window(new_window)
65-
65+
6666
wait = WebDriverWait(driver, 60)
6767

68-
print("Wait for device confirmation...")
69-
contine_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[span[text()='Continue']]")))
70-
contine_button.click()
71-
print("Confirmed device")
68+
if not use_pkce:
69+
print("Wait for device confirmation...")
70+
contine_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[span[text()='Continue']]")))
71+
contine_button.click()
72+
print("Confirmed device")
7273

7374
print("Wait for email input...")
7475
email_field = wait.until(EC.presence_of_element_located((By.ID, ':r1:')))
@@ -95,7 +96,8 @@ def login():
9596
otp_field.send_keys(code)
9697

9798
print("Wait for success page...")
98-
success = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'h1[data-testid="device_success_title"]')))
99+
success_title = 'h1[data-testid="checking_title"]' if use_pkce else 'h1[data-testid="device_success_title"]'
100+
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, success_title)))
99101
print("Connected to Passport!")
100102

101103
driver.quit()

0 commit comments

Comments
 (0)