def test_json_renderer(db: Session, democontent: None) -> None:
    """Test that Article is correctly rendered for an OpenAPI JSON response."""
    user = User.by_username("two", db=db)
    article = Article.by_slug("foo", db=db)

    request = DummyRequest()
    request.user = user

    renderer = json_renderer()
    output = renderer(None)(article, {"request": request})

    assert json.loads(output) == {
        "author": {
            "bio": None,
            "following": True,
            "image": None,
            "username": "******"
        },
        "body": "Foö body",
        "createdAt": "2019-01-01T01:01:01.000Z",
        "description": "Foö desc",
        "favorited": False,
        "favoritesCount": 0,
        "slug": "foo",
        "tagList": ["dogs", "cats"],
        "title": "Foö",
        "updatedAt": "2019-02-02T02:02:02.000Z",
    }
Пример #2
0
def test_follow(db: Session, democontent: None) -> None:
    """Test following a user."""
    one = User.by_username("one", db)
    two = User.by_username("two", db)

    assert two not in one.follows  # type: ignore

    one.follow(two)  # type: ignore
    assert two in one.follows  # type: ignore

    one.follow(two)  # type: ignore # again, to test idempotence
    assert two in one.follows  # type: ignore

    one.unfollow(two)  # type: ignore
    assert two not in one.follows  # type: ignore

    one.unfollow(two)  # type: ignore # again, to test idempotence
    assert two not in one.follows  # type: ignore
def test_follow_unfollow_profile(testapp: TestApp, db: Session,
                                 democontent: None) -> None:
    """Test POST/DELETE /api/profiles/{username}/follow."""
    one = User.by_username("one", db=db)
    two = User.by_username("two", db=db)
    assert one.follows == []  # type: ignore

    res = testapp.post_json(
        "/api/profiles/two/follow",
        headers={"Authorization": f"Token {USER_ONE_JWT}"},
        status=200,
    )
    assert res.json == {
        "profile": {
            "username": "******",
            "bio": None,
            "image": None,
            "following": True
        }
    }
    one = User.by_username("one", db=db)  # refetch db values
    assert [u.username
            for u in one.follows] == [  # type: ignore  # pragma: no branch
                u.username for u in [two]  # type: ignore
            ]

    res = testapp.delete(
        "/api/profiles/two/follow",
        headers={"Authorization": f"Token {USER_ONE_JWT}"},
        status=200,
    )
    assert res.json == {
        "profile": {
            "username": "******",
            "bio": None,
            "image": None,
            "following": False
        }
    }
    one = User.by_username("one", db=db)  # refetch db values
    assert one.follows == []  # type: ignore
def test_json_renderer(db: Session, democontent: None) -> None:
    """Test that Profile is correctly rendered for an OpenAPI JSON response."""
    user = User.by_username("one", db=db)
    request = DummyRequest()
    request.user = user

    profile = Profile(user=user)  # type: ignore

    renderer = json_renderer()
    output = renderer(None)(profile, {"request": request})

    assert json.loads(output) == {
        "username": "******",
        "following": False,
        "bio": None,
        "image": None,
    }
Пример #5
0
def test_favorite(db: Session, democontent: None) -> None:
    """Test favoriting an article."""
    user = User.by_username("one", db)
    article = Article.by_slug("foo", db)

    assert article not in user.favorites  # type: ignore

    user.favorite(article)  # type: ignore
    assert article in user.favorites  # type: ignore

    user.favorite(article)  # type: ignore # again, to test idempotence
    assert article in user.favorites  # type: ignore

    user.unfavorite(article)  # type: ignore
    assert article not in user.favorites  # type: ignore

    user.unfavorite(article)  # type: ignore # again, to test idempotence
    assert article not in user.favorites  # type: ignore
Пример #6
0
def test_json_renderer(db: Session, democontent: None) -> None:
    """Test that Comment is correctly rendered for an OpenAPI JSON response."""
    user = User.by_username("two", db=db)
    article = Article.by_slug("foo", db=db)
    comment = article.comments[0]  # type: ignore

    request = DummyRequest()
    request.user = user

    renderer = json_renderer()
    output = renderer(None)(comment, {"request": request})

    assert json.loads(output) == {
        "id": 99,
        "body": "I like this!",
        "createdAt": "2019-07-07T07:07:07.000Z",
        "updatedAt": "2019-08-08T08:08:08.000Z",
        "author": {"username": "******", "bio": None, "image": None, "following": False},
    }
Пример #7
0
def articles(request: Request) -> MultipleArticlesResponse:
    """Get recent articles globally."""

    q = request.db.query(Article)

    if request.openapi_validated.parameters["query"].get("author"):
        author = User.by_username(
            request.openapi_validated.parameters["query"]["author"],
            db=request.db)
        q = q.filter(Article.author == author)

    if request.openapi_validated.parameters["query"].get("tag"):
        q = q.filter(
            Article.tags.any(Tag.name == request.openapi_validated.
                             parameters["query"]["tag"]))

    q = q.order_by(desc("created"))
    q = paginate(q, request)

    articles = q.all()
    count = q.count()
    return {"articles": articles, "articlesCount": count}
Пример #8
0
def test_verify_password(db: Session, democontent: None) -> None:
    """Test verifying user's password."""
    user = User.by_username("one", db)
    assert user.verify_password("secret")  # type: ignore
    assert not user.verify_password("invalid")  # type: ignore
Пример #9
0
def test_by_shortcuts(db: Session, democontent: None) -> None:
    """Test that by_* shortcuts work."""
    assert User.by_username("one", db) == User.by_email("*****@*****.**", db)
    assert User.by_username("one", db) == User.by_id(USER_ONE_ID, db)
def test_by_shortcuts(db: Session, democontent: None) -> None:
    """Test that by_* shortcuts work."""
    assert Profile.by_username("one",
                               db).user == User.by_username(  # type: ignore
                                   "one", db)
    assert Profile.by_username("foo", db) is None