Esempio n. 1
0
def test_create_response_factory(
    base_endpoint,
    configuration_endpoint,
    default_factories_application,
):
    """Check that default responses are registered in the default response factory.

    1. Configure application with default factories.
    2. Create default response factory for client.
    3. Create a method rule with the client.
    4. Set a fixed response with the client.
    4. Check that response is successful.
    """
    application_client = default_factories_application.test_client()

    client = FlaskClient(
        configuration_url=configuration_endpoint,
        rule_factory=create_rule_factory(),
        response_factory=create_response_factory(),
        application_client=application_client,
    )

    rule = client.create_rule(rule=MethodRule(method="GET"))

    fixed_response = FixedResponse(status=200)
    client.set_response(rule_id=rule.rule_id, response=fixed_response)

    assert application_client.get(
        base_endpoint).status_code == fixed_response.status, (
            "Response was not set")
def test_create_rule_factory(
    configuration_endpoint,
    client_response_factory,
    default_factories_application,
):
    """Check that default rules are registered in the default rule factory.

    1. Configure application with default rule factory.
    2. Create default rule factory for client.
    3. Create a path, method and composite rules with the client.
    4. Check that responses are successful.
    """
    rule_factory = create_rule_factory()

    client = FlaskClient(
        configuration_url=configuration_endpoint,
        rule_factory=rule_factory,
        response_factory=client_response_factory,
        application_client=default_factories_application.test_client(),
    )

    path_rule_spec = PathRule(path="path")
    path_rule = client.create_rule(rule=path_rule_spec)
    assert path_rule.rule_id is not None, "Rule was not created"

    method_rule_spec = MethodRule(method="DELETE")
    method_rule = client.create_rule(rule=method_rule_spec)
    assert method_rule.rule_id is not None, "Rule was not created"

    composite_rule_spec = CompositeRule(
        children=[path_rule_spec, method_rule_spec])
    composite_rule = client.create_rule(rule=composite_rule_spec)
    assert composite_rule.rule_id is not None, "Rule was not created"
Esempio n. 3
0
def test_application(flask_run_parameters, base_endpoint, configuration_endpoint):
    """Check that application is configured with specified endpoints.

    1. Run application with specified base endpoint and configuration endpoint.
    2. Create a flask client with the specified configuration endpoint.
    3. Create a method rule and set a successful response for it.
    4. Make a GET-request for the specified based endpoint.
    5. Check the response.
    """
    commandline_arguments = [
        "--base-endpoint", base_endpoint,
        "--configuration-endpoint", configuration_endpoint,
        ]
    run_module._run(commandline_arguments)  # pylint: disable=protected-access

    application = flask_run_parameters["self"]
    application_client = application.test_client()

    client = FlaskClient(
        configuration_url=configuration_endpoint,
        application_client=application_client,
        )

    rule = client.create_rule(rule=MethodRule(method="GET"))
    client.set_response(rule_id=rule.rule_id, response=FixedResponse(status=200))

    assert application_client.get(base_endpoint).status_code == 200, "Wrong status"
Esempio n. 4
0
def test_default_factories():
    """Check that default factories are used if None was specified.

    1. Create default application.
    2. Create a flask client without specifying rule factory.
    3. Create a method rule with the client.
    4. Set a fixed response with the client.
    5. Check the response.
    """
    application = configure_application()
    application_client = application.test_client()

    client = FlaskClient(
        configuration_url=DEFAULT_CONFIGURATION_ENDPOINT,
        application_client=application.test_client(),
    )

    rule = client.create_rule(rule=MethodRule(method="GET"))
    assert rule.rule_id is not None, "Rule was not created"

    client.set_response(rule_id=rule.rule_id,
                        response=FixedResponse(status=200))

    assert application_client.get(
        DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"
Esempio n. 5
0
def test_default_rule_type():
    """Check the default rule type of the method rule.

    1. Create a method rule without specifying its type.
    2. Check the rule type.
    """
    rule = MethodRule(method="GET")
    assert rule.rule_type == RuleType.METHOD.name, "Wrong rule type"
Esempio n. 6
0
def test_rule_representation():
    """Check the representation of the method rule.

    1. Create a method rule.
    2. Check result of the repr function.
    """
    rule = MethodRule(method="POST")
    assert repr(rule) == "MethodRule(method='POST')", "Wrong representation"
Esempio n. 7
0
def children(request):
    """Children of the composite rule."""
    methods = [
        "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "CONNECT", "TRACE",
        "PATCH"
    ]
    return [
        MethodRule(method=random.choice(methods)) for _ in range(request.param)
    ]
Esempio n. 8
0
def existing_get_rule(
    configuration_endpoint,
    default_factories_application,
    client_response_factory,
):
    """Rule that matches every GET-request."""
    client = FlaskClient(
        configuration_url=configuration_endpoint,
        rule_factory=create_rule_factory(),
        response_factory=client_response_factory,
        application_client=default_factories_application.test_client(),
    )
    return client.create_rule(rule=MethodRule(method="GET"))
Esempio n. 9
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"