Example #1
0
    def test_get_ad_item_(self):
        metamock = patch.object(
            APIClient, "__call__", return_value=TestAdItem.JSON_DATA
        )

        with metamock as ad_metadata_mock:
            transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

            client = APIClient(transport, None, None, None, None)
            client._authenticate = Mock()

            ad_item = client.get_ad_item("id_mock", "token_mock")
            assert ad_item.station_id == "id_mock"
            assert ad_item.ad_token == "token_mock"

            ad_metadata_mock.assert_has_calls(
                [
                    call(
                        "ad.getAdMetadata",
                        adToken="token_mock",
                        returnAdTrackingTokens=True,
                        supportAudioAds=True,
                    )
                ]
            )
Example #2
0
    def test_with_enum(self):
        with patch.object(APIClient, "__call__") as playlist_mock:
            transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

            client = APIClient(transport, None, None, None, None)
            client._authenticate = Mock()

            urls = [
                AdditionalAudioUrl.HTTP_128_MP3,
                AdditionalAudioUrl.HTTP_24_AACPLUS_ADTS,
            ]

            desired = "HTTP_128_MP3,HTTP_24_AACPLUS_ADTS"

            client.get_playlist("token_mock", additional_urls=urls)

            playlist_mock.assert_has_calls(
                [
                    call(
                        "station.getPlaylist",
                        additionalAudioUrl=desired,
                        audioAdPodCapable=True,
                        includeTrackLength=True,
                        stationToken="token_mock",
                        xplatformAdCapable=True,
                    )
                ]
            )
Example #3
0
    def test_non_iterable_string(self):
        with self.assertRaises(TypeError):
            transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

            client = APIClient(transport, None, None, None, None)
            client._authenticate = Mock()

            client.get_playlist('token_mock', additional_urls='')
Example #4
0
    def test_non_iterable_string(self):
        with self.assertRaises(TypeError):
            transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

            client = APIClient(transport, None, None, None, None)
            client._authenticate = Mock()

            client.get_playlist("token_mock", additional_urls="")
Example #5
0
    def test_get_ad_item_with_no_station_id_specified_raises_exception(self):
        transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

        client = APIClient(transport, None, None, None, None)
        client.get_ad_metadata = Mock()

        self.assertRaises(
                errors.ParameterMissing, client.get_ad_item, '', 'token_mock')
Example #6
0
    def test_get_ad_item_with_no_station_id_specified_raises_exception(self):
        transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

        client = APIClient(transport, None, None, None, None)
        client.get_ad_metadata = Mock()

        self.assertRaises(errors.ParameterMissing, client.get_ad_item, '',
                          'token_mock')
Example #7
0
    def test_non_iterable_other(self):
        with self.assertRaises(TypeError):
            transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

            client = APIClient(transport, None, None, None, None)
            client._authenticate = Mock()

            client.get_playlist('token_mock',
                                additional_urls=AdditionalAudioUrl.HTTP_32_WMA)
Example #8
0
 def test_has_initial_checksum(self):
     fake_data = {
         "categories": [{"categoryName": "foo", "stations": []},],
         # Not actually part of the genre station response but is needed to
         # fake out the mock for get_genre_stations_checksum
         "checksum": "foo",
     }
     with patch.object(APIClient, "__call__", return_value=fake_data):
         client = APIClient(Mock(), None, None, None, None)
         station = client.get_genre_stations()
         self.assertEqual(station.checksum, "foo")
Example #9
0
    def test_playlist_fetches_ads(self):
        fake_playlist = {"items": [
            {"songName": "test"},
            {"adToken": "foo"},
            {"songName": "test"},
        ]}
        with patch.object(APIClient, '__call__', return_value=fake_playlist):
            client = APIClient(Mock(), None, None, None, None)
            client._authenticate = Mock()

            items = client.get_playlist('token_mock')
            self.assertIsInstance(items[1], AdItem)
Example #10
0
    def test_playlist_fetches_ads(self):
        fake_playlist = {
            "items": [
                {"songName": "test"},
                {"adToken": "foo"},
                {"songName": "test"},
            ]
        }
        with patch.object(APIClient, "__call__", return_value=fake_playlist):
            client = APIClient(Mock(), None, None, None, None)
            client._authenticate = Mock()

            items = client.get_playlist("token_mock")
            self.assertIsInstance(items[1], AdItem)
Example #11
0
    def test_has_initial_checksum(self):
        fake_data = {
            "categories": [
                {"categoryName": "foo", "stations": []},
            ],

            # Not actually part of the genre station response but is needed to
            # fake out the mock for get_genre_stations_checksum
            "checksum": "foo"
        }
        with patch.object(APIClient, '__call__', return_value=fake_data):
            client = APIClient(Mock(), None, None, None, None)
            station = client.get_genre_stations()
            self.assertEqual(station.checksum, "foo")
Example #12
0
    def test_ad_support_enabled_parameters(self):
        with patch.object(APIClient, '__call__') as playlist_mock:
            transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

            client = APIClient(transport, None, None, None, None)
            client._authenticate = Mock()

            client.get_playlist('token_mock')

            playlist_mock.assert_has_calls([call("station.getPlaylist",
                                             audioAdPodCapable=True,
                                             includeTrackLength=True,
                                             stationToken='token_mock',
                                             xplatformAdCapable=True)])
Example #13
0
    def test_ad_support_enabled_parameters(self):
        with patch.object(APIClient, '__call__') as playlist_mock:
            transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

            client = APIClient(transport, None, None, None, None)
            client._authenticate = Mock()

            client.get_playlist('token_mock')

            playlist_mock.assert_has_calls([
                call("station.getPlaylist",
                     audioAdPodCapable=True,
                     includeTrackLength=True,
                     stationToken='token_mock',
                     xplatformAdCapable=True)
            ])
Example #14
0
    def test_get_ad_item_(self):
        with patch.object(APIClient, '__call__',
                return_value=TestAdItem.JSON_DATA) as ad_metadata_mock:
            transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

            client = APIClient(transport, None, None, None, None)
            client._authenticate = Mock()

            ad_item = client.get_ad_item('id_mock', 'token_mock')
            assert ad_item.station_id == 'id_mock'
            assert ad_item.ad_token == 'token_mock'

            ad_metadata_mock.assert_has_calls([call("ad.getAdMetadata",
                                                    adToken='token_mock',
                                                    returnAdTrackingTokens=True,
                                                    supportAudioAds=True)])
Example #15
0
    def test_with_enum(self):
        with patch.object(APIClient, '__call__') as playlist_mock:
            transport = Mock(side_effect=[errors.InvalidAuthToken(), None])

            client = APIClient(transport, None, None, None, None)
            client._authenticate = Mock()

            urls = [AdditionalAudioUrl.HTTP_128_MP3,
                    AdditionalAudioUrl.HTTP_24_AACPLUS_ADTS]

            desired = 'HTTP_128_MP3,HTTP_24_AACPLUS_ADTS'

            client.get_playlist('token_mock', additional_urls=urls)

            playlist_mock.assert_has_calls([call("station.getPlaylist",
                                                 additionalAudioUrl=desired,
                                                 audioAdPodCapable=True,
                                                 includeTrackLength=True,
                                                 stationToken='token_mock',
                                                 xplatformAdCapable=True)])
Example #16
0
 def test_using_track_token(self):
     client = APIClient(Mock(return_value={}), None, None, None, None)
     client.create_station(track_token="foo")
     client.transport.assert_called_with(
         "station.createStation", trackToken="foo", musicType="song")
Example #17
0
 def test_using_track_token(self):
     client = APIClient(Mock(return_value={}), None, None, None, None)
     client.create_station(track_token="foo")
     client.transport.assert_called_with(
         "station.createStation", trackToken="foo", musicType="song"
     )
Example #18
0
 def setUp(self):
     self.transport = Mock(side_effect=[InvalidAuthToken(), None])
     self.client = APIClient(self.transport, None, None, None, None)
     self.client._authenticate = Mock()
Example #19
0
 def test_with_no_token(self):
     with self.assertRaises(KeyError):
         client = APIClient(Mock(), None, None, None, None)
         client.create_station()
Example #20
0
 def setUp(self):
     self.transport = Mock()
     self.api = APIClient(self.transport, "puser", "ppass", "device")
Example #21
0
class TestAPIClientExhaustive(TestCase):

    def setUp(self):
        self.transport = Mock()
        self.api = APIClient(self.transport, "puser", "ppass", "device")

    def test_register_ad(self):
        self.api.register_ad("sid", "tokens")
        self.transport.assert_called_with(
            "ad.registerAd", stationId="sid", adTrackingTokens="tokens")

    def test_share_music(self):
        self.api.share_music("token", "*****@*****.**")
        self.transport.assert_called_with(
            "music.shareMusic", musicToken="token", email="*****@*****.**")

    def test_transform_shared_station(self):
        self.api.transform_shared_station("token")
        self.transport.assert_called_with(
            "station.transformSharedStation", stationToken="token")

    def test_share_station(self):
        self.api.share_station("sid", "token", "*****@*****.**")
        self.transport.assert_called_with(
            "station.shareStation", stationId="sid", stationToken="token",
            emails=("*****@*****.**",))

    def test_sleep_song(self):
        self.api.sleep_song("token")
        self.transport.assert_called_with("user.sleepSong", trackToken="token")

    def test_set_quick_mix(self):
        self.api.set_quick_mix("id")
        self.transport.assert_called_with(
            "user.setQuickMix", quickMixStationIds=("id",))

    def test_explain_track(self):
        self.api.explain_track("token")
        self.transport.assert_called_with(
            "track.explainTrack", trackToken="token")

    def test_rename_station(self):
        self.api.rename_station("token", "name")
        self.transport.assert_called_with(
            "station.renameStation", stationToken="token", stationName="name")

    def test_delete_station(self):
        self.api.delete_station("token")
        self.transport.assert_called_with(
            "station.deleteStation", stationToken="token")

    def test_delete_music(self):
        self.api.delete_music("seed")
        self.transport.assert_called_with("station.deleteMusic", seedId="seed")

    def test_delete_feedback(self):
        self.api.delete_feedback("id")
        self.transport.assert_called_with(
            "station.deleteFeedback", feedbackId="id")

    def test_add_music(self):
        self.api.add_music("mt", "st")
        self.transport.assert_called_with(
            "station.addMusic", musicToken="mt", stationToken="st")

    def test_add_feedback(self):
        self.api.add_feedback("token", False)
        self.transport.assert_called_with(
            "station.addFeedback", trackToken="token", isPositive=False)

    def test_add_artist_bookmark(self):
        self.api.add_artist_bookmark("tt")
        self.transport.assert_called_with(
            "bookmark.addArtistBookmark", trackToken="tt")

    def test_add_song_bookmark(self):
        self.api.add_song_bookmark("tt")
        self.transport.assert_called_with(
            "bookmark.addSongBookmark", trackToken="tt")

    def test_delete_song_bookmark(self):
        self.api.delete_song_bookmark("bt")
        self.transport.assert_called_with(
            "bookmark.deleteSongBookmark", bookmarkToken="bt")

    def test_delete_artist_bookmark(self):
        self.api.delete_artist_bookmark("bt")
        self.transport.assert_called_with(
            "bookmark.deleteArtistBookmark", bookmarkToken="bt")

    def test_get_station_list_checksum(self):
        self.transport.return_value = {"checksum": "foo"}
        self.assertEqual("foo", self.api.get_station_list_checksum())
        self.transport.assert_called_with("user.getStationListChecksum")

    # The following methods use the bare minimum JSON required to construct the
    # models for more detailed model tests look at test_models instead

    def test_get_station_list(self):
        self.transport.return_value = {"stations": []}
        self.assertIsInstance(self.api.get_station_list(), StationList)
        self.transport.assert_called_with(
            "user.getStationList", includeStationArtUrl=True)

    def test_get_bookmarks(self):
        self.transport.return_value = {}
        self.assertIsInstance(self.api.get_bookmarks(), BookmarkList)
        self.transport.assert_called_with("user.getBookmarks")

    def test_get_station(self):
        self.transport.return_value = {}
        self.assertIsInstance(self.api.get_station("st"), Station)
        self.transport.assert_called_with(
            "station.getStation", stationToken="st",
            includeExtendedAttributes=True)

    def test_search(self):
        self.transport.return_value = {}
        self.assertIsInstance(self.api.search(
            "text", include_near_matches=True, include_genre_stations=True),
            SearchResult)
        self.transport.assert_called_with(
            "music.search", searchText="text", includeNearMatches=True,
            includeGenreStations=True)
Example #22
0
class TestAPIClientExhaustive(TestCase):
    def setUp(self):
        self.transport = Mock()
        self.api = APIClient(self.transport, "puser", "ppass", "device")

    def test_register_ad(self):
        self.api.register_ad("sid", "tokens")
        self.transport.assert_called_with(
            "ad.registerAd", stationId="sid", adTrackingTokens="tokens"
        )

    def test_share_music(self):
        self.api.share_music("token", "*****@*****.**")
        self.transport.assert_called_with(
            "music.shareMusic", musicToken="token", email="*****@*****.**"
        )

    def test_transform_shared_station(self):
        self.api.transform_shared_station("token")
        self.transport.assert_called_with(
            "station.transformSharedStation", stationToken="token"
        )

    def test_share_station(self):
        self.api.share_station("sid", "token", "*****@*****.**")
        self.transport.assert_called_with(
            "station.shareStation",
            stationId="sid",
            stationToken="token",
            emails=("*****@*****.**",),
        )

    def test_sleep_song(self):
        self.api.sleep_song("token")
        self.transport.assert_called_with("user.sleepSong", trackToken="token")

    def test_set_quick_mix(self):
        self.api.set_quick_mix("id")
        self.transport.assert_called_with(
            "user.setQuickMix", quickMixStationIds=("id",)
        )

    def test_explain_track(self):
        self.api.explain_track("token")
        self.transport.assert_called_with(
            "track.explainTrack", trackToken="token"
        )

    def test_rename_station(self):
        self.api.rename_station("token", "name")
        self.transport.assert_called_with(
            "station.renameStation", stationToken="token", stationName="name"
        )

    def test_delete_station(self):
        self.api.delete_station("token")
        self.transport.assert_called_with(
            "station.deleteStation", stationToken="token"
        )

    def test_delete_music(self):
        self.api.delete_music("seed")
        self.transport.assert_called_with("station.deleteMusic", seedId="seed")

    def test_delete_feedback(self):
        self.api.delete_feedback("id")
        self.transport.assert_called_with(
            "station.deleteFeedback", feedbackId="id"
        )

    def test_add_music(self):
        self.api.add_music("mt", "st")
        self.transport.assert_called_with(
            "station.addMusic", musicToken="mt", stationToken="st"
        )

    def test_add_feedback(self):
        self.api.add_feedback("token", False)
        self.transport.assert_called_with(
            "station.addFeedback", trackToken="token", isPositive=False
        )

    def test_add_artist_bookmark(self):
        self.api.add_artist_bookmark("tt")
        self.transport.assert_called_with(
            "bookmark.addArtistBookmark", trackToken="tt"
        )

    def test_add_song_bookmark(self):
        self.api.add_song_bookmark("tt")
        self.transport.assert_called_with(
            "bookmark.addSongBookmark", trackToken="tt"
        )

    def test_delete_song_bookmark(self):
        self.api.delete_song_bookmark("bt")
        self.transport.assert_called_with(
            "bookmark.deleteSongBookmark", bookmarkToken="bt"
        )

    def test_delete_artist_bookmark(self):
        self.api.delete_artist_bookmark("bt")
        self.transport.assert_called_with(
            "bookmark.deleteArtistBookmark", bookmarkToken="bt"
        )

    def test_get_station_list_checksum(self):
        self.transport.return_value = {"checksum": "foo"}
        self.assertEqual("foo", self.api.get_station_list_checksum())
        self.transport.assert_called_with("user.getStationListChecksum")

    # The following methods use the bare minimum JSON required to construct the
    # models for more detailed model tests look at test_models instead

    def test_get_station_list(self):
        self.transport.return_value = {"stations": []}
        self.assertIsInstance(self.api.get_station_list(), StationList)
        self.transport.assert_called_with(
            "user.getStationList", includeStationArtUrl=True
        )

    def test_get_bookmarks(self):
        self.transport.return_value = {}
        self.assertIsInstance(self.api.get_bookmarks(), BookmarkList)
        self.transport.assert_called_with("user.getBookmarks")

    def test_get_station(self):
        self.transport.return_value = {}
        self.assertIsInstance(self.api.get_station("st"), Station)
        self.transport.assert_called_with(
            "station.getStation",
            stationToken="st",
            includeExtendedAttributes=True,
        )

    def test_search(self):
        self.transport.return_value = {}
        self.assertIsInstance(
            self.api.search(
                "text", include_near_matches=True, include_genre_stations=True
            ),
            SearchResult,
        )
        self.transport.assert_called_with(
            "music.search",
            searchText="text",
            includeNearMatches=True,
            includeGenreStations=True,
        )
Example #23
0
 def setUp(self):
     self.transport = Mock()
     self.api = APIClient(self.transport, "puser", "ppass", "device")
Example #24
0
 def test_with_no_token(self):
     with self.assertRaises(KeyError):
         client = APIClient(Mock(), None, None, None, None)
         client.create_station()