def test_create_user():
    create_user('test_user', 'test_password')

    session = Session()

    test_user = session.query(User).filter(User.username == 'test_user').first()

    assert test_user
def test_verify_password():
    create_user('test_user', 'test_password')
    # correct pw should be true
    res = verify_user_password('test_user', 'test_password')
    assert res
    # incorrect pw should be false
    res = verify_user_password('test_user', 'wrong password')
    assert res == False
    # nonexistent user should raise exception
    with pytest.raises(UserNotFoundException):
        verify_user_password('fake_user', 'some password')
def test_get_snacks_with_votes():
    snacks = get_snacks()
    assert isinstance(snacks, dict)
    total_snacks = len(snacks["permanent"]) + len(
        snacks["suggestedCurrent"]) + len(snacks["suggestedExpired"])

    user = create_user('me', 'me')

    snack_id = None
    if len(snacks["suggestedCurrent"]) > 0:
        snack_id = snacks["suggestedCurrent"]["id"]
    else:
        new_snack = add_snack("fruit by the foot", "super america")
        snack_id = new_snack.id

    add_vote(user.id, snack_id)
    new_snacks = get_snacks()
    new_total_snacks = len(snacks["permanent"]) + len(
        snacks["suggestedCurrent"]) + len(snacks["suggestedExpired"])
    # make sure length did not change
    assert new_total_snacks == total_snacks
    # make sure our snack id has a vote count of one
    voted_snack = [
        x for x in new_snacks["suggestedCurrent"] if x["id"] == snack_id
    ][0]
    assert voted_snack["votes"] == 1
def test_get_user_votes():
    # when no user should raise exception
    with pytest.raises(UserNotFoundException):
        get_user_votes(1)

    user = create_user('test_user', 'test_password')

    vote_count = get_user_votes(user.id)
    # should be 0 since no votes added
    assert vote_count == 0
def test_user_suggestion():
    user = create_user('test_user', 'test_password')

    can_suggest = check_user_suggestion(user.id)

    assert can_suggest

    set_user_suggestion(user.id)

    cannot_suggest = check_user_suggestion(user.id)

    assert cannot_suggest == False
Ejemplo n.º 6
0
def test_voting():
    my_user: User = create_user('test_user', 'test_password')
    vote_count: int = get_user_votes(my_user.id)
    assert vote_count == 0

    for i in range(max_votes):
        # add votes
        add_vote(my_user.id, 123)
        vote_count: int = get_user_votes(my_user.id)
        assert vote_count == i + 1

    with pytest.raises(VotesExceededException):
        add_vote(my_user.id, 123)
Ejemplo n.º 7
0
    def on_post(self, req, resp):
        """
        Creates user
        """
        user = req.media

        if not user or (not user.get("username") or not user.get("password")):
            resp.status = falcon.HTTP_400
            resp.body = json.dumps({
                "status": "error",
                "error": "must provide 'username' and 'password' in json body"
            })
            return

        try:
            create_user(user["username"], user["password"])
            resp.body = json.dumps({"status": "ok"})

        except UserAlreadyExistsException:
            resp.status = falcon.HTTP_400
            resp.body = json.dumps({
                "status": "error",
                "error": "user {} already exists".format(user["username"])
            })
Ejemplo n.º 8
0
def test_vote_expire():
    my_user: User = create_user('test_user', 'test_password')
    vote_count: int = get_user_votes(my_user.id)
    assert vote_count == 0

    # add votes that expired before now
    session = Session()
    vote = Vote(
        user_id = my_user.id,
        snack_id = 234,
        vote_expiry = datetime.datetime.now() - datetime.timedelta(days=1)
    )
    # assert that the vote count is still 0 with expired votes
    vote_count: int = get_user_votes(my_user.id)
    assert vote_count == 0
def test_create_user_already_exists():
    with pytest.raises(UserAlreadyExistsException):
        create_user('test_user', 'test_password')
        create_user('test_user', 'test_password')