Search in sources :

Example 1 with UnregistrationRequest

use of org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest in project identity-inbound-auth-oauth by wso2-extensions.

the class UnRegistrationHandlerTest method testHandle.

@Test
public void testHandle() throws Exception {
    when(mockDcrMessageContext.getIdentityRequest()).thenReturn(mockUnregistrationRequest);
    UnregistrationRequest.DCRUnregisterRequestBuilder dCRUnregisterRequestBuilder = new UnregistrationRequest.DCRUnregisterRequestBuilder();
    UnregistrationResponse.DCUnregisterResponseBuilder dCUnregisterResponseBuilder = new UnregistrationResponse.DCUnregisterResponseBuilder();
    whenNew(UnregistrationResponse.DCUnregisterResponseBuilder.class).withNoArguments().thenReturn(dCUnregisterResponseBuilder);
    String dummyUserId = "1234";
    dCRUnregisterRequestBuilder.setUserId(dummyUserId);
    String dummyApplicationName = "testApplicationname";
    dCRUnregisterRequestBuilder.setApplicationName(dummyApplicationName);
    String dummyConsumerKey = "testConsumerKey";
    dCRUnregisterRequestBuilder.setConsumerKey(dummyConsumerKey);
    UnregistrationRequest requestBuilder = dCRUnregisterRequestBuilder.build();
    mockStatic(DCRManagementService.class);
    when(DCRManagementService.getInstance()).thenReturn(mockDCRManagementService);
    when(mockDcrMessageContext.getIdentityRequest()).thenReturn(requestBuilder);
    final String[] params = new String[3];
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            params[0] = (String) invocation.getArguments()[0];
            params[1] = (String) invocation.getArguments()[1];
            params[2] = (String) invocation.getArguments()[2];
            return null;
        }
    }).when(mockDCRManagementService).unregisterOAuthApplication(anyString(), anyString(), anyString());
    unRegistrationHandler.handle(mockDcrMessageContext);
    assertEquals(unRegistrationHandler.handle(mockDcrMessageContext), dCUnregisterResponseBuilder, "Expected response builder is different from the actual");
    assertEquals(params[0], dummyUserId, "Expected tenant user Id is not equal to the actual");
    assertEquals(params[1], dummyApplicationName, "Expected application name is not equal to the actual");
    assertEquals(params[2], dummyConsumerKey, "Expected consumer key is not equal to the actual");
}
Also used : UnregistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest) Matchers.anyString(org.mockito.Matchers.anyString) UnregistrationResponse(org.wso2.carbon.identity.oauth.dcr.model.UnregistrationResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with UnregistrationRequest

use of org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest in project identity-inbound-auth-oauth by wso2-extensions.

the class DCRProcessorTest method getInstanceType.

@DataProvider(name = "instanceTypeprovider")
public Object[][] getInstanceType() throws FrameworkClientException {
    RegistrationRequest registrationRequest = mock(RegistrationRequest.class);
    UnregistrationRequest unregistrationRequest = mock(UnregistrationRequest.class);
    return new Object[][] { { "RegistrationRequest", registrationRequest }, { "UnregistrationRequest", unregistrationRequest } };
}
Also used : UnregistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest) RegistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest) DataProvider(org.testng.annotations.DataProvider)

Example 3 with UnregistrationRequest

use of org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest in project identity-inbound-auth-oauth by wso2-extensions.

the class DCRProcessorTest method testProcessWithException.

@Test(dataProvider = "instanceType&ErrorcodeProvider")
public void testProcessWithException(String request, Object identityRequest, String errorCode) throws Exception {
    mockHandlerManager = mock(HandlerManager.class);
    mockStatic(HandlerManager.class);
    when(HandlerManager.getInstance()).thenReturn(mockHandlerManager);
    DCRMessageContext dcrMessageContext = mock(DCRMessageContext.class);
    whenNew(DCRMessageContext.class).withArguments(identityRequest).thenReturn(dcrMessageContext);
    if (request.equals("RegistrationRequest")) {
        RegistrationHandler registrationHandler = mock(RegistrationHandler.class);
        when(mockHandlerManager.getRegistrationHandler(dcrMessageContext)).thenReturn(registrationHandler);
        if (errorCode.isEmpty()) {
            doThrow(new DCRException("")).when(registrationHandler).handle(dcrMessageContext);
        } else {
            doThrow(new DCRException(errorCode, "")).when(registrationHandler).handle(dcrMessageContext);
        }
        try {
            dcrProcessor.process((RegistrationRequest) identityRequest);
            fail("Expected exception IdentityException not thrown by process method");
        } catch (IdentityException ex) {
            if (errorCode.isEmpty()) {
                assertEquals(ex.getErrorCode(), ErrorCodes.BAD_REQUEST.toString());
            } else {
                assertEquals(ex.getErrorCode(), errorCode);
            }
        }
    } else if (request.equals("UnregistrationRequest")) {
        UnRegistrationHandler unRegistrationHandler = mock(UnRegistrationHandler.class);
        when(mockHandlerManager.getUnRegistrationHandler(dcrMessageContext)).thenReturn(unRegistrationHandler);
        if (errorCode.isEmpty()) {
            doThrow(new DCRException("")).when(unRegistrationHandler).handle(dcrMessageContext);
        } else {
            doThrow(new DCRException(errorCode, "")).when(unRegistrationHandler).handle(dcrMessageContext);
        }
        try {
            dcrProcessor.process((UnregistrationRequest) identityRequest);
            fail("Expected exception IdentityException not thrown by registerOAuthApplication");
        } catch (IdentityException ex) {
            if (errorCode.isEmpty()) {
                assertEquals(ex.getMessage(), ErrorCodes.BAD_REQUEST.toString());
            } else {
                assertEquals(ex.getMessage(), errorCode);
            }
        }
    }
}
Also used : UnRegistrationHandler(org.wso2.carbon.identity.oauth.dcr.handler.UnRegistrationHandler) UnregistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest) HandlerManager(org.wso2.carbon.identity.oauth.dcr.util.HandlerManager) RegistrationHandler(org.wso2.carbon.identity.oauth.dcr.handler.RegistrationHandler) UnRegistrationHandler(org.wso2.carbon.identity.oauth.dcr.handler.UnRegistrationHandler) DCRMessageContext(org.wso2.carbon.identity.oauth.dcr.context.DCRMessageContext) IdentityException(org.wso2.carbon.identity.base.IdentityException) DCRException(org.wso2.carbon.identity.oauth.dcr.DCRException) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with UnregistrationRequest

use of org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest in project identity-inbound-auth-oauth by wso2-extensions.

the class DCRProcessor method process.

@Override
public IdentityResponse.IdentityResponseBuilder process(IdentityRequest identityRequest) throws DCRException {
    if (log.isDebugEnabled()) {
        log.debug("Request processing started by DCRProcessor.");
    }
    DCRMessageContext dcrMessageContext = new DCRMessageContext(identityRequest);
    IdentityResponse.IdentityResponseBuilder identityResponseBuilder = null;
    if (identityRequest instanceof RegistrationRequest) {
        identityResponseBuilder = registerOAuthApplication(dcrMessageContext);
    } else if (identityRequest instanceof UnregistrationRequest) {
        identityResponseBuilder = unRegisterOAuthApplication(dcrMessageContext);
    }
    return identityResponseBuilder;
}
Also used : UnregistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest) IdentityResponse(org.wso2.carbon.identity.application.authentication.framework.inbound.IdentityResponse) DCRMessageContext(org.wso2.carbon.identity.oauth.dcr.context.DCRMessageContext) RegistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest)

Example 5 with UnregistrationRequest

use of org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest in project identity-inbound-auth-oauth by wso2-extensions.

the class DCRProcessorTest method getInstanceErrorcode.

@DataProvider(name = "instanceType&ErrorcodeProvider")
public Object[][] getInstanceErrorcode() throws FrameworkClientException {
    RegistrationRequest registrationRequest = mock(RegistrationRequest.class);
    UnregistrationRequest unregistrationRequest = mock(UnregistrationRequest.class);
    return new Object[][] { { "RegistrationRequest", registrationRequest, "dummyErrorCode" }, { "RegistrationRequest", registrationRequest, "" }, { "UnregistrationRequest", unregistrationRequest, "dummyErrorCode" }, { "UnregistrationRequest", unregistrationRequest, "" } };
}
Also used : UnregistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest) RegistrationRequest(org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest) DataProvider(org.testng.annotations.DataProvider)

Aggregations

UnregistrationRequest (org.wso2.carbon.identity.oauth.dcr.model.UnregistrationRequest)6 RegistrationRequest (org.wso2.carbon.identity.oauth.dcr.model.RegistrationRequest)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 DCRMessageContext (org.wso2.carbon.identity.oauth.dcr.context.DCRMessageContext)3 DataProvider (org.testng.annotations.DataProvider)2 IdentityResponse (org.wso2.carbon.identity.application.authentication.framework.inbound.IdentityResponse)2 RegistrationHandler (org.wso2.carbon.identity.oauth.dcr.handler.RegistrationHandler)2 UnRegistrationHandler (org.wso2.carbon.identity.oauth.dcr.handler.UnRegistrationHandler)2 UnregistrationResponse (org.wso2.carbon.identity.oauth.dcr.model.UnregistrationResponse)2 HandlerManager (org.wso2.carbon.identity.oauth.dcr.util.HandlerManager)2 Matchers.anyString (org.mockito.Matchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 IdentityException (org.wso2.carbon.identity.base.IdentityException)1 DCRException (org.wso2.carbon.identity.oauth.dcr.DCRException)1