示例#1
0
async def test_get_playlist_info(response_get_mock: MagicMock, ) -> None:
    """Test get playlist info if everything is fine."""
    playlist_id: str = "test"
    expected_tracks: List[Dict[str, Any]] = [{"name": "test"}]
    expected_playlist: Dict[str, Any] = {
        "url": "test_url",
        "name": "test",
        "spotify_id": playlist_id,
    }
    response_data: Dict[str, Any] = {
        "external_urls": {
            "spotify": expected_playlist["url"]
        },
        "name": expected_playlist["name"],
        "tracks": {
            "items": expected_tracks
        },
    }
    response_get_mock.return_value = get_response_mock(
        method="POST",
        response_data=response_data,
        valid=True,
    )

    playlist_info, tracks = await get_playlist_info(playlist_id=playlist_id,
                                                    access_token="test")

    AssertThat(playlist_info).IsEqualTo(expected_playlist)
    AssertThat(tracks).IsEqualTo(expected_tracks)
示例#2
0
async def test_code_auth_invalid_response(  # type: ignore
        response_post_mock: MagicMock,
        response_get_mock: MagicMock,
        provider,
        response_data: Dict[str, Any],
) -> None:
    """Check code auth method in provider if response is not valid."""
    response_get_mock.return_value = get_response_mock(
        method="GET", response_data=response_data, valid=False,
    )
    response_post_mock.return_value = get_response_mock(
        method="POST", response_data=response_data, valid=False,
    )

    with AssertThat(UnauthorizedError).IsRaised():
        route = provider(endpoint=endpoint_logic, path="/test/")
        await route.code_auth(code="test")
示例#3
0
async def test_get_playlist_info_bad_request(
    response_get_mock: MagicMock, ) -> None:
    """Test get playlist info if bad request."""
    response_get_mock.return_value = get_response_mock(
        method="POST",
        response_data={},
        valid=False,
    )

    with AssertThat(UnauthorizedError).IsRaised():
        await get_playlist_info(playlist_id="test", access_token="test")
示例#4
0
async def test_get_account_info_empty_response(
        response_get_mock: MagicMock,
) -> None:
    """Check get_account_info method in vk if empty response."""
    response_data: Dict[str, Any] = {}
    response_get_mock.return_value = get_response_mock(
        method="GET", response_data=response_data, valid=True,
    )

    with AssertThat(UnauthorizedError).IsRaised():
        route = VKAuth(endpoint=endpoint_logic, path="/test/")
        await route.get_account_info(access_token="test")
示例#5
0
async def test_refresh_spotify_token_invalid_response(
    response_post_mock: MagicMock, ) -> None:
    """Check refresh_spotify_token if response is not valid."""
    response_data: Dict[str, Any] = {}
    response_post_mock.return_value = get_response_mock(
        method="GET",
        response_data=response_data,
        valid=False,
    )

    with AssertThat(UnauthorizedError).IsRaised():
        await refresh_spotify_token(refresh_token="test")
示例#6
0
async def test_get_playlist_info_not_found(
    response_get_mock: MagicMock, ) -> None:
    """Test get playlist info if playlist was not found."""
    response_get_mock.return_value = get_response_mock(
        method="POST",
        response_data={},
        valid=False,
        status=404,
    )

    with AssertThat(NotFoundError).IsRaised():
        await get_playlist_info(playlist_id="test", access_token="test")
示例#7
0
async def test_code_auth_valid_response(  # type: ignore
        response_post_mock: MagicMock,
        response_get_mock: MagicMock,
        provider,
        response_data: Dict[str, Any],
) -> None:
    """Check code auth method in provider if response is valid."""
    response_get_mock.return_value = get_response_mock(
        method="GET", response_data=response_data, valid=True,
    )
    response_post_mock.return_value = get_response_mock(
        method="POST", response_data=response_data, valid=True,
    )

    route = provider(endpoint=endpoint_logic, path="/test/")
    access_token, refresh_token, expires = await route.code_auth(code="test")

    AssertThat(access_token).IsEqualTo(response_data["access_token"])
    AssertThat(refresh_token).IsEqualTo(response_data["refresh_token"])
    AssertThat(expires).IsEqualTo(
        response_data["expires_in"] + int(datetime.utcnow().timestamp())
    )
async def test_get_account_info_invalid_response(  # type: ignore
        response_get_mock: MagicMock,
        provider,
        response_data: Dict[str, Any],
        expected_data: Dict[str, Any],  # pylint: disable=unused-argument
) -> None:
    """Check get_account_info method in provider if response is not valid."""
    response_get_mock.return_value = get_response_mock(
        method="GET", response_data=response_data, valid=False,
    )

    with AssertThat(UnauthorizedError).IsRaised():
        route = provider(endpoint=endpoint_logic, path="/test/")
        await route.get_account_info(access_token="test")
async def test_get_account_info_valid_response(  # type: ignore
        response_get_mock: MagicMock,
        provider,
        response_data: Dict[str, Any],
        expected_data: Dict[str, Any],
) -> None:
    """Check get_account_info method in provider if response is valid."""
    response_get_mock.return_value = get_response_mock(
        method="GET", response_data=response_data, valid=True,
    )

    route = provider(endpoint=endpoint_logic, path="/test/")
    info: Dict[str, str] = await route.get_account_info(access_token="test")

    AssertThat(info["_id"]).IsEqualTo(expected_data["_id"])
    AssertThat(info["name"]).IsEqualTo(expected_data["name"])
    AssertThat(info["image"]).IsEqualTo(expected_data["image"])
    AssertThat(info["url"]).ContainsAnyIn(expected_data["url"])
示例#10
0
async def test_refresh_spotify_token_valid_response(
    response_post_mock: MagicMock, ) -> None:
    """Check refresh_spotify_token if response is valid."""
    refresh_token: str = "test_refresh"
    response_data: Dict[str, Any] = {
        "access_token": "test_token",
        "expires_in": 1,
    }
    response_post_mock.return_value = get_response_mock(
        method="POST",
        response_data=response_data,
        valid=True,
    )

    access_token, new_refresh_token, expires = await refresh_spotify_token(
        refresh_token=refresh_token)

    AssertThat(access_token).IsEqualTo(response_data["access_token"])
    AssertThat(new_refresh_token).IsEqualTo(refresh_token)
    AssertThat(expires).IsEqualTo(response_data["expires_in"])