def test_serialize_wrong_body(server_response_factory):
    """Check that ResponseSerializeError is raised if response body can't be encoded with base64.

    1. Create preparator for a response factory.
    2. Prepare fixed response.
    3. Try to serialize response with a list specified for the body.
    4. Check that ResponseSerializeError is raised.
    5. Check the error.
    """
    preparator = ResponseFactoryPreparator(server_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=_FixedResponse)

    response = _FixedResponse(
        response_type=ResponseType.FIXED.name,
        status=200,
        headers={},
        body=[],
    )

    with pytest.raises(ResponseSerializeError) as exception_info:
        server_response_factory.serialize_response(response=response)

    expected_message = "Body can't be encoded with base64 encoding"
    assert exception_info.value.args[
        0] == expected_message, "Wrong error message"
def test_parse_wrong_parameters_type(server_response_factory):
    """Check that ResponseParseError is raised if parameters are of a wrong type.

    1. Create preparator for a response factory.
    2. Prepare fixed response.
    3. Try to parse data with string parameters.
    4. Check that ResponseParseError is raised.
    5. Check the error.
    """
    preparator = ResponseFactoryPreparator(server_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=_FixedResponse)

    response = _FixedResponse(
        response_type=ResponseType.FIXED.name,
        status=200,
        headers={},
        body="body",
    )
    serialized_response = server_response_factory.serialize_response(
        response=response)
    serialized_response["parameters"] = ""

    with pytest.raises(ResponseParseError) as exception_info:
        server_response_factory.parse_response(serialized_response)

    expected_message = (
        "Response parameters must be a dictionary with keys 'body', 'status', 'headers'"
    )
    assert exception_info.value.args[
        0] == expected_message, "Wrong error message"
Esempio n. 3
0
def test_headers(
    base_endpoint,
    default_factories_application,
    client_response_factory,
    configured_flask_client,
    existing_get_rule,
    headers,
):
    # pylint: disable=too-many-arguments
    """Check that response headers can be set.

    1. Prepare a fixed response in the response factory.
    2. Set a fixed response with specified headers for an existing rule.
    3. Check the headers of the response.
    """
    preparator = ResponseFactoryPreparator(client_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=FixedResponse)

    fixed_response = FixedResponse(headers=headers)
    configured_flask_client.set_response(rule_id=existing_get_rule.rule_id,
                                         response=fixed_response)

    http_response = default_factories_application.test_client().get(
        base_endpoint)
    assert set(http_response.headers.items()).issuperset(
        headers.items()), ("Headers were not set")
def test_serialize_missing_attribute(server_response_factory, attribute):
    """Check that ResponseSerializeError is raised if response class does not have an attribute.

    1. Create preparator for a response factory.
    2. Prepare fixed response.
    3. Try to serialize response without one of the attributes.
    4. Check that ResponseSerializeError is raised.
    5. Check the error.
    """
    fields = list(_RESPONSE_FIELDS)
    fields.remove(attribute)

    _WrongResponse = namedtuple("_WrongResponse",
                                ["response_type"] + list(fields))

    def _create_response(**kwargs):
        kwargs.pop(attribute, None)
        return _WrongResponse(**kwargs)

    preparator = ResponseFactoryPreparator(server_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=_create_response)

    field_values = {field: "" for field in fields}
    response = _WrongResponse(response_type=ResponseType.FIXED.name,
                              **field_values)

    with pytest.raises(ResponseSerializeError) as exception_info:
        server_response_factory.serialize_response(response=response)

    expected_message = "Response must have attributes 'body', 'status' and 'headers'"
    assert exception_info.value.args[
        0] == expected_message, "Wrong error message"
def test_parse_wrong_body(server_response_factory, body):
    """Check that ResponseParseError is raised if the specified body is not base64 encoded.

    1. Create preparator for a response factory.
    2. Prepare fixed response.
    3. Try to parse data with wrong body.
    4. Check that ResponseParseError is raised.
    5. Check the error.
    """
    preparator = ResponseFactoryPreparator(server_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=_FixedResponse)

    response = _FixedResponse(
        response_type=ResponseType.FIXED.name,
        status=200,
        headers={},
        body="body",
    )
    serialized_response = server_response_factory.serialize_response(
        response=response)
    serialized_response["parameters"]["body"] = body

    with pytest.raises(ResponseParseError) as exception_info:
        server_response_factory.parse_response(serialized_response)

    expected_message = "Body can't be decoded with base64 encoding"
    assert exception_info.value.args[
        0] == expected_message, "Wrong error message"
Esempio n. 6
0
def test_status(
    base_endpoint,
    default_factories_application,
    client_response_factory,
    configured_flask_client,
    existing_get_rule,
    status,
):
    # pylint: disable=too-many-arguments
    """Check that response status can be set.

    1. Prepare a fixed response in the response factory.
    2. Set a fixed response with specified status for an existing rule.
    3. Check the status of the response.
    """
    preparator = ResponseFactoryPreparator(client_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=FixedResponse)

    fixed_response = FixedResponse(status=status)
    configured_flask_client.set_response(rule_id=existing_get_rule.rule_id,
                                         response=fixed_response)

    http_response = default_factories_application.test_client().get(
        base_endpoint)
    assert http_response.status_code == status, "Wrong status code"
Esempio n. 7
0
def test_bytes_body(
    base_endpoint,
    default_factories_application,
    client_response_factory,
    configured_flask_client,
    existing_get_rule,
    unicode_body,
    encoding,
):
    # pylint: disable=too-many-arguments
    """Check that response body can be set as a bytes object.

    1. Prepare a fixed response in the response factory.
    2. Set a fixed response with specified bytes body for an existing rule.
    3. Check the body of the response.
    """
    preparator = ResponseFactoryPreparator(client_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=FixedResponse)

    body = unicode_body.encode(encoding)
    headers = {"Content-Type": "text/plain; charset={0}".format(encoding)}

    fixed_response = FixedResponse(headers=headers, body=body)
    configured_flask_client.set_response(rule_id=existing_get_rule.rule_id,
                                         response=fixed_response)

    http_response = default_factories_application.test_client().get(
        base_endpoint)
    assert http_response.data.decode(encoding) == unicode_body, "Wrong body"
Esempio n. 8
0
def test_string_body(
    base_endpoint,
    default_factories_application,
    client_response_factory,
    configured_flask_client,
    existing_get_rule,
    body,
):
    # pylint: disable=too-many-arguments
    """Check that response body can be set as a string.

    1. Prepare a fixed response in the response factory.
    2. Set a fixed response with specified string body for an existing rule.
    3. Check the body of the response.
    """
    preparator = ResponseFactoryPreparator(client_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=FixedResponse)

    fixed_response = FixedResponse(body=body)
    configured_flask_client.set_response(rule_id=existing_get_rule.rule_id,
                                         response=fixed_response)

    http_response = default_factories_application.test_client().get(
        base_endpoint)
    assert http_response.data == body.encode("utf8"), "Wrong body"
Esempio n. 9
0
def create_response_factory():
    """Create and prepare response factory.

    :returns: instance of :class:`ResponseFactory <looseserver.common.response.ResponseFactory>`.
    """
    response_factory = ResponseFactory()

    server_factory_preparator = ResponseFactoryPreparator(response_factory=response_factory)
    server_factory_preparator.prepare_fixed_response(fixed_response_class=FixedResponse)

    return response_factory
def test_prepare_fixed_response(server_response_factory):
    """Check that fixed response can be serialized.

    1. Create preparator for a response factory.
    2. Prepare fixed response.
    3. Serialize new response.
    4. Parse serialized data.
    5. Check parsed response.
    """
    preparator = ResponseFactoryPreparator(server_response_factory)
    preparator.prepare_fixed_response(fixed_response_class=_FixedResponse)

    response = _FixedResponse(
        status=200,
        headers={"key": "value"},
        body="body",
        response_type=ResponseType.FIXED.name,
    )
    serialized_response = server_response_factory.serialize_response(
        response=response)

    expected_data = {
        "status": 200,
        "headers": {
            "key": "value"
        },
        "body": base64.b64encode(b"body").decode("utf8"),
    }
    assert serialized_response[
        "parameters"] == expected_data, "Incorrect serialization"

    parsed_response = server_response_factory.parse_response(
        data=serialized_response)

    assert isinstance(parsed_response,
                      _FixedResponse), "Wrong type of the response"
    assert parsed_response.response_type == ResponseType.FIXED.name, "Wrong response type"
    assert parsed_response.status == response.status, "Wrong status"
    assert parsed_response.headers == response.headers, "Wrong headers"
    assert parsed_response.body == response.body.encode("utf8"), "Wrong body"
Esempio n. 11
0
def client_fixed_response_class(client_response_factory):
    """Class for client fixed responses."""
    class _FixedResponse(ClientResponse):
        # pylint: disable=too-few-public-methods
        def __init__(
            self,
            status=200,
            headers=None,
            body="",
            response_type=ResponseType.FIXED.name,
        ):
            super(_FixedResponse, self).__init__(response_type=response_type)
            self.status = status
            self.headers = headers or {}
            self.body = body

    ResponseFactoryPreparator(client_response_factory).prepare_fixed_response(
        fixed_response_class=_FixedResponse, )

    return _FixedResponse