async def test_user_can_change_following_for_another_user(
    app: FastAPI,
    authorized_client: AsyncClient,
    db: Database,
    test_user: UserInDB,
    api_method: str,
    route_name: str,
    following: bool,
) -> None:
    async with db.pool.acquire() as conn:
        users_repo = UsersRepository(conn)
        user = await users_repo.create_user(
            username="******",
            email="*****@*****.**",
            password="******",
        )

        if not following:
            profiles_repo = ProfilesRepository(conn)
            await profiles_repo.add_user_into_followers(
                target_user=user, requested_user=test_user)

    change_following_response = await authorized_client.request(
        api_method, app.url_path_for(route_name, username=user.username))
    assert change_following_response.status_code == status.HTTP_200_OK

    response = await authorized_client.get(
        app.url_path_for("profiles:get-profile", username=user.username))
    profile = ProfileInResponse(**response.json())
    assert profile.profile.username == user.username
    assert profile.profile.following == following
async def test_user_can_not_change_following_state_to_the_same_twice(
    app: FastAPI,
    authorized_client: AsyncClient,
    db: Database,
    test_user: UserInDB,
    api_method: str,
    route_name: str,
    following: bool,
) -> None:
    async with db.pool.acquire() as conn:
        users_repo = UsersRepository(conn)
        user = await users_repo.create_user(
            username="******",
            email="*****@*****.**",
            password="******",
        )

        if following:
            profiles_repo = ProfilesRepository(conn)
            await profiles_repo.add_user_into_followers(
                target_user=user, requested_user=test_user)

    response = await authorized_client.request(
        api_method, app.url_path_for(route_name, username=user.username))

    assert response.status_code == status.HTTP_400_BAD_REQUEST
 async def test_profile_created_for_new_users(self, app: FastAPI, client: AsyncClient, db: Database) -> None:
     profile_repo = ProfilesRepository(db)
     new_user = {"email": "*****@*****.**", "username": "******", "password": "******"}
     res = await client.post(app.url_path_for("users:register-new-user"), json={"new_user": new_user})
     assert res.status_code == status.HTTP_201_CREATED
     created_user = UserPublic(**res.json())
     user_profile = await profile_repo.get_profile_by_user_id(user_id=created_user.id)
     assert user_profile is not None
     assert isinstance(user_profile, ProfileInDB)
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_user_that_follows_another_will_receive_profile_with_follow(
        app: FastAPI, authorized_client: AsyncClient, db: Database,
        test_user: UserInDB) -> None:
    async with db.pool.acquire() as conn:
        users_repo = UsersRepository(conn)
        user = await users_repo.create_user(
            username="******",
            email="*****@*****.**",
            password="******",
        )

        profiles_repo = ProfilesRepository(conn)
        await profiles_repo.add_user_into_followers(target_user=user,
                                                    requested_user=test_user)

    response = await authorized_client.get(
        app.url_path_for("profiles:get-profile", username=user.username))
    profile = ProfileInResponse(**response.json())
    assert profile.profile.username == user.username
    assert profile.profile.following
 def __init__(self, db: Database) -> None:
     super().__init__(db)
     self.auth_service = auth_service
     # make sure that when a new user is created, our UsersRepository
     # will also create_profile_for_user below
     self.profiles_repo = ProfilesRepository(db)
Exemple #8
0
 def __init__(self, db: Database) -> None:
     super().__init__(db)
     self.auth_service = auth_service
     self.profiles_repo = ProfilesRepository(db)
Exemple #9
0
 def __init__(self, conn: Connection) -> None:
     super().__init__(conn)
     self._profiles_repo = ProfilesRepository(conn)
     self._tags_repo = TagsRepository(conn)