Exemple #1
0
def test_timeout_asyncRequests_two_peers_get(mock_requests, client):
    APP.app.config["peers"] = TWO
    with client:
        url = "http://{}:{}".format(TP['URI'], TP['PORT0'])
        FR = FederationResponse(url=url,
                                request="GET",
                                endpoint_payload="",
                                endpoint_path=TP["path"],
                                request_dict=TP["Headers"])

        resp = FR.async_requests(uri_list=[
            "http://{}".format(TP["Tyk1"]), "http://{}".format(TP["Tyk2"])
        ],
                                 request_type='GET',
                                 endpoint_path=TP["path"],
                                 endpoint_payload="",
                                 header=TP["Headers"])

        TimeoutErrs = list(
            map(lambda a: isinstance(a, exceptions.Timeout), resp))

        # Error should just be propagated through since handle_peer_request will address it

        assert len(resp) == 2
        assert TimeoutErrs == [True, True]
Exemple #2
0
def test_timeout_noFed_post(mock_requests, client):
    APP.app.config["peers"] = TWO
    with client:
        url = "http://{}:{}".format(TP['URI'], TP['PORT0'])
        FR = FederationResponse(url=url,
                                request="POST",
                                endpoint_payload="",
                                endpoint_path=TP["path"],
                                request_dict=TP["Headers"])

        RO = FR.get_response_object()
        assert RO["status"] == 408
        assert RO["results"] == []
Exemple #3
0
def test_valid_PeerRequest_two_peer_post(mock_session, mock_requests, client):
    APP.app.config["peers"] = THREE
    with client:
        url = "http://{}:{}".format(TP['URI'], TP['PORT0'])
        FR = FederationResponse(url=url,
                                request="POST",
                                endpoint_payload="",
                                endpoint_path=TP["path"],
                                request_dict=TP["Federate"])

        RO = FR.get_response_object()

        assert RO["status"] == 200
        assert RO["results"] == [PostListV1, PostListV2, PostListV3]
Exemple #4
0
def test_valid_PeerRequest_one_peer_get(mock_requests, mock_session, client):
    APP.app.config["peers"] = TWO
    with client:
        url = "http://{}:{}".format(TP['URI'], TP['PORT0'])
        FR = FederationResponse(url=url,
                                request="GET",
                                endpoint_payload="",
                                endpoint_path=TP["path"],
                                request_dict=TP["Federate"])

        RO = FR.get_response_object()

        assert RO["status"] == 200
        assert RO["results"] == [AP["v1"], AP["v2"]]
Exemple #5
0
def get_federation_response(request_type, headers="Headers"):

    return FederationResponse(url="http://{}:{}".format(
        TP['URI'], TP['PORT0']),
                              request=request_type,
                              endpoint_payload="",
                              endpoint_path=TP["path"],
                              request_dict=TP[headers],
                              endpoint_service=TP["service"])
Exemple #6
0
def test_timeout_asyncRequests_two_peers_post(mock_requests, client):
    APP.app.config["peers"] = TWO
    with client:
        url = "http://{}:{}".format(TP['URI'], TP['PORT0'])
        FR = FederationResponse(url=url,
                                request="POST",
                                endpoint_payload="",
                                endpoint_path=TP["path"],
                                request_dict=TP["Headers"])

        resp = FR.async_requests(uri_list=[
            "http://{}".format(TP["Tyk1"]), "http://{}".format(TP["Tyk2"])
        ],
                                 request_type='POST',
                                 endpoint_path=TP["path"],
                                 endpoint_payload="",
                                 header=TP["Headers"])

        Success = list(map(lambda a: isinstance(a, exceptions.Timeout), resp))

        assert len(resp) == 2
        assert Success == [True, True]
Exemple #7
0
def test_valid_asyncRequests_two_peers_post(mock_requests, client):
    APP.app.config["peers"] = TWO
    with client:
        url = "http://{}:{}".format(TP['URI'], TP['PORT0'])
        FR = FederationResponse(url=url,
                                request="POST",
                                endpoint_payload="",
                                endpoint_path=TP["path"],
                                request_dict=TP["Headers"])

        resp = FR.async_requests(uri_list=[
            "http://{}".format(TP["Tyk1"]), "http://{}".format(TP["Tyk2"])
        ],
                                 request_type='POST',
                                 endpoint_path=TP["path"],
                                 endpoint_payload="",
                                 header=TP["Headers"])

        Success = list(
            filter(lambda x: x == 200, map(lambda a: a.status_code, resp)))

        assert len(resp) == 2
        assert Success == [200, 200]
Exemple #8
0
def post_search():
    """
    Send a POST request to CanDIG services and possibly federate it.
    Method defined by federation.yaml OpenAPI document.
    Retrieves an endpoint_path and endpoint_payload from POST request body,
    following the endpoint_path conventions set in get_search().
    The endpoint_payload is microservice specific but will typically be a
    JSON object of sorts.
    
    :return: response_object
    response_object: json string
    Merged responses from the federation nodes. response_object structure:
    ** This still needs to be finalized **
    {
    "status": Status,
    "results": [Response],
    "service": ServiceName
    }

    Status - Aggregate HTTP response code
    Response - List of service specific responses
    ServiceName - Name of service (used for logstash tagging)
    """
    try:

        data = json.loads(flask.request.data)
        request_type = data["request_type"]
        endpoint_path = data["endpoint_path"]
        if endpoint_path[0] == "/":
            return {
                    "response": ("Invalid endpoint_path: {}. "
                    "Please remove the / at the beginning: ".format(endpoint_path)),
                    "status": 400,
                    "service": "ErrorHandling"
                    }, 400

        endpoint_payload = data["endpoint_payload"]
        endpoint_service = data["endpoint_service"]
        microservice_URL = APP.config['services'][endpoint_service]
        federation_response = FederationResponse(url=microservice_URL,
                                                request=request_type,
                                                endpoint_path=endpoint_path,
                                                endpoint_payload=endpoint_payload,
                                                request_dict=flask.request,
                                                endpoint_service=endpoint_service
                                                )
        return federation_response.get_response_object()

    except KeyError:
        """     
        Due to Connexion parsing the args prior this code running, it will be assumed that we
        have a valid request_type, endpoint_path and endpoint_payload. A KeyError occuring here 
        will be due to the service dictionary receiving an invalid key.
        """
    return {
           "response": ("Invalid service name: {}. "
           "Please make sure that the service requested matches a registered service: "
           "{} "
           .format(endpoint_service, list(APP.config['services'].keys()))),
           "status": 404,
           "service": "ErrorHandling"
           }, 404