Search in sources :

Example 1 with SubscriptionDataHolder

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");
}
Also used : API(org.wso2.carbon.apimgt.keymgt.model.entity.API) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) SubscriptionDataHolder(org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with SubscriptionDataHolder

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");
}
Also used : API(org.wso2.carbon.apimgt.keymgt.model.entity.API) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) SubscriptionDataHolder(org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with SubscriptionDataHolder

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");
}
Also used : API(org.wso2.carbon.apimgt.keymgt.model.entity.API) SubscriptionDataStore(org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore) SubscriptionDataHolder(org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with SubscriptionDataHolder

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);
}
Also used : URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) TokenValidationContext(org.wso2.carbon.apimgt.keymgt.service.TokenValidationContext) HashMap(java.util.HashMap) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) APIKeyValidationInfoDTO(org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with SubscriptionDataHolder

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());
}
Also used : URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) API(org.wso2.carbon.apimgt.keymgt.model.entity.API) APIKeyValidationInfoDTO(org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Test (org.junit.Test)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 SubscriptionDataHolder (org.wso2.carbon.apimgt.keymgt.SubscriptionDataHolder)9 SubscriptionDataStore (org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)9 API (org.wso2.carbon.apimgt.keymgt.model.entity.API)8 MessageContext (org.apache.synapse.MessageContext)4 URLMapping (org.wso2.carbon.apimgt.api.model.subscription.URLMapping)2 APIKeyValidationInfoDTO (org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)1 APIManagerConfigurationServiceImpl (org.wso2.carbon.apimgt.impl.APIManagerConfigurationServiceImpl)1 Application (org.wso2.carbon.apimgt.keymgt.model.entity.Application)1 TokenValidationContext (org.wso2.carbon.apimgt.keymgt.service.TokenValidationContext)1 ServerConfiguration (org.wso2.carbon.base.ServerConfiguration)1