Skip to content

Commit

Permalink
Improve test coverage for ciba weblink authenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
hasithakn committed Nov 6, 2024
1 parent 3de4b0d commit 30cd106
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.wso2.openbanking.accelerator.identity.auth.extensions.response.validator;

import com.wso2.openbanking.accelerator.common.util.Generated;
import org.apache.oltu.oauth2.common.exception.OAuthProblemException;
import org.wso2.carbon.identity.oauth.ciba.handlers.CibaResponseTypeValidator;

Expand All @@ -27,9 +28,11 @@
/**
* Validates authorize responses with cibaAuthCode as response type.
*/
@Generated(message = "Ignoring since method do not contain a logic")
public class OBCibaResponseTypeValidator extends CibaResponseTypeValidator {

@Override
@Generated(message = "Ignoring since method do not contain a logic")
public void validateContentType(HttpServletRequest request) throws OAuthProblemException {
// Overriding content type validation
// This is for browser flow with cibaAuthCode response type. (Web-Auth link scenario)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@
import org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser;
import org.wso2.carbon.identity.oauth.ciba.dao.CibaDAOFactory;
import org.wso2.carbon.identity.oauth.ciba.dao.CibaMgtDAO;
import org.wso2.carbon.identity.oauth.ciba.exceptions.CibaCoreException;
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
import org.wso2.carbon.identity.oauth2.authz.OAuthAuthzReqMessageContext;
import org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeReqDTO;

import java.util.ArrayList;

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;

@PowerMockIgnore("jdk.internal.reflect.*")
Expand Down Expand Up @@ -114,4 +117,80 @@ public void obCibaResponseTypeHandlerValidConsentIdTest() throws Exception {
obCibaResponseTypeHandler.issue(oAuthAuthzReqMessageContext);

}

@Test(expectedExceptions = IdentityOAuth2Exception.class)
public void obCibaResponseTypeHandlerInValidCallbackTest() throws Exception {

PowerMockito.when(OpenBankingConfigParser.getInstance().getCibaAuthenticationRedirectEndpoint())
.thenReturn("");
OBCibaResponseTypeHandler obCibaResponseTypeHandler = new OBCibaResponseTypeHandler();
OAuthAuthzReqMessageContext oAuthAuthzReqMessageContext = mock(OAuthAuthzReqMessageContext.class);
AuthenticatedUser cibaAuthenticatedUser = mock(AuthenticatedUser.class);
OAuth2AuthorizeReqDTO authorizationReqDTO = mock(OAuth2AuthorizeReqDTO.class);
doReturn(authorizationReqDTO).when(oAuthAuthzReqMessageContext).getAuthorizationReqDTO();
doReturn(cibaAuthenticatedUser).when(authorizationReqDTO).getUser();
ArrayList<Object> consentIds = new ArrayList<>();
consentIds.add("test1");
doReturn(consentIds).when(consentCoreServiceMock)
.getConsentIdByConsentAttributeNameAndValue(anyString(), anyString());
DetailedConsentResource consentResourceMock = mock(DetailedConsentResource.class);
doReturn(consentResourceMock).when(consentCoreServiceMock).getDetailedConsent(anyString());
doReturn("authorised").when(consentResourceMock).getCurrentStatus();

CibaMgtDAO cibaMgtDAO = mock(CibaMgtDAO.class);
doReturn(cibaMgtDAO).when(cibaDAOFactory).getCibaAuthMgtDAO();
doReturn("authCode").when(cibaMgtDAO).getCibaAuthCodeKey(anyString());
obCibaResponseTypeHandler.issue(oAuthAuthzReqMessageContext);

}

@Test(expectedExceptions = IdentityOAuth2Exception.class)
public void obCibaResponseTypeHandlerInValidTest() throws Exception {

PowerMockito.when(OpenBankingConfigParser.getInstance().getCibaAuthenticationRedirectEndpoint())
.thenReturn("test");
OBCibaResponseTypeHandler obCibaResponseTypeHandler = new OBCibaResponseTypeHandler();
OAuthAuthzReqMessageContext oAuthAuthzReqMessageContext = mock(OAuthAuthzReqMessageContext.class);
AuthenticatedUser cibaAuthenticatedUser = mock(AuthenticatedUser.class);
OAuth2AuthorizeReqDTO authorizationReqDTO = mock(OAuth2AuthorizeReqDTO.class);
doReturn(authorizationReqDTO).when(oAuthAuthzReqMessageContext).getAuthorizationReqDTO();
doReturn(cibaAuthenticatedUser).when(authorizationReqDTO).getUser();
ArrayList<Object> consentIds = new ArrayList<>();
consentIds.add("test1");
doReturn(consentIds).when(consentCoreServiceMock)
.getConsentIdByConsentAttributeNameAndValue(anyString(), anyString());
DetailedConsentResource consentResourceMock = mock(DetailedConsentResource.class);
doReturn(consentResourceMock).when(consentCoreServiceMock).getDetailedConsent(anyString());
doReturn("authorised").when(consentResourceMock).getCurrentStatus();

CibaMgtDAO cibaMgtDAO = mock(CibaMgtDAO.class);
doReturn(cibaMgtDAO).when(cibaDAOFactory).getCibaAuthMgtDAO();
doThrow(new CibaCoreException("")).when(cibaMgtDAO).getCibaAuthCodeKey(anyString());
obCibaResponseTypeHandler.issue(oAuthAuthzReqMessageContext);

}

@Test(expectedExceptions = IdentityOAuth2Exception.class)
public void obCibaResponseTypeHandlerConsentManagementErrorTest() throws Exception {

PowerMockito.when(OpenBankingConfigParser.getInstance().getCibaAuthenticationRedirectEndpoint())
.thenReturn("test");
OBCibaResponseTypeHandler obCibaResponseTypeHandler = new OBCibaResponseTypeHandler();
OAuthAuthzReqMessageContext oAuthAuthzReqMessageContext = mock(OAuthAuthzReqMessageContext.class);
AuthenticatedUser cibaAuthenticatedUser = mock(AuthenticatedUser.class);
OAuth2AuthorizeReqDTO authorizationReqDTO = mock(OAuth2AuthorizeReqDTO.class);
doReturn(authorizationReqDTO).when(oAuthAuthzReqMessageContext).getAuthorizationReqDTO();
doReturn(cibaAuthenticatedUser).when(authorizationReqDTO).getUser();
ArrayList<Object> consentIds = new ArrayList<>();
consentIds.add("test1");
doReturn(consentIds).when(consentCoreServiceMock)
.getConsentIdByConsentAttributeNameAndValue(anyString(), anyString());
DetailedConsentResource consentResourceMock = mock(DetailedConsentResource.class);
doThrow(new ConsentManagementException("")).when(consentCoreServiceMock).getDetailedConsent(anyString());
doReturn("authorised").when(consentResourceMock).getCurrentStatus();
CibaMgtDAO cibaMgtDAO = mock(CibaMgtDAO.class);
doReturn(cibaMgtDAO).when(cibaDAOFactory).getCibaAuthMgtDAO();
obCibaResponseTypeHandler.issue(oAuthAuthzReqMessageContext);

}
}

0 comments on commit 30cd106

Please sign in to comment.