Skip to content

Commit

Permalink
Fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Aug 8, 2023
1 parent bfdb706 commit e9e69c3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
public interface OrcidClientCredentialEndPointDelegator {

Response obtainOauth2Token(String authorization, MultivaluedMap<String, String> formParams);

void setTokenCacheEnabled(boolean enabled);

boolean isTokenCacheEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public Response obtainOauth2Token(String authorization, MultivaluedMap<String, S
}
}

//TODO: Store the token in the cache before returning it to the user
removeMetadataFromToken(token);
setToCache(client.getName(), token);
return getResponse(token);
Expand Down Expand Up @@ -329,4 +328,12 @@ protected Authentication getClientAuthentication() {

}

public boolean isTokenCacheEnabled() {
return isTokenCacheEnabled;
}

public void setTokenCacheEnabled(boolean isTokenCacheEnabled) {
this.isTokenCacheEnabled = isTokenCacheEnabled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.core.MultivaluedMap;
Expand All @@ -23,8 +26,11 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.orcid.core.constants.OrcidOauth2Constants;
import org.orcid.core.oauth.openid.OpenIDConnectKeyService;
import org.orcid.core.utils.JsonUtils;
import org.orcid.core.utils.SecurityContextTestUtils;
import org.orcid.core.utils.cache.redis.RedisClient;
import org.orcid.jaxb.model.message.ScopePathType;
import org.orcid.persistence.dao.OrcidOauth2AuthoriziationCodeDetailDao;
import org.orcid.persistence.dao.ProfileLastModifiedDao;
Expand Down Expand Up @@ -64,6 +70,9 @@ public class OrcidClientCredentialEndPointDelegatorTest extends DBUnitTest {
@Mock
private ProfileLastModifiedDao profileLastModifiedDaoMock;

@Mock
private RedisClient redisClientMock;

@Resource
private ProfileLastModifiedDao profileLastModifiedDao;

Expand All @@ -77,6 +86,9 @@ public static void initDBUnitData() throws Exception {
public void before() {
MockitoAnnotations.initMocks(this);
TargetProxyHelper.injectIntoProxy(orcidClientCredentialEndPointDelegator, "profileLastModifiedDao", profileLastModifiedDaoMock);
TargetProxyHelper.injectIntoProxy(orcidClientCredentialEndPointDelegator, "redisClient", redisClientMock);
// Keep the cache disabled
orcidClientCredentialEndPointDelegator.setTokenCacheEnabled(false);
}

@AfterClass
Expand Down Expand Up @@ -268,4 +280,66 @@ public void generateRefreshTokenThatExpireAfterParentTokenTest() {
assertTrue(token.getExpiration().getTime() > refreshToken.getExpiration().getTime());
}

@Test
public void obtainOauth2TokenSetCacheTest() {
// Enable cache
orcidClientCredentialEndPointDelegator.setTokenCacheEnabled(true);
SecurityContextTestUtils.setUpSecurityContextForClientOnly(CLIENT_ID_1, ScopePathType.ACTIVITIES_UPDATE, ScopePathType.READ_LIMITED);
OrcidOauth2AuthoriziationCodeDetail authCode = createAuthorizationCode("code-1", CLIENT_ID_1, "http://www.APP-5555555555555555.com/redirect/oauth", true,
"/activities/update");
MultivaluedMap<String, String> formParams = new MultivaluedHashMap<String, String>();
formParams.add("client_id", CLIENT_ID_1);
formParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
formParams.add("grant_type", "authorization_code");
formParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
formParams.add("code", authCode.getId());
Response response = orcidClientCredentialEndPointDelegator.obtainOauth2Token(null, formParams);
assertNotNull(response);
assertNotNull(response.getEntity());
DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) response.getEntity();
assertNotNull(token);
assertTrue(!PojoUtil.isEmpty(token.getValue()));

String tokenValue = token.getValue();


Map<String, String> tokenData = new HashMap<String, String>();
tokenData.put(OrcidOauth2Constants.ACCESS_TOKEN, tokenValue);
tokenData.put(OrcidOauth2Constants.TOKEN_EXPIRATION_TIME, String.valueOf(token.getExpiration().getTime()));
StringBuilder sb = new StringBuilder();
token.getScope().forEach(x -> {sb.append(x); sb.append(' ');});
tokenData.put(OrcidOauth2Constants.SCOPE_PARAM, sb.toString());
tokenData.put(OrcidOauth2Constants.ORCID, (String) token.getAdditionalInformation().get(OrcidOauth2Constants.ORCID));
tokenData.put(OrcidOauth2Constants.CLIENT_ID, CLIENT_ID_1);
tokenData.put(OrcidOauth2Constants.RESOURCE_IDS, OrcidOauth2Constants.ORCID);
tokenData.put(OrcidOauth2Constants.APPROVED, Boolean.TRUE.toString());

String tokenDataString = JsonUtils.convertToJsonString(tokenData);

verify(redisClientMock, times(1)).set(Mockito.eq(tokenValue), Mockito.eq(tokenDataString));
}

@Test
public void obtainOauth2TokenSkipCacheTest() {
// Ensure cache is disabled
orcidClientCredentialEndPointDelegator.setTokenCacheEnabled(false);

SecurityContextTestUtils.setUpSecurityContextForClientOnly(CLIENT_ID_1, ScopePathType.ACTIVITIES_UPDATE, ScopePathType.READ_LIMITED);
OrcidOauth2AuthoriziationCodeDetail authCode = createAuthorizationCode("code-1", CLIENT_ID_1, "http://www.APP-5555555555555555.com/redirect/oauth", true,
"/activities/update");
MultivaluedMap<String, String> formParams = new MultivaluedHashMap<String, String>();
formParams.add("client_id", CLIENT_ID_1);
formParams.add("client_secret", "DhkFj5EI0qp6GsUKi55Vja+h+bsaKpBx");
formParams.add("grant_type", "authorization_code");
formParams.add("redirect_uri", "http://www.APP-5555555555555555.com/redirect/oauth");
formParams.add("code", authCode.getId());
Response response = orcidClientCredentialEndPointDelegator.obtainOauth2Token(null, formParams);
assertNotNull(response);
assertNotNull(response.getEntity());
DefaultOAuth2AccessToken token = (DefaultOAuth2AccessToken) response.getEntity();
assertNotNull(token);
assertTrue(!PojoUtil.isEmpty(token.getValue()));

verify(redisClientMock, never()).set(Mockito.any(), Mockito.any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ org.orcid.core.api.analytics.endpoint=https://www.google-analytics.com/collect

# Swagger
org.orcid.swagger.tokenendpoint=https://localhost:8443/orcid-api-web/oauth/token
org.orcid.swagger.authendpoint=https://localhost:8443/orcid-web/oauth/authorize
org.orcid.swagger.authendpoint=https://localhost:8443/orcid-web/oauth/authorize

# Redis
org.orcid.core.utils.cache.redis.host=xxx.xxx.com
org.orcid.core.utils.cache.redis.port=6379
org.orcid.core.utils.cache.redis.password=XXXX

0 comments on commit e9e69c3

Please sign in to comment.