コード例 #1
0
def test_get_user_from_token(session):
    user = User()
    user.get_token()
    session.add(user)
    session.commit()

    assert get_user_from_token(session, user.token) == user
コード例 #2
0
def test_get_user_from_wrong_token(session):
    user = User()
    user.get_token()
    session.add(user)
    session.commit()

    assert get_user_from_token(session, "foo") is None
コード例 #3
0
def test_get_expired_token():
    now = datetime.datetime.utcnow()
    user = User()
    t1 = user.get_token()
    user.token_expires = now - datetime.timedelta(seconds=1)
    t2 = user.get_token()
    assert t1 != t2
    assert t2 == user.token
コード例 #4
0
def test_get_user_from_expired_token(session):
    now = datetime.datetime.utcnow()

    user = User()
    user.get_token()
    user.token_expires = now - datetime.timedelta(seconds=1)
    session.add(user)
    session.commit()

    assert get_user_from_token(session, user.token) is None
コード例 #5
0
def test_get_items_for_user(session):
    u1 = User(id=1)
    u2 = User(id=2)
    session.add_all(
        [
            u1,
            u2,
            Item(id=1, user=u1),
            Item(id=2, user=u2),
        ]
    )
    items = get_items_for_user(session, u1).all()
    assert len(items) == 1 and items[0].id == 1
コード例 #6
0
def test_get_authenticated_user_long_password(session, password_hash):
    user = User(username="******", password_hash=password_hash)
    session.add(user)
    session.commit()

    authenticated_user = get_authenticated_user(session,
                                                username="******",
                                                password="******" * 65)

    assert authenticated_user is None
コード例 #7
0
def test_get_authenticated_user(session, password_hash):
    user = User(username="******", password_hash=password_hash)
    session.add(user)
    session.commit()

    authenticated_user = get_authenticated_user(session,
                                                username="******",
                                                password="******")

    assert authenticated_user == user
コード例 #8
0
def test_login_with_long_password(flask_anonymous_client, session,
                                  password_hash):
    user = User(username="******", password_hash=password_hash)
    session.add(user)
    session.commit()

    response = flask_anonymous_client.post(url_for("login"),
                                           data={
                                               "username": "******",
                                               "password": "******" * 65
                                           })

    assert response.status_code == 200
コード例 #9
0
def test_register_existing_username(flask_anonymous_client, session):
    session.add(User(username="******"))
    session.commit()

    response = flask_anonymous_client.post(
        url_for("register"),
        data={
            "username": "******",
            "password": "******",
            "repeat_password": "******",
        },
    )
    assert response.status_code == 200
    assert b"Dieser Name existiert bereits." in response.data
コード例 #10
0
def test_include_inactive_items(session):
    user = User(id=1)
    session.add_all(
        [
            user,
            Item(id=1, user=user),
            Item(id=2, user=user, next_iteration=datetime.datetime.utcnow()),
            Item(
                id=3,
                user=user,
                next_iteration=datetime.datetime.utcnow(),
                is_active=False,
            ),
        ]
    )
    items = get_items_for_user(session, user, include_inactive=True).all()
    assert len(items) == 3
コード例 #11
0
def test_get_items_sorted_by_due_date(session):
    user = User(id=1)
    session.add_all(
        [
            user,
            Item(
                id=1,
                user=user,
                next_iteration=datetime.datetime.utcnow() + datetime.timedelta(days=10),
            ),
            Item(
                id=2,
                user=user,
                next_iteration=datetime.datetime.utcnow() + datetime.timedelta(days=1),
            ),
        ]
    )
    items = get_items_for_user(session, user).all()
    assert [x.id for x in items] == [2, 1]
コード例 #12
0
def test_get_todo_items(session):
    user = User(id=1)
    session.add_all(
        [
            user,
            Item(id=1, user=user, next_iteration=None),
            Item(
                id=2,
                user=user,
                next_iteration=datetime.datetime.utcnow()
                - datetime.timedelta(seconds=1),
            ),
            Item(
                id=3,
                user=user,
                next_iteration=datetime.datetime.utcnow() + datetime.timedelta(days=1),
            ),
        ]
    )
    items = get_items_for_user(session, user, todo=True).all()
    assert [x.id for x in items] == [1, 2]
コード例 #13
0
def test_train_view_cycles_through_pending_items(flask_client, session):
    url = url_for("train")
    now = datetime.datetime.utcnow()

    session.add_all([
        Item(
            user_id=1,
            word="barbazfoo",
            next_iteration=now - datetime.timedelta(days=2),
        ),
        Item(
            user_id=1,
            word="bazfoobar",
            next_iteration=now - datetime.timedelta(days=1),
        ),
        User(id=2),
        Item(
            user_id=2,  # this item should be skipped!
            word="foobarbaz",
            next_iteration=now - datetime.timedelta(days=3),
        ),
    ])
    session.commit()

    response = flask_client.get(url)
    assert b"barbazfoo" in response.data
    assert b"foobarbaz" not in response.data

    response = flask_client.post(url, data={"grade": "5"})
    assert response.status_code == 302

    response = flask_client.get(url)
    assert b"bazfoobar" in response.data
    assert b"foobarbaz" not in response.data

    response = flask_client.post(url, data={"grade": "5"})
    assert response.status_code == 302

    response = flask_client.get(url)
    assert b"foobarbaz" not in response.data
コード例 #14
0
def test_get_token():
    user = User()
    user.get_token()
    assert user.token is not None
    assert user.token_expires is not None