class TestServer(TestCase): def setUp(self): self.server = Server(transport=FakeTransport()) def test_get_current(self): assert self.server.get_status()['genre'] def test_status(self): assert self.server.get_milliseconds() == FAKE_LENGTH * 1000 def test_str(self): assert 'localhost:0' in str(self.server) def test_login(self): self.server = Server(transport=FakeTransport(), user='******', password='******') assert self.server.user == 'admin' def test_groups(self): raw = """"something%3Afoobar playerid%3Aecho6fd1 uuid%3A ip%3A127.0.0.1%3A39365 name%3ALavf%20from%20echo6fd1 seq_no%3A0 model%3Ahttp power%3A1 isplaying%3A0 canpoweroff%3A0 connected%3A0 isplayer%3A1 sn%20player%20count%3A0 other%20player%20count%3A0""".replace('\n', '') groups = self.server._groups(raw, 'playerid', ['connected']) expected = { 'playerid': 'echo6fd1', 'uuid': None, 'ip': '127.0.0.1:39365', 'name': 'Lavf from echo6fd1', 'seq_no': 0, 'model': 'http', 'power': True, 'isplaying': False, 'canpoweroff': False, 'connected': False, 'isplayer': True, 'sn player count': 0, 'other player count': 0 } assert next(groups) == expected def test_groups_status(self): data = next(self.server._groups(A_REAL_STATUS)) assert data['player_name'] == 'Study' assert data['playlist_cur_index'] == 20 assert data['artist'] == 'Jamie Cullum' assert isinstance(data['can_seek'], bool) def test_faves(self): assert len(self.server.favorites) == 2 def test_playlists(self): assert len(self.server.playlists) == 0 def test_genres(self): assert len(self.server.genres) == 0
def test_handling_all_intents_without_session_or_slots(self): server = Server(transport=(FakeTransport().start())) alexa = SqueezeAlexa(server=server) for name, func in handler._handlers.items(): request = self.request_for({'name': name, 'slots': {}}, NO_SESSION) output = alexa.handle(request, None) self.validate_response(name, output)
def test_commas_in_title(self): fake_output = FakeTransport().start() server = Server(transport=fake_output) alexa = SqueezeAlexa(server=server) resp = alexa.now_playing([], None) speech = speech_in(resp) assert "I Think, I Love" in speech assert "by Jamie Cullum" in speech
def test_multiple_artists(self): fake_output = FakeTransport(fake_status=MULTI_ARTIST_STATUS).start() server = Server(transport=fake_output) alexa = SqueezeAlexa(server=server) resp = alexa.now_playing([], None) speech = speech_in(resp) assert '"Shut \'Em Down"' in speech assert "by Public Enemy and Pete Rock" in speech
def test_classical(self): fake_output = FakeTransport(fake_status=CLASSICAL_STATUS).start() server = Server(transport=fake_output) alexa = SqueezeAlexa(server=server) resp = alexa.now_playing([], None) speech = speech_in(resp) assert '"Kyrie Eleison"' in speech assert "by Johann Sebastian Bach" in speech
def test_handling_all_intents(self): fake_output = FakeTransport() server = Server(transport=fake_output) alexa = SqueezeAlexa(server=server) for name, func in handler._handlers.items(): intent = {'name': name, 'slots': {'Player': {'name': 'Player', 'value': 'fake'}, 'Volume': {'name': 'Volume', 'value': '5'}}} output = alexa.handle(self.request_for(intent, SOME_SESSION)) self.validate_response(name, output)
def run_diagnostics(sslw): server = Server(debug=DEBUG_LMS, ssl_wrap=sslw, cur_player_id=DEFAULT_PLAYER, user=SERVER_USERNAME, password=SERVER_PASSWORD) assert server.genres assert server.playlists assert server.favorites cur_play_details = server.get_track_details().values() if cur_play_details: print("Currently playing: %s" % " >> ".join(cur_play_details)) else: print("Nothing currently in playlist") status = server.get_status() print("Up next: %s >> %s >> %s" % (status.get( 'genre', "Unknown genre"), status.get( 'title', 'Unknown track'), status.get('artist', 'Unknown artist')))
def run_diagnostics(transport: Transport): server = Server(transport=transport, debug=LMS_SETTINGS.DEBUG, cur_player_id=LMS_SETTINGS.DEFAULT_PLAYER, user=LMS_SETTINGS.USERNAME, password=LMS_SETTINGS.PASSWORD) assert server.genres assert server.playlists cur_play_details = server.get_track_details().values() if cur_play_details: print("Currently playing: %s" % " >> ".join(cur_play_details)) else: print("Nothing currently in playlist") status = server.get_status() print("Up next: %s >> %s >> %s" % (status.get('genre', "Unknown genre"), status.get('title', 'Unknown track'), status.get('artist', 'Unknown artist'))) del server
def test_all_handler(self): fake_output = FakeTransport() server = Server(transport=fake_output) alexa = SqueezeAlexa(server=server) for name, func in handler._handlers.items(): print_d(">>> Testing %s() <<<" % func.__name__) session = {'sessionId': None} intent = {'requestId': 'abcd', 'slots': {}} raw = func(alexa, intent, session, None) response = raw['response'] assert 'directives' in response or 'outputSpeech' in response assert 'shouldEndSession' in response
def run_diagnostics(transport: Transport): server = Server(transport=transport, debug=LMS_SETTINGS.DEBUG, cur_player_id=LMS_SETTINGS.DEFAULT_PLAYER, user=LMS_SETTINGS.USERNAME, password=LMS_SETTINGS.PASSWORD) assert server.genres assert server.playlists cur_play_details = server.get_track_details() if cur_play_details: print("Currently playing: \n >> %s" % "\n >> ".join("%s: %s" % (k, ", ".join(v)) for k, v in cur_play_details.items())) else: print("Nothing currently in playlist") d = server.get_track_details(offset=+1) print("Up next: %s >> %s >> %s" % (d.get( 'genre', ["Unknown genre"])[0], people_from( d, ["Unknown people"])[0], d.get('title', ['Unknown track'])[0])) del server
def test_tricky_players_parsing(): """See https://github.com/declension/squeeze-alexa/issues/93""" tricky_players = """serverstatus 0 99 lastscan%3A1536990512 version%3A7.9.1 uuid%3Aa6abbce0-edfe-447d-9c4b-2f132345733f mac%3A00%3A01%3A02%3A03%3A04%3A05 info%20total%20albums%3A107 info%20total%20artists%3A209 info%20total%20genres%3A13 info%20total%20songs%3A2151 info%20total%20duration%3A534087.510000002 player%20count%3A3 playerindex%3A0 playerid%3A00%3A01%3A02%3A03%3A04%3Ad1 uuid%3Ab1a5d6e01890c4c440d2da913233e622 ip%3A192.168.168.173%3A35566 name%3ACuisine seq_no%3A180 model%3Ababy modelname%3ASqueezebox%20Radio power%3A0 isplaying%3A0 displaytype%3Anone isplayer%3A1 canpoweroff%3A1 connected%3A1 firmware%3A7.7.3-r16676 playerindex%3A1 playerid%3A00%3A01%3A02%3A03%3A04%3Ab6 uuid%3Afeeaab78bf7d9d4773495e7112eefaff ip%3A192.168.168.134%3A34510 name%3AChambre seq_no%3A73 model%3Ababy modelname%3ASqueezebox%20Radio power%3A0 isplaying%3A0 displaytype%3Anone isplayer%3A1 canpoweroff%3A1 connected%3A1 firmware%3A7.7.3-r16676 playerindex%3A2 playerid%3A00%3A01%3A02%3A03%3A04%3Af6 uuid%3A8d470575086e09c3995a5fcb7a1667c8 ip%3A192.168.168.186%3A39845 name%3ASalon seq_no%3A17 model%3Afab4 modelname%3ASqueezebox%20Touch power%3A0 isplaying%3A0 displaytype%3Anone isplayer%3A1 canpoweroff%3A1 connected%3A1 firmware%3A7.8.0-r16754 sn%20player%20count%3A2 id%3A31579863 name%3ASqueezebox%20Radio%20Fanal playerid%3A00%3A01%3A02%3A03%3A04%3Ac2 model%3Ababy id%3A11266387 name%3ASqueezebox%20Radio%20Meme playerid%3A00%3A01%3A02%3A03%3A04%3A5a model%3Ababy other%20player%20count%3A0""".replace("\n", "") transport = FakeTransport(fake_server_status=tricky_players).start() server = Server(transport) server.refresh_status() assert len(server.player_names) == 3, "Should only have found 3 players" assert server.player_names == {'Cuisine', 'Chambre', 'Salon'}
def get_server(cls): """ :return a Server instance :rtype Server """ if not cls._server or cls._server.is_stale(): sslw = SslSocketWrapper(hostname=SERVER_HOSTNAME, port=SERVER_SSL_PORT, ca_file=CA_FILE_PATH, cert_file=CERT_FILE_PATH, verify_hostname=VERIFY_SERVER_HOSTNAME) cls._server = Server(sslw, user=SERVER_USERNAME, password=SERVER_PASSWORD, cur_player_id=DEFAULT_PLAYER, debug=DEBUG_LMS) print_d("Created %r" % cls._server) else: print_d("Reusing cached %r" % cls._server) return cls._server
def setUp(self): self.server = Server(transport=FakeTransport())
def test_login(self): self.server = Server(transport=self.transport, user='******', password='******') assert self.server.user == 'admin'
def test_unknown_default_player(self): transport = FakeTransport(fake_id="foo").start() self.server = Server(transport=transport, cur_player_id="GONE") assert self.server.cur_player_id == "foo"
def test_debug(self): Server(self.transport, debug=True)
def setUp(self): self.transport = FakeTransport().start() self.server = Server(transport=self.transport)
class TestServerWithFakeTransport(TestCase): def setUp(self): self.transport = FakeTransport().start() self.server = Server(transport=self.transport) def test_debug(self): Server(self.transport, debug=True) def test_unknown_default_player(self): transport = FakeTransport(fake_id="foo").start() self.server = Server(transport=transport, cur_player_id="GONE") assert self.server.cur_player_id == "foo" def test_status(self): assert self.server.get_milliseconds() == FAKE_LENGTH * 1000 def test_str(self): assert 'localhost:0' in str(self.server) def test_login(self): self.server = Server(transport=self.transport, user='******', password='******') assert self.server.user == 'admin' def test_groups(self): raw = """"something%3Afoobar playerid%3Aecho6fd1 uuid%3A ip%3A127.0.0.1%3A39365 name%3ALavf%20from%20echo6fd1 seq_no%3A0 model%3Ahttp power%3A1 isplaying%3A0 canpoweroff%3A0 connected%3A0 isplayer%3A1 sn%20player%20count%3A0 other%20player%20count%3A0""".replace('\n', '') groups = self.server._groups(raw, 'playerid', ['connected']) expected = { 'playerid': 'echo6fd1', 'uuid': None, 'ip': '127.0.0.1:39365', 'name': 'Lavf from echo6fd1', 'seq_no': 0, 'model': 'http', 'power': True, 'isplaying': False, 'canpoweroff': False, 'connected': False, 'isplayer': True } assert next(groups) == expected def test_groups_multiple(self): raw = """BLAH playerindex%3A0 playerid%3A00%3A04%3A20%3A17%3A6f%3Ad1 uuid%3A968b401ba4791d3fadd152bbac2f1dab ip%3A192.168.1.35%3A23238 name%3AUpstairs%20Music seq_no%3A0 model%3Areceiver modelname%3ASqueezebox%20Receiver power%3A0 isplaying%3A0 displaytype%3Anone isplayer%3A1 canpoweroff%3A1 connected%3A1 firmware%3A77 playerindex%3A2 playerid%3A40%3A16%3A7e%3Aad%3A87%3A07 uuid%3A ip%3A192.168.1.37%3A54652 name%3AStudy seq_no%3A0 model%3Asqueezelite modelname%3ASqueezeLite power%3A0 isplaying%3A0 displaytype%3Anone isplayer%3A1 canpoweroff%3A1 connected%3A1 firmware%3Av1.8 sn%20player%20count%3A0 other%20player%20count%3A0 """.replace('\n', '') groups = self.server._groups(raw, 'playerid') players = list(groups) assert len(players) == 2 first = players[0] assert first['playerid'] == "00:04:20:17:6f:d1" assert players[1]['name'] == "Study" for data in groups: assert 'playerid' in data def test_groups_dodgy(self): raw = "blah bar%3Abaz" groups = list(self.server._groups(raw, start_key="id")) assert not groups def test_groups_status(self): data = next(self.server._groups(A_REAL_STATUS)) assert data['player_name'] == 'Study' assert data['playlist_cur_index'] == 20 assert data['artist'] == 'Jamie Cullum' assert isinstance(data['can_seek'], bool) def test_faves(self): assert len(self.server.favorites) == 2 def test_playlists(self): assert len(self.server.playlists) == 0 def test_genres(self): assert len(self.server.genres) == 0 def test_change_volume(self): self.server.change_volume(3) assert "mixer volume +3" in self.transport.all_input def test_change_volume_zero(self): self.server.change_volume(0) assert "mixer volume" not in self.transport.all_input def test_track_details(self): details = self.server.get_track_details() assert ["Jamie Cullum"] == details['artist'] def test_disconnected_transport(self): self.transport.is_connected = False with raises(SqueezeboxException) as e: self.server._request(["foo"]) assert "Can't do 'foo'" in str(e) def test_disconnected_transport_player_request(self): self.transport.is_connected = False with raises(SqueezeboxException) as e: self.server.player_request("foo", player_id=A_PLAYER_ID) assert "Can't do 'foo'" in str(e)
def setUp(self): self.server = Server(ssl_wrap=FakeSsl())
def test_login(self): self.server = Server(ssl_wrap=FakeSsl(), user='******', password='******') assert self.server.user == 'admin'
def get_server(): return Server(create_transport(), user=LMS_SETTINGS.username, password=LMS_SETTINGS.password, cur_player_id=LMS_SETTINGS.default_player, debug=LMS_SETTINGS.debug)
def test_singleton(self): second = Server(transport=self.transport) assert second is self.server
def test_staleness_creates_new_instance(self): Server._CREATION_TIME = time.time() - Server._MAX_CACHE_SECS - 1 second = Server(transport=self.transport) assert second is not self.server