Files
heos/tests/test_spotify.py
T
franzz 5ca504c680
Deploy HEOS panel / deploy (push) Successful in 26s
Fix account swaping
2026-09-18 02:05:48 +02:00

103 lines
4.5 KiB
Python

"""Run against the fake Spotify Web API in fake_spotify.py:
python3 -m unittest discover -s tests -t .
"""
import sys
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
from spotify import SpotifyClient, SpotifyDeviceUnavailable, SpotifyError # noqa: E402
from tests.fake_spotify import FakeSpotify # noqa: E402
class SpotifyTest(unittest.TestCase):
def setUp(self):
self.fake = FakeSpotify()
self.addCleanup(self.fake.stop)
self.client = SpotifyClient(
"client-id", "client-secret", "refresh-token",
accounts_url=self.fake.base_url, api_url=self.fake.base_url,
)
def test_devices_lists_what_spotify_reports(self):
names = [d["name"] for d in self.client.devices()]
self.assertEqual(names, ["Lego Room", "Home Cinema"])
self.assertEqual(self.fake.tokens_issued, 1) # one refresh for the whole call
def test_resume_transfers_playback_to_the_matched_device(self):
device = self.client.resume("Lego Room")
self.assertEqual(device["id"], "dev-1")
self.assertEqual(self.fake.transfers, [{"device_ids": ["dev-1"], "play": True}])
def test_resume_raises_when_no_device_has_that_name(self):
with self.assertRaises(SpotifyDeviceUnavailable):
self.client.resume("Kitchen")
self.assertEqual(self.fake.transfers, [])
def test_an_invisible_device_reports_what_it_looked_for(self):
# A room another Spotify account is signed in to is simply absent
# here, and app.py needs the name back to go and ask who has it.
with self.assertRaises(SpotifyDeviceUnavailable) as caught:
self.client.resume("Living Room")
self.assertEqual(caught.exception.device_name, "Living Room")
def test_it_knows_what_its_login_is_allowed_to_do(self):
# Signing a speaker in needs the "streaming" scope, and finding that
# out afterwards costs the room -- see app.py's _spotify_take_over.
self.fake.scope = "user-read-playback-state streaming"
self.assertTrue(self.client.has_scope("streaming"))
self.assertFalse(self.client.has_scope("user-modify-playback-state"))
def test_pause_stops_what_the_account_is_playing(self):
# What handing a room back starts with: the account keeps its place
# in the queue, so its own app -- or this panel's button -- can pick
# the stream up again somewhere else.
self.fake.player = {"device": {"id": "dev-1", "name": "Lego Room"}, "is_playing": True}
self.client.pause()
self.assertEqual(self.fake.pauses, 1)
self.assertFalse(self.client.playback()["is_playing"])
def test_seek_asks_for_the_position(self):
self.client.seek(90000)
self.assertEqual(self.fake.seeks, [90000])
def test_playback_reports_the_device_and_whether_it_plays(self):
self.fake.player = {"device": {"id": "dev-1", "name": "Lego Room"}, "is_playing": True}
player = self.client.playback()
self.assertEqual(player["device"]["name"], "Lego Room")
self.assertTrue(player["is_playing"])
def test_playback_is_empty_without_a_session(self):
self.assertEqual(self.client.playback(), {})
def test_a_rejected_access_token_is_refreshed_and_retried_once(self):
self.client.devices() # get a real token first
self.assertEqual(self.fake.tokens_issued, 1)
self.client._access_token = "stale-but-not-yet-expired"
# _expires_at is untouched, so only the 401 -- not the pre-call
# expiry check -- can be what forces this to work.
names = [d["name"] for d in self.client.devices()]
self.assertEqual(names, ["Lego Room", "Home Cinema"])
self.assertEqual(self.fake.tokens_issued, 2)
def test_a_plain_text_refusal_is_quoted_rather_than_flattened(self):
# The 403 for an account missing from the developer app's User
# Management is Spotify's only hint that that is what is wrong,
# and it arrives as prose rather than as a JSON error object.
self.fake.forbid = "The user is not registered for this application."
with self.assertRaises(SpotifyError) as caught:
self.client.devices()
self.assertIn("not registered for this application", str(caught.exception))
def test_a_revoked_refresh_token_raises_a_clear_error(self):
self.fake.reject_refresh = True
with self.assertRaises(SpotifyError):
self.client.devices()
if __name__ == "__main__":
unittest.main()