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"
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_default_response_factory( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, ): """Check that default response factory is used if custom one is not specified. 1. Configure application without specifying response factory. 2. Create a flask client. 3. Create a GET-rule and set a response for it. 4. Make a request to the base url. 5. Check the response. """ application = configure_application() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule_id = client.create_rule(client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get(DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"
def test_default_configuration_endpoint( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, ): """Test the default configuration endpoint of the application. 1. Configure application without specifying configuration endpoint. 2. Create a flask client with default configuration endpoint. 3. Create a rule with the client (RulesManager resource). 4. Get the rule with the client (Rule resource). 5. Set a response for the rule with the client (Response resource). 6. Make a request to trigger the configured rule and response. 7. Check the response. """ application = configure_application() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule_id = client.create_rule( client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get( DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"
def test_default_routes( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, ): """Check that rules are applied for all nested routes of the default base endpoint. 1. Configure application without specifying base endpoint. 2. Create a flask client. 3. Create a GET-rule and set a response for it. 4. Make a GET-request to the default endpoint. 5. Check the response. """ application = configure_application() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule_id = client.create_rule( client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get( DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"
def test_response_factory( client_rule_factory, client_response_factory, client_method_rule_class, server_response_prototype, ): """Check that custom response factory is used when specified. 1. Create a response factory. 2. Register a new response type. 3. Configure application with the created response factory. 4. Create a flask client. 5. Create a PUT-rule with the client. 6. Make a POST-request to set a response for the rule. 7. Check that response is successful. 8. Make a request to the base url. 9. Check the response. """ response_factory = ResponseFactory() application = configure_application(response_factory=response_factory) application_client = application.test_client() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application_client, ) rule_id = client.create_rule(client_method_rule_class(method="PUT")).rule_id response_type = "".join(random.choice(string.ascii_uppercase) for _ in range(10)) response = server_response_prototype.create_new( response_type=response_type, builder_implementation=b"body", ) response_factory.register_response( response_type=response_type, parser=lambda *args, **kwargs: response, serializer=lambda *args, **kwargs: {}, ) serialized_response = response_factory.serialize_response(response=response) http_response = application_client.post( urljoin(DEFAULT_CONFIGURATION_ENDPOINT, "response/{0}".format(rule_id)), json=serialized_response, ) assert http_response.status_code == 200, "Can't set a response" assert application_client.put(DEFAULT_BASE_ENDPOINT).data == b"body"
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"))
def test_default_rule_factory( client_rule_factory, client_response_factory, client_method_rule_class, ): """Check that default rule factory is used if custom one is not specified. 1. Configure application without specifying rule factory. 2. Create a flask client. 3. Create a method rule for PUT requests with clients. 4. Check that rule is created and contains rule ID. """ application = configure_application() client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule = client.create_rule(client_method_rule_class(method="GET")) assert rule.rule_id is not None, "Rule was not created"
def test_routes( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, specified_endpoint, expected_endpoint, ): # pylint: disable=too-many-arguments """Check that rules are applied for all nested routes of the specified base endpoint. 1. Configure application with specifying base endpoint. 2. Create a flask client. 3. Create a GET-rule and set a response for it. 4. Make a GET-request to the default endpoint. 5. Check the response. """ server_rule_factory = create_rule_factory(base_url=expected_endpoint) application = configure_application( base_endpoint=specified_endpoint, rule_factory=server_rule_factory, ) client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=DEFAULT_CONFIGURATION_ENDPOINT, application_client=application.test_client(), ) rule_id = client.create_rule( client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get( expected_endpoint).status_code == 200, "Wrong status"
def test_configuration_endpoint( client_rule_factory, client_response_factory, client_method_rule_class, client_fixed_response_class, specified_endpoint, expected_endpoint, ): # pylint: disable=too-many-arguments """Test that specified configuration endpoint is used. 1. Configure application with specifying configuration endpoint. 2. Create a flask client with expected configuration endpoint. 3. Create a rule with the client (RulesManager resource). 4. Get the rule with the client (Rule resource). 5. Set a response for the rule with the client (Response resource). 6. Make a request to trigger the configured rule and response. 7. Check the response. """ application = configure_application( configuration_endpoint=specified_endpoint) client = FlaskClient( rule_factory=client_rule_factory, response_factory=client_response_factory, configuration_url=expected_endpoint, application_client=application.test_client(), ) rule_id = client.create_rule( client_method_rule_class(method="GET")).rule_id client.set_response(rule_id=rule_id, response=client_fixed_response_class(status=200)) assert application.test_client().get( DEFAULT_BASE_ENDPOINT).status_code == 200, "Wrong status"