def _test_post_message_validation_failed( message_class, message_class_path, client, valid_data, url=POST_URL, api="" ): # it's pretty much impossible yet to get around from_dict validation without mocking require_allowed = ["sender_ref"] if api == "rx" else [] message = message_class.from_dict(valid_data, require_allowed=require_allowed) message.kwargs['spurious_attr'] = 'Something suspicious' remove_message_params(message.kwargs, keys=message_class.required_attrs, copy_data=False, set_none=True) with mock.patch(message_class_path + '.from_dict') as from_dict: from_dict.return_value = message resp = client.post(url, json=valid_data) from_dict.assert_called_once() assert resp.mimetype == VALID_RESPONSE_MIMETYPE assert resp.status_code == HTTPStatus.BAD_REQUEST assert resp.get_json() == error_response_json_template( MessageValidationError( source=[ 'spurious attr: spurious_attr', 'sender is not a jurisdiction: None', 'receiver is not a jurisdiction: None', 'subject is empty', 'obj is not a URI: None', 'predicate is not a URI: None' ] ) )
def _test_post_message_missing_required_attr(client, valid_data, url=POST_URL): for key in Message.required_attrs: data = remove_message_params(valid_data, keys=[key]) resp = client.post(url, json=data) assert resp.mimetype == VALID_RESPONSE_MIMETYPE assert resp.status_code == HTTPStatus.BAD_REQUEST assert resp.get_json() == error_response_json_template( MessageDeserializationError(source=['\'{}\''.format(key)]))