use of org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO in project carbon-apimgt by wso2.
the class GraphQLRequestProcessorTest method testHandleRequestNonSubscribeMessage.
@Test
public void testHandleRequestNonSubscribeMessage() {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
int msgSize = 100;
String msgText = "{\"type\":\"connection_init\",\"payload\":{}}";
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
PowerMockito.when(InboundWebsocketProcessorUtil.authenticateToken(inboundMessageContext)).thenReturn(responseDTO);
GraphQLRequestProcessor graphQLRequestProcessor = new GraphQLRequestProcessor();
InboundProcessorResponseDTO processorResponseDTO = graphQLRequestProcessor.handleRequest(msgSize, msgText, inboundMessageContext);
Assert.assertFalse(processorResponseDTO.isError());
Assert.assertNull(processorResponseDTO.getErrorMessage());
Assert.assertFalse(processorResponseDTO.isCloseConnection());
}
use of org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO in project carbon-apimgt by wso2.
the class InboundWebsocketProcessorUtilTest method testDoThrottleFail.
@Test
public void testDoThrottleFail() throws ParseException {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
int msgSize = 100;
VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
verbInfoDTO.setThrottling("Gold");
verbInfoDTO.setRequestKey("liftStatusChange");
String operationId = "1";
APIKeyValidationInfoDTO apiKeyValidationInfoDTO = new APIKeyValidationInfoDTO();
apiKeyValidationInfoDTO.setApplicationTier(APIConstants.UNLIMITED_TIER);
apiKeyValidationInfoDTO.setTier(APIConstants.UNLIMITED_TIER);
apiKeyValidationInfoDTO.setSubscriberTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
apiKeyValidationInfoDTO.setSubscriber("admin");
apiKeyValidationInfoDTO.setApiName("GraphQLAPI");
apiKeyValidationInfoDTO.setApplicationId("12");
inboundMessageContext.setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
inboundMessageContext.setApiContext("/graphql");
inboundMessageContext.setVersion("1.0.0");
inboundMessageContext.setUserIP("198.162.10.2");
inboundMessageContext.setInfoDTO(apiKeyValidationInfoDTO);
String subscriptionLevelThrottleKey = apiKeyValidationInfoDTO.getApplicationId() + ":" + inboundMessageContext.getApiContext() + ":" + inboundMessageContext.getVersion();
String applicationLevelThrottleKey = apiKeyValidationInfoDTO.getApplicationId() + ":" + apiKeyValidationInfoDTO.getSubscriber() + "@" + apiKeyValidationInfoDTO.getSubscriberTenantDomain();
Mockito.when(dataPublisher.tryPublish(Mockito.anyObject())).thenReturn(true);
PowerMockito.when(WebsocketUtil.isThrottled(verbInfoDTO.getRequestKey(), subscriptionLevelThrottleKey, applicationLevelThrottleKey)).thenReturn(true);
InboundProcessorResponseDTO inboundProcessorResponseDTO = InboundWebsocketProcessorUtil.doThrottleForGraphQL(msgSize, verbInfoDTO, inboundMessageContext, operationId);
Assert.assertTrue(inboundProcessorResponseDTO.isError());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorMessage(), WebSocketApiConstants.FrameErrorConstants.THROTTLED_OUT_ERROR_MESSAGE);
Assert.assertEquals(inboundProcessorResponseDTO.getErrorCode(), WebSocketApiConstants.FrameErrorConstants.THROTTLED_OUT_ERROR);
Assert.assertFalse(inboundProcessorResponseDTO.isCloseConnection());
JSONParser jsonParser = new JSONParser();
JSONObject errorJson = (JSONObject) jsonParser.parse(inboundProcessorResponseDTO.getErrorResponseString());
org.junit.Assert.assertEquals(errorJson.get(GraphQLConstants.SubscriptionConstants.PAYLOAD_FIELD_NAME_TYPE), GraphQLConstants.SubscriptionConstants.PAYLOAD_FIELD_TYPE_ERROR);
org.junit.Assert.assertEquals(errorJson.get(GraphQLConstants.SubscriptionConstants.PAYLOAD_FIELD_NAME_ID), "1");
JSONObject payload = (JSONObject) errorJson.get(GraphQLConstants.SubscriptionConstants.PAYLOAD_FIELD_NAME_PAYLOAD);
org.junit.Assert.assertEquals(payload.get(WebSocketApiConstants.FrameErrorConstants.ERROR_MESSAGE), WebSocketApiConstants.FrameErrorConstants.THROTTLED_OUT_ERROR_MESSAGE);
org.junit.Assert.assertEquals(String.valueOf(payload.get(WebSocketApiConstants.FrameErrorConstants.ERROR_CODE)), String.valueOf(WebSocketApiConstants.FrameErrorConstants.THROTTLED_OUT_ERROR));
}
use of org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO in project carbon-apimgt by wso2.
the class HandshakeProcessorTest method handleFailedAuthentication.
@Test
public void handleFailedAuthentication() throws Exception {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
PowerMockito.when(InboundWebsocketProcessorUtil.isAuthenticated(inboundMessageContext)).thenReturn(false);
HandshakeProcessor handshakeProcessor = new HandshakeProcessor();
InboundProcessorResponseDTO errorResponseDTO = new InboundProcessorResponseDTO();
errorResponseDTO.setError(true);
errorResponseDTO.setErrorCode(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR);
errorResponseDTO.setErrorMessage(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
errorResponseDTO.setCloseConnection(true);
PowerMockito.when(InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE)).thenReturn(errorResponseDTO);
InboundProcessorResponseDTO inboundProcessorResponseDTO = handshakeProcessor.processHandshake(inboundMessageContext);
Assert.assertTrue(inboundProcessorResponseDTO.isError());
Assert.assertNotNull(inboundProcessorResponseDTO.getErrorMessage());
Assert.assertTrue(inboundProcessorResponseDTO.isCloseConnection());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorResponseString(), errorResponseDTO.getErrorResponseString());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorCode(), errorResponseDTO.getErrorCode());
Assert.assertEquals(inboundProcessorResponseDTO.getErrorMessage(), errorResponseDTO.getErrorMessage());
}
use of org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO in project carbon-apimgt by wso2.
the class GraphQLResponseProcessorTest method testHandleBadResponse.
@Test
public void testHandleBadResponse() {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
int msgSize = 100;
String msgText = "{\"type\":\"data\",\"payload\":{\"data\":" + "{\"liftStatusChange\":{\"name\":\"Astra Express\"}}}}";
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
PowerMockito.when(InboundWebsocketProcessorUtil.authenticateToken(inboundMessageContext)).thenReturn(responseDTO);
GraphQLResponseProcessor responseProcessor = new GraphQLResponseProcessor();
InboundProcessorResponseDTO inboundProcessorResponseDTO = new InboundProcessorResponseDTO();
inboundProcessorResponseDTO.setError(true);
inboundProcessorResponseDTO.setErrorCode(WebSocketApiConstants.FrameErrorConstants.BAD_REQUEST);
inboundProcessorResponseDTO.setErrorMessage("Missing mandatory id field in the message");
PowerMockito.when(InboundWebsocketProcessorUtil.getBadRequestFrameErrorDTO("Missing mandatory id field in the message")).thenReturn(inboundProcessorResponseDTO);
InboundProcessorResponseDTO processorResponseDTO = responseProcessor.handleResponse(msgSize, msgText, inboundMessageContext);
Assert.assertTrue(processorResponseDTO.isError());
Assert.assertNotNull(processorResponseDTO.getErrorMessage());
Assert.assertFalse(processorResponseDTO.isCloseConnection());
Assert.assertEquals(processorResponseDTO.getErrorResponseString(), inboundProcessorResponseDTO.getErrorResponseString());
Assert.assertEquals(processorResponseDTO.getErrorMessage(), inboundProcessorResponseDTO.getErrorMessage());
Assert.assertEquals(processorResponseDTO.getErrorCode(), inboundProcessorResponseDTO.getErrorCode());
}
use of org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO in project carbon-apimgt by wso2.
the class GraphQLResponseProcessorTest method testHandleResponseScopeValidationSkipWhenSecurityDisabled.
@Test
public void testHandleResponseScopeValidationSkipWhenSecurityDisabled() {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
int msgSize = 100;
String msgText = "{\"type\":\"data\",\"id\":\"1\",\"payload\":{\"data\":" + "{\"liftStatusChange\":{\"name\":\"Astra Express\"}}}}";
PowerMockito.mockStatic(InboundWebsocketProcessorUtil.class);
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
PowerMockito.when(InboundWebsocketProcessorUtil.authenticateToken(inboundMessageContext)).thenReturn(responseDTO);
// VerbInfoDTO with security disabled
VerbInfoDTO verbInfoDTO = new VerbInfoDTO();
verbInfoDTO.setHttpVerb("SUBSCRIPTION");
verbInfoDTO.setThrottling("Unlimited");
verbInfoDTO.setAuthType("None");
GraphQLOperationDTO graphQLOperationDTO = new GraphQLOperationDTO(verbInfoDTO, "liftStatusChange");
inboundMessageContext.addVerbInfoForGraphQLMsgId("1", graphQLOperationDTO);
// Creating response for scope validation
GraphQLProcessorResponseDTO graphQLProcessorResponseDTO = new GraphQLProcessorResponseDTO();
graphQLProcessorResponseDTO.setError(true);
graphQLProcessorResponseDTO.setErrorCode(WebSocketApiConstants.FrameErrorConstants.RESOURCE_FORBIDDEN_ERROR);
graphQLProcessorResponseDTO.setErrorMessage("User is NOT authorized to access the Resource");
graphQLProcessorResponseDTO.setCloseConnection(false);
graphQLProcessorResponseDTO.setId("1");
PowerMockito.when(InboundWebsocketProcessorUtil.validateScopes(inboundMessageContext, "liftStatusChange", "1")).thenReturn(graphQLProcessorResponseDTO);
PowerMockito.when(InboundWebsocketProcessorUtil.doThrottleForGraphQL(msgSize, verbInfoDTO, inboundMessageContext, "1")).thenReturn(responseDTO);
GraphQLResponseProcessor responseProcessor = new GraphQLResponseProcessor();
InboundProcessorResponseDTO processorResponseDTO = responseProcessor.handleResponse(msgSize, msgText, inboundMessageContext);
Assert.assertFalse(processorResponseDTO.isError());
Assert.assertNull(processorResponseDTO.getErrorMessage());
Assert.assertNotEquals(processorResponseDTO.getErrorMessage(), "User is NOT authorized to access the Resource");
}
Aggregations