async def test_filtering_with_limit_and_offset(app: FastAPI,
                                               authorized_client: Client,
                                               test_user: UserInDB,
                                               pool: Pool) -> None:
    async with pool.acquire() as connection:
        articles_repo = ArticlesRepository(connection)

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

    full_response = await authorized_client.get(
        app.url_path_for("articles:list-articles"))
    full_articles = ListOfArticlesInResponse(**full_response.json())

    response = await authorized_client.get(
        app.url_path_for("articles:list-articles"),
        params={
            "limit": 2,
            "offset": 3
        })

    articles_from_response = ListOfArticlesInResponse(**response.json())
    assert full_articles.articles[3:] == articles_from_response.articles
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 test_empty_feed_if_user_has_not_followings(
    app: FastAPI,
    authorized_client: Client,
    test_article: Article,
    test_user: UserInDB,
    pool: Pool,
) -> None:
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        articles_repo = ArticlesRepository(connection)

        for i in range(5):
            user = await users_repo.create_user(username=f"user-{i}",
                                                email=f"user-{i}@email.com",
                                                password="******")
            for j in range(5):
                await articles_repo.create_article(
                    slug=f"slug-{i}-{j}",
                    title="tmp",
                    description="tmp",
                    body="tmp",
                    author=user,
                    tags=[f"tag-{i}-{j}"],
                )

    response = await authorized_client.get(
        app.url_path_for("articles:get-user-feed-articles"))

    articles = ListOfArticlesInResponse(**response.json())
    assert articles.articles == []
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_not_modify_article_that_is_not_authored_by_him(
    app: FastAPI,
    authorized_client: Client,
    pool: Pool,
    api_method: str,
    route_name: str,
) -> None:
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        user = await users_repo.create_user(username="******",
                                            email="*****@*****.**",
                                            password="******")
        articles_repo = ArticlesRepository(connection)
        await articles_repo.create_article(
            slug="test-slug",
            title="Test Slug",
            description="Slug for tests",
            body="Test " * 100,
            author=user,
            tags=["tests", "testing", "pytest"],
        )

    response = await authorized_client.request(
        api_method,
        app.url_path_for(route_name, slug="test-slug"),
        json={"article": {
            "title": "Updated Title"
        }},
    )
    assert response.status_code == status.HTTP_403_FORBIDDEN
Example #6
0
async def test_article(test_user: UserInDB, pool: Pool) -> Article:
    async with pool.acquire() as connection:
        articles_repo = ArticlesRepository(connection)
        return await articles_repo.create_article(
            slug="test-slug",
            title="Test Slug",
            description="Slug for tests",
            body="Test " * 100,
            author=test_user,
            tags=["tests", "testing", "pytest"],
        )
Example #7
0
async def test_user_can_delete_his_article(app: FastAPI,
                                           authorized_client: AsyncClient,
                                           test_article: Article,
                                           db: Database) -> None:
    await authorized_client.delete(
        app.url_path_for("articles:delete-article", slug=test_article.slug))

    async with db.pool.acquire() as connection:
        articles_repo = ArticlesRepository(connection)
        with pytest.raises(EntityDoesNotExist):
            await articles_repo.get_article_by_slug(slug=test_article.slug)
async def test_filtering_by_favorited(
    app: FastAPI,
    authorized_client: Client,
    test_user: UserInDB,
    pool: Pool,
    favorited: str,
    result: int,
) -> None:
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        articles_repo = ArticlesRepository(connection)

        fan1 = await users_repo.create_user(username="******",
                                            email="*****@*****.**",
                                            password="******")
        fan2 = await users_repo.create_user(username="******",
                                            email="*****@*****.**",
                                            password="******")

        article1 = await articles_repo.create_article(slug=f"slug-1",
                                                      title="tmp",
                                                      description="tmp",
                                                      body="tmp",
                                                      author=test_user)
        article2 = await articles_repo.create_article(slug=f"slug-2",
                                                      title="tmp",
                                                      description="tmp",
                                                      body="tmp",
                                                      author=test_user)

        await articles_repo.add_article_into_favorites(article=article1,
                                                       user=fan1)
        await articles_repo.add_article_into_favorites(article=article1,
                                                       user=fan2)
        await articles_repo.add_article_into_favorites(article=article2,
                                                       user=fan2)

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

    response = await authorized_client.get(
        app.url_path_for("articles:list-articles"),
        params={"favorited": favorited})
    articles = ListOfArticlesInResponse(**response.json())
    assert articles.articles_count == result
Example #9
0
async def test_filtering_by_authors(
    app: FastAPI,
    authorized_client: AsyncClient,
    test_user: UserInDB,
    pool: Pool,
    author: str,
    result: int,
) -> None:
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        articles_repo = ArticlesRepository(connection)

        author1 = await users_repo.create_user(username="******",
                                               email="*****@*****.**",
                                               password="******")
        author2 = await users_repo.create_user(username="******",
                                               email="*****@*****.**",
                                               password="******")

        await articles_repo.create_article(slug=f"slug-1",
                                           title="tmp",
                                           description="tmp",
                                           body="tmp",
                                           author=author1)
        await articles_repo.create_article(slug=f"slug-2-1",
                                           title="tmp",
                                           description="tmp",
                                           body="tmp",
                                           author=author2)
        await articles_repo.create_article(slug=f"slug-2-2",
                                           title="tmp",
                                           description="tmp",
                                           body="tmp",
                                           author=author2)

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

    response = await authorized_client.get(
        app.url_path_for("articles:list-articles"), params={"author": author})
    articles = ListOfArticlesInResponse(**response.json())
    assert articles.articles_count == result
async def test_user_receiving_feed_with_limit_and_offset(
    app: FastAPI,
    authorized_client: Client,
    test_article: Article,
    test_user: UserInDB,
    pool: Pool,
) -> None:
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        profiles_repo = ProfilesRepository(connection)
        articles_repo = ArticlesRepository(connection)

        for i in range(5):
            user = await users_repo.create_user(username=f"user-{i}",
                                                email=f"user-{i}@email.com",
                                                password="******")
            if i == 2:
                await profiles_repo.add_user_into_followers(
                    target_user=user, requested_user=test_user)

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

    full_response = await authorized_client.get(
        app.url_path_for("articles:get-user-feed-articles"))
    full_articles = ListOfArticlesInResponse(**full_response.json())

    response = await authorized_client.get(
        app.url_path_for("articles:get-user-feed-articles"),
        params={
            "limit": 2,
            "offset": 3
        },
    )

    articles_from_response = ListOfArticlesInResponse(**response.json())
    assert full_articles.articles[3:] == articles_from_response.articles
async def test_user_will_receive_only_following_articles(
    app: FastAPI,
    authorized_client: AsyncClient,
    test_article: Article,
    test_user: UserInDB,
    pool: Pool,
) -> None:
    following_author_username = "******"
    async with pool.acquire() as connection:
        users_repo = UsersRepository(connection)
        profiles_repo = ProfilesRepository(connection)
        articles_repo = ArticlesRepository(connection)

        for i in range(5):
            user = await users_repo.create_user(
                username=f"user-{i}", email=f"user-{i}@email.com", password="******"
            )
            if i == 2:
                await profiles_repo.add_user_into_followers(
                    target_user=user, requested_user=test_user
                )

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

    response = await authorized_client.get(
        app.url_path_for("articles:get-user-feed-articles")
    )

    articles_from_response = ListOfArticlesInResponse(**response.json())
    assert len(articles_from_response.articles) == 5

    all_from_following = (
        article.author.username == following_author_username
        for article in articles_from_response.articles
    )
    assert all(all_from_following)
async def test_filtering_by_tags(
    app: FastAPI,
    authorized_client: AsyncClient,
    test_user: UserInDB,
    pool: Pool,
    tag: str,
    result: int,
) -> None:
    async with pool.acquire() as connection:
        articles_repo = ArticlesRepository(connection)

        await articles_repo.create_article(
            slug=f"slug-1",
            title="tmp",
            description="tmp",
            body="tmp",
            author=test_user,
            tags=["tag1", "tag2"],
        )
        await articles_repo.create_article(
            slug=f"slug-2",
            title="tmp",
            description="tmp",
            body="tmp",
            author=test_user,
            tags=["tag2"],
        )

        for i in range(5, 10):
            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:list-articles"), params={"tag": tag}
    )
    articles = ListOfArticlesInResponse(**response.json())
    assert articles.articles_count == result
async def test_user_can_not_change_article_state_twice(
    app: FastAPI,
    authorized_client: Client,
    test_article: Article,
    test_user: UserInDB,
    pool: Pool,
    api_method: str,
    route_name: str,
    favorite_state: bool,
) -> None:
    if 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)

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

    assert response.status_code == status.HTTP_400_BAD_REQUEST