Beispiel #1
0
def test_update_vote(db_conn, session):
    """
  Expect update vote to handle proposals correctly.
  """

    vote_a_uuid = uuid.uuid4()
    create_users_in_db(db_conn)
    create_unit_in_db(db_conn)
    create_topic_in_db(db_conn)
    create_proposal_in_db(db_conn)
    raw_insert_posts(db_conn, [{
        'id': vote_a_uuid,
        'user_id': user_id,
        'topic_id': topic_a_uuid,
        'body': 'Boo!',
        'response': False,
        'kind': 'vote',
        'replies_to_id': proposal_a_uuid,
    }])

    request = {
        'cookies': {
            'session_id': session
        },
        'params': {
            'body': 'Yay!',
            'response': True,
        },
        'db_conn': db_conn
    }
    code, response = routes.post.update_post_route(request, topic_a_uuid,
                                                   vote_a_uuid)
    assert code == 200
    assert True is response['post']['response']
Beispiel #2
0
def test_get_posts(db_conn):
    """
  Expect to get posts for given topic.
  """

    create_user_in_db(db_conn)
    create_topic_in_db(db_conn)
    raw_insert_posts(db_conn, [{
        'user_id': user_id,
        'topic_id': topic_a_uuid,
        'body': '''A Modest Proposal for Preventing the Children of Poor
    People From Being a Burthen to Their Parents or Country, and
    for Making Them Beneficial to the Publick.''',
        'kind': 'post',
    }, {
        'user_id': user_id,
        'topic_id': topic_a_uuid,
        'body': 'A follow up.',
        'kind': 'post',
    }])

    request = {'params': {}, 'db_conn': db_conn}
    code, response = routes.post.get_posts_route(request, topic_a_uuid)
    assert code == 200
    assert ('Beneficial to the Publick' in response['posts'][0]['body']
            or 'Beneficial to the Publick' in response['posts'][1]['body'])
Beispiel #3
0
def create_post_in_db(db_conn, user_id=user_id):
    raw_insert_posts(db_conn, [{
        'id': post_a_uuid,
        'user_id': user_id,
        'topic_id': topic_a_uuid,
        'body': '''A Modest Proposal for Preventing the Children of Poor
    People From Being a Burthen to Their Parents or Country, and
    for Making Them Beneficial to the Publick.''',
        'kind': 'post',
    }])
def test_is_valid_reply_kind(db_conn):
  create_test_posts(db_conn)
  # 1  Doesn't reply to anything
  data = {'replies_to_id': uuid.uuid4()}
  errors = is_valid_reply_kind(db_conn, data)
  assert errors[0]['message'] == 'No proposal found.'
  # 2  Doesn't reply to a proposal
  data = {'replies_to_id': post_uuid}
  errors = is_valid_reply_kind(db_conn, data)
  assert errors[0]['message'] == 'A vote must reply to a proposal.'
  # 3  Trying to vote on own proposal
  data = {
    'replies_to_id': proposal_uuid,
    'user_id': user_uuid,
  }
  errors = is_valid_reply_kind(db_conn, data)
  assert errors[0]['message'] == 'You cannot vote on your own proposal.'
  # 4  Voting on a proposal with no entity version
  bad_proposal_uuid = uuid.uuid4()
  posts = [{
    'id': bad_proposal_uuid,
    'kind': 'proposal',
    'body': 'A new version',
    'entity_versions': [{
      'id': convert_uuid_to_slug(uuid.uuid4()),
      'kind': 'unit',
    }],
    'user_id': user_uuid,
    'topic_id': test_topic_id,
  }]
  raw_insert_posts(db_conn, posts)
  data = {
    'replies_to_id': bad_proposal_uuid,
    'user_id': user_b_uuid,
  }
  errors = is_valid_reply_kind(db_conn, data)
  assert errors[0]['message'] == 'No entity version for proposal.'
  # 5  Voting on a an already complete proposal
  data = {
    'replies_to_id': proposal_uuid,
    'user_id': user_b_uuid,
  }
  errors = is_valid_reply_kind(db_conn, data)
  assert errors[0]['message'] == 'Proposal is already complete.'
  # 6  Voting a on a valid proposal
  update_unit(db_conn, test_unit_version_uuid, status='pending')
  data = {
    'replies_to_id': proposal_uuid,
    'user_id': user_b_uuid,
  }
  errors = is_valid_reply_kind(db_conn, data)
  assert not errors
def test_update_entity_statuses(db_conn):
    create_test_user(db_conn)
    unit_version_a_uuid = uuid.uuid4()
    topic_uuid = uuid.uuid4()
    proposal_uuid = uuid.uuid4()
    units = [{
        'version_id': unit_version_a_uuid,
        'user_id': user_a_uuid,
        'entity_id': unit_a_uuid,
        'name': 'test unit add',
        'body': 'adding numbers is fun',
        'status': 'pending',
    }]
    raw_insert_units(db_conn, units)
    topics = [{
        'id': topic_uuid,
        'user_id': user_a_uuid,
        'entity_id': unit_a_uuid,
        'entity_kind': 'unit',
        'name': 'Lets talk about adding numbers',
    }]
    raw_insert_topics(db_conn, topics)
    posts = [{
        'id':
        proposal_uuid,
        'kind':
        'proposal',
        'body':
        'A new version',
        'entity_versions': [{
            'id': convert_uuid_to_slug(unit_version_a_uuid),
            'kind': 'unit',
        }],
        'user_id':
        user_a_uuid,
        'topic_id':
        topic_uuid,
    }]
    raw_insert_posts(db_conn, posts)
    proposal = get_post(db_conn, {'id': proposal_uuid})
    update_entity_statuses(db_conn, proposal)
    unit = get_unit_version(db_conn, unit_version_a_uuid)
    assert unit
    assert unit['status'] == 'accepted'
Beispiel #6
0
def create_proposal_in_db(db_conn, user_id=user_id):
    raw_insert_posts(db_conn, [{
        'id':
        proposal_a_uuid,
        'user_id':
        user_id,
        'topic_id':
        topic_a_uuid,
        'body':
        '''A Modest Proposal for Preventing the Children of Poor
    People From Being a Burthen to Their Parents or Country, and
    for Making Them Beneficial to the Publick.''',
        'kind':
        'proposal',
        'name':
        'New Unit',
        'replies_to_id':
        None,
        'entity_versions': [{
            'id': convert_uuid_to_slug(unit_version_a_uuid),
            'kind': 'unit',
        }],
    }])
Beispiel #7
0
def test_get_posts_votes(db_conn):
    """
  Expect get posts for topic to render votes correctly.
  """

    create_user_in_db(db_conn)
    create_topic_in_db(db_conn)
    create_proposal_in_db(db_conn)
    raw_insert_posts(db_conn, [{
        'kind': 'vote',
        'body': 'Hooray!',
        'replies_to_id': proposal_a_uuid,
        'topic_id': topic_a_uuid,
        'response': True,
        'user_id': user_id,
    }])

    request = {'params': {}, 'db_conn': db_conn}
    code, response = routes.post.get_posts_route(request, topic_a_uuid)

    assert code == 200
    assert response['posts'][0]['kind'] in ('proposal', 'vote')
    assert response['posts'][1]['kind'] in ('proposal', 'vote')
Beispiel #8
0
def test_get_posts_paginate(db_conn):
    """
  Expect get posts for topic to paginate.
  """
    create_user_in_db(db_conn)
    create_topic_in_db(db_conn)
    raw_insert_posts(db_conn, [{
        'user_id': user_id,
        'topic_id': topic_a_uuid,
        'body': 'test %s' % i,
        'kind': 'post',
    } for i in range(0, 25)])
    request = {'params': {}, 'db_conn': db_conn}
    code, response = routes.post.get_posts_route(request, topic_a_uuid)
    assert code == 200
    assert len(response['posts']) == 10
    request = {
        'params': {
            'offset': 20,
        },
        'db_conn': db_conn
    }
    code, response = routes.post.get_posts_route(request, topic_a_uuid)
    assert len(response['posts']) == 5
def create_test_posts(db_conn):
  users = [{
    'id': user_uuid,
    'name': 'test',
    'email': '*****@*****.**',
    'password': '******',
  }, {
    'id': user_b_uuid,
    'name': 'other',
    'email': '*****@*****.**',
    'password': '******',
  }]
  raw_insert_users(db_conn, users)
  units = [{
    'version_id': test_unit_version_uuid,
    'user_id': user_uuid,
    'entity_id': test_unit_uuid,
    'name': 'test unit add',
    'body': 'adding numbers is fun',
  }, {
    'version_id': test_unit_b_version_uuid,
    'user_id': user_uuid,
    'entity_id': test_unit_b_uuid,
    'name': 'test unit subtract',
    'body': 'subtracting numbers is fun',
  }]
  raw_insert_units(db_conn, units)
  topics = [{
    'id': test_topic_id,
    'user_id': user_uuid,
    'entity_id': test_unit_uuid,
    'entity_kind': 'unit',
    'name': 'Lets talk about adding numbers',
  }, {
    'id': test_topic_b_id,
    'user_id': user_uuid,
    'entity_id': test_unit_b_uuid,
    'entity_kind': 'unit',
    'name': 'Lets talk about subtracting numbers',
  }]
  raw_insert_topics(db_conn, topics)
  posts = [{
    'id': proposal_uuid,
    'kind': 'proposal',
    'body': 'A new version',
    'entity_versions': [{
      'id': convert_uuid_to_slug(test_unit_version_uuid),
      'kind': 'unit',
    }],
    'user_id': user_uuid,
    'topic_id': test_topic_id,
  }, {
    'id': post_uuid,
    'kind': 'post',
    'body': 'I just love adding numbers.',
    'user_id': user_uuid,
    'topic_id': test_topic_id,
  }, {
    'id': vote_uuid,
    'kind': 'vote',
    'body': 'Yay!',
    'replies_to_id': proposal_uuid,
    'response': True,
    'user_id': user_b_uuid,
    'topic_id': test_topic_id,
  }]
  raw_insert_posts(db_conn, posts)