Skip to content

Commit eaba6be

Browse files
committed
Add unit tests
1 parent 9196f79 commit eaba6be

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/token/JWTTokenIssuer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public JWTTokenIssuer() throws IdentityOAuth2Exception {
123123
@Override
124124
public String getAccessTokenType() {
125125

126-
return "JWT";
126+
return JWT_TYP_HEADER_VALUE;
127127
}
128128

129129
/**

components/org.wso2.carbon.identity.oauth/src/main/java/org/wso2/carbon/identity/oauth2/token/handlers/grant/AbstractAuthorizationGrantHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ protected String getTokenBindingReference(OAuthTokenReqMessageContext tokReqMsgC
12411241
"Error while retrieving oauth issuer for the app with clientId: " + consumerKey, e);
12421242
}
12431243

1244-
String tokenType = oauthTokenIssuer.getAccessTokenType();
1244+
String tokenType = oauthTokenIssuer.getAccessTokenType();
12451245

12461246
if (JWT.equalsIgnoreCase(tokenType)) {
12471247
if (getRenewWithoutRevokingExistingStatus()

components/org.wso2.carbon.identity.oauth/src/test/java/org/wso2/carbon/identity/oauth2/token/handlers/grant/AbstractAuthorizationGrantHandlerTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.wso2.carbon.identity.oauth2.token.handlers.grant;
2020

2121
import org.mockito.Mock;
22+
import org.mockito.MockedStatic;
2223
import org.mockito.MockitoAnnotations;
2324
import org.testng.annotations.AfterClass;
2425
import org.testng.annotations.BeforeClass;
@@ -36,6 +37,7 @@
3637
import org.wso2.carbon.identity.common.testng.WithH2Database;
3738
import org.wso2.carbon.identity.common.testng.WithRealmService;
3839
import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
40+
import org.wso2.carbon.identity.core.util.IdentityUtil;
3941
import org.wso2.carbon.identity.event.services.IdentityEventService;
4042
import org.wso2.carbon.identity.oauth.IdentityOAuthAdminException;
4143
import org.wso2.carbon.identity.oauth.common.GrantType;
@@ -47,11 +49,17 @@
4749
import org.wso2.carbon.identity.oauth.internal.OAuthComponentServiceHolder;
4850
import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
4951
import org.wso2.carbon.identity.oauth2.TestConstants;
52+
import org.wso2.carbon.identity.oauth2.dao.AccessTokenDAOImpl;
53+
import org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory;
5054
import org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO;
5155
import org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO;
5256
import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
5357
import org.wso2.carbon.identity.oauth2.model.AccessTokenDO;
58+
import org.wso2.carbon.identity.oauth2.token.JWTTokenIssuer;
5459
import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;
60+
import org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer;
61+
import org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding;
62+
import org.wso2.carbon.identity.oauth2.util.OAuth2Util;
5563
import org.wso2.carbon.identity.oauth2.validators.OAuth2ScopeHandler;
5664

5765
import java.util.Collections;
@@ -63,7 +71,9 @@
6371

6472
import static org.mockito.ArgumentMatchers.any;
6573
import static org.mockito.ArgumentMatchers.anyMap;
74+
import static org.mockito.ArgumentMatchers.anyString;
6675
import static org.mockito.Mockito.mock;
76+
import static org.mockito.Mockito.mockStatic;
6777
import static org.mockito.Mockito.verify;
6878
import static org.mockito.Mockito.when;
6979
import static org.testng.Assert.assertEquals;
@@ -142,6 +152,58 @@ public void tearDown() {
142152
CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(null);
143153
}
144154

155+
@Test(dataProvider = "IssueWithRenewDataProvider", expectedExceptions = IdentityOAuth2Exception.class)
156+
public void testIssueWithRenewWithoutRevokingExistingEnabled
157+
(boolean cacheEnabled, boolean cacheEntryAvailable, long cachedTokenValidity,
158+
long cachedRefreshTokenValidity, long dbTokenValidity, long dbRefreshTokenValidity,
159+
boolean dbEntryAvailable, String dbTokenState, boolean tokenLoggable, boolean isIDPIdColumnEnabled,
160+
boolean setBindingReference) throws Exception {
161+
162+
OAuth2ServiceComponentHolder.setIDPIdColumnEnabled(isIDPIdColumnEnabled);
163+
164+
Map<String, AuthorizationGrantHandler> supportedGrantTypes = new HashMap<>();
165+
supportedGrantTypes.put("refresh_token", refreshGrantHandler);
166+
167+
OAuth2AccessTokenReqDTO oAuth2AccessTokenReqDTO = new OAuth2AccessTokenReqDTO();
168+
oAuth2AccessTokenReqDTO.setClientId(clientId);
169+
oAuth2AccessTokenReqDTO.setGrantType(PASSWORD_GRANT); // Ensure the grant type is valid for renewal
170+
171+
OAuthTokenReqMessageContext tokReqMsgCtx = new OAuthTokenReqMessageContext(oAuth2AccessTokenReqDTO);
172+
tokReqMsgCtx.setAuthorizedUser(authenticatedUser);
173+
tokReqMsgCtx.setScope(new String[]{"scope1", "scope2"});
174+
175+
tokReqMsgCtx.addProperty("OAuthAppDO", oAuthAppDO);
176+
177+
TokenBinding tokenBinding = new TokenBinding();
178+
if (setBindingReference) {
179+
tokenBinding.setBindingReference("bindingReference");
180+
}
181+
tokReqMsgCtx.setTokenBinding(tokenBinding);
182+
183+
// Mocking static methods using try-with-resources
184+
try (MockedStatic<IdentityUtil> identityUtil = mockStatic(IdentityUtil.class);
185+
MockedStatic<OAuth2Util> oauth2Util = mockStatic(OAuth2Util.class)) {
186+
187+
identityUtil.when(() -> IdentityUtil.getProperty(anyString()))
188+
.thenReturn(Boolean.TRUE.toString());
189+
190+
OAuthComponentServiceHolder.getInstance().setActionExecutorService(mockActionExecutionService);
191+
OAuthTokenPersistenceFactory persistenceFactory = mock(OAuthTokenPersistenceFactory.class);
192+
when(persistenceFactory.getAccessTokenDAO()).thenReturn(new AccessTokenDAOImpl());
193+
194+
OauthTokenIssuer oauthTokenIssuer = mock(JWTTokenIssuer.class);
195+
when(oauthTokenIssuer.getAccessTokenType()).thenReturn("jwt");
196+
oauth2Util.when(() -> OAuth2Util.getOAuthTokenIssuerForOAuthApp(clientId)).thenReturn(oauthTokenIssuer);
197+
oauth2Util.when(() -> OAuth2Util.getAppInformationByClientId(clientId)).thenReturn(oAuthAppDO);
198+
199+
// Set allowed grant types (ensure PASSWORD_GRANT is allowed for renewal)
200+
OAuth2ServiceComponentHolder.setJwtRenewWithoutRevokeAllowedGrantTypes(
201+
Collections.singletonList("password")); // This allows PASSWORD_GRANT
202+
203+
OAuth2AccessTokenRespDTO tokenRespDTO = handler.issue(tokReqMsgCtx);
204+
}
205+
}
206+
145207
@DataProvider(name = "IssueDataProvider")
146208
public Object[][] issueDataProvider() {
147209
return new Object[][] {
@@ -174,6 +236,14 @@ public Object[][] issueDataProvider() {
174236
{false, true, 0L, 0L, -1L, 3600L, true, TOKEN_STATE_ACTIVE, true, false}};
175237
}
176238

239+
@DataProvider(name = "IssueWithRenewDataProvider")
240+
public Object[][] issueWithRenewDataProvider() {
241+
return new Object[][]{
242+
{true, true, 3600L, 3600L, 0L, 0L, false, TOKEN_STATE_ACTIVE, false, true, true},
243+
{true, true, 3600L, 3600L, 0L, 0L, false, TOKEN_STATE_ACTIVE, false, true, false}
244+
};
245+
}
246+
177247
@Test(dataProvider = "IssueDataProvider")
178248
public void testIssue(boolean cacheEnabled, boolean cacheEntryAvailable, long cachedTokenValidity,
179249
long cachedRefreshTokenValidity, long dbTokenValidity, long dbRefreshTokenValidity,

0 commit comments

Comments
 (0)