Exemple #1
0
def test_no_match(
    base_endpoint,
    server_rule_factory,
    configured_application_client,
    apply_rule,
    path,
    unmanaged_path,
):
    # pylint: disable=too-many-arguments
    """Check that PathRule is not triggered for different paths.

    1. Prepare path rule in the rule factory.
    2. Create a path rule and set successful response for it.
    3. Make a request to a different path.
    4. Check that the rule does not find a match.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_path_rule(path_rule_class=PathRule,
                                 base_url=base_endpoint)

    rule = PathRule(rule_type=RuleType.PATH.name, path=path)
    apply_rule(rule)

    http_response = configured_application_client.get(
        urljoin(base_endpoint, unmanaged_path))
    assert http_response.status_code == 404, "Wrong status code"
Exemple #2
0
def test_serialize_missing_path(server_rule_factory):
    """Check that RuleSerializeError is raised if rule class does not have path attribute.

    1. Create preparator for a rule factory.
    2. Prepare path rule.
    3. Try to serialize rule without path attribute.
    4. Check that RuleSerializeError is raised.
    5. Check the error.
    """
    class _WrongRule:
        # pylint: disable=too-few-public-methods
        def __init__(self, path, rule_type):
            # pylint: disable=unused-argument
            self.rule_type = rule_type

    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_path_rule(path_rule_class=_WrongRule, base_url="/")

    rule = _WrongRule(path="/", rule_type=RuleType.PATH.name)

    with pytest.raises(RuleSerializeError) as exception_info:
        server_rule_factory.serialize_rule(rule=rule)

    assert exception_info.value.args[
        0] == "Path rule must have path attribute", ("Wrong error message")
Exemple #3
0
def test_prepare_path_rule(server_rule_factory):
    """Check that path rule can be serialized.

    1. Create preparator for a rule factory.
    2. Prepare path rule.
    3. Serialize new rule.
    4. Parse serialized data.
    5. Check parsed rule.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    base_url = "/base/"
    preparator.prepare_path_rule(path_rule_class=_PathRule, base_url=base_url)

    path = "test-path"
    rule = _PathRule(path=path, rule_type=RuleType.PATH.name)
    serialized_rule = server_rule_factory.serialize_rule(rule=rule)

    assert serialized_rule["parameters"] == {
        "path": rule.path
    }, "Incorrect serialization"

    parsed_rule = server_rule_factory.parse_rule(data=serialized_rule)

    assert isinstance(parsed_rule, _PathRule), "Wrong type of the rule"
    assert parsed_rule.rule_type == RuleType.PATH.name, "Wrong rule type"
Exemple #4
0
def test_no_match(
    base_endpoint,
    server_rule_factory,
    configured_application_client,
    apply_rule,
    method,
):
    """Check that MethodRule is not triggered for different methods.

    1. Prepare method rule in the rule factory.
    2. Create a method rule and set successful response for it.
    3. Make a request of different types.
    4. Check that the rule does not find a match for any of the requests.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_method_rule(method_rule_class=MethodRule)

    rule = MethodRule(rule_type=RuleType.METHOD.name, method=method)
    apply_rule(rule)

    method_responses = [
        configured_application_client.open(base_endpoint, method=wrong_method)
        for wrong_method in _METHODS if wrong_method != method
    ]
    assert all(method_response.status_code == 404
               for method_response in method_responses), (
                   "Match found for at least one of the other methods")
def test_serialize_missing_children(server_rule_factory):
    """Check that RuleSerializeError is raised if rule class does not have children attribute.

    1. Create preparator for a rule factory.
    2. Prepare composite rule.
    3. Try to serialize rule without children attribute.
    4. Check that RuleSerializeError is raised.
    5. Check the error.
    """
    class _WrongRule:
        # pylint: disable=too-few-public-methods
        def __init__(self, children, rule_type):
            # pylint: disable=unused-argument
            self.rule_type = rule_type

    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_composite_rule(composite_rule_class=_WrongRule)

    rule = _WrongRule(children=(), rule_type=RuleType.COMPOSITE.name)

    with pytest.raises(RuleSerializeError) as exception_info:
        server_rule_factory.serialize_rule(rule=rule)

    assert exception_info.value.args[
        0] == "Composite rule must have children attribute", (
            "Wrong error message")
def test_match_found(base_endpoint, server_rule_factory,
                     configured_application_client, apply_rule):
    """Check that CompositeRule is triggered when all child rules are triggered.

    1. Prepare path, method and composite rules in the rule factory.
    2. Create a composite rule and set successful response for it.
    3. Make a request of the specified type to the specified path.
    4. Check that the rule finds a match.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_path_rule(path_rule_class=PathRule,
                                 base_url=base_endpoint)
    preparator.prepare_method_rule(method_rule_class=MethodRule)
    preparator.prepare_composite_rule(composite_rule_class=CompositeRule)

    path = "test"
    method = "POST"

    composite_rule = CompositeRule(
        rule_type=RuleType.COMPOSITE.name,
        children=[
            PathRule(rule_type=RuleType.PATH.name, path=path),
            MethodRule(rule_type=RuleType.METHOD.name, method=method),
        ],
    )
    apply_rule(composite_rule)

    http_response = configured_application_client.open(urljoin(
        base_endpoint, path),
                                                       method=method)
    assert http_response.status_code == 200, "Wrong status code"
def test_creation(base_endpoint, client_rule_factory, configured_flask_client):
    """Check that PathRule can be created.

    1. Prepare path rule in the rule factory of the flask client.
    2. Create a path rule with the client.
    3. Check the created rule.
    """
    preparator = RuleFactoryPreparator(client_rule_factory)
    preparator.prepare_path_rule(path_rule_class=PathRule, base_url=base_endpoint)

    rule_spec = PathRule(path="path-rule")
    rule = configured_flask_client.create_rule(rule=rule_spec)

    assert rule.rule_id is not None, "Rule was not created"
    assert rule.path == urljoin(base_endpoint, rule_spec.path), "Wrong path"
Exemple #8
0
def test_creation(client_rule_factory, configured_flask_client):
    """Check that MethodRule can be created.

    1. Prepare method rule in the rule factory of the flask client.
    2. Create a method rule with the client.
    3. Check the created rule.
    """
    preparator = RuleFactoryPreparator(client_rule_factory)
    preparator.prepare_method_rule(method_rule_class=MethodRule)

    rule_spec = MethodRule(method="PUT")
    rule = configured_flask_client.create_rule(rule=rule_spec)

    assert rule.rule_id is not None, "Rule was not created"
    assert rule.method == rule_spec.method, "Wrong method"
Exemple #9
0
def test_path_without_base_url(server_rule_factory):
    """Check that parser does not add base url if it was not specified.

    1. Create preparator for a rule factory.
    2. Prepare path rule without specifying base url.
    3. Serialize new rule.
    4. Parse serialized data.
    5. Check parsed rule path.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_path_rule(path_rule_class=_PathRule)

    rule = _PathRule(path="path", rule_type=RuleType.PATH.name)
    serialized_rule = server_rule_factory.serialize_rule(rule=rule)
    parsed_rule = server_rule_factory.parse_rule(data=serialized_rule)
    assert parsed_rule.path == rule.path, "Wrong path"
def test_no_children(base_endpoint, server_rule_factory,
                     configured_application_client, apply_rule):
    """Check that composite rule is not triggered without children.

    1. Prepare composite rule in the rule factory.
    2. Create a composite rule and set successful response for it.
    3. Make a request to the base endpoint.
    4. Check that the rule does not find a match.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_composite_rule(composite_rule_class=CompositeRule)

    composite_rule = CompositeRule(rule_type=RuleType.COMPOSITE.name,
                                   children=())
    apply_rule(composite_rule)

    http_response = configured_application_client.get(base_endpoint)
    assert http_response.status_code == 404, "Wrong status code"
Exemple #11
0
def test_path_with_base_url(server_rule_factory):
    """Check that parser adds base url if it was specified.

    1. Create preparator for a rule factory.
    2. Prepare path rule with custom base url.
    3. Serialize new rule.
    4. Parse serialized data.
    5. Check parsed rule path.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    base_url = "/base/"
    preparator.prepare_path_rule(path_rule_class=_PathRule, base_url=base_url)

    path = "test-path"
    rule = _PathRule(path=path, rule_type=RuleType.PATH.name)
    serialized_rule = server_rule_factory.serialize_rule(rule=rule)
    parsed_rule = server_rule_factory.parse_rule(data=serialized_rule)
    assert parsed_rule.path == urljoin(base_url, path), "Wrong path"
def test_no_match(base_endpoint, server_rule_factory,
                  configured_application_client, apply_rule):
    """Check that CompositeRule is not triggered if any of the child rules is not triggered.

    1. Prepare method and composite rules in the rule factory.
    2. Create a composite rule and set successful response for it.
    3. Make 2 requests of the specified types to the base endpoint.
    4. Check that the rule does not find a match.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_method_rule(method_rule_class=MethodRule)
    preparator.prepare_composite_rule(composite_rule_class=CompositeRule)

    first_rule = MethodRule(rule_type=RuleType.METHOD.name, method="GET")
    second_rule = MethodRule(rule_type=RuleType.METHOD.name, method="POST")

    composite_rule = CompositeRule(
        rule_type=RuleType.COMPOSITE.name,
        children=[first_rule, second_rule],
    )
    apply_rule(composite_rule)

    get_http_response = configured_application_client.get(base_endpoint)
    assert get_http_response.status_code == 404, "Wrong status code"

    post_http_response = configured_application_client.post(base_endpoint)
    assert post_http_response.status_code == 404, "Wrong status code"
Exemple #13
0
def create_rule_factory(base_url):
    """Create and prepare rule factory.

    :param base_url: base url for dynamically configured routes.
    :returns: instance of :class:`RuleFactory <looseserver.common.rule.RuleFactory>`.
    """
    rule_factory = RuleFactory()

    server_factory_preparator = RuleFactoryPreparator(rule_factory=rule_factory)
    server_factory_preparator.prepare_path_rule(path_rule_class=PathRule, base_url=base_url)
    server_factory_preparator.prepare_method_rule(method_rule_class=MethodRule)
    server_factory_preparator.prepare_composite_rule(composite_rule_class=CompositeRule)

    return rule_factory
def test_parse_wrong_parameters_type(server_rule_factory):
    """Check that RuleParseError is raised if parameters are of a wrong type.

    1. Create preparator for a rule factory.
    2. Prepare composite rule.
    3. Try to parse data with string parameters.
    4. Check that RuleParseError is raised.
    5. Check the error.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_composite_rule(composite_rule_class=_CompositeRule)

    rule = _CompositeRule(rule_type=RuleType.COMPOSITE.name, children=())
    serialized_rule = server_rule_factory.serialize_rule(rule=rule)
    serialized_rule["parameters"] = ""

    with pytest.raises(RuleParseError) as exception_info:
        server_rule_factory.parse_rule(serialized_rule)

    expected_message = "Rule parameters must be a dictionary with 'children' key"
    assert exception_info.value.args[
        0] == expected_message, "Wrong error message"
Exemple #15
0
def test_parse_missing_path(server_rule_factory):
    """Check that RuleParseError is raised if path is missing.

    1. Create preparator for a rule factory.
    2. Prepare path rule.
    3. Try to parse data without "path" key.
    4. Check that RuleParseError is raised.
    5. Check the error.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_path_rule(path_rule_class=_PathRule, base_url="/")

    rule = _PathRule(rule_type=RuleType.PATH.name, path="test")
    serialized_rule = server_rule_factory.serialize_rule(rule=rule)
    serialized_rule["parameters"].pop("path")

    with pytest.raises(RuleParseError) as exception_info:
        server_rule_factory.parse_rule(serialized_rule)

    assert exception_info.value.args[
        0] == "Rule parameters must be a dictionary with 'path' key", (
            "Wrong error message")
def test_parse_wrong_parameters_type(server_rule_factory):
    """Check that RuleParseError is raised if parameters are of a wrong type.

    1. Create preparator for a rule factory.
    2. Prepare method rule.
    3. Try to parse data with string parameters.
    4. Check that RuleParseError is raised.
    5. Check the error.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_method_rule(method_rule_class=_MethodRule)

    rule = _MethodRule(rule_type=RuleType.METHOD.name, method="DELETE")
    serialized_rule = server_rule_factory.serialize_rule(rule=rule)
    serialized_rule["parameters"] = ""

    with pytest.raises(RuleParseError) as exception_info:
        server_rule_factory.parse_rule(serialized_rule)

    expected_message = "Rule parameters must be a dictionary with 'method' key"
    assert exception_info.value.args[
        0] == expected_message, "Wrong error message"
Exemple #17
0
def test_match_found(
    base_endpoint,
    server_rule_factory,
    configured_application_client,
    apply_rule,
    method,
):
    """Check that MethodRule is triggered for the specified method.

    1. Prepare method rule in the rule factory.
    2. Create a method rule and set successful response for it.
    3. Make a request of the specified type.
    4. Check that the rule finds a match.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_method_rule(method_rule_class=MethodRule)

    rule = MethodRule(rule_type=RuleType.METHOD.name, method=method)
    apply_rule(rule)

    http_response = configured_application_client.open(base_endpoint,
                                                       method=method)
    assert http_response.status_code == 200, "Wrong status code"
Exemple #18
0
def test_match_found(
    base_endpoint,
    server_rule_factory,
    configured_application_client,
    apply_rule,
    path,
):
    """Check that PathRule is triggered for the specified path.

    1. Prepare path rule in the rule factory.
    2. Create a path rule and set successful response for it.
    3. Make a request to the specified path.
    4. Check that the rule finds a match.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_path_rule(path_rule_class=PathRule,
                                 base_url=base_endpoint)

    rule = PathRule(rule_type=RuleType.PATH.name, path=path)
    apply_rule(rule)

    http_response = configured_application_client.get(
        urljoin(base_endpoint, path))
    assert http_response.status_code == 200, "Wrong status code"
def test_prepare_method_rule(server_rule_factory):
    """Check that method rule can be serialized.

    1. Create preparator for a rule factory.
    2. Prepare method rule.
    3. Serialize new rule.
    4. Parse serialized data.
    5. Check parsed rule.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_method_rule(method_rule_class=_MethodRule)

    rule = _MethodRule(method="PUT", rule_type=RuleType.METHOD.name)
    serialized_rule = server_rule_factory.serialize_rule(rule=rule)

    assert serialized_rule["parameters"] == {
        "method": rule.method
    }, "Incorrect serialization"

    parsed_rule = server_rule_factory.parse_rule(data=serialized_rule)

    assert isinstance(parsed_rule, _MethodRule), "Wrong type of the rule"
    assert parsed_rule.rule_type == RuleType.METHOD.name, "Wrong rule type"
    assert parsed_rule.method == rule.method, "Wrong method"
Exemple #20
0
def client_method_rule_class(client_rule_factory):
    """Class for client method rules."""
    class _MethodRule(ClientRule):
        # pylint: disable=too-few-public-methods
        def __init__(self,
                     method,
                     rule_type=RuleType.METHOD.name,
                     rule_id=None):
            super(_MethodRule, self).__init__(rule_type=rule_type,
                                              rule_id=rule_id)
            self.method = method

    RuleFactoryPreparator(client_rule_factory).prepare_method_rule(
        method_rule_class=_MethodRule)

    return _MethodRule
Exemple #21
0
def test_creation(configured_flask_client, client_rule_factory, children):
    """Check that CompositeRule can be created.

    1. Prepare composite rule in the rule factory of the flask client.
    2. Create a composite rule with the client.
    3. Check the created rule.
    """
    preparator = RuleFactoryPreparator(client_rule_factory)
    preparator.prepare_method_rule(method_rule_class=MethodRule)
    preparator.prepare_composite_rule(composite_rule_class=CompositeRule)

    rule_spec = CompositeRule(children=children)
    rule = configured_flask_client.create_rule(rule=rule_spec)

    assert rule.rule_id is not None, "Rule was not created"
    assert len(rule_spec.children) == len(children), "Wrong number of children"
def test_prepare_composite_rule(server_rule_factory, number_of_children):
    """Check that composite rule can be serialized.

    1. Create preparator for a rule factory.
    2. Prepare method and composite rules.
    3. Serialize new composite rule.
    4. Parse serialized data.
    5. Check parsed rule.
    """
    preparator = RuleFactoryPreparator(server_rule_factory)
    preparator.prepare_method_rule(method_rule_class=_MethodRule)
    preparator.prepare_composite_rule(composite_rule_class=_CompositeRule)

    children = [
        _MethodRule(rule_type=RuleType.METHOD.name, method=str(index))
        for index in range(number_of_children)
    ]
    rule = _CompositeRule(children=children, rule_type=RuleType.COMPOSITE.name)
    serialized_rule = server_rule_factory.serialize_rule(rule=rule)

    expected_serialized_children = [
        server_rule_factory.serialize_rule(child) for child in children
    ]

    assert serialized_rule["parameters"] == {
        "children": expected_serialized_children
    }, ("Incorrect serialization")

    parsed_rule = server_rule_factory.parse_rule(data=serialized_rule)

    assert isinstance(parsed_rule, _CompositeRule), "Wrong type of the rule"
    assert parsed_rule.rule_type == RuleType.COMPOSITE.name, "Wrong rule type"
    assert len(
        parsed_rule.children) == number_of_children, "Wrong number of children"
    assert all(child == expected_child for child, expected_child in zip(
        parsed_rule.children, children)), "Wrong children"