async def test_user_can_update_article(
    app: FastAPI,
    authorized_client: Client,
    test_article: Article,
    update_field: str,
    update_value: str,
    extra_updates: dict,
) -> None:
    response = await authorized_client.put(
        app.url_path_for("articles:update-article", slug=test_article.slug),
        json={"article": {
            update_field: update_value
        }},
    )

    assert response.status_code == status.HTTP_200_OK

    article = ArticleInResponse(**response.json()).article
    article_as_dict = article.dict()
    assert article_as_dict[update_field] == update_value

    for extra_field, extra_value in extra_updates.items():
        assert article_as_dict[extra_field] == extra_value

    exclude_fields = {update_field, *extra_updates.keys(), "updated_at"}
    assert article.dict(exclude=exclude_fields) == test_article.dict(
        exclude=exclude_fields)
async def test_user_can_change_favorite_state(
    app: FastAPI,
    authorized_client: Client,
    test_article: Article,
    test_user: UserInDB,
    pool: Pool,
    api_method: str,
    route_name: str,
    favorite_state: bool,
) -> None:
    if not favorite_state:
        async with pool.acquire() as connection:
            articles_repo = ArticlesRepository(connection)
            await articles_repo.add_article_into_favorites(
                article=test_article, user=test_user)

    await authorized_client.request(
        api_method, app.url_path_for(route_name, slug=test_article.slug))

    response = await authorized_client.get(
        app.url_path_for("articles:get-article", slug=test_article.slug))

    article = ArticleInResponse(**response.json())

    assert article.article.favorited == favorite_state
    assert article.article.favorites_count == int(favorite_state)
async def test_user_can_retrieve_article_if_exists(
        app: FastAPI, authorized_client: Client,
        test_article: Article) -> None:
    response = await authorized_client.get(
        app.url_path_for("articles:get-article", slug=test_article.slug))
    article = ArticleInResponse(**response.json())
    assert article.article == test_article
async def test_article_will_contain_only_attached_tags(
        app: FastAPI, authorized_client: Client, test_user: UserInDB,
        pool: Pool) -> None:
    attached_tags = ["tag1", "tag3"]

    async with pool.acquire() as connection:
        articles_repo = ArticlesRepository(connection)

        await articles_repo.create_article(
            slug=f"test-slug",
            title="tmp",
            description="tmp",
            body="tmp",
            author=test_user,
            tags=attached_tags,
        )

        for i in range(5):
            await articles_repo.create_article(
                slug=f"slug-{i}",
                title="tmp",
                description="tmp",
                body="tmp",
                author=test_user,
                tags=[f"tag-{i}"],
            )

    response = await authorized_client.get(
        app.url_path_for("articles:get-article", slug="test-slug"))
    article = ArticleInResponse(**response.json())
    assert len(article.article.tags) == len(attached_tags)
    assert set(article.article.tags) == set(attached_tags)
async def update_article_by_slug(
    article_update: ArticleInUpdate = Body(..., embed=True, alias="article"),
    current_article: Article = Depends(get_article_by_slug_from_path),
    articles_repo: ArticlesRepository = Depends(get_repository(ArticlesRepository)),
) -> ArticleInResponse:
    slug = get_slug_for_article(article_update.title) if article_update.title else None
    article = await articles_repo.update_article(
        article=current_article, slug=slug, **article_update.dict()
    )
    return ArticleInResponse(article=article)
async def test_not_existing_tags_will_be_created_without_duplication(
        app: FastAPI, authorized_client: Client, test_user: UserInDB) -> None:
    article_data = {
        "title": "Test Slug",
        "body": "does not matter",
        "description": "¯\\_(ツ)_/¯",
        "tagList": ["tag1", "tag2", "tag3", "tag3"],
    }
    response = await authorized_client.post(
        app.url_path_for("articles:create-article"),
        json={"article": article_data})
    article = ArticleInResponse(**response.json())
    assert set(article.article.tags) == {"tag1", "tag2", "tag3"}
async def test_user_can_create_article(app: FastAPI, authorized_client: Client,
                                       test_user: UserInDB) -> None:
    article_data = {
        "title": "Test Slug",
        "body": "does not matter",
        "description": "¯\\_(ツ)_/¯",
    }
    response = await authorized_client.post(
        app.url_path_for("articles:create-article"),
        json={"article": article_data})
    article = ArticleInResponse(**response.json())
    assert article.article.title == article_data["title"]
    assert article.article.author.username == test_user.username
示例#8
0
async def remove_article_from_favorites(
    article: Article = Depends(get_article_by_slug_from_path),
    user: User = Depends(get_current_user_authorizer()),
    articles_repo: ArticlesRepository = Depends(
        get_repository(ArticlesRepository)),
) -> ArticleInResponse:
    if article.favorited:
        await articles_repo.remove_article_from_favorites(article=article,
                                                          user=user)

        return ArticleInResponse(article=article.copy(
            update={
                "favorited": False,
                "favorites_count": article.favorites_count - 1,
            }))

    raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST,
                        detail=strings.ARTICLE_IS_NOT_FAVORITED)
示例#9
0
async def mark_article_as_favorite(
    article: Article = Depends(get_article_by_slug_from_path),
    user: User = Depends(get_current_user_authorizer()),
    articles_repo: ArticlesRepository = Depends(
        get_repository(ArticlesRepository)),
) -> ArticleInResponse:
    if not article.favorited:
        await articles_repo.add_article_into_favorites(article=article,
                                                       user=user)

        return ArticleInResponse(article=article.copy(
            update={
                "favorited": True,
                "favorites_count": article.favorites_count + 1,
            }))

    raise HTTPException(
        status_code=status.HTTP_400_BAD_REQUEST,
        detail=strings.ARTICLE_IS_ALREADY_FAVORITED,
    )
async def create_new_article(
    article_create: ArticleInCreate = Body(..., embed=True, alias="article"),
    user: User = Depends(get_current_user_authorizer()),
    articles_repo: ArticlesRepository = Depends(get_repository(ArticlesRepository)),
) -> ArticleInResponse:
    slug = get_slug_for_article(article_create.title)
    if await check_article_exists(articles_repo, slug):
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=strings.ARTICLE_ALREADY_EXISTS,
        )

    article = await articles_repo.create_article(
        slug=slug,
        title=article_create.title,
        description=article_create.description,
        body=article_create.body,
        author=user,
        tags=article_create.tags,
    )
    return ArticleInResponse(article=article)
async def retrieve_article_by_slug(
    article: Article = Depends(get_article_by_slug_from_path),
) -> ArticleInResponse:
    return ArticleInResponse(article=ArticleForResponse.from_orm(article))
async def retrieve_article_by_slug(
    article: Article = Depends(get_article_by_slug_from_path)
) -> ArticleInResponse:
    return ArticleInResponse(article=article)