Skip to content

Commit

Permalink
fix issues after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
AmaliMatharaarachchi committed Oct 30, 2023
1 parent 34c427b commit f5c075b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
Expand Down Expand Up @@ -60,6 +59,7 @@ public class JWTValidatorTest {
@Test
public void testJWTValidator() throws APISecurityException, EnforcerException {
String organization = "org1";
String environment = "development";
String issuer = "https://localhost:9443/oauth2/token";
String signature = "sBgeoqJn0log5EZflj_G7ADvm6B3KQ9bdfFCEFVQS1U3oY9" +
"-cqPwAPyOLLh95pdfjYjakkf1UtjPZjeIupwXnzg0SffIc704RoVlZocAx9Ns2XihjU6Imx2MbXq9ARmQxQkyGVkJUMTwZ8" +
Expand Down Expand Up @@ -102,6 +102,7 @@ public void testJWTValidator() throws APISecurityException, EnforcerException {
Mockito.when(requestContext.getAuthenticationContext()).thenReturn(new AuthenticationContext());
APIConfig apiConfig = Mockito.mock(APIConfig.class);
Mockito.when(apiConfig.getName()).thenReturn("api1");
Mockito.when(apiConfig.getEnvironment()).thenReturn(environment);
Mockito.when(apiConfig.getOrganizationId()).thenReturn(organization);
Mockito.when(requestContext.getMatchedAPI()).thenReturn(apiConfig);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
Expand All @@ -121,7 +122,6 @@ public void testJWTValidator() throws APISecurityException, EnforcerException {
logManagerDummy.when(() -> LogManager.getLogger(JWTAuthenticator.class)).thenReturn(logger);
Log logger2 = Mockito.mock(Log.class);
logFactoryDummy.when(() -> LogFactory.getLog(AbstractAPIMgtGatewayJWTGenerator.class)).thenReturn(logger2);
////
cacheProviderUtilDummy.when(() -> CacheProviderUtil.getOrganizationCache(organization))
.thenReturn(cacheProvider);
ExtendedTokenIssuerDto tokenIssuerDto = Mockito.mock(ExtendedTokenIssuerDto.class);
Expand All @@ -140,7 +140,7 @@ public void testJWTValidator() throws APISecurityException, EnforcerException {

SubscriptionDataStoreImpl subscriptionDataStore = Mockito.mock(SubscriptionDataStoreImpl.class);
Mockito.when(subscriptionDataStore.getJWTValidatorByIssuer(issuer,
organization)).thenReturn(jwtValidator);
organization, environment)).thenReturn(jwtValidator);
subscriptionDataStoreImplDummy.when(SubscriptionDataStoreImpl::getInstance).thenReturn(subscriptionDataStore);
Mockito.when(jwtValidator.validateToken(Mockito.eq(jwt), Mockito.any())).thenReturn(jwtValidationInfo);
keyValidaterDummy.when(()->KeyValidator.validateScopes(Mockito.any())).thenReturn(true);
Expand All @@ -153,6 +153,7 @@ public void testJWTValidator() throws APISecurityException, EnforcerException {
@Test
public void testCachedJWTValidator() throws APISecurityException, EnforcerException {
String organization = "org1";
String environment = "development";
String issuer = "https://localhost:9443/oauth2/token";
String signature = "sBgeoqJn0log5EZflj_G7ADvm6B3KQ9bdfF" +
"CEFVQS1U3oY9" +
Expand Down Expand Up @@ -200,6 +201,7 @@ public void testCachedJWTValidator() throws APISecurityException, EnforcerExcept
Mockito.when(requestContext.getAuthenticationContext()).thenReturn(new AuthenticationContext());
APIConfig apiConfig = Mockito.mock(APIConfig.class);
Mockito.when(apiConfig.getName()).thenReturn("api1");
Mockito.when(apiConfig.getEnvironment()).thenReturn(environment);
Mockito.when(apiConfig.getOrganizationId()).thenReturn(organization);
Mockito.when(requestContext.getMatchedAPI()).thenReturn(apiConfig);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
Expand All @@ -214,7 +216,7 @@ public void testCachedJWTValidator() throws APISecurityException, EnforcerExcept
JWTValidator jwtValidator = Mockito.mock(JWTValidator.class);
SubscriptionDataStoreImpl subscriptionDataStore = Mockito.mock(SubscriptionDataStoreImpl.class);
Mockito.when(subscriptionDataStore.getJWTValidatorByIssuer(issuer,
organization)).thenReturn(jwtValidator);
organization, environment)).thenReturn(jwtValidator);
subscriptionDataStoreImplDummy.when(SubscriptionDataStoreImpl::getInstance).thenReturn(subscriptionDataStore);
Mockito.when(jwtValidator.validateToken(Mockito.eq(jwt), Mockito.any())).thenReturn(jwtValidationInfo);
keyValidatorDummy.when(() -> KeyValidator.validateScopes(Mockito.any())).thenReturn(true);
Expand All @@ -228,6 +230,7 @@ public void testCachedJWTValidator() throws APISecurityException, EnforcerExcept
@Test
public void testNonJTIJWTValidator() throws APISecurityException, EnforcerException {
String organization = "org1";
String environment = "development";
String issuer = "https://localhost:9443/oauth2/token";
String signature = "SSQyg_VTxF5drIogztn2SyEK2wRE07wG6OW3tufD3vo";
String jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9" +
Expand Down Expand Up @@ -264,6 +267,7 @@ public void testNonJTIJWTValidator() throws APISecurityException, EnforcerExcept
Mockito.when(requestContext.getAuthenticationContext()).thenReturn(new AuthenticationContext());
APIConfig apiConfig = Mockito.mock(APIConfig.class);
Mockito.when(apiConfig.getName()).thenReturn("api1");
Mockito.when(apiConfig.getEnvironment()).thenReturn(environment);
Mockito.when(apiConfig.getOrganizationId()).thenReturn(organization);
Mockito.when(requestContext.getMatchedAPI()).thenReturn(apiConfig);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
Expand All @@ -276,8 +280,8 @@ public void testNonJTIJWTValidator() throws APISecurityException, EnforcerExcept
cacheProviderUtilDummy.when(() -> CacheProviderUtil.getOrganizationCache(organization)).thenReturn(cacheProvider);
JWTValidator jwtValidator = Mockito.mock(JWTValidator.class);
SubscriptionDataStoreImpl subscriptionDataStore = Mockito.mock(SubscriptionDataStoreImpl.class);
Mockito.when(subscriptionDataStore.getJWTValidatorByIssuer(issuer,
organization)).thenReturn(jwtValidator);
Mockito.when(subscriptionDataStore.getJWTValidatorByIssuer(issuer, organization, environment))
.thenReturn(jwtValidator);
subscriptionDataStoreImplDummy.when(SubscriptionDataStoreImpl::getInstance).thenReturn(subscriptionDataStore);
Mockito.when(jwtValidator.validateToken(Mockito.eq(jwt), Mockito.any())).thenReturn(jwtValidationInfo);
keyValidatorDummy.when(() -> KeyValidator.validateScopes(Mockito.any())).thenReturn(true);
Expand Down Expand Up @@ -361,6 +365,7 @@ public void testExpiredJWTValidator() {
@Test
public void testNoCacheExpiredJWTValidator() throws EnforcerException {
String organization = "org1";
String environment = "development";
String issuer = "https://localhost:9443/oauth2/token";
String signature = "sBgeoqJn0log5EZflj_G7ADvm6B3KQ9bdfFCEFVQS1U3oY9" +
"-cqPwAPyOLLh95pdfjYjakkf1UtjPZjeIupwXnzg0SffIc704RoVlZocAx9Ns2XihjU6Imx2MbXq9ARmQxQkyGVkJUMTwZ8" +
Expand Down Expand Up @@ -406,6 +411,7 @@ public void testNoCacheExpiredJWTValidator() throws EnforcerException {
Mockito.when(requestContext.getAuthenticationContext()).thenReturn(new AuthenticationContext());
APIConfig apiConfig = Mockito.mock(APIConfig.class);
Mockito.when(apiConfig.getName()).thenReturn("api1");
Mockito.when(apiConfig.getEnvironment()).thenReturn(environment);
Mockito.when(apiConfig.getOrganizationId()).thenReturn(organization);
Mockito.when(requestContext.getMatchedAPI()).thenReturn(apiConfig);
CacheProvider cacheProvider = Mockito.mock(CacheProvider.class);
Expand All @@ -424,7 +430,8 @@ public void testNoCacheExpiredJWTValidator() throws EnforcerException {
thenReturn(cacheProvider);
JWTValidator jwtValidator = Mockito.mock(JWTValidator.class);
SubscriptionDataStoreImpl subscriptionDataStore = Mockito.mock(SubscriptionDataStoreImpl.class);
Mockito.when(subscriptionDataStore.getJWTValidatorByIssuer(issuer, organization)).thenReturn(jwtValidator);
Mockito.when(subscriptionDataStore.getJWTValidatorByIssuer(issuer, organization, environment))
.thenReturn(jwtValidator);
subscriptionDataStoreImplDummy.when(SubscriptionDataStoreImpl::getInstance).thenReturn(subscriptionDataStore);
Mockito.when(jwtValidator.validateToken(Mockito.eq(jwt), Mockito.any())).thenReturn(jwtValidationInfo);
keyValidatorDummy.when(() -> KeyValidator.validateScopes(Mockito.any())).thenReturn(true);
Expand All @@ -443,6 +450,7 @@ public void testNoCacheExpiredJWTValidator() throws EnforcerException {
@Test
public void testTamperedPayloadJWTValidator() throws EnforcerException {
String organization = "org1";
String environment = "development";
String issuer = "https://localhost:9443/oauth2/token";
String signature = "sBgeoqJn0log5EZflj_G7ADvm6B3KQ9bdfFCEFVQS1U3oY9" +
"-cqPwAPyOLLh95pdfjYjakkf1UtjPZjeIupwXnzg0SffIc704RoVlZocAx9Ns2XihjU6Imx2MbXq9ARmQxQkyGVkJUMTwZ8" +
Expand Down Expand Up @@ -499,6 +507,7 @@ public void testTamperedPayloadJWTValidator() throws EnforcerException {
Mockito.when(requestContext.getAuthenticationContext()).thenReturn(new AuthenticationContext());
APIConfig apiConfig = Mockito.mock(APIConfig.class);
Mockito.when(apiConfig.getName()).thenReturn("api1");
Mockito.when(apiConfig.getEnvironment()).thenReturn(environment);
Mockito.when(apiConfig.getOrganizationId()).thenReturn(organization);
Mockito.when(requestContext.getMatchedAPI()).thenReturn(apiConfig);
try (MockedStatic<CacheProviderUtil> cacheProviderUtilDummy = Mockito.mockStatic(CacheProviderUtil.class);
Expand All @@ -513,12 +522,13 @@ public void testTamperedPayloadJWTValidator() throws EnforcerException {
Mockito.when(invalidTokenCache.getIfPresent(signature)).thenReturn(null);
Mockito.when(cacheProvider.getGatewayKeyCache()).thenReturn(gatewayKeyCache);
Mockito.when(cacheProvider.getInvalidTokenCache()).thenReturn(invalidTokenCache);
cacheProviderUtilDummy.when(() -> CacheProviderUtil.getOrganizationCache(organization)).thenReturn(cacheProvider);
cacheProviderUtilDummy.when(() -> CacheProviderUtil.getOrganizationCache(organization))
.thenReturn(cacheProvider);

JWTValidator jwtValidator = Mockito.mock(JWTValidator.class);
SubscriptionDataStoreImpl subscriptionDataStore = Mockito.mock(SubscriptionDataStoreImpl.class);
Mockito.when(subscriptionDataStore.getJWTValidatorByIssuer(issuer,
organization)).thenReturn(jwtValidator);
Mockito.when(subscriptionDataStore.getJWTValidatorByIssuer(issuer, organization, environment))
.thenReturn(jwtValidator);

subscriptionDataStoreImplDummy.when(SubscriptionDataStoreImpl::getInstance).thenReturn(subscriptionDataStore);
Mockito.when(jwtValidator.validateToken(Mockito.eq(jwt), Mockito.any())).thenReturn(jwtValidationInfo);
Expand Down
1 change: 0 additions & 1 deletion libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ protobuf-java = {module = "com.google.protobuf:protobuf-java", version.ref = "pr
hamcrest = {module = "org.hamcrest:hamcrest-all", version.ref = "hamcrest"}
jacoco-agent = {module = "org.jacoco:org.jacoco.agent", version.ref = "jacoco"}
mockito-core = {module = "org.mockito:mockito-core", version.ref = "mockito"}
mockito-inline = {module = "org.mockito:mockito-inline", version.ref = "mockito"}
xml-apis = {module = "xml-apis:xml-apis", version.ref = "xml-apis"}
mock-server-netty = {module = "org.mock-server:mockserver-netty", version.ref = "mock-server-netty"}
mockito-inline = {module = "org.mockito:mockito-inline", version.ref = "mockito-inline"}
Expand Down

0 comments on commit f5c075b

Please sign in to comment.