def add():
    data_request = request.get_json()
    schema = Schema({
        Required("master_user_id"): validate_user_by_id,
        Required("comment_thread_id"): int,
        Optional("comment_reply_thread_id"): int,
        Required("comment"): All(str, Length(min=1, max=200))
    })
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    master_user_id = data_request.get('master_user_id')
    comment_thread_id = data_request.get('comment_thread_id')
    comment = data_request.get('comment')
    try:
        comment_reply_thread_id = data_request.get('comment_reply_thread_id')
    except:
        comment_reply_thread_id = None
    comments = CommentsDelegate()
    if comments.add(master_user_id, comment_thread_id, comment_reply_thread_id, comment):
        response_data = SUCCESS.copy()
        response_data["message"] = ADDED
        response = generic_success_response(response_data)
    else:
        response = ERROR_RESPONSE
    return response
def test_error_reporting():
    expected = S({
        'info': Partial({
            'package_url': 'http://pypi.python.org/pypi/pytest',
            'platform': 'INVALID VALUE',
            'description': Length(max=10),
            'downloads': list,
            'classifiers': dict,
        }),
        'urls': int
    })
    _ = expected == TEST_DATA
    msgs = pytest_assertrepr_compare('==', expected, TEST_DATA)
    assert S(Unordered([
        "failed due to validation error(s):",
        "- info.platform: not a valid value (actual: 'unix')",
        "- info.description: length of value must be at most 10 (actual: 'lorem ipsum lorem ipsum')",
        "- info.downloads: expected list (actual: {'last_month': 0})",
        "- info.classifiers: expected dict (actual: ['Development Status :: 6 - Mature', 'Intended Audience :: Developers'])",
        "- urls: expected int (actual: [{}, {}])",
        Any(
            "- releases: extra keys not allowed (actual: {'3.0.7': [], '3.1.3': []})",
            "- releases: extra keys not allowed (actual: {'3.1.3': [], '3.0.7': []})",
        ),
    ])) == msgs
Beispiel #3
0
def test_error_reporting():
    expected = S({
        'info':
        Partial({
            'package_url': 'http://pypi.python.org/pypi/pytest',
            'platform': 'INVALID VALUE',
            'description': Length(max=10),
            'downloads': list,
            'classifiers': dict,
        }),
        'urls':
        int
    })
    _ = expected == TEST_DATA
    msgs = pytest_assertrepr_compare('==', expected, TEST_DATA)
    assert S(
        Unordered([
            "failed to validation error(s):",
            "- info.platform: not a valid value for dictionary value @ data['info']['platform']",
            "- info.description: length of value must be at most 10 for dictionary value @ data['info']['description']",
            "- info.downloads: expected list for dictionary value @ data['info']['downloads']",
            "- info.classifiers: expected dict for dictionary value @ data['info']['classifiers']",
            "- urls: expected int for dictionary value @ data['urls']",
            "- releases: extra keys not allowed @ data['releases']"
        ])) == msgs
def add():
    data_request = request.get_json()
    schema = Schema({
        Required("master_user_id"): validate_user_by_id,
        Required("post_text"): All(str, Length(min=20, max=100)),
        Required("has_attachment"): validate_is_active,
        Optional("attachment_url"): Url
    })
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    master_user_id = data_request.get('master_user_id')
    post_text = data_request.get('post_text')
    has_attachment = data_request.get('has_attachment')
    try:
        attachment_url = data_request.get('attachment_url')
    except:
        attachment_url = None
    if has_attachment == TRUE and attachment_url == None:
        response_data = ERROR.copy()
        response_data["message"] = "Attachment URL missing!"
        response = generic_success_response(response_data)
        return response

    posts = PostsDelegate()
    if posts.add(master_user_id, post_text, has_attachment, attachment_url):
        response_data = SUCCESS.copy()
        response_data["message"] = ADDED
        response = generic_success_response(response_data)
    else:
        response = ERROR_RESPONSE
    return response
def test_validation_ok():
    assert S({
        'info': Partial({
            'package_url': 'http://pypi.python.org/pypi/pytest',
            'platform': 'unix',
            'description': Length(min=10),
            'downloads': dict,
            'classifiers': list,
        }),
        'releases': {
            any: list
        },
        'urls': list
    }) == TEST_DATA
class validator:
    idSchema = Schema(All(str, Length(min=24, max=24)))
    jsStrSchema = Schema(str)
    jsIntSchema = Schema(int)

    @classmethod
    def validateId(cls, sid):
        return cls.idSchema(sid)

    @classmethod
    def validateData(cls, data):
        cls.idSchema(sid)
        cls.jsStrSchema(data["title"])
        cls.jsIntSchema(data["price"])
        cls.jsIntSchema(data["inventory_count"])
        return
def update_post_text(post_id=None):
    data_request = request.get_json()
    schema = Schema({Required("post_text"): All(str, Length(min=1, max=200))})
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    post_text = data_request.get('post_text')
    posts = PostsDelegate(post_id)
    if posts.update_post_text(post_text):
        response_data = SUCCESS.copy()
        response_data["message"] = UPDATED
        response = generic_success_response(response_data)
    else:
        response = ERROR_RESPONSE
    return response
Beispiel #8
0
class validator:
    idSchema = Schema(All(str, Length(min=24, max=24)))
    jsSchema = Schema(str)

    @classmethod
    def validateId(cls, sid):
        return cls.idSchema(sid)

    @classmethod
    def validatePut(cls, sid, data):
        cls.jsSchema(data)
        cls.idSchema(sid)
        return

    @classmethod
    def validateData(cls, data):
        cls.jsSchema(data)
        return
Beispiel #9
0
def update_blog_subject(blog_id=None):
    data_request = request.get_json()
    schema = Schema({
        Required("blog_subject"): All(str, Length(min=20, max=100))
    })
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    blog_subject = data_request.get('blog_subject')
    blogs = BlogsDelegate(blog_id)
    if blogs.update_blog_subject(blog_subject):
        response_data = SUCCESS.copy()
        response_data["message"] = UPDATED
        response = generic_success_response(response_data)
    else:
        response = ERROR_RESPONSE
    return response
def add_message(master_user_id=None, thread_id=None):
    data_request = request.get_json()
    schema = Schema({
        Required("message_text"): All(str, Length(min=1, max=400)),
        Required("has_attachment"): All(int, validate_is_active),
        Optional("attachment_url"): Url
    })
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    message_text = data_request.get('message_text')
    has_attachment = data_request.get('has_attachment')
    try:
        attachment_url = data_request.get('attachment_url')
    except:
        attachment_url = ""
    message = UserMessageDelegate(master_user_id, thread_id)
    message_id = message.add(message_text, has_attachment, attachment_url)
    if message_id is not None:
        message_status = UserMessageStatusDelegate(master_user_id, thread_id)
        if message_status.add(message_id):
            thread = UserMessageThreadsDelegate(
                thread_id)  # update threads last message
            updated_last_user_message = thread.update_last_user_message(
                message_id)
            thread_participants = UserMessageThreadParticipantsDelegate(
                master_user_id,
                thread_id)  # update thread for new message info
            updated_thread_participant_new_message = thread_participants.update_thread_new_message(
            )
            if updated_last_user_message and updated_thread_participant_new_message:
                response_data = SUCCESS.copy()  # return success response
                response_data["message"] = ADDED
                response = generic_success_response(response_data)
            else:
                response = FAILURE_RESPONSE
        else:
            response = ERROR_RESPONSE
    else:
        response = ERROR_RESPONSE
    return response
Beispiel #11
0
def add():
    data_request = request.get_json()
    schema = Schema({
        Required("master_user_id"): validate_user_by_id,
        Required("blog_subject"): All(str, Length(min=20, max=100)),
        Required("blog_body"): All(str),
        Required("tags"): list
    })
    try:
        schema(data_request)
    except MultipleInvalid as e:
        return multiple_invalid_response(e)
    master_user_id = data_request.get('master_user_id')
    blog_subject = data_request.get('blog_subject')
    blog_body = data_request.get('blog_body')
    tags = data_request.get('tags')
    blogs = BlogsDelegate()
    if blogs.add(master_user_id, blog_subject, blog_body, tags):
        response_data = SUCCESS.copy()
        response_data["message"] = ADDED
        response = generic_success_response(response_data)
    else:
        response = ERROR_RESPONSE
    return response
def ExactLen(nb):
    return Length(min=nb, max=nb)