@@ -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 ()
0 commit comments