Search in sources :

Example 1 with ScopeToUpdateDTO

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

the class ScopeUtils method getScope.

public static Scope getScope(ScopeToUpdateDTO scopeDTO, String scopeName) {
    Scope scope = new Scope(scopeName, 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 2 with ScopeToUpdateDTO

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

the class ScopesApiServiceImplTest method testUpdateScope.

@Test(dataProvider = "BuildUpdateScope")
public void testUpdateScope(Response.Status expectation, Throwable throwable) throws Exception {
    ScopeToUpdateDTO scopeToUpdateDTO = new ScopeToUpdateDTO();
    scopeToUpdateDTO.setDescription("some description");
    scopeToUpdateDTO.setBindings(Collections.<String>emptyList());
    if (Response.Status.OK.equals(expectation)) {
        when(ScopeUtils.getScopeDTO(any(Scope.class))).thenReturn(any(ScopeDTO.class));
        assertEquals(scopesApiService.updateScope(scopeToUpdateDTO, someScopeName).getStatus(), Response.Status.OK.getStatusCode(), "Error occurred while updating scopes");
    } else if (Response.Status.BAD_REQUEST.equals(expectation)) {
        when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeClientException.class);
        callRealMethod();
        try {
            scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
        } 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.NOT_FOUND.equals(expectation)) {
        ((IdentityOAuth2ScopeException) throwable).setErrorCode(Oauth2ScopeConstants.ErrorMessages.ERROR_CODE_NOT_FOUND_SCOPE.getCode());
        when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(throwable);
        callRealMethod();
        try {
            scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
        } catch (ScopeEndpointException e) {
            assertEquals(e.getResponse().getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Cannot find HTTP Response, Not Found in Case of " + "IdentityOAuth2ScopeClientException");
            assertEquals(((ErrorDTO) (e.getResponse().getEntity())).getMessage(), Response.Status.NOT_FOUND.getReasonPhrase(), "Cannot find appropriate error message " + "for HTTP Response, Not Found");
        } finally {
            reset(oAuth2ScopeService);
        }
    } else if (Response.Status.INTERNAL_SERVER_ERROR.equals(expectation)) {
        when(oAuth2ScopeService.updateScope(any(Scope.class))).thenThrow(IdentityOAuth2ScopeException.class);
        callRealMethod();
        try {
            scopesApiService.updateScope(scopeToUpdateDTO, someScopeName);
        } 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) ScopeToUpdateDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeToUpdateDTO) IdentityOAuth2ScopeClientException(org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException) 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 3 with ScopeToUpdateDTO

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

the class ScopeUtilsTest method testGetUpdatedScope.

@Test(description = "Testing getUpdateScope")
public void testGetUpdatedScope() throws Exception {
    ScopeToUpdateDTO sc = new ScopeToUpdateDTO();
    ArrayList bindings = new ArrayList();
    sc.setDisplayName(CLIENT_NAME);
    sc.setDescription(SCOPE_DESCRIPTION);
    Scope scope1 = ScopeUtils.getUpdatedScope(sc, "Actual name is not match for expected name");
    assertEquals(scope1.getBindings(), bindings, "Actual binding is not match for expected binding");
    assertEquals(scope1.getDescription(), SCOPE_DESCRIPTION, "Actual description is not match for expected description");
}
Also used : Scope(org.wso2.carbon.identity.oauth2.bean.Scope) ArrayList(java.util.ArrayList) ScopeToUpdateDTO(org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeToUpdateDTO) Test(org.testng.annotations.Test)

Example 4 with ScopeToUpdateDTO

use of org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeToUpdateDTO 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)

Aggregations

Scope (org.wso2.carbon.identity.oauth2.bean.Scope)3 Test (org.testng.annotations.Test)2 ScopeDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeDTO)2 ScopeToUpdateDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ScopeToUpdateDTO)2 IdentityOAuth2ScopeClientException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeClientException)2 IdentityOAuth2ScopeException (org.wso2.carbon.identity.oauth2.IdentityOAuth2ScopeException)2 ArrayList (java.util.ArrayList)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 ErrorDTO (org.wso2.carbon.identity.oauth.scope.endpoint.dto.ErrorDTO)1 ScopeEndpointException (org.wso2.carbon.identity.oauth.scope.endpoint.exceptions.ScopeEndpointException)1 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)1