diff --git a/components/org.wso2.carbon.identity.application.authenticator.fido/src/test/java/org/wso2/carbon/identity/application/authenticator/fido/FIDOAuthenticatorTest.java b/components/org.wso2.carbon.identity.application.authenticator.fido/src/test/java/org/wso2/carbon/identity/application/authenticator/fido/FIDOAuthenticatorTest.java index 0592cfe2..a607ad14 100644 --- a/components/org.wso2.carbon.identity.application.authenticator.fido/src/test/java/org/wso2/carbon/identity/application/authenticator/fido/FIDOAuthenticatorTest.java +++ b/components/org.wso2.carbon.identity.application.authenticator.fido/src/test/java/org/wso2/carbon/identity/application/authenticator/fido/FIDOAuthenticatorTest.java @@ -380,62 +380,21 @@ public void testInitiateAuthenticationRequestWebauthnEnabled(String startAuthent fidoAuthenticator.initiateAuthenticationRequest(httpServletRequest, httpServletResponse, context); } - @Test(description = "Test case for initiateAuthenticationRequest() method when Webauthn is disabled and returns " + - "non null response") - public void testInitiateAuthenticationRequestWebauthnDisabled() throws Exception { + @DataProvider(name = "initiateAuthenticationRequestU2FDataProvider") + public static Object[][] initiateAuthenticationRequestU2FDataProvider() { - AuthenticationContext context = new AuthenticationContext(); - List authenticatorList = new ArrayList<>(); - AuthenticatorConfig authenticatorConfig = new AuthenticatorConfig(); - authenticatorConfig.setApplicationAuthenticator(fidoAuthenticator); - authenticatorList.add(authenticatorConfig); - - AuthenticatedUser authenticatedUser = AuthenticatedUser - .createLocalAuthenticatedUserFromSubjectIdentifier(USERNAME); - authenticatedUser.setFederatedUser(false); - authenticatedUser.setUserName(USERNAME); - authenticatedUser.setTenantDomain(SUPER_TENANT_DOMAIN); - - StepConfig stepConfig = new StepConfig(); - stepConfig.setAuthenticatorList(authenticatorList); - stepConfig.setAuthenticatedUser(authenticatedUser); - Map stepMap = new HashMap<>(); - stepMap.put(1, stepConfig); - SequenceConfig sequenceConfig = new SequenceConfig(); - sequenceConfig.setStepMap(stepMap); - context.setSequenceConfig(sequenceConfig); - - when(IdentityUtil.getPrimaryDomainName()).thenReturn(USER_STORE_DOMAIN); - - context.setProperty("username", USERNAME); - context.setProperty("authenticatedUser", authenticatedUser); - context.setContextIdentifier(UUID.randomUUID().toString()); - when(IdentityUtil.getProperty(FIDOAuthenticatorConstants.WEBAUTHN_ENABLED)).thenReturn(String.valueOf(false)); - - mockStatic(U2FService.class); - when(U2FService.getInstance()).thenReturn(u2FService); - mockStatic(AuthenticateResponse.class); - when(u2FService.startAuthentication(any())).thenReturn(authenticateRequestData); + AuthenticateRequestData authenticateRequestData = mock(AuthenticateRequestData.class); when(authenticateRequestData.toJson()).thenReturn("1234"); - Map parameterMap = new HashMap<>(); - parameterMap.put(FIDOAuthenticatorConstants.APP_ID, "https://localhost:9443"); - parameterMap.put(FIDOAuthenticatorConstants.FIDO_AUTH, "fido-auth"); - authenticatorConfig.setParameterMap(parameterMap); - - mockStatic(FileBasedConfigurationBuilder.class); - when(FileBasedConfigurationBuilder.getInstance()).thenReturn(fileBasedConfigurationBuilder); - when(fileBasedConfigurationBuilder.getAuthenticatorBean(anyString())).thenReturn(authenticatorConfig); - - mockStatic(URLEncoder.class); - when(URLEncoder.encode(anyString(), anyString())).thenReturn("encodedUrl"); - mockServiceURLBuilder(); - fidoAuthenticator.initiateAuthenticationRequest(httpServletRequest, httpServletResponse, context); + return new Object[][] { + {false}, {true} + }; } - @Test(description = "Test case for initiateAuthenticationRequest() method when Webauthn is disabled and returns " + - "null response") - public void testInitiateAuthenticationRequestWebauthnDisabledNullResponse() throws Exception { + @Test(description = "Test case for initiateAuthenticationRequest() method when Webauthn is disabled", + dataProvider = "initiateAuthenticationRequestU2FDataProvider") + public void testInitiateAuthenticationRequestWebauthnDisabled(boolean isU2FNullResponse) + throws Exception { AuthenticationContext context = new AuthenticationContext(); List authenticatorList = new ArrayList<>(); @@ -468,7 +427,13 @@ public void testInitiateAuthenticationRequestWebauthnDisabledNullResponse() thro mockStatic(U2FService.class); when(U2FService.getInstance()).thenReturn(u2FService); mockStatic(AuthenticateResponse.class); - when(u2FService.startAuthentication(any())).thenReturn(null); + + if (isU2FNullResponse) { + when(u2FService.startAuthentication(any())).thenReturn(null); + } else { + when(u2FService.startAuthentication(any())).thenReturn(authenticateRequestData); + } + Map parameterMap = new HashMap<>(); parameterMap.put(FIDOAuthenticatorConstants.APP_ID, "https://localhost:9443"); parameterMap.put(FIDOAuthenticatorConstants.FIDO_AUTH, "fido-auth");