Exemplo n.º 1
0
async def test_get_profile_without_authorized(async_client: AsyncClient,
                                              test_user: schemas.UserDB):
    r = await async_client.get(f"{API_PROFILES}/{test_user.username}")
    assert r.status_code == status.HTTP_200_OK
    profile_response = schemas.ProfileResponse(**r.json())
    profile = profile_response.profile
    assert_profile_with_user(profile, test_user)
Exemplo n.º 2
0
async def test_get_profile_with_authorized(async_client: AsyncClient,
                                           test_user: schemas.UserDB,
                                           token: str):
    headers = {"Authorization": f"{JWT_TOKEN_PREFIX} {token}"}
    r = await async_client.get(f"{API_PROFILES}/{test_user.username}",
                               headers=headers)
    assert r.status_code == status.HTTP_200_OK
    profile_response = schemas.ProfileResponse(**r.json())
    profile = profile_response.profile
    assert_profile_with_user(profile, test_user)
    assert not profile.following
Exemplo n.º 3
0
async def get_profile_response(
        username: str,
        requested_user: schemas.UserDB) -> schemas.ProfileResponse:
    profile = await crud_profile.get_profile_by_username(
        username, requested_user=requested_user)
    if profile is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="user not existed",
        )
    return schemas.ProfileResponse(profile=profile)
Exemplo n.º 4
0
async def test_unfollow_user(
    async_client: AsyncClient,
    test_user: schemas.UserDB,
    token: str,
    other_user: schemas.UserDB,
):
    result = await crud_profile.follow(other_user, test_user)
    assert result
    headers = {"Authorization": f"{JWT_TOKEN_PREFIX} {token}"}
    r = await async_client.delete(
        f"{API_PROFILES}/{other_user.username}/follow", headers=headers)
    assert r.status_code == status.HTTP_200_OK
    profile_response = schemas.ProfileResponse(**r.json())
    profile = profile_response.profile
    assert_profile_with_user(profile, other_user)
    assert not profile.following
Exemplo n.º 5
0
async def test_get_profile_with_wrong_authorized(async_client: AsyncClient,
                                                 test_user: schemas.UserDB,
                                                 token: str):
    wrong_signature_token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTA3NzM3LCJ1c2VybmFtZSI6InUxNTk2MzUyMDIxIiwiZXhwIjoxNjA0MjExMjUyfQ.qsyv6QGAIE1kk_ZQucj2IIs_zRvvO-HYqjMQ1Z9TGcw"  # noqa
    headers = {"Authorization": f"{JWT_TOKEN_PREFIX} {wrong_signature_token}"}
    r = await async_client.get(f"{API_PROFILES}/{test_user.username}",
                               headers=headers)
    assert r.status_code == status.HTTP_401_UNAUTHORIZED

    headers = {"Authorization": f"{JWT_TOKEN_PREFIX} failed_token"}
    r = await async_client.get(f"{API_PROFILES}/{test_user.username}",
                               headers=headers)
    assert r.status_code == status.HTTP_401_UNAUTHORIZED

    invalid_authorization = "invalid_authorization"
    headers = {"Authorization": invalid_authorization}
    r = await async_client.get(f"{API_PROFILES}/{test_user.username}",
                               headers=headers)
    assert r.status_code == status.HTTP_200_OK

    headers = {"Authorization": f"{JWT_TOKEN_PREFIX}xxx failed_token"}
    r = await async_client.get(f"{API_PROFILES}/{test_user.username}",
                               headers=headers)
    assert r.status_code == status.HTTP_200_OK
    profile_response = schemas.ProfileResponse(**r.json())
    profile = profile_response.profile
    assert_profile_with_user(profile, test_user)
    assert not profile.following

    await delete_user(test_user)
    headers = {"Authorization": f"{JWT_TOKEN_PREFIX} {token}"}
    r = await async_client.get(f"{API_PROFILES}/{test_user.username}",
                               headers=headers)
    assert r.status_code == status.HTTP_400_BAD_REQUEST
    user_response = r.json()
    assert "detail" in user_response
    assert user_response["detail"] == "user not existed"
Exemplo n.º 6
0
async def unfollow_user(
    username: str,
    requested_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> schemas.ProfileResponse:
    follower_user = await crud_user.get_user_by_username(username=username)
    if follower_user is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="user with this username is not existed",
        )
    if follower_user.id == requested_user.id:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="cannot unfollow yourself",
        )
    if not await crud_profile.is_following(follower_user, requested_user):
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="you don't follow this user already",
        )
    await crud_profile.unfollow(follower_user, requested_user)
    profile = await crud_profile.get_profile_by_username(
        username, requested_user)
    return schemas.ProfileResponse(profile=profile)  # type: ignore