Skip to content

Commit d7d7cb1

Browse files
committed
harden login - upgrade tests
1 parent e708aba commit d7d7cb1

2 files changed

Lines changed: 126 additions & 10 deletions

File tree

tests/e2e_v2/ui/conftest.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,21 +351,50 @@ def handle_openshift_oauth_login(driver, test_credentials):
351351
continue
352352

353353
if all_idp_buttons:
354+
# Remove duplicates based on text and href
355+
seen = set()
356+
unique_idp_buttons = []
357+
for elem, text, href in all_idp_buttons:
358+
key = (text.lower(), href)
359+
if key not in seen:
360+
seen.add(key)
361+
unique_idp_buttons.append((elem, text, href))
362+
363+
all_idp_buttons = unique_idp_buttons
364+
print(
365+
f"After removing duplicates, found {len(all_idp_buttons)} unique IDPs: {[text for _, text, _ in all_idp_buttons]}"
366+
)
367+
354368
# Try to intelligently select the right IDP based on username
355369
username = test_credentials["username"].lower()
356370
selected_idp = None
357371

358372
# Strategy 1: Match username pattern to IDP name
373+
# First, try exact matches
359374
if "ldap" in username:
360-
# Look for ldap IDP
375+
# Look for ldap IDP first
361376
for elem, text, href in all_idp_buttons:
362377
if "ldap" in text.lower() or "ldap" in href.lower():
363378
selected_idp = (elem, text)
364379
print(
365380
f"Selected LDAP IDP based on username pattern: {text}"
366381
)
367382
break
368-
elif "htpasswd" in username or "admin" in username:
383+
384+
# If no LDAP IDP found but username contains "admin", try cluster-admin
385+
if not selected_idp and "admin" in username:
386+
for elem, text, href in all_idp_buttons:
387+
if (
388+
"cluster-admin" in text.lower()
389+
or "admin" in text.lower()
390+
):
391+
selected_idp = (elem, text)
392+
print(
393+
f"Selected admin IDP as fallback for LDAP user: {text}"
394+
)
395+
break
396+
397+
elif "htpasswd" in username:
369398
# Look for htpasswd IDP
370399
for elem, text, href in all_idp_buttons:
371400
if "htpasswd" in text.lower() or "htpasswd" in href.lower():
@@ -375,6 +404,20 @@ def handle_openshift_oauth_login(driver, test_credentials):
375404
)
376405
break
377406

407+
elif "admin" in username:
408+
# Look for admin/cluster-admin IDP
409+
for elem, text, href in all_idp_buttons:
410+
if (
411+
"cluster-admin" in text.lower()
412+
or "admin" in text.lower()
413+
or "htpasswd" in text.lower()
414+
):
415+
selected_idp = (elem, text)
416+
print(
417+
f"Selected admin IDP based on username pattern: {text}"
418+
)
419+
break
420+
378421
# Strategy 2: If no match, use environment variable if set
379422
if not selected_idp:
380423
idp_name = os.getenv("OPENSHIFT_IDP_NAME", "").lower()
@@ -390,13 +433,28 @@ def handle_openshift_oauth_login(driver, test_credentials):
390433
selected_idp = (all_idp_buttons[0][0], all_idp_buttons[0][1])
391434
print(f"Only one IDP available, using: {selected_idp[1]}")
392435

393-
# Strategy 4: If multiple IDPs and no match, skip IDP selection
394-
# (some clusters may not require IDP selection if there's a default)
436+
# Strategy 4: If multiple IDPs and no match, try smart fallback
395437
if not selected_idp:
396438
print(
397439
f"Multiple IDPs found but couldn't determine which to use. Available: {[text for _, text, _ in all_idp_buttons]}"
398440
)
399-
print("Skipping IDP selection, will try direct login form")
441+
442+
# Smart fallback: prefer cluster-admin, htpasswd, or admin over redhat-sso
443+
preferred_idps = ["cluster-admin", "htpasswd", "admin", "kube"]
444+
for preferred in preferred_idps:
445+
for elem, text, href in all_idp_buttons:
446+
if preferred in text.lower():
447+
selected_idp = (elem, text)
448+
print(f"Selected preferred IDP as fallback: {text}")
449+
break
450+
if selected_idp:
451+
break
452+
453+
# If still no match, skip IDP selection
454+
if not selected_idp:
455+
print(
456+
"No preferred IDP found, skipping IDP selection, will try direct login form"
457+
)
400458
else:
401459
print(f"Clicking identity provider button: {selected_idp[1]}")
402460
selected_idp[0].click()

tests/ui/conftest.py

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,50 @@ def handle_openshift_oauth_login(driver, test_credentials):
365365
continue
366366

367367
if all_idp_buttons:
368+
# Remove duplicates based on text and href
369+
seen = set()
370+
unique_idp_buttons = []
371+
for elem, text, href in all_idp_buttons:
372+
key = (text.lower(), href)
373+
if key not in seen:
374+
seen.add(key)
375+
unique_idp_buttons.append((elem, text, href))
376+
377+
all_idp_buttons = unique_idp_buttons
378+
print(
379+
f"After removing duplicates, found {len(all_idp_buttons)} unique IDPs: {[text for _, text, _ in all_idp_buttons]}"
380+
)
381+
368382
# Try to intelligently select the right IDP based on username
369383
username = test_credentials["username"].lower()
370384
selected_idp = None
371385

372386
# Strategy 1: Match username pattern to IDP name
387+
# First, try exact matches
373388
if "ldap" in username:
374-
# Look for ldap IDP
389+
# Look for ldap IDP first
375390
for elem, text, href in all_idp_buttons:
376391
if "ldap" in text.lower() or "ldap" in href.lower():
377392
selected_idp = (elem, text)
378393
print(
379394
f"Selected LDAP IDP based on username pattern: {text}"
380395
)
381396
break
382-
elif "htpasswd" in username or "admin" in username:
397+
398+
# If no LDAP IDP found but username contains "admin", try cluster-admin
399+
if not selected_idp and "admin" in username:
400+
for elem, text, href in all_idp_buttons:
401+
if (
402+
"cluster-admin" in text.lower()
403+
or "admin" in text.lower()
404+
):
405+
selected_idp = (elem, text)
406+
print(
407+
f"Selected admin IDP as fallback for LDAP user: {text}"
408+
)
409+
break
410+
411+
elif "htpasswd" in username:
383412
# Look for htpasswd IDP
384413
for elem, text, href in all_idp_buttons:
385414
if "htpasswd" in text.lower() or "htpasswd" in href.lower():
@@ -389,6 +418,20 @@ def handle_openshift_oauth_login(driver, test_credentials):
389418
)
390419
break
391420

421+
elif "admin" in username:
422+
# Look for admin/cluster-admin IDP
423+
for elem, text, href in all_idp_buttons:
424+
if (
425+
"cluster-admin" in text.lower()
426+
or "admin" in text.lower()
427+
or "htpasswd" in text.lower()
428+
):
429+
selected_idp = (elem, text)
430+
print(
431+
f"Selected admin IDP based on username pattern: {text}"
432+
)
433+
break
434+
392435
# Strategy 2: If no match, use environment variable if set
393436
if not selected_idp:
394437
idp_name = os.getenv("OPENSHIFT_IDP_NAME", "").lower()
@@ -404,13 +447,28 @@ def handle_openshift_oauth_login(driver, test_credentials):
404447
selected_idp = (all_idp_buttons[0][0], all_idp_buttons[0][1])
405448
print(f"Only one IDP available, using: {selected_idp[1]}")
406449

407-
# Strategy 4: If multiple IDPs and no match, skip IDP selection
408-
# (some clusters may not require IDP selection if there's a default)
450+
# Strategy 4: If multiple IDPs and no match, try smart fallback
409451
if not selected_idp:
410452
print(
411453
f"Multiple IDPs found but couldn't determine which to use. Available: {[text for _, text, _ in all_idp_buttons]}"
412454
)
413-
print("Skipping IDP selection, will try direct login form")
455+
456+
# Smart fallback: prefer cluster-admin, htpasswd, or admin over redhat-sso
457+
preferred_idps = ["cluster-admin", "htpasswd", "admin", "kube"]
458+
for preferred in preferred_idps:
459+
for elem, text, href in all_idp_buttons:
460+
if preferred in text.lower():
461+
selected_idp = (elem, text)
462+
print(f"Selected preferred IDP as fallback: {text}")
463+
break
464+
if selected_idp:
465+
break
466+
467+
# If still no match, skip IDP selection
468+
if not selected_idp:
469+
print(
470+
"No preferred IDP found, skipping IDP selection, will try direct login form"
471+
)
414472
else:
415473
print(f"Clicking identity provider button: {selected_idp[1]}")
416474
selected_idp[0].click()

0 commit comments

Comments
 (0)