from server.spotify.client import Spotify import pytest import collections from typing import List, Dict from server.spotify.exceptions import SpotifyError from http import HTTPStatus from aioresponses import aioresponses pytest_plugins = ["tests.spotify.fixtures.responses"] @pytest.mark.parametrize( "status,expected_status,retry_after,exception,result", ((HTTPStatus.OK, HTTPStatus.OK, 0, False, {"res": 1}), (HTTPStatus.TOO_MANY_REQUESTS, 530, 15, True, {})), ) async def test_handler_too_many_requests_status( client, spotify_client, mocker, status, expected_status, retry_after, exception, result ): async def return__auth_headers(*args, **kwargs): return {"Authorization": "Bearer Token"} mock_auth_headers = mocker.patch.object(Spotify, "_auth_headers") mock_auth_headers.side_effect = return__auth_headers sub_url = "v1/tracks/track_id" url = f"{spotify_client.prefix}/{sub_url}" with aioresponses() as mocked_session: mocked_session.get(url, status=status, headers={"Retry-After": str(retry_after)}, payload=result) try: response = await spotify_client._get(sub_url) assert response == result assert not exception except SpotifyError as e: assert e.status_code == expected_status assert exception async def test_handler_too_many_requests_status_with_retrying(client, spotify_client, mocker): async def return__auth_headers(*args, **kwargs): return {"Authorization": "Bearer Token"} mock_auth_headers = mocker.patch.object(Spotify, "_auth_headers") mock_auth_headers.side_effect = return__auth_headers sub_url = "v1/tracks/track_id" url = f"{spotify_client.prefix}/{sub_url}" result = {"res": 1} with aioresponses() as mocked_session: mocked_session.get(url, status=HTTPStatus.TOO_MANY_REQUESTS, headers={"Retry-After": str(1)}) mocked_session.get(url, status=HTTPStatus.OK, payload=result) response = await spotify_client._get(sub_url) assert response == result async def test_request_in_chunks(client, mocker, auth_header, spotify_track_response): track_ids: List[str] = [str(i + 1) for i in range(120)] tracks: Dict[int, dict] = {id_: spotify_track_response(track_id=id_, track_number=id_) for id_ in track_ids} tracks = collections.OrderedDict(sorted(tracks.items(), key=lambda item: int(item[0]))) async def return_async_value(url, *args, **kwargs): ids = url.replace("v1/tracks/?ids=", "").split(",") return {"tracks": [tracks[id_] for id_ in ids]} mock_async_func2 = mocker.patch.object(Spotify, "_get") mock_async_func2.side_effect = return_async_value response = await client.get("/api/spotify/v1/tracks", params={"ids": ",".join(track_ids)}, headers=auth_header) assert response.status == HTTPStatus.OK resp_data = await response.json() tracks_response = resp_data["tracks"] for track in tracks_response: assert track == tracks[track["id"]] assert mock_async_func2.call_count == 3 async def test_tracks_images_returns_empty_list_if_tracks_is_none(client, mocker, auth_header): track_ids: List[str] = [str(i + 1) for i in range(120)] async def return_async_empty_dict(url, *args, **kwargs): return {} mock_get = mocker.patch.object(Spotify, "_get") mock_get.side_effect = return_async_empty_dict response = await client.post("/api/spotify/v1/tracks-images", json={"ids": track_ids}, headers=auth_header) assert response.status == HTTPStatus.OK tracks_images_data = await response.json() assert not tracks_images_data