use of org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder in project carbon-apimgt by wso2.
the class UtilsTest method getSelectedAPI3.
@Test
public void getSelectedAPI3() {
SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class);
Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
SubscriptionDataStore subscriptionDataStore = Mockito.mock(SubscriptionDataStore.class);
Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore("carbon.super")).thenReturn(subscriptionDataStore);
Mockito.when(subscriptionDataStore.getAllAPIsByContextList()).thenReturn(apiMap());
Map<String, API> selectedAPIList = Utils.getSelectedAPIList("/api1/1.0.0/cde?c=y", "carbon.super");
Assert.assertEquals(selectedAPIList.size(), 2);
Assert.assertEquals(selectedAPIList.keySet().iterator().next(), "/api1/1.0.0");
}
use of org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder in project carbon-apimgt by wso2.
the class UtilsTest method getSelectedAPI4.
@Test
public void getSelectedAPI4() {
SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class);
Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
SubscriptionDataStore subscriptionDataStore = Mockito.mock(SubscriptionDataStore.class);
Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore("carbon.super")).thenReturn(subscriptionDataStore);
Mockito.when(subscriptionDataStore.getAllAPIsByContextList()).thenReturn(apiMap());
Map<String, API> selectedAPIList = Utils.getSelectedAPIList("/api1/abc/1.0.0/cde?c=y", "carbon.super");
Assert.assertEquals(selectedAPIList.size(), 3);
Assert.assertEquals(selectedAPIList.keySet().iterator().next(), "/api1/abc/1.0.0");
}
use of org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder in project carbon-apimgt by wso2.
the class UtilsTest method getSelectedAPI2.
@Test
public void getSelectedAPI2() {
SubscriptionDataHolder subscriptionDataHolder = Mockito.mock(SubscriptionDataHolder.class);
Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
SubscriptionDataStore subscriptionDataStore = Mockito.mock(SubscriptionDataStore.class);
Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore("carbon.super")).thenReturn(subscriptionDataStore);
Mockito.when(subscriptionDataStore.getAllAPIsByContextList()).thenReturn(apiMap());
Map<String, API> selectedAPIList = Utils.getSelectedAPIList("/api1/abc/cde?c=y", "carbon.super");
Assert.assertEquals(selectedAPIList.size(), 2);
Assert.assertEquals(selectedAPIList.keySet().iterator().next(), "/api1/abc");
}
use of org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder in project carbon-apimgt by wso2.
the class DefaultKeyValidationHandlerTest method testValidateScopes.
@Test
public void testValidateScopes() throws APIKeyMgtException {
API api = new API();
api.setApiId(1);
api.setApiProvider(USER_NAME);
api.setApiName(API_NAME);
api.setApiVersion(API_VERSION);
api.setContext(API_CONTEXT);
URLMapping urlMapping = new URLMapping();
urlMapping.addScope(SCOPES);
urlMapping.setHttpMethod(HTTP_VERB);
urlMapping.setUrlPattern(RESOURCE);
api.addResource(urlMapping);
Map<String, API> apiMap = new HashMap<>();
String key = API_CONTEXT + ":" + API_VERSION;
apiMap.put(key, api);
APIKeyValidationInfoDTO dto = new APIKeyValidationInfoDTO();
dto.setSubscriber(SUBSCRIBER);
dto.setApplicationName(APPLICATION_NAME);
dto.setApplicationId(APPLICATION_ID);
dto.setApplicationTier(TIER);
Set<String> scopeSet = new HashSet<>();
scopeSet.add(SCOPES);
dto.setScopes(scopeSet);
dto.setSubscriberTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
dto.setUserType(APIConstants.ACCESS_TOKEN_USER_TYPE_APPLICATION);
// TokenValidationContext for non default API
TokenValidationContext param1 = new TokenValidationContext();
param1.setValidationInfoDTO(dto);
param1.setContext(API_CONTEXT);
param1.setVersion(API_VERSION);
param1.setAccessToken(ACCESS_TOKEN);
param1.setMatchingResource(RESOURCE);
param1.setHttpVerb(HTTP_VERB);
// TokenValidationContext for default API version
TokenValidationContext param2 = new TokenValidationContext();
param2.setValidationInfoDTO(dto);
param2.setContext(API_CONTEXT);
param2.setVersion(DEFAULT_API_VERSION);
param2.setAccessToken(ACCESS_TOKEN);
param2.setMatchingResource(RESOURCE);
param2.setHttpVerb(HTTP_VERB);
Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);
Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore(eq(TENANT_DOMAIN))).thenReturn(tenantSubscriptionStore);
Mockito.when(tenantSubscriptionStore.getApiByContextAndVersion(eq(API_CONTEXT), eq(API_VERSION))).thenReturn(api);
DefaultKeyValidationHandler defaultKeyValidationHandler = new DefaultKeyValidationHandler();
boolean isScopeValidated = defaultKeyValidationHandler.validateScopes(param1);
boolean isScopeValidated_default = defaultKeyValidationHandler.validateScopes(param2);
Assert.assertTrue("Scope validation fails for API " + API_NAME, isScopeValidated);
Assert.assertTrue("Scope validation fails for default API " + API_NAME, isScopeValidated_default);
}
use of org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder in project carbon-apimgt by wso2.
the class DefaultKeyValidationHandlerTest method testInvalidSubscription.
@Test
public void testInvalidSubscription() throws APIKeyMgtException {
DefaultKeyValidationHandler defaultKeyValidationHandler = new DefaultKeyValidationHandler();
API api = new API();
api.setApiId(1);
api.setApiProvider(USER_NAME);
api.setApiName(API_NAME);
api.setApiVersion(API_VERSION);
api.setContext(API_CONTEXT);
URLMapping urlMapping = new URLMapping();
urlMapping.addScope(SCOPES);
urlMapping.setHttpMethod(HTTP_VERB);
urlMapping.setUrlPattern(RESOURCE);
api.addResource(urlMapping);
Mockito.when(SubscriptionDataHolder.getInstance()).thenReturn(subscriptionDataHolder);
Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(TENANT_DOMAIN);
Mockito.when(subscriptionDataHolder.getTenantSubscriptionStore(eq(TENANT_DOMAIN))).thenReturn(tenantSubscriptionStore);
Mockito.when(tenantSubscriptionStore.getApiByContextAndVersion(eq(API_CONTEXT), eq(API_VERSION))).thenReturn(api);
APIKeyValidationInfoDTO info = defaultKeyValidationHandler.validateSubscription(API_CONTEXT, API_VERSION, "xxxxxx", "default");
Assert.assertEquals("Invalid error message status code ", APIConstants.KeyValidationStatus.API_AUTH_RESOURCE_FORBIDDEN, info.getValidationStatus());
}
Aggregations