-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mfazones.py
704 lines (617 loc) · 30.7 KB
/
test_mfazones.py
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
""" Selenium tests for Sunet Drive
Author: Richard Freitag <freitag@sunet.se>
Selenium tests for mfa zones
* Non mfa user does not have MFA option available
* Non mfa user cannot access MFA protected folder from other user
* Non mfa users see MFA protected folders in their shares, but cannot access them
* MFA user has MFA option available
* MFA user can access MFA protected folder from other user
* MFA protected folders cannot be shared via link/anonymously
"""
import xmlrunner
import unittest
import sunetnextcloud
from webdav3.client import Client
import requests
from requests.auth import HTTPBasicAuth
import json
import pyotp
import pyautogui
import time
import traceback
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver import FirefoxOptions
from webdav3.client import Client
import os
import yaml
import time
import logging
from datetime import datetime
g_browser = 'chrome'
expectedResultsFile = 'expected.yaml'
geckodriver_path = "/snap/bin/geckodriver"
g_filename=datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
ocsheaders = { "OCS-APIRequest" : "true" }
g_expectedResultsFile = 'expected.yaml'
g_testtarget = os.environ.get('NextcloudTestTarget')
g_driver_timeout = 20
g_webdav_timeout = 30
g_wait={}
g_driver={}
g_logger={}
g_drv={}
g_loggedInNodes={}
g_failedNodes = []
use_driver_service = False
if os.environ.get('SELENIUM_DRIVER_SERVICE') == 'True':
use_driver_service = True
def prepareOcsMFaShares(nextcloudnode):
global g_logger, g_drv
logger = g_logger
logger.info(f'Prepare OCS Shares for {nextcloudnode}')
mainfolder = 'OcsMfaTestfolder'
subfolders = ['OcsTestFolder_NonMfaShared', 'OcsTestFolder_MfaShared']
users = ['_selenium_' + nextcloudnode, '_selenium_' + nextcloudnode + '_mfa']
nodeuser = g_drv.get_ocsuser(nextcloudnode)
nodepwd = g_drv.get_ocsuserapppassword(nextcloudnode)
url = g_drv.get_webdav_url(nextcloudnode, nodeuser)
logger.info(f'URL: {url}')
options = {
'webdav_hostname': url,
'webdav_login' : nodeuser,
'webdav_password' : nodepwd,
'webdav_timeout': g_webdav_timeout
}
client = Client(options)
try:
logger.info(client.list())
if not client.check(mainfolder):
logger.info(f'Creating main test folder: {mainfolder}')
client.mkdir(mainfolder)
else:
logger.info(f'Main test folder {mainfolder} already exists')
except Exception as e:
logger.error(f'Error checking or creating folder {mainfolder}: {e}')
g_failedNodes.append(nextcloudnode)
return
try:
subfolder = ''
logger.info(client.list(mainfolder))
for folder in subfolders:
subfolder = mainfolder + '/' + folder
if not client.check(subfolder):
logger.info(f'Creating subfolder {subfolder}')
client.mkdir(subfolder)
except Exception as e:
logger.error(f'Error creating subfolder {subfolder}: {e}')
g_failedNodes.append(nextcloudnode)
return
sharesUrl = g_drv.get_shares_url(nextcloudnode)
logger.info(f'Preparing shares: {sharesUrl}')
sharesUrl = sharesUrl.replace("$USERNAME$", nodeuser)
sharesUrl = sharesUrl.replace("$PASSWORD$", nodepwd)
session = requests.Session()
try:
r=session.get(sharesUrl, headers=ocsheaders)
except:
logger.error(f'Error getting {sharesUrl}')
g_failedNodes.append(nextcloudnode)
return
j = json.loads(r.text)
logger.info(json.dumps(j, indent=4, sort_keys=True))
numShares = len(j["ocs"]["data"])
logger.info(f'{numShares} shares found!')
for folder in subfolders:
subfolder = mainfolder + '/' + folder
for user in users:
data = {'shareType': 0, 'path': subfolder, 'shareWith': user}
logger.info(f'Create share: {data}')
r = session.post(sharesUrl, headers=ocsheaders, data=data)
# for share in j["ocs"]["data"]:
# shareId = share["id"]
# deleteShareUrl = g_drv.get_delete_share_url(nextcloudnode, shareId)
# logger.info(f'Delete share: {deleteShareUrl}')
# deleteShareUrl = deleteShareUrl.replace("$USERNAME$", nodeuser)
# deleteShareUrl = deleteShareUrl.replace("$PASSWORD$", nodepwd)
# r = session.delete(deleteShareUrl, headers=ocsheaders)
return
def deleteCookies():
cookies = g_driver.get_cookies()
# g_logger.info(f'Deleting all cookies: {cookies}')
g_logger.info(f'Deleting all cookies.')
g_driver.delete_all_cookies()
cookies = g_driver.get_cookies()
# g_logger.info(f'Cookies deleted: {cookies}')
g_logger.info(f'Cookies deleted.')
def nodelogin(nextcloudnode,user='selenium'):
global g_wait
deleteCookies()
g_logger.info(f'Logging in to {nextcloudnode}')
loginurl = g_drv.get_node_login_url(nextcloudnode)
g_logger.info(f'Login url: {loginurl}')
if user == 'selenium':
nodeuser = g_drv.get_seleniumuser(nextcloudnode)
nodepwd = g_drv.get_seleniumuserpassword(nextcloudnode)
elif user == 'selenium_mfa':
nodeuser = g_drv.get_seleniummfauser(nextcloudnode)
nodepwd = g_drv.get_seleniummfauserpassword(nextcloudnode)
nodetotpsecret = g_drv.get_seleniummfausertotpsecret(nextcloudnode)
else:
g_logger.error(f'Unknown user type {user}')
return
g_isLoggedIn = True
g_loggedInNodes[nextcloudnode] = True
g_driver.maximize_window()
# actions = ActionChains(g_driver)
# driver2 = webdriver.Firefox()
g_driver.get(loginurl)
g_wait.until(EC.presence_of_element_located((By.ID, 'user'))).send_keys(nodeuser)
g_wait.until(EC.presence_of_element_located((By.ID, 'password'))).send_keys(nodepwd + Keys.ENTER)
if user == 'selenium_mfa':
# Try totp to save some time
try:
totp = pyotp.TOTP(nodetotpsecret)
time.sleep(3)
g_wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="body-login"]/div[1]/div/main/div/form/input'))).send_keys(totp.now() + Keys.ENTER)
checkForTotp=False
except:
checkForTotp=True
# Wait for TOTP screen
if checkForTotp:
try:
g_logger.info(f'Check if TOTP selection dialogue is visible')
g_wait.until(EC.presence_of_element_located((By.XPATH, '//a[@href="'+ '/index.php/login/challenge/totp' +'"]')))
totpselect = g_driver.find_element(By.XPATH, '//a[@href="'+ '/index.php/login/challenge/totp' +'"]')
g_logger.warning(f'Found TOTP selection dialogue')
totpselect.click()
except:
g_logger.info(f'No need to select TOTP provider')
totp = pyotp.TOTP(nodetotpsecret)
time.sleep(3)
g_wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="body-login"]/div[1]/div/main/div/form/input'))).send_keys(totp.now() + Keys.ENTER)
return
class TestMfaZonesSelenium(unittest.TestCase):
global g_loggedInNodes, g_logger, g_drv, g_wait, g_driver, g_browser
drv = sunetnextcloud.TestTarget(g_testtarget)
g_drv=drv
logger = logging.getLogger(__name__)
g_logger = logger
logging.basicConfig(format = '%(asctime)s - %(module)s.%(funcName)s - %(levelname)s: %(message)s',
datefmt = '%Y-%m-%d %H:%M:%S', level = logging.INFO)
with open(g_expectedResultsFile, "r") as stream:
expectedResults=yaml.safe_load(stream)
mainFolder = 'MfaZones'
testfolders = ['SeleniumCollaboraTest', 'selenium-system', 'selenium-personal']
# Some class names of icons changed from Nextcloud 27 to 28
version = expectedResults[drv.target]['status']['version']
if version.startswith('27'):
homeIcon = 'icon-home'
addIcon = 'icon-add'
else:
homeIcon = 'home-icon'
addIcon = 'plus-icon'
try:
if g_browser == 'chrome':
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-gpu")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(options=options)
elif g_browser == 'firefox':
options = FirefoxOptions()
# options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
else:
g_logger.error(f'Unknown browser {g_browser}')
g_driver=driver
except Exception as error:
logger.error(f'Error initializing driver: {error}')
for nextcloudnode in drv.fullnodes:
g_loggedInNodes[nextcloudnode] = False
def test_logger(self):
self.logger.info(f'TestID: {self._testMethodName}')
def test_prepared_mfa_folders(self):
global g_drv, g_failedNodes, g_testtarget
g_failedNodes = []
for fullnode in g_drv.fullnodes:
if g_testtarget=='prod' and fullnode == 'su':
self.logger.info(f'We are not testing su in prod right now')
continue
with self.subTest(mynode=fullnode):
self.logger.info(f'TestID: {self._testMethodName} - {fullnode}')
prepareOcsMFaShares(fullnode)
self.logger.info(f'Preparing folders failed for {len(g_failedNodes)} nodes')
for node in g_failedNodes:
self.logger.info(f'{node}')
if len(g_failedNodes) > 0:
self.assertTrue(False)
def test_mfa_webdav_folders(self):
global g_testtarget
failedNodes = []
for fullnode in g_drv.fullnodes:
if g_testtarget=='prod' and fullnode == 'su':
self.logger.info(f'We are not testing su in prod right now')
continue
with self.subTest(mynode=fullnode):
self.logger.info(f'TestID: Testing node {fullnode}')
# Make sure test folder exists
nodeuser = g_drv.get_seleniummfauser(fullnode)
self.logger.info(f'Username: {nodeuser}')
nodeapppwd = g_drv.get_seleniummfauserapppassword(fullnode)
# Create folder for testing using webdav
url = g_drv.get_webdav_url(fullnode, nodeuser)
options = {
'webdav_hostname': url,
'webdav_login' : nodeuser,
'webdav_password' : nodeapppwd
}
client = Client(options)
dir = 'MfaTestFolder'
try:
client.list(dir)
self.logger.info(f'Folder good on node {fullnode}')
except Exception as e:
error_message=str(e)
if "403" in error_message:
self.logger.info(f'Expected 403')
else:
self.logger.error(f'Folder locked on node {fullnode}')
failedNodes.append(fullnode)
for node in failedNodes:
self.logger.error(f'Locked on node {node}')
if len(failedNodes)>0:
self.assertTrue(False)
def test_mfa_webdav_shared_folders(self):
global g_testtarget
failedNodes = []
passedNodes = []
for fullnode in g_drv.fullnodes:
if g_testtarget=='prod' and fullnode == 'su':
self.logger.info(f'We are not testing su in prod right now')
continue
with self.subTest(mynode=fullnode):
self.logger.info(f'TestID: Testing node {fullnode}')
# Make sure test folder exists
nodeuser = g_drv.get_seleniummfauser(fullnode)
self.logger.info(f'Username: {nodeuser}')
nodeapppwd = g_drv.get_seleniummfauserapppassword(fullnode)
# Create folder for testing using webdav
url = g_drv.get_webdav_url(fullnode, nodeuser)
self.logger.info(f'URL: {url}')
options = {
'webdav_hostname': url,
'webdav_login' : nodeuser,
'webdav_password' : nodeapppwd
}
client = Client(options)
nonMfaFolder = 'OcsTestFolder_NonMfaShared'
mfaFolder = 'OcsTestFolder_MfaShared'
# List non mfa folder, this should succeed
try:
client.list(nonMfaFolder)
self.logger.info(f'Folder {nonMfaFolder} good on node {fullnode}')
self.assertTrue(True)
except Exception as e:
self.logger.info(f'Error listing non mfa shared folder: {e}')
failedNodes.append(fullnode)
# List mfa folder, which should result in a 403 exception
try:
client.list(mfaFolder)
self.logger.error(f'Folder {mfaFolder} on node {fullnode} should not be listed')
self.assertTrue(False)
except Exception as e:
self.logger.info(f'Check if this is an expected exception or not')
error_message = str(e)
if "failed with code 403" in error_message:
g_logger.info(f'Expected 403 has occurred')
self.assertTrue(True)
passedNodes.append(fullnode)
elif "False is not true" in error_message:
g_logger.error(f'MFA Zones do not seem to work on webdav for {fullnode}: {error_message}')
failedNodes.append(fullnode)
self.assertTrue(False)
else:
g_logger.error(f'Unexpected error on {fullnode}: {error_message}')
failedNodes.append(fullnode)
self.assertTrue(False)
for node in passedNodes:
self.logger.info(f'Passed for {node}')
for node in failedNodes:
self.logger.error(f'Failed for {node}')
if len(g_failedNodes) > 0:
self.assertTrue(False)
self.logger.info(f'And done...')
def test_nonmfa_webdav_shared_folders(self):
global g_testtarget
failedNodes = []
passedNodes = []
for fullnode in g_drv.fullnodes:
if g_testtarget=='prod' and fullnode == 'su':
self.logger.info(f'We are not testing su in prod right now')
continue
with self.subTest(mynode=fullnode):
self.logger.info(f'TestID: Testing node {fullnode}')
# Make sure test folder exists
nodeuser = g_drv.get_seleniumuser(fullnode)
self.logger.info(f'Username: {nodeuser}')
nodeapppwd = g_drv.get_seleniumuserapppassword(fullnode)
# Create folder for testing using webdav
url = g_drv.get_webdav_url(fullnode, nodeuser)
self.logger.info(f'URL: {url}')
options = {
'webdav_hostname': url,
'webdav_login' : nodeuser,
'webdav_password' : nodeapppwd
}
client = Client(options)
nonMfaFolder = 'OcsTestFolder_NonMfaShared'
mfaFolder = 'OcsTestFolder_MfaShared'
# List non mfa folder, this should succeed
try:
client.list(nonMfaFolder)
self.logger.info(f'Folder {nonMfaFolder} good on node {fullnode}')
self.assertTrue(True)
except Exception as e:
self.logger.info(f'Error listing non mfa shared folder: {e}')
failedNodes.append(fullnode)
# List mfa folder, which should result in a 403 exception
try:
client.list(mfaFolder)
self.logger.error(f'Folder {mfaFolder} on node {fullnode} should not be listed')
self.assertTrue(False)
except Exception as e:
self.logger.info(f'Check if this is an expected exception or not')
error_message = str(e)
if "failed with code 403" in error_message:
g_logger.info(f'Expected 403 has occurred')
self.assertTrue(True)
passedNodes.append(fullnode)
elif "False is not true" in error_message:
g_logger.error(f'MFA Zones do not seem to work on webdav for {fullnode}: {error_message}')
failedNodes.append(fullnode)
self.assertTrue(False)
else:
g_logger.error(f'Unexpected error on {fullnode}: {error_message}')
failedNodes.append(fullnode)
self.assertTrue(False)
for node in passedNodes:
self.logger.info(f'Passed for {node}')
for node in failedNodes:
self.logger.error(f'Failed for {node}')
if len(g_failedNodes) > 0:
self.assertTrue(False)
self.logger.info(f'And done...')
def test_mfazones_no_mfauser(self):
delay = 30 # seconds
global g_isLoggedIn, g_loggedInNodes, g_wait, g_testtarget
wait = WebDriverWait(self.driver, delay)
g_wait = wait
# The class name of the share icon changed in Nextcloud 28
version = self.expectedResults[g_drv.target]['status']['version']
self.logger.info(f'Expected Nextcloud version: {version}')
if version.startswith('27'):
sharedClass = 'icon-shared'
simpleLogoutUrl = False
self.logger.info(f'We are on Nextcloud 27 and are not using the simple logout url')
else:
# This will select the first available sharing button
sharedClass = 'files-list__row-action-sharing-status'
simpleLogoutUrl = True
self.logger.info(f'We are on Nextcloud 28 and are therefore using the simple logout url')
for fullnode in g_drv.fullnodes:
if g_testtarget=='prod' and fullnode == 'su':
self.logger.info(f'We are not testing su in prod right now')
continue
with self.subTest(mynode=fullnode):
self.logger.info(f'TestID: Testing node {fullnode}')
nodelogin(fullnode) #No-MFA user
try:
myElem = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'app-menu')))
self.logger.info(f'App menu is ready!')
except TimeoutException:
self.logger.info(f'Loading of app menu took too much time, saving screenshot')
screenshot = pyautogui.screenshot()
screenshot.save("screenshots/" + fullnode + g_filename + ".png")
files = g_driver.find_element(By.XPATH, '//a[@href="' + g_drv.indexsuffix + '/apps/files/' +'"]')
files.click()
try:
myElem = wait.until(EC.presence_of_element_located((By.LINK_TEXT, 'All files')))
self.logger.info(f'All files visible!')
except TimeoutException:
self.logger.info(f'Loading of all files took too much time!')
# Click on first sharing icon in list
try:
wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="app-content-vue"]/div[3]/table/tbody/tr[1]/td[3]/div/button[2]/span')))
g_driver.implicitly_wait(g_driver_timeout)
sharingIcon = g_driver.find_element(By.XPATH, '//*[@id="app-content-vue"]/div[3]/table/tbody/tr[1]/td[3]/div/button[2]/span')
sharingIcon.click()
except Exception as e:
self.logger.error(f'Error for node {fullnode}: {e}')
return
# Click on MFA Zone
try:
wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="tab-button-mfazone"]')))
g_driver.implicitly_wait(g_driver_timeout)
mfaZone = g_driver.find_element(By.XPATH, '//*[@id="tab-button-mfazone"]')
mfaZone.click()
except Exception as e:
self.logger.error(f'Error for node {fullnode}: {e}')
return
# Find by text
try:
wait.until(EC.presence_of_element_located((By.ID, 'need-mfa')))
g_driver.implicitly_wait(g_driver_timeout)
except Exception as e:
self.logger.error(f'Error for node {fullnode}: {e}')
return
self.logger.info(f'Subtest done for {fullnode}')
def test_mfazones_mfauser(self):
delay = 30 # seconds
global g_isLoggedIn, g_loggedInNodes, g_wait, g_testtarget
wait = WebDriverWait(self.driver, delay)
g_wait = wait
# The class name of the share icon changed in Nextcloud 28
version = self.expectedResults[g_drv.target]['status']['version']
self.logger.info(f'Expected Nextcloud version: {version}')
if version.startswith('27'):
sharedClass = 'icon-shared'
simpleLogoutUrl = False
self.logger.info(f'We are on Nextcloud 27 and are not using the simple logout url')
else:
# This will select the first available sharing button
sharedClass = 'files-list__row-action-sharing-status'
simpleLogoutUrl = True
self.logger.info(f'We are on Nextcloud 28 and are therefore using the simple logout url')
for fullnode in g_drv.fullnodes:
if g_testtarget=='prod' and fullnode == 'su':
self.logger.info(f'We are not testing su in prod right now')
continue
with self.subTest(mynode=fullnode):
self.logger.info(f'TestID: Testing node {fullnode}')
actions = ActionChains(g_driver)
# Make sure test folder exists
nodeuser = g_drv.get_seleniummfauser(fullnode)
self.logger.info(f'Username: {nodeuser}')
nodeapppwd = g_drv.get_seleniummfauserapppassword(fullnode)
# Create folder for testing using webdav
url = g_drv.get_webdav_url(fullnode, nodeuser)
options = {
'webdav_hostname': url,
'webdav_login' : nodeuser,
'webdav_password' : nodeapppwd
}
client = Client(options)
dir = 'MfaTestFolder'
self.logger.info(f'Make and check directory: {dir}')
client.mkdir(dir)
self.assertEqual(client.list().count(dir + '/'), 1)
# Log in to the node
nodelogin(fullnode, user='selenium_mfa')
try:
myElem = wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'app-menu')))
self.logger.info(f'App menu is ready!')
except TimeoutException:
self.logger.info(f'Loading of app menu took too much time, saving screenshot')
screenshot = pyautogui.screenshot()
screenshot.save("screenshots/" + fullnode + g_filename + ".png")
try:
wait.until(EC.element_to_be_clickable((By.XPATH, '//a[@href="' + g_drv.indexsuffix + '/apps/files/' +'"]')))
files = g_driver.find_element(By.XPATH, '//a[@href="' + g_drv.indexsuffix + '/apps/files/' +'"]')
files.click()
except:
self.logger.error(f'Files app icon not found, current url is {g_driver.current_url}')
self.assertTrue(False)
try:
wait.until(EC.presence_of_element_located((By.LINK_TEXT, 'All files')))
self.logger.info(f'All files visible!')
except TimeoutException:
self.logger.info(f'Loading of all files took too much time, current url is {g_driver.current_url}')
# Right click on MFA test folder
try:
g_wait.until(EC.presence_of_element_located((By.XPATH, f"//*[@class='files-list__row-name-' and text()='{dir}']")))
mfaFolder = g_driver.find_element(By.XPATH, f"//*[@class='files-list__row-name-' and text()='{dir}']")
actions.context_click(mfaFolder).perform()
time.sleep(1)
wait.until(EC.presence_of_element_located((By.XPATH, "//*[contains(text(), 'Open details')]"))).click()
# g_driver.find_element((By.XPATH, "//*[contains(text(), 'MfaTestFolder')]")).context_click()
except Exception as e:
self.logger.error(f'Error for node {fullnode}: {e}')
return
# Click on MFA Zone
try:
wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="tab-button-mfazone"]')))
g_driver.implicitly_wait(g_driver_timeout)
mfaZone = g_driver.find_element(By.XPATH, '//*[@id="tab-button-mfazone"]')
mfaZone.click()
except Exception as e:
self.logger.error(f'Error for node {fullnode}: {e}')
return
# Check if folder already has MFA so we can deactivate it before the first test
try:
g_logger.info(f'List folder before MFA Zone: {client.list(dir)}')
deactivateMfaBeforeStart=False
except Exception as e:
error_message = str(e)
if "403" in error_message:
deactivateMfaBeforeStart=True
else:
g_logger.error(f'Error checking MFA zone for {fullnode}: {e}')
self.assertTrue(False)
return
# Find by checkbox ID and click it twice to activate/deactivate
try:
g_logger.info(f'Wait for MFA checkbox')
wait.until(EC.presence_of_element_located((By.ID, 'checkbox-radio-switch-mfa')))
g_logger.info(f'MFA checkbox found')
wait.until(EC.presence_of_element_located((By.ID, 'have-mfa')))
except Exception as e:
g_logger.error(f'Unable to locate mfa zone menus: {e}')
# Click to deactivate MFA Zone if it is already enabled
if deactivateMfaBeforeStart:
try:
haveMfa = g_driver.find_element(by=By.ID, value='checkbox-radio-switch-mfa')
actions.move_to_element(haveMfa)
actions.move_by_offset(50, 10)
time.sleep(1)
g_logger.info(f'Klick to deactivate MFA')
actions.click().perform()
time.sleep(3)
except Exception as e:
g_logger.error(f'Error deactivating previously activated MFA zone: {e}')
# List files before activating MFA Zone
try:
g_logger.info(f'List folder before MFA Zone: {client.list(dir)}')
time.sleep(3)
except Exception as e:
g_logger.error(f'Error before activating MFA zone on node {fullnode}: {e}')
self.assertTrue(False)
return
# Activate MFA Zone and ensure we cannot access the files via WebDAV
try:
# g_logger.info(f'Activate MFA')
haveMfa = g_driver.find_element(by=By.ID, value='checkbox-radio-switch-mfa')
actions.move_to_element(haveMfa)
actions.move_by_offset(50, 10)
time.sleep(1)
g_logger.info(f'Klick to activate MFA')
actions.click().perform()
time.sleep(3)
content=client.list(dir)
g_logger.info(f'List after activating MFA Zone: {content}')
g_logger.error(f'We should not be able to list folders in an active MFA zone!')
self.assertTrue(False)
except Exception as e:
g_logger.info(f'Check if this exception is expected or not')
error_message = str(e)
if "failed with code 403" in error_message:
g_logger.info(f'Expected 403 has occurred')
self.assertTrue(True)
elif "False is not true" in error_message:
g_logger.error(f'MFA Zones do not seem to work on webdav for {fullnode}: {error_message}')
self.assertTrue(False)
return
else:
g_logger.error(f'Unexpected error on {fullnode}: {error_message}')
self.assertTrue(False)
return
# Deactivate MFA Zone again
try:
g_logger.info(f'Deactivate MFA and wait for 3 seconds')
actions.click().perform()
time.sleep(3)
g_logger.info(f'List after deactivating MFA Zone again: {client.list(dir)}')
except Exception as e:
g_logger.error(f'Error after deactivating MFA zone: {e}')
self.assertTrue(False)
return
self.logger.info(f'Subtest done for {fullnode}')
if __name__ == '__main__':
unittest.main(testRunner=xmlrunner.XMLTestRunner(output='test-reports'))