Search in sources :

Example 61 with ScopeDTO

use of org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopesApiServiceImplTest method testRegisterScope.

@Test(dataProvider = "BuildRegisterScope")
public void testRegisterScope(Response.Status expectation, Throwable throwable) throws Exception {
    ScopeDTO scopeDTO = new ScopeDTO();
    scopeDTO.setDescription("some description");
    scopeDTO.setBindings(Collections.<String>emptyList());
    if (Response.Status.OK.equals(expectation)) {
        when(oAuth2ScopeService.registerScope(any(Scope.class))).thenReturn(any(Scope.class));
        assertEquals(scopesApiService.registerScope(scopeDTO).getStatus(), Response.Status.CREATED.getStatusCode(), "Error occurred while registering scopes");
    } else if (Response.Status.BAD_REQUEST.equals(expectation)) {
        when(oAuth2ScopeService.registerScope(any(Scope.class))).thenThrow(throwable);
        callRealMethod();
        try {
            scopesApiService.registerScope(scopeDTO);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), "Cannot find HTTP Response, Bad Request in Case of " + "IdentityOAuth2ScopeClientException");
            assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.BAD_REQUEST.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Bad Request");
        } finally {
            reset(oAuth2ScopeService);
        }
    } else if (Response.Status.CONFLICT.equals(expectation)) {
        ((IdentityOAuth2ScopeException) throwable).setErrorCode(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_CONFLICT_REQUEST_EXISTING_SCOPE.getCode());
        when(oAuth2ScopeService.registerScope(any(Scope.class))).thenThrow(throwable);
        callRealMethod();
        try {
            scopesApiService.registerScope(scopeDTO);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.CONFLICT.getStatusCode(), "Cannot find HTTP Response, Conflict in Case of " + "IdentityOAuth2ScopeClientException");
            assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.CONFLICT.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Conflict");
        } finally {
            reset(oAuth2ScopeService);
        }
    } else if (Response.Status.INTERNAL_SERVER_ERROR.equals(expectation)) {
        when(oAuth2ScopeService.registerScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeException.class);
        callRealMethod();
        try {
            scopesApiService.registerScope(scopeDTO);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Cannot find HTTP Response, Internal Server Error in case of " + "IdentityOAuth2ScopeException");
            assertNull(e.getResponse().getEntity(), "Do not include error message in case of " + "Server Exception");
        } finally {
            reset(oAuth2ScopeService);
        }
    }
}
Also used : ScopeEndpointException(org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException) Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ScopeDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO) ErrorDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ErrorDTO) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 62 with ScopeDTO

use of org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeUtilsTest method testGetScopeDTO.

@Test(description = "Testing getScopeDTO")
public void testGetScopeDTO() throws Exception {
    ArrayList bindings = new ArrayList();
    bindings.add("binding1");
    Scope scope = new Scope(CLIENT_NAME, CLIENT_NAME, SCOPE_DESCRIPTION, bindings);
    ScopeDTO scopeDTO1 = ScopeUtils.getScopeDTO(scope);
    assertEquals(scopeDTO1.getBindings(), bindings, "Actual binding is not match for expected binding");
    assertTrue(scopeDTO1.getBindings().get(0).contains("binding1"));
    assertEquals(scopeDTO1.getDisplayName(), CLIENT_NAME, "Actual display name is not match for expected display name");
    assertEquals(scopeDTO1.getDescription(), SCOPE_DESCRIPTION, "Actual description is not match for expected description");
    assertEquals(scopeDTO1.getName(), CLIENT_NAME, "Actual name is not match for expected name");
}
Also used : Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ScopeDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 63 with ScopeDTO

use of org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopesApiServiceImpl method updateScope.

/**
 * Update a scope
 *
 * @param scope details of the scope to be updated.
 * @param name  name of the scope to be updated.
 * @return
 */
@Override
public Response updateScope(ScopeToUpdateDTO scope, String name) {
    ScopeDTO updatedScope = null;
    try {
        validateUpdateRequest(name);
        updatedScope = ScopeUtils.getScopeDTO(ScopeUtils.getOAuth2ScopeService().updateScope(ScopeUtils.getUpdatedScope(scope, name)));
    } catch (IdentityOAuth2ScopeClientException e) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Client Error while updating scope \n" + scope.toString(), e);
        }
        if (Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_NOT_FOUND_SCOPE.getCode().equals(e.getErrorCode())) {
            ScopeUtils.handleErrorResponse(Response.Status.NOT_FOUND, Response.Status.NOT_FOUND.getReasonPhrase(), e, false, LOG);
        } else if (Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_NOT_AUTHORIZED_UPDATE_INTERNAL_SCOPE.getCode().equals(e.getErrorCode())) {
            ScopeUtils.handleErrorResponse(Response.Status.FORBIDDEN, Response.Status.FORBIDDEN.getReasonPhrase(), e, false, LOG);
        } else {
            ScopeUtils.handleErrorResponse(Response.Status.BAD_REQUEST, Response.Status.BAD_REQUEST.getReasonPhrase(), e, false, LOG);
        }
    } catch (IdentityOAuth2ScopeException e) {
        ScopeUtils.handleErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase(), e, true, LOG);
    } catch (Throwable throwable) {
        ScopeUtils.handleErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase(), throwable, true, LOG);
    }
    return Response.status(Response.Status.OK).entity(updatedScope).build();
}
Also used : ScopeDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO) IdentityOAuth2ScopeClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException) IdentityOAuth2ScopeException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException)

Example 64 with ScopeDTO

use of org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeUtils method getScope.

public static Scope getScope(ScopeDTO scopeDTO) {
    Scope scope = new Scope(scopeDTO.getName(), scopeDTO.getDisplayName(), getScopeBindings(scopeDTO.getScopeBindings()), scopeDTO.getDescription());
    scope.addScopeBindings(DEFAULT_SCOPE_BINDING, scopeDTO.getBindings());
    return scope;
}
Also used : Scope(org.wso2.carbon.identity.oauth2.bean.Scope)

Example 65 with ScopeDTO

use of org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO in project identity-inbound-auth-oauth by wso2-extensions.

the class ScopeUtils method getScopeDTO.

public static ScopeDTO getScopeDTO(Scope scope) {
    ScopeDTO scopeDTO = new ScopeDTO();
    scopeDTO.setName(scope.getName());
    scopeDTO.setDisplayName(scope.getDisplayName());
    scopeDTO.setDescription(scope.getDescription());
    scopeDTO.setBindings(scope.getBindings());
    scopeDTO.setScopeBindings(getScopeBindingDTOs(scope.getScopeBindings()));
    return scopeDTO;
}
Also used : ScopeDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO)

Aggregations

ArrayList (java.util.ArrayList)23 ScopeDTO (org.wso2.carbon.identity.oauth.dto.ScopeDTO)18 HashMap (java.util.HashMap)13 Scope (org.wso2.carbon.apimgt.api.model.Scope)11 ScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.ScopeDTO)11 Test (org.testng.annotations.Test)8 APIScopeDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIScopeDTO)8 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)8 Scope (org.wso2.carbon.identity.oauth2.bean.Scope)8 HashSet (java.util.HashSet)7 List (java.util.List)7 ScopeDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO)7 Map (java.util.Map)6 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)6 JdbcTemplate (org.wso2.carbon.database.utils.jdbc.JdbcTemplate)6 DataAccessException (org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException)6 OIDCScopeClaimCacheEntry (org.wso2.carbon.identity.openidconnect.cache.OIDCScopeClaimCacheEntry)6 Matchers.anyString (org.mockito.Matchers.anyString)5 SQLIntegrityConstraintViolationException (java.sql.SQLIntegrityConstraintViolationException)4 Arrays (java.util.Arrays)4