Esempio n. 1
0
class PairFunctionalTest(AioHTTPTestCase):
    def setUp(self):
        AioHTTPTestCase.setUp(self)
        self.pairing = None

        self.service = AirPlayService('airplay_id', port=self.server.port)
        self.conf = AppleTV('127.0.0.1', 'Apple TV')
        self.conf.add_service(self.service)

    async def tearDownAsync(self):
        await self.pairing.close()
        await super().tearDownAsync()

    async def get_application(self, loop=None):
        self.fake_atv = FakeAirPlayDevice(self)
        self.usecase = AirPlayUseCases(self.fake_atv)
        return self.fake_atv.app

    async def do_pairing(self, pin=DEVICE_PIN):
        self.usecase.airplay_require_authentication()

        self.pairing = await pair(self.conf, Protocol.AirPlay, self.loop)

        self.assertTrue(self.pairing.device_provides_pin)

        await self.pairing.begin()
        if pin:
            self.pairing.pin(pin)

        self.assertFalse(self.pairing.has_paired)

        await self.pairing.finish()
        self.assertTrue(self.pairing.has_paired)
        self.assertEqual(self.service.credentials, DEVICE_CREDENTIALS)

    @unittest_run_loop
    async def test_pairing_exception_invalid_pin(self):
        with self.assertRaises(exceptions.PairingError):
            await self.do_pairing(9999)

    @unittest_run_loop
    async def test_pairing_exception_no_pin(self):
        with self.assertRaises(exceptions.PairingError):
            await self.do_pairing(None)

    @unittest_run_loop
    @patch('os.urandom')
    async def test_pairing_with_device_new_credentials(self, rand_func):
        rand_func.side_effect = predetermined_key
        await self.do_pairing()

    @unittest_run_loop
    async def test_pairing_with_device_existing_credentials(self):
        self.conf.get_service(
            Protocol.AirPlay).credentials = DEVICE_CREDENTIALS
        await self.do_pairing()
Esempio n. 2
0
class AirPlayPlayerTest(AioHTTPTestCase):
    def setUp(self):
        AioHTTPTestCase.setUp(self)

        # This is a hack that overrides asyncio.sleep to avoid making the test
        # slow. It also counts number of calls, since this is quite important
        # to the general function.
        player.asyncio.sleep = self.fake_asyncio_sleep
        self.no_of_sleeps = 0

    async def get_application(self, loop=None):
        self.fake_device = FakeAirPlayDevice(self)
        self.usecase = AirPlayUseCases(self.fake_device)
        return self.fake_device

    async def fake_asyncio_sleep(self, time, loop):
        self.no_of_sleeps += 1

    @unittest_run_loop
    async def test_play_video(self):
        self.usecase.airplay_playback_idle()
        self.usecase.airplay_playback_playing()
        self.usecase.airplay_playback_idle()

        session = ClientSession(loop=self.loop)
        aplay = player.AirPlayPlayer(self.loop,
                                     session,
                                     '127.0.0.1',
                                     port=self.server.port)
        await aplay.play_url(STREAM, position=START_POSITION)

        self.assertEqual(self.fake_device.last_airplay_url, STREAM)
        self.assertEqual(self.fake_device.last_airplay_start, START_POSITION)
        self.assertEqual(self.no_of_sleeps, 2)  # playback + idle = 3

        await session.close()

    @unittest_run_loop
    async def test_play_video_no_permission(self):
        self.usecase.airplay_playback_playing_no_permission()

        session = ClientSession(loop=self.loop)
        aplay = player.AirPlayPlayer(self.loop,
                                     session,
                                     '127.0.0.1',
                                     port=self.server.port)

        with self.assertRaises(exceptions.NoCredentialsError):
            await aplay.play_url(STREAM, position=START_POSITION)
Esempio n. 3
0
 async def get_application(self, loop=None):
     self.fake_device = FakeAirPlayDevice(self)
     self.usecase = AirPlayUseCases(self.fake_device)
     return self.fake_device.app
Esempio n. 4
0
class AirPlayPlayerTest(AioHTTPTestCase):
    async def setUpAsync(self):
        await AioHTTPTestCase.setUpAsync(self)

        # This is a hack that overrides asyncio.sleep to avoid making the test
        # slow. It also counts number of calls, since this is quite important
        # to the general function.
        player.asyncio.sleep = self.fake_asyncio_sleep
        self.no_of_sleeps = 0

        self.session = ClientSession(loop=self.loop)
        http = net.HttpSession(
            self.session, 'http://127.0.0.1:{0}/'.format(self.server.port))
        self.player = player.AirPlayPlayer(self.loop, http)

    async def tearDownAsync(self):
        await AioHTTPTestCase.tearDownAsync(self)
        await self.session.close()

    async def get_application(self, loop=None):
        self.fake_device = FakeAirPlayDevice(self)
        self.usecase = AirPlayUseCases(self.fake_device)
        return self.fake_device.app

    async def fake_asyncio_sleep(self, time, loop=None):
        self.no_of_sleeps += 1

    @unittest_run_loop
    async def test_play_video(self):
        self.usecase.airplay_playback_idle()
        self.usecase.airplay_playback_playing()
        self.usecase.airplay_playback_idle()

        await self.player.play_url(STREAM, position=START_POSITION)

        self.assertEqual(self.fake_device.last_airplay_url, STREAM)
        self.assertEqual(self.fake_device.last_airplay_start, START_POSITION)
        self.assertIsNotNone(self.fake_device.last_airplay_uuid)
        self.assertEqual(self.no_of_sleeps, 2)  # playback + idle = 3

    @unittest_run_loop
    async def test_play_video_no_permission(self):
        self.usecase.airplay_playback_playing_no_permission()

        with self.assertRaises(exceptions.NoCredentialsError):
            await self.player.play_url(STREAM, position=START_POSITION)

    @unittest_run_loop
    async def test_play_with_retries(self):
        self.usecase.airplay_play_failure(2)
        self.usecase.airplay_playback_playing()
        self.usecase.airplay_playback_idle()

        await self.player.play_url(STREAM, position=START_POSITION)

        self.assertEqual(self.fake_device.play_count,
                         3)  # Two retries + success

    @unittest_run_loop
    async def test_play_with_too_many_retries(self):
        self.usecase.airplay_play_failure(10)
        self.usecase.airplay_playback_playing()
        self.usecase.airplay_playback_idle()

        with self.assertRaises(exceptions.PlaybackError):
            await self.player.play_url(STREAM, position=START_POSITION)
Esempio n. 5
0
 async def get_application(self, loop=None):
     self.fake_atv = FakeAirPlayDevice()
     self.usecase = AirPlayUseCases(self.fake_atv)
     return self.fake_atv.app