def test_serialization_failed_unknown_error( core_manager, rules_manager_endpoint, server_rule_factory, server_rule_prototype, application_client, ): """Check that error is returned if RuleError is raised on attempt to serialize a rule. 1. Register rule type with serializer that raises RuleError. 2. Make a POST request to create a rule. 3. Check that response contains an error. 4. Check the error. 5. Check that rule has not been created. """ parser = lambda rule_type, rule_data: server_rule_prototype def _serializer(*args, **kwargs): raise RuleError() server_rule_factory.register_rule(rule_type="RuleError", parser=parser, serializer=_serializer) rule_data = {"rule_type": "RuleError", "parameters": {}} http_response = application_client.post(rules_manager_endpoint, json=rule_data) assert http_response.status_code == 500, "Wrong status code" expected_error = APIError("Rule may be created, but can't be serialized") assert http_response.json == build_response( error=expected_error), "Wrong response" assert not core_manager.get_rules_order(), "Rule has been created"
def test_parse_failed_parse_error( core_manager, rules_manager_endpoint, server_rule_factory, application_client, ): """Check that error is returned if RuleParseError is raised on attempt to parse data. 1. Make a POST request without rule type in the data. 2. Check that response contains an error. 3. Check the error. 4. Check that rule has not been created. """ rule_data = {} http_response = application_client.post(rules_manager_endpoint, json=rule_data) assert http_response.status_code == 400, "Wrong status code" parent_error = None try: server_rule_factory.parse_rule(data=rule_data) except RuleParseError as error: parent_error = error message = "Failed to create a rule for specified parameters. Error: '{0}'".format( parent_error) assert http_response.json == build_response( error=APIError(message)), "Wrong response" assert not core_manager.get_rules_order(), "Rule has been created"
def test_create_rule( core_manager, rules_manager_endpoint, server_rule_factory, registered_rule_prototype, application_client, ): """Check that rule can be created with API. 1. Make a POST request to create a rule. 2. Check that rule is created. """ serialized_rule = server_rule_factory.serialize_rule( registered_rule_prototype) http_response = application_client.post(rules_manager_endpoint, json=serialized_rule) assert http_response.status_code == 200, "Wrong status code" json_response = http_response.json rule_id = json_response["data"].get("rule_id") assert rule_id is not None, "Rule ID has not been set" expected_data = {"rule_id": rule_id} expected_data.update(serialized_rule) assert json_response == build_response( data=expected_data), "Wrong response" assert core_manager.get_rule( rule_id=rule_id) is not None, "Rule has not been created"
def test_set_response_non_existent_rule( core_manager, response_endpoint, server_response_factory, server_rule_prototype, registered_response_prototype, application_client, ): # pylint: disable=too-many-arguments """Check that error is returned on attempt to set response for a non-existent rule. 1. Create a rule. 2. Make a POST request to set a response for a not-existent rule. 3. Check that response contains an error. 4. Check the error. """ core_manager.add_rule(server_rule_prototype) serialized_response = server_response_factory.serialize_response( registered_response_prototype) http_response = application_client.post( response_endpoint.format(rule_id="FakeID"), json=serialized_response, ) assert http_response.status_code == 400, "Wrong status code" expected_error = APIError( "Failed to create a response: Rule does not exist") assert http_response.json == build_response( error=expected_error), "Wrong response"
def test_get_response( core_manager, response_endpoint, server_response_factory, server_rule_prototype, registered_response_prototype, application_client, ): # pylint: disable=too-many-arguments """Check that response can be obtained with API. 1. Create a rule. 2. Set a response for the rule. 2. Make a GET request to get a response. 3. Check that response is obtained. """ rule_id = core_manager.add_rule(server_rule_prototype) core_manager.set_response(rule_id=rule_id, response=registered_response_prototype) http_response = application_client.get( response_endpoint.format(rule_id=rule_id)) assert http_response.status_code == 200, "Wrong status code" expected_data = server_response_factory.serialize_response( registered_response_prototype) assert http_response.json == build_response( data=expected_data), "Wrong response"
def test_set_response( core_manager, response_endpoint, server_response_factory, server_rule_prototype, registered_response_prototype, application_client, ): # pylint: disable=too-many-arguments """Check that response can be set with API. 1. Create a rule. 2. Make a POST request to set a response for the rule. 3. Check that response is set. """ rule_id = core_manager.add_rule(server_rule_prototype) serialized_response = server_response_factory.serialize_response( registered_response_prototype) http_response = application_client.post( response_endpoint.format(rule_id=rule_id), json=serialized_response, ) assert http_response.status_code == 200, "Wrong status code" assert http_response.json == build_response( data=serialized_response), "Wrong response" assert core_manager.get_response( rule_id=rule_id) is not None, "Response has not been set"
def test_parse_failed_unknown_error( core_manager, rules_manager_endpoint, server_rule_factory, server_rule_prototype, application_client, ): """Check that error is returned if RuleError is raised on attempt to parse data. 1. Register rule type with parser that raises RuleError. 2. Make a POST request to create a rule. 3. Check that response contains an error. 4. Check the error. 5. Check that rule has not been created. """ def _parser(*args, **kwargs): raise RuleError() serializer = lambda rule_type, rule: rule_type server_rule_factory.register_rule(rule_type="RuleError", parser=_parser, serializer=serializer) rule = server_rule_prototype.create_new(rule_type="RuleError") serialized_rule = server_rule_factory.serialize_rule(rule=rule) http_response = application_client.post(rules_manager_endpoint, json=serialized_rule) assert http_response.status_code == 500, "Wrong status code" expected_error = APIError("Exception has been raised during rule creation") assert http_response.json == build_response( error=expected_error), "Wrong response" assert not core_manager.get_rules_order(), "Rule has been created"
def test_request_data_error( core_manager, response_endpoint, server_rule_prototype, application_client, data, ): """Check that error is returned if a request does not contain JSON data. 1. Create a rule. 2. Make a POST request to set response without JSON data. 3. Check that response contains an error. 4. Check the error. 5. Check that response has not been set. """ rule_id = core_manager.add_rule(server_rule_prototype) http_response = application_client.post( response_endpoint.format(rule_id=rule_id), data=data, ) assert http_response.status_code == 400, "Wrong status code" expected_error = APIError("Failed to parse JSON data from the request") assert http_response.json == build_response( error=expected_error), "Wrong response" with pytest.raises(KeyError): core_manager.get_response(rule_id=rule_id)
def test_default_version(): """Check default version. 1. Build a response. 2. Check the default version. """ response = build_response() assert response.get("version") == DEFAULT_VERSION, "Wrong version is set"
def test_version(): """Check that version is included to the response. 1. Build a response. 2. Check that version is included. """ version = DEFAULT_VERSION + 1 response = build_response(version=version) assert response.get("version") == version, "Wrong version is set"
def test_success_without_data(): """Check status and fields of a successful response. 1. Build a response without error and data. 2. Check the status. 3. Check data fields. """ response = build_response(error=None, data=None) assert response["status"] == ResponseStatus.SUCCESS.name, "Wrong status" assert "error" not in response, "Error field is present in a successful response" assert "data" not in response, "Data have been set"
def test_error_without_data(): """Check status and fields of a failed response. 1. Build a response with error, but without data. 2. Check the status. 3. Check data fields. """ error = APIError("Test") response = build_response(error=error, data=None) assert response["status"] == ResponseStatus.FAILURE.name, "Wrong status" assert response["error"][ "description"] == error.description, "Wrong error data" assert "data" not in response, "Data have been set"
def test_error_with_data(): """Check status and fields of a failed response. 1. Build a response without error. 2. Check the status. 3. Check data fields. """ data = {} error = APIError("Test") response = build_response(error=error, data=data) assert response["status"] == ResponseStatus.FAILURE.name, "Wrong status" assert response["error"][ "description"] == error.description, "Wrong error data" assert response.get("data") == data, "Data have not been set"
def test_remove_rule(core_manager, rule_endpoint, registered_rule_prototype, application_client): """Check that rule can be removed with API. 1. Create a rule. 2. Make a DELETE request to remove the rule. 3. Check that rule is removed. """ rule_id = core_manager.add_rule(rule=registered_rule_prototype) http_response = application_client.delete( rule_endpoint.format(rule_id=rule_id)) assert http_response.status_code == 200, "Wrong status code" assert http_response.json == build_response(), "Wrong response" assert not core_manager.get_rules_order(), "Rule has not been removed"
def test_parse_failed_unknown_error( core_manager, response_endpoint, server_response_factory, server_rule_prototype, server_response_prototype, application_client, ): # pylint: disable=too-many-arguments """Check that error is returned if ResponseError is raised on attempt to parse data. 1. Create a rule. 2. Register response type with parser that raises ResponseError. 3. Make a POST request to set a response. 4. Check that response contains an error. 5. Check the error. 6. Check that response has not been set. """ rule_id = core_manager.add_rule(server_rule_prototype) def _parser(*args, **kwargs): raise ResponseError() serializer = lambda response_type, response: response_type response = server_response_prototype.create_new(response_type="RuleError") server_response_factory.register_response( response_type=response.response_type, parser=_parser, serializer=serializer, ) serialized_response = server_response_factory.serialize_response( response=response) http_response = application_client.post( response_endpoint.format(rule_id=rule_id), json=serialized_response, ) assert http_response.status_code == 500, "Wrong status code" expected_error = APIError( "Exception has been raised during response creation") assert http_response.json == build_response( error=expected_error), "Wrong response" with pytest.raises(KeyError): core_manager.get_response(rule_id=rule_id)
def test_request_data_error(core_manager, rules_manager_endpoint, application_client, data): """Check that error is returned if a request does not contain JSON data. 1. Make a POST request without JSON data. 2. Check that response contains an error. 3. Check the error. 4. Check that rule has not been created. """ http_response = application_client.post(rules_manager_endpoint, data=data) assert http_response.status_code == 400, "Wrong status code" expected_error = APIError("Failed to parse JSON data from the request") assert http_response.json == build_response( error=expected_error), "Wrong response" assert not core_manager.get_rules_order(), "Rule has been created"
def test_set_response_serialization_failed( core_manager, response_endpoint, server_response_factory, server_rule_prototype, server_response_prototype, application_client, ): # pylint: disable=too-many-arguments """Check that error is returned if ResponseError is raised on attempt to serialize a rule. 1. Create a rule. 2. Register response type with serializer that raises ResponseError. 3. Make a POST request to set a response. 4. Check that response contains an error. 5. Check the error. 6. Check that response has not been set. """ rule_id = core_manager.add_rule(server_rule_prototype) parser = lambda response_type, response_data: server_response_prototype def _serializer(*args, **kwargs): raise ResponseError() server_response_factory.register_response( response_type="ResponseError", parser=parser, serializer=_serializer, ) response_data = {"response_type": "ResponseError", "parameters": {}} http_response = application_client.post( response_endpoint.format(rule_id=rule_id), json=response_data, ) assert http_response.status_code == 500, "Wrong status code" expected_error = APIError("Response can't be serialized") assert http_response.json == build_response( error=expected_error), "Wrong response" with pytest.raises(KeyError): core_manager.get_response(rule_id=rule_id)
def test_get_response_serialization_failed( core_manager, response_endpoint, server_response_factory, server_rule_prototype, server_response_prototype, application_client, ): # pylint: disable=too-many-arguments """Check that error is returned if ResponseError is raised on attempt to serialize a rule. 1. Create a rule. 2. Register response type with serializer that raises ResponseError. 3. Set response for the rule. 4. Make a GET request to get a response. 5. Check that response contains an error. 6. Check the error. """ rule_id = core_manager.add_rule(server_rule_prototype) response = server_response_prototype.create_new( response_type="ResponseError") core_manager.set_response(rule_id=rule_id, response=response) parser = lambda response_type, response_data: server_response_prototype def _serializer(*args, **kwargs): raise ResponseError() server_response_factory.register_response( response_type=response.response_type, parser=parser, serializer=_serializer, ) http_response = application_client.get( response_endpoint.format(rule_id=rule_id)) assert http_response.status_code == 500, "Wrong status code" expected_error = APIError("Response can't be serialized") assert http_response.json == build_response( error=expected_error), "Wrong response"
def test_get_non_existent_rule( core_manager, rule_endpoint, registered_rule_prototype, application_client, ): """Check that error is returned if rule does not exist. 1. Create a rule. 2. Make a GET request to get a non-existent rule. 3. Check that response contains an error. 4. Check the error. """ core_manager.add_rule(rule=registered_rule_prototype) http_response = application_client.get( rule_endpoint.format(rule_id="FakeID")) assert http_response.status_code == 404, "Wrong status code" expected_error = APIError("Failed to find rule with ID 'FakeID'") assert http_response.json == build_response( error=expected_error), "Wrong response"
def test_parse_failed_parse_error( core_manager, response_endpoint, server_response_factory, server_rule_prototype, application_client, ): """Check that error is returned if ResponseParseError is raised on attempt to parse data. 1. Create a rule. 2. Make a POST request to set resonse without response type in the data. 3. Check that response contains an error. 4. Check the error. 5. Check that response has not been set. """ rule_id = core_manager.add_rule(server_rule_prototype) response_data = {} http_response = application_client.post( response_endpoint.format(rule_id=rule_id), json=response_data, ) assert http_response.status_code == 400, "Wrong status code" parent_error = None try: server_response_factory.parse_response(data=response_data) except ResponseParseError as error: parent_error = error message = "Failed to create a response for specified parameters. Error: '{0}'".format( parent_error, ) assert http_response.json == build_response( error=APIError(message)), "Wrong response" with pytest.raises(KeyError): core_manager.get_response(rule_id=rule_id)
def test_get_response_non_existent_rule( core_manager, response_endpoint, server_rule_prototype, application_client, ): """Check that error is returned on attempt to get response for a non-existent rule. 1. Create a rule. 2. Make a GET request to get a response for a not-existent rule. 3. Check that response contains an error. 4. Check the error. """ core_manager.add_rule(server_rule_prototype) http_response = application_client.get( response_endpoint.format(rule_id="FakeID")) assert http_response.status_code == 404, "Wrong status code" message = "Failed to get response for the rule 'FakeID'" assert http_response.json == build_response( error=APIError(message)), "Wrong response"
def test_serialization_failed_unknown_error( core_manager, rule_endpoint, server_rule_factory, server_rule_prototype, application_client, ): """Check that error is returned if RuleError is raised on attempt to serialize a rule. 1. Register rule type with serializer that raises RuleError. 2. Create a rule. 3. Make a GET request to the rule. 4. Check that response contains an error. 5. Check the error. """ parser = lambda rule_type, rule_data: server_rule_prototype def _serializer(*args, **kwargs): raise RuleError() rule = server_rule_prototype.create_new(rule_type="RuleError") server_rule_factory.register_rule( rule_type=rule.rule_type, parser=parser, serializer=_serializer, ) rule_id = core_manager.add_rule(rule=rule) http_response = application_client.get( rule_endpoint.format(rule_id=rule_id)) assert http_response.status_code == 500, "Wrong status code" message = "Exception has been raised during serialization of the rule" assert http_response.json == build_response( error=APIError(message)), "Wrong response"