Example #1
0
async def test_get_article(async_client: AsyncClient,
                           test_user: schemas.UserDB):
    article_in, _article_id = await create_test_article(test_user)

    slug = slugify(article_in.get("title"))
    r = await async_client.get(f"{API_ARTICLES}/{slug}")
    assert r.status_code == status.HTTP_200_OK
    article = schemas.ArticleInResponse(**r.json()).article
    assert_article_in_response(article_in, article, test_user)
Example #2
0
async def test_create_articles(async_client: AsyncClient,
                               test_user: schemas.UserDB, token: str):
    headers = {"Authorization": f"{JWT_TOKEN_PREFIX} {token}"}
    article_in = {
        "title":
        "How to train your dragon" + datetime.datetime.now().__str__(),
        "description": "Ever wonder how?",
        "body": "You have to believe",
        "tagList": ["reactjs", "angularjs", "dragons"],
    }
    r = await async_client.post(f"{API_ARTICLES}",
                                json={"article": article_in},
                                headers=headers)
    assert r.status_code == status.HTTP_200_OK
    article = schemas.ArticleInResponse(**r.json()).article
    assert_article_in_response(article_in, article, test_user)
Example #3
0
async def test_list_articles(
    async_client: AsyncClient,
    test_user: schemas.UserDB,
    token: str,
    other_user: schemas.UserDB,
    tag_filter: bool,
    author_filter: bool,
    favorited_filter: bool,
    authorization: bool,
):
    article_in, article_id = await create_test_article(other_user)
    await crud_profile.follow(other_user, test_user)
    await crud_article.favorite(article_id=article_id, user_id=test_user.id)

    params = {}
    if tag_filter:
        tags = await crud_article.get_article_tags(article_id)
        if len(tags):
            tag = tags[0]
            params["tag"] = tag
    if author_filter:
        params["author"] = test_user.username
    if favorited_filter:
        params["favorited"] = other_user.username
    headers = {
        "Authorization": f"{JWT_TOKEN_PREFIX} {token}"
    } if authorization else {}
    r = await async_client.get(f"{API_ARTICLES}",
                               params=params,
                               headers=headers)
    assert r.status_code == status.HTTP_200_OK
    assert "articlesCount" in r.json()
    assert "articles" in r.json()
    assert r.json().get("articlesCount") == len(r.json().get("articles"))
    if len(r.json().get("articles")) > 0:
        article = schemas.ArticleForResponse(**r.json().get("articles")[0])
        assert_article_in_response(
            expected=article_in,
            actual=article,
            author=other_user,
            favorites_count=1,
            favorited=authorization,
            following=authorization,
        )
Example #4
0
async def test_unfavorite_article(
    async_client: AsyncClient,
    test_user: schemas.UserDB,
    token: str,
    other_user: schemas.UserDB,
):
    article_in, article_id = await create_test_article(other_user)
    await crud_article.favorite(article_id=article_id, user_id=test_user.id)

    headers = {"Authorization": f"{JWT_TOKEN_PREFIX} {token}"}
    slug = slugify(article_in.get("title"))
    r = await async_client.delete(f"{API_ARTICLES}/{slug}/favorite",
                                  headers=headers)
    assert r.status_code == status.HTTP_200_OK
    article = schemas.ArticleInResponse(**r.json()).article
    assert_article_in_response(expected=article_in,
                               actual=article,
                               author=other_user,
                               favorited=False)
Example #5
0
async def test_feed_articles(
    async_client: AsyncClient,
    test_user: schemas.UserDB,
    token: str,
    other_user: schemas.UserDB,
):
    headers = {"Authorization": f"{JWT_TOKEN_PREFIX} {token}"}
    article_in, _article_id = await create_test_article(other_user)
    await crud_profile.follow(other_user, test_user)

    r = await async_client.get(f"{API_ARTICLES}/feed", headers=headers)
    assert r.status_code == status.HTTP_200_OK
    assert "articlesCount" in r.json()
    assert "articles" in r.json()
    assert r.json().get("articlesCount") == len(r.json().get("articles"))
    if len(r.json().get("articles")) > 0:
        article = schemas.ArticleForResponse(**r.json().get("articles")[0])
        assert_article_in_response(expected=article_in,
                                   actual=article,
                                   author=other_user,
                                   following=True)