Skip to content

Commit

Permalink
Adding a data provider to reduce code lines
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaminduDilshan committed Apr 28, 2022
1 parent 1b8c506 commit 1e943f4
Showing 1 changed file with 17 additions and 52 deletions.
Expand Up @@ -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<AuthenticatorConfig> 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<Integer, StepConfig> 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<String, String> 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<AuthenticatorConfig> authenticatorList = new ArrayList<>();
Expand Down Expand Up @@ -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<String, String> parameterMap = new HashMap<>();
parameterMap.put(FIDOAuthenticatorConstants.APP_ID, "https://localhost:9443");
parameterMap.put(FIDOAuthenticatorConstants.FIDO_AUTH, "fido-auth");
Expand Down

0 comments on commit 1e943f4

Please sign in to comment.