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_unregistered_user_will_receive_profile_without_following( app: FastAPI, client: AsyncClient, test_user: UserInDB) -> None: response = await client.get( app.url_path_for("profiles:get-profile", username=test_user.username)) profile = ProfileInResponse(**response.json()) assert profile.profile.username == test_user.username assert not profile.profile.following
async def test_user_that_does_not_follows_another_will_receive_profile_without_follow( app: FastAPI, authorized_client: AsyncClient, db: Database) -> None: async with db.pool.acquire() as conn: users_repo = UsersRepository(conn) user = await users_repo.create_user( username="******", email="*****@*****.**", password="******", ) 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 not profile.profile.following
async def follow_for_user( profile: Profile = Depends(get_profile_by_username_from_path), user: User = Depends(get_current_user_authorizer()), profiles_repo: ProfilesRepository = Depends( get_repository(ProfilesRepository)), ) -> ProfileInResponse: if user.username == profile.username: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.UNABLE_TO_FOLLOW_YOURSELF) if profile.following: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.USER_IS_ALREADY_FOLLOWED) await profiles_repo.add_user_into_followers(target_user=profile, requested_user=user) return ProfileInResponse(profile=profile.copy(update={"following": True}))
async def unsubscribe_from_user( profile: Profile = Depends(get_profile_by_username_from_path), user: User = Depends(get_current_user_authorizer()), profiles_repo: ProfilesRepository = Depends( get_repository(ProfilesRepository)), ) -> ProfileInResponse: if user.username == profile.username: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail=strings.UNABLE_TO_UNSUBSCRIBE_FROM_YOURSELF, ) if not profile.following: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail=strings.USER_IS_NOT_FOLLOWED) await profiles_repo.remove_user_from_followers(target_user=profile, requested_user=user) return ProfileInResponse(profile=profile.copy(update={"following": False}))
async def test_user_that_follows_another_will_receive_profile_with_follow( app: FastAPI, authorized_client: AsyncClient, pool: Pool, test_user: UserInDB) -> None: async with 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
async def retrieve_profile_by_username( profile: Profile = Depends(get_profile_by_username_from_path), ) -> ProfileInResponse: return ProfileInResponse(profile=profile)