Esempio n. 1
0
def test_replies(db_conn, posts_table):
    """
    Expect a proposal to allow a replies to id.
    """

    prev, errors = insert_post(
        {
            'kind': 'post',
            'user_id': 'A',
            'topic_id': 'B',
            'body': 'C',
        }, db_conn)
    proposal, errors = insert_post(
        {
            'kind': 'proposal',
            'user_id': 'A',
            'topic_id': 'B',
            'body': 'C',
            'entity_versions': [{
                'id': 'D',
                'kind': 'unit'
            }],
            'name': 'E',
            'replies_to_id': prev['id'],
        }, db_conn)
    assert len(errors) == 0
Esempio n. 2
0
def test_replies(db_conn, posts_table, units_table):
    """
    Expect a vote to require a replies to id.
    """

    create_proposal(posts_table, units_table, db_conn)
    vote, errors = insert_post(
        {
            'kind': 'vote',
            'user_id': 'A',
            'topic_id': 'B',
            'response': True,
        }, db_conn)
    assert len(errors) == 1
    vote['replies_to_id'] = 'D'
    vote, errors = insert_post(vote, db_conn)
    assert len(errors) == 0
Esempio n. 3
0
def test_entity(db_conn, posts_table):
    """
    Expect a proposal to require an entity version id.
    """

    proposal, errors = insert_post(
        {
            'kind': 'proposal',
            'user_id': 'A',
            'topic_id': 'B',
            'body': 'C',
            'name': 'E',
        }, db_conn)
    assert len(errors) == 1
    proposal['entity_versions'] = [{'id': 'D', 'kind': 'unit'}]
    proposal, errors = insert_post(proposal, db_conn)
    assert len(errors) == 0
def test_body(db_conn, posts_table):
    """
    Expect a proposal to require a body.
    """

    proposal, errors = insert_post({
        'kind': 'proposal',
        'user_id': 'A',
        'topic_id': 'B',
        'entity_versions': [{
            'id': 'D',
            'kind': 'unit'
        }],
    }, db_conn)
    assert len(errors) == 1
    proposal['body'] = 'C'
    proposal, errors = insert_post(proposal, db_conn)
    assert len(errors) == 0
Esempio n. 5
0
def test_user(db_conn, posts_table):
    """
    Expect a post to require a user id.
    """

    post, errors = insert_post({
        'kind': 'post',
        'topic_id': 'B',
        'body': 'C',
    }, db_conn)
    assert len(errors) == 1
    post, errors = insert_post(
        {
            'user_id': 'A',
            'kind': 'post',
            'topic_id': 'B',
            'body': 'C',
        }, db_conn)
    assert len(errors) == 0
Esempio n. 6
0
def test_body(db_conn, posts_table, units_table):
    """
    Expect a vote to allow, but not require, a body.
    """

    create_proposal(posts_table, units_table, db_conn)
    vote, errors = insert_post(
        {
            'kind': 'vote',
            'user_id': 'A',
            'topic_id': 'B',
            'replies_to_id': 'D',
            'response': True,
        }, db_conn)
    assert len(errors) == 0
    vote['body'] = 'A'
    vote['user_id'] = 'B'
    vote, errors = insert_post(vote, db_conn)
    assert len(errors) == 0
def test_insert_post(db_conn):
  create_test_posts(db_conn)
  data = {
    'user_id': user_b_uuid,
    'topic_id': test_topic_b_id,
    'body': 'Isnt this fun?',
    'replies_to_id': uuid.uuid4(),
  }
  post, errors = insert_post(db_conn, data)
  assert errors
  assert not post
  data = {
    'user_id': user_b_uuid,
    'topic_id': test_topic_b_id,
    'body': 'Isnt this fun?',
  }
  post, errors = insert_post(db_conn, data)
  assert not errors
  assert post
Esempio n. 8
0
def test_kind(db_conn, posts_table):
    """
    Expect a post to have a kind.
    """

    post, errors = insert_post(
        {
            'user_id': 'A',
            'topic_id': 'B',
            # 'body': 'C',
        },
        db_conn)
    assert len(errors) == 1
    post, errors = insert_post(
        {
            'user_id': 'A',
            'topic_id': 'B',
            'body': 'C',
            'kind': 'post',
        }, db_conn)
    assert len(errors) == 0
Esempio n. 9
0
def test_replies(db_conn, posts_table):
    """
    Expect a post to allow a replies to id.
    """

    prev, errors = insert_post(
        {
            'id': 'D',
            'user_id': 'A',
            'topic_id': 'B',
            'body': 'C',
            'kind': 'post',
        }, db_conn)
    assert len(errors) == 0
    post, errors = insert_post(
        {
            'user_id': 'A',
            'topic_id': 'B',
            'body': 'C',
            'kind': 'post',
            'replies_to_id': prev['id'],
        }, db_conn)
    assert len(errors) == 0
Esempio n. 10
0
def create_topic_route(request):
    """
    Create a new topic.
    The first post (or proposal) must be provided.
    """

    db_conn = request['db_conn']

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    # ## STEP 1) Create post and topic (and entity) instances
    topic_data = request['params'].get('topic')
    post_data = request['params'].get('post')
    if not topic_data:
        return 400, {
            'errors': [{
                'name': 'topic',
                'message': 'Missing topic data.'
            }],
            'ref': 'zknSd46f2hRNjSjVHCg6YLwN'
        }
    if not post_data:
        return 400, {
            'errors': [{
                'name': 'post',
                'message': 'Missing post data.'
            }],
            'ref': 'Qki4oWX4nTdNAjYI8z5iNawr'
        }
    topic_data = omit(topic_data, ('id', 'created', 'modified'))
    topic_data['user_id'] = current_user['id']
    topic_data, topic_errors = validate_topic(topic_data, db_conn)

    post_data = omit(post_data, (
        'id',
        'created',
        'modified',
    ))
    post_data['user_id'] = current_user['id']
    post_data['topic_id'] = topic_data['id']
    post_kind = post_data['kind']
    if post_kind == 'proposal':
        entities = instance_entities(request['params'])
        post_data['entity_versions'] = []
        for entity in entities:
            entity_kind = get_kind(entity)
            post_data['entity_versions'].append({
                'id': entity['id'],
                'kind': entity_kind,
            })

    # ## STEP 2) Validate post and topic (and entity) instances
    errors = prefix_error_names('topic.', topic_errors)
    _, post_errors = validate_post(post_data, db_conn)
    errors = errors + prefix_error_names('post.', post_errors)
    if post_kind == 'proposal':
        for entity in entities:
            errors = (errors +
                      prefix_error_names('entity.', entity.validate(db_conn)))
    if len(errors):
        return 400, {'errors': errors, 'ref': 'TAY5pX3ghWBkSIVGTHzpQySa'}

    # ## STEP 3) Save post and topic (and entity)
    topic_data, topic_errors = insert_topic(topic_data, db_conn)
    post_data['topic_id'] = topic_data['id']
    post_, errors = insert_post(post_data, db_conn)
    if post_kind == 'proposal':
        for entity in entities:
            entity.save(db_conn)

    # ## STEP 4) Add author as a follower
    insert_follow(
        {
            'user_id': current_user['id'],
            'entity': {
                'id': topic_data['id'],
                'kind': 'topic',
            }
        }, db_conn)
    # TODO-2 also follow the entity automatically IF needed

    # ## STEP 5) Send out any needed notices
    send_notices(db_conn,
                 entity_id=topic_data['entity']['id'],
                 entity_kind=topic_data['entity']['kind'],
                 notice_kind='create_topic',
                 notice_data={
                     'user_name': current_user['name'],
                     'topic_name': topic_data['name'],
                     'entity_kind': topic_data['entity']['kind'],
                     'entity_name': topic_data['entity']['id'],
                 })

    if post_kind == 'proposal':
        send_notices(db_conn,
                     entity_id=topic_data['entity']['id'],
                     entity_kind=topic_data['entity']['kind'],
                     notice_kind='create_proposal',
                     notice_data={
                         'user_name': current_user['name'],
                         'topic_name': topic_data['name'],
                         'entity_kind': topic_data['entity']['kind'],
                         'entity_name': topic_data['entity']['id'],
                     })

    # ## STEP 5) Return response
    return 200, {
        'topic': deliver_topic(topic_data),
        'post': deliver_post(post_),
    }
Esempio n. 11
0
def create_post_route(request, topic_id):
    """
    Create a new post on a given topic.
    Proposal: must include entity (card, unit, or subject) information.
    Vote: must refer to a valid proposal.
    """

    db_conn = request['db_conn']

    current_user = get_current_user(request)
    if not current_user:
        return abort(401)

    topic = get_topic({'id': topic_id}, db_conn)
    if not topic:
        return 404, {
            'errors': [{
                'name': 'topic_id',
                'message': c('no_topic'),
            }],
            'ref': 'PCSFCxsJtnlP0x9WzbPoKcwM',
        }

    # ## STEP 1) Create post (and entity) instances
    post_data = request['params'].get('post')
    if not post_data:
        return 400, {
            'errors': [{
                'name': 'post',
                'message': 'Missing post data.',
            }],
            'ref': 'ykQpZwJKq54MTCxgkx0p6baW'
        }
    post_data = omit(post_data, (
        'id',
        'created',
        'modified',
    ))
    post_data['user_id'] = current_user['id']
    post_data['topic_id'] = topic_id
    post_kind = post_data['kind']
    if post_kind == 'proposal':
        entities = instance_entities(request['params'])
        post_data['entity_versions'] = []
        for entity in entities:
            entity_kind = get_kind(entity)
            post_data['entity_versions'].push({
                'id': entity['id'],
                'kind': entity_kind,
            })

    # ## STEP 2) Validate post (and entity) instances
    _, post_errors = validate_post(post_data, db_conn)
    errors = prefix_error_names('post.', post_errors)
    if post_kind == 'proposal':
        for entity in entities:
            errors = (errors +
                      prefix_error_names('entity.', entity.validate(db_conn)))

    if len(errors):
        return 400, {'errors': errors, 'ref': 'tux33ztgFj9ittSpS7WKIkq7'}

    # ## STEP 3) Save post (and entity)
    post_, post_errors = insert_post(post_data, db_conn)
    if post_kind == 'proposal':
        for entity in entities:
            entity.save(db_conn)

    # ## STEP 4) Add author as a follower
    insert_follow(
        {
            'user_id': current_user['id'],
            'entity': {
                'id': topic['id'],
                'kind': 'topic',
            }
        }, db_conn)
    # TODO-2 also follow the entity

    # ## STEP 5) Make updates based on proposal / vote status
    if post_kind == 'proposal':
        update_entity_statuses(db_conn, post_)
    if post_kind == 'vote':
        proposal = get_post({'id': post_data['replies_to_id']}, db_conn)
        update_entity_statuses(db_conn, proposal)

    # ## STEP 6) Send notices
    if post_kind == 'proposal':
        send_notices(db_conn,
                     entity_id=topic['entity']['id'],
                     entity_kind=topic['entity']['kind'],
                     notice_kind='create_proposal',
                     notice_data={
                         'user_name': current_user['name'],
                         'topic_name': topic['name'],
                         'entity_kind': topic['entity']['kind'],
                         'entity_name': topic['entity']['id'],
                     })

    # ## STEP 7) Return response
    return 200, {'post': deliver_post(post_data)}