Search in sources :

Example 16 with AuthzCodeDO

use of org.wso2.carbon.identity.oauth2.model.AuthzCodeDO in project identity-inbound-auth-oauth by wso2-extensions.

the class AuthorizationCodeDAOImplTest method testGetAuthorizationCodesByConsumerKey.

@Test
public void testGetAuthorizationCodesByConsumerKey() throws Exception {
    String consumerKey = UUID.randomUUID().toString();
    String authzCodeID = UUID.randomUUID().toString();
    String authzCode = UUID.randomUUID().toString();
    mockStatic(OAuth2Util.class);
    when(OAuth2Util.getTenantId(anyString())).thenReturn(DEFAULT_TENANT_ID);
    AuthzCodeDO authzCodeDO = persistAuthorizationCode(consumerKey, authzCodeID, authzCode, OAuthConstants.AuthorizationCodeState.ACTIVE);
    Set<String> availableAuthzCodes = new HashSet<>();
    availableAuthzCodes.add(authzCode);
    Assert.assertEquals(authorizationCodeDAO.getAuthorizationCodesByConsumerKey(authzCodeDO.getConsumerKey()), availableAuthzCodes);
    Assert.assertTrue(authorizationCodeDAO.getAuthorizationCodesByConsumerKey(UUID.randomUUID().toString()).isEmpty());
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 17 with AuthzCodeDO

use of org.wso2.carbon.identity.oauth2.model.AuthzCodeDO in project identity-inbound-auth-oauth by wso2-extensions.

the class AuthorizationCodeDAOImplTest method testGetActiveAuthorizationCodesByConsumerKey.

@Test
public void testGetActiveAuthorizationCodesByConsumerKey() throws Exception {
    String consumerKey1 = UUID.randomUUID().toString();
    String authzCodeID1 = UUID.randomUUID().toString();
    String authzCode1 = UUID.randomUUID().toString();
    String consumerKey2 = UUID.randomUUID().toString();
    String authzCodeID2 = UUID.randomUUID().toString();
    String authzCode2 = UUID.randomUUID().toString();
    mockStatic(OAuth2Util.class);
    when(OAuth2Util.getTenantId(anyString())).thenReturn(DEFAULT_TENANT_ID);
    AuthzCodeDO authzCodeDO1 = persistAuthorizationCode(consumerKey1, authzCodeID1, authzCode1, OAuthConstants.AuthorizationCodeState.ACTIVE);
    AuthzCodeDO authzCodeDO2 = persistAuthorizationCode(consumerKey2, authzCodeID2, authzCode2, OAuthConstants.AuthorizationCodeState.ACTIVE);
    // If state is EXPIRED/INACTIVE needs to revoke token as well.
    mockStatic(OAuth2TokenUtil.class);
    doNothing().when(OAuth2TokenUtil.class, "postRevokeCode", anyString(), anyString(), anyString());
    authorizationCodeDAO.updateAuthorizationCodeState(authzCodeDO1.getAuthorizationCode(), OAuthConstants.AuthorizationCodeState.REVOKED);
    Set<String> availableAuthzCodes = new HashSet<>();
    availableAuthzCodes.add(authzCode2);
    Assert.assertEquals(authorizationCodeDAO.getActiveAuthorizationCodesByConsumerKey(authzCodeDO2.getConsumerKey()), availableAuthzCodes);
    Assert.assertTrue(authorizationCodeDAO.getActiveAuthorizationCodesByConsumerKey(UUID.randomUUID().toString()).isEmpty());
    Assert.assertTrue(authorizationCodeDAO.getActiveAuthorizationCodesByConsumerKey(authzCodeDO1.getConsumerKey()).isEmpty());
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 18 with AuthzCodeDO

use of org.wso2.carbon.identity.oauth2.model.AuthzCodeDO in project identity-inbound-auth-oauth by wso2-extensions.

the class AuthorizationCodeDAOImplTest method persistAuthorizationCode.

private AuthzCodeDO persistAuthorizationCode(String consumerKey, String authzCodeId, String authzCode, String status) throws Exception {
    createApplication(consumerKey, UUID.randomUUID().toString(), DEFAULT_TENANT_ID);
    AuthzCodeDO authzCodeDO = new AuthzCodeDO(authenticatedUser, scopes, new Timestamp(System.currentTimeMillis()), 3600000L, CALLBACK, consumerKey, authzCode, authzCodeId, status, null, null);
    mockStatic(OAuth2Util.class);
    when(OAuth2Util.getTenantId(anyString())).thenReturn(DEFAULT_TENANT_ID);
    when(OAuth2Util.getUserStoreDomain(any())).thenReturn(UserCoreConstants.PRIMARY_DEFAULT_DOMAIN_NAME);
    when(OAuth2Util.getAuthenticatedIDP(any())).thenReturn("LOCAL");
    authorizationCodeDAO.insertAuthorizationCode(authzCode, consumerKey, CALLBACK, authzCodeDO);
    return authzCodeDO;
}
Also used : AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) Timestamp(java.sql.Timestamp)

Example 19 with AuthzCodeDO

use of org.wso2.carbon.identity.oauth2.model.AuthzCodeDO in project identity-inbound-auth-oauth by wso2-extensions.

the class AuthorizationCodeDAOImplTest method testDeactivateAuthorizationCode.

@Test
public void testDeactivateAuthorizationCode() throws Exception {
    String consumerKey1 = UUID.randomUUID().toString();
    String authzCodeID1 = UUID.randomUUID().toString();
    String authzCode1 = UUID.randomUUID().toString();
    String consumerKey2 = UUID.randomUUID().toString();
    String authzCodeID2 = UUID.randomUUID().toString();
    String authzCode2 = UUID.randomUUID().toString();
    AuthzCodeDO authzCodeDO1 = persistAuthorizationCode(consumerKey1, authzCodeID1, authzCode1, OAuthConstants.AuthorizationCodeState.ACTIVE);
    AuthzCodeDO authzCodeDO2 = persistAuthorizationCode(consumerKey2, authzCodeID2, authzCode2, OAuthConstants.AuthorizationCodeState.ACTIVE);
    mockStatic(OAuth2Util.class);
    mockStatic(IdentityUtil.class);
    authzCodeDO1.setOauthTokenId(UUID.randomUUID().toString());
    // If state is EXPIRED/INACTIVE needs to revoke token as well.
    mockStatic(OAuth2TokenUtil.class);
    doNothing().when(OAuth2TokenUtil.class, "postRevokeCode", anyString(), anyString(), anyString());
    authorizationCodeDAO.deactivateAuthorizationCode(authzCodeDO1);
    Assert.assertTrue(authorizationCodeDAO.getActiveAuthorizationCodesByConsumerKey(authzCodeDO1.getConsumerKey()).isEmpty());
    Assert.assertFalse(authorizationCodeDAO.getAuthorizationCodesByConsumerKey(authzCodeDO2.getConsumerKey()).isEmpty());
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 20 with AuthzCodeDO

use of org.wso2.carbon.identity.oauth2.model.AuthzCodeDO in project identity-inbound-auth-oauth by wso2-extensions.

the class AuthorizationCodeDAOImplTest method testValidateAuthorizationCode.

@Test
public void testValidateAuthorizationCode() throws Exception {
    String consumerKey = UUID.randomUUID().toString();
    String authzCodeID = UUID.randomUUID().toString();
    String authzCode = UUID.randomUUID().toString();
    AuthzCodeDO authzCodeDO = persistAuthorizationCode(consumerKey, authzCodeID, authzCode, OAuthConstants.AuthorizationCodeState.ACTIVE);
    OAuth2ServiceComponentHolder.setIDPIdColumnEnabled(false);
    OAuth2ServiceComponentHolder.setApplicationMgtService(mockedApplicationManagementService);
    when(mockedApplicationManagementService.getServiceProviderByClientId(anyString(), any(), anyString())).thenReturn(mockedServiceProvider);
    when(OAuth2Util.createAuthenticatedUser(anyString(), anyString(), anyString(), anyString())).thenReturn(mockedAuthenticatedUser);
    doNothing().when(mockedAuthenticatedUser, "setAuthenticatedSubjectIdentifier", anyString(), anyObject());
    Assert.assertNotNull(authorizationCodeDAO.validateAuthorizationCode(authzCodeDO.getConsumerKey(), authzCodeDO.getAuthorizationCode()));
}
Also used : Matchers.anyString(org.mockito.Matchers.anyString) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Aggregations

AuthzCodeDO (org.wso2.carbon.identity.oauth2.model.AuthzCodeDO)38 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)18 Test (org.testng.annotations.Test)11 Connection (java.sql.Connection)8 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 Timestamp (java.sql.Timestamp)8 Matchers.anyString (org.mockito.Matchers.anyString)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)8 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)8 ArrayList (java.util.ArrayList)7 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 OAuthCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthCacheKey)5 HashSet (java.util.HashSet)4 OAuthTokenReqMessageContext (org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext)4 Map (java.util.Map)3 DataProvider (org.testng.annotations.DataProvider)3