Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SujanSanjula96 committed Jan 8, 2025
1 parent d04e7dc commit f928a9a
Showing 1 changed file with 17 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -829,23 +829,8 @@ public void testAuthorizeForAuthenticationResponse(boolean isResultInRequest, bo
}
}

@DataProvider(name = "provideAuthenticatedDataDevice")
public Object[][] provideAuthenticatedDataDevice() {

return addDiagnosticLogStatusToExistingDataProvider(new Object[][]{
{true, true, new HashMap(), null, null, null,
new HashSet<>(Collections.singletonList(OAuthConstants.Scope.OPENID)),
RESPONSE_MODE_FORM_POST, APP_REDIRECT_URL, HttpServletResponse.SC_FOUND}
});
}

@Test(dataProvider = "provideAuthenticatedDataDevice", groups = "testWithConnection")
public void testAuthorizeForAuthenticationResponseForDevice(boolean isResultInRequest, boolean isAuthenticated,
Map<ClaimMapping, String> attributes, String errorCode,
String errorMsg, String errorUri, Set<String> scopes,
String responseMode, String redirectUri, int expected,
boolean diagnosticLogsEnabled)
throws Exception {
@Test(groups = "testWithConnection")
public void testAuthorizeForDeviceFlowAuthenticationResponse() throws Exception {

try (MockedStatic<OAuthServerConfiguration> oAuthServerConfiguration = mockStatic(
OAuthServerConfiguration.class);) {
Expand All @@ -868,19 +853,10 @@ public void testAuthorizeForAuthenticationResponseForDevice(boolean isResultInRe
sessionDataCache.when(SessionDataCache::getInstance).thenReturn(mockSessionDataCache);
SessionDataCacheKey loginDataCacheKey = new SessionDataCacheKey(SESSION_DATA_KEY_VALUE);
when(mockSessionDataCache.getValueFromCache(loginDataCacheKey)).thenReturn(loginCacheEntry);
loggerUtils.when(LoggerUtils::isDiagnosticLogsEnabled).thenReturn(diagnosticLogsEnabled);
loggerUtils.when(LoggerUtils::isDiagnosticLogsEnabled).thenReturn(false);

AuthenticationResult result =
setAuthenticationResult(isAuthenticated, attributes, errorCode, errorMsg, errorUri);

AuthenticationResult resultInRequest = null;
AuthenticationResultCacheEntry authResultCacheEntry = null;
if (isResultInRequest) {
resultInRequest = result;
} else {
authResultCacheEntry = new AuthenticationResultCacheEntry();
authResultCacheEntry.setResult(result);
}
setAuthenticationResult(true, new HashMap<>(), null, null, null);

Map<String, String[]> requestParams = new HashMap<>();
Map<String, Object> requestAttributes = new HashMap<>();
Expand All @@ -891,7 +867,7 @@ public void testAuthorizeForAuthenticationResponseForDevice(boolean isResultInRe

requestAttributes.put(FrameworkConstants.RequestParams.FLOW_STATUS, AuthenticatorFlowStatus.INCOMPLETE);
requestAttributes.put(FrameworkConstants.SESSION_DATA_KEY, SESSION_DATA_KEY_VALUE);
requestAttributes.put(FrameworkConstants.RequestAttribute.AUTH_RESULT, resultInRequest);
requestAttributes.put(FrameworkConstants.RequestAttribute.AUTH_RESULT, result);

mockHttpRequest(requestParams, requestAttributes, HttpMethod.POST);

Expand All @@ -904,7 +880,8 @@ public void testAuthorizeForAuthenticationResponseForDevice(boolean isResultInRe
identityUtil.when(() -> IdentityUtil.getServerURL(anyString(), anyBoolean(), anyBoolean()))
.thenReturn("https://localhost:9443/carbon");

OAuth2Parameters oAuth2Params = setOAuth2Parameters(scopes, APP_NAME, responseMode, redirectUri);
Set<String> scopes = new HashSet<>(Collections.singletonList(OAuthConstants.Scope.OPENID));
OAuth2Parameters oAuth2Params = setOAuth2Parameters(scopes, APP_NAME, null, null);
oAuth2Params.setClientId(CLIENT_ID_VALUE);
oAuth2Params.setState(STATE);
oAuth2Params.setResponseType(Constants.RESPONSE_TYPE_DEVICE);
Expand All @@ -926,9 +903,8 @@ public void testAuthorizeForAuthenticationResponseForDevice(boolean isResultInRe
OAuth2AuthorizeReqDTO authzReqDTO = new OAuth2AuthorizeReqDTO();
authzReqDTO.setConsumerKey(CLIENT_ID_VALUE);
authzReqDTO.setScopes(new String[]{OAuthConstants.Scope.OPENID});
authzReqDTO.setCallbackUrl(redirectUri);
authzReqDTO.setCallbackUrl(null);
authzReqDTO.setUser(loginCacheEntry.getLoggedInUser());
authzReqDTO.setResponseType(Constants.RESPONSE_TYPE_DEVICE);
OAuthAuthzReqMessageContext authzReqMsgCtx = new OAuthAuthzReqMessageContext(authzReqDTO);
authzReqMsgCtx.setApprovedScope(new String[]{OAuthConstants.Scope.OPENID});
when(oAuth2Service.validateScopesBeforeConsent(any(OAuth2AuthorizeReqDTO.class))).thenReturn(
Expand All @@ -950,19 +926,21 @@ public void testAuthorizeForAuthenticationResponseForDevice(boolean isResultInRe
mockApplicationManagementService();

mockEndpointUtil(false, endpointUtil);
when(oAuth2Service.handleAuthenticationFailure(oAuth2Params)).thenReturn(oAuthErrorDTO);
when(oAuth2ScopeService.hasUserProvidedConsentForAllRequestedScopes(
anyString(), isNull(), anyInt(), anyList())).thenReturn(true);

OAuth2AuthorizeRespDTO authzRespDTO = new OAuth2AuthorizeRespDTO();
authzRespDTO.setCallbackURI("https://localhost:9443/authenticationendpoint/device_success.do" +
"?app_name=" + APP_NAME);
when(oAuth2Service.authorize(authzReqMsgCtx)).thenReturn(authzRespDTO);

mockServiceURLBuilder(serviceURLBuilder);
setSupportedResponseModes();
Response response = oAuth2AuthzEndpoint.authorize(httpServletRequest, httpServletResponse);
assertEquals(response.getStatus(), expected, "Unexpected HTTP response status");
if (!isAuthenticated) {
String expectedState =
"name=\"" + OAuthConstants.OAuth20Params.STATE + "\" value=\"" + STATE + "\"";
assertTrue(response.getEntity().toString().contains(expectedState));
}
assertEquals(response.getStatus(), HttpServletResponse.SC_FOUND, "Unexpected HTTP response status");
String expectedState = OAuthConstants.OAuth20Params.STATE + "=" + STATE;
MultivaluedMap<String, Object> responseMetadata = response.getMetadata();
assertTrue(responseMetadata.get("Location").toString().contains(expectedState));
}
}
}
Expand Down

0 comments on commit f928a9a

Please sign in to comment.