def test_post_api_stream(self): API_STREAM = Garena.API_STREAM schema = Garena._stream_schema api_data = { "reply": { "streams": [ { "url": "https://test.se/stream1", "bitrate": 0, "resolution": 1080, "format": 3 }, ] }, "result": "success" } api_resp = Mock() api_resp.text = json.dumps(api_data) self.session.http.post.return_value = api_resp self.session.http.json.return_value = api_data payload = {"channel_id": 358220} Garena.bind(self.session, "test.garena") plugin = Garena("https://garena.live/358220") stream_data = plugin._post_api(API_STREAM, payload, schema) self.assertEqual(stream_data["streams"], api_data["reply"]["streams"]) self.session.http.post.assert_called_with(API_STREAM, json=dict(channel_id=358220))
def test_get_streams(self, hlsstream, mock_get_stream_data): mock_get_stream_data.return_value = { "stream": "http://test.se/stream1" } page_resp = Mock() page_resp.text = u""" <div class="video-js theoplayer-skin theo-seekbar-above-controls content-box vjs-fluid" data-resource= "bbcone" data-token = "1324567894561268987948596154656418448489159" data-content-type="live" data-environment="live" data-subscription="free" data-channel-id="89"> <div id="channel-info" class="channel-info"> <div class="row visible-xs visible-sm"> """ self.session.http.get.return_value = page_resp hlsstream.parse_variant_playlist.return_value = {"test": HLSStream(self.session, "http://test.se/stream1")} TVPlayer.bind(self.session, "test.tvplayer") plugin = TVPlayer("http://tvplayer.com/watch/dave") streams = plugin.streams() self.assertTrue("test" in streams) # test the url is used correctly self.session.http.get.assert_called_with("http://tvplayer.com/watch/dave") # test that the correct API call is made mock_get_stream_data.assert_called_with(resource="bbcone", channel_id="89", token="1324567894561268987948596154656418448489159") # test that the correct URL is used for the HLSStream hlsstream.parse_variant_playlist.assert_called_with(ANY, "http://test.se/stream1")
def test_post_api_info(self): API_INFO = Garena.API_INFO schema = Garena._info_schema api_data = { "reply": { "channel_id": 358220, }, "result": "success" } api_resp = Mock() api_resp.text = json.dumps(api_data) self.session.http.post.return_value = api_resp self.session.http.json.return_value = api_data payload = {"alias": "LOLTW"} Garena.bind(self.session, "test.garena") plugin = Garena("https://garena.live/LOLTW") info_data = plugin._post_api(API_INFO, payload, schema) self.assertEqual(info_data["channel_id"], 358220) self.session.http.post.assert_called_with(API_INFO, json=dict(alias="LOLTW"))
def test_log(self, mock_enable_trace): # type: (Mock) with patch("streamlink.plugin.api.websocket.rootlogger", Mock(level=DEBUG)): WebsocketClient(self.session, "wss://localhost:0") self.assertFalse(mock_enable_trace.called) with patch("streamlink.plugin.api.websocket.rootlogger", Mock(level=TRACE)): WebsocketClient(self.session, "wss://localhost:0") self.assertTrue(mock_enable_trace.called)
def test_stream_open_video_only(self, muxer, reader): stream = DASHStream(self.session, Mock(), Mock(id=1, mimeType="video/mp4")) open_reader = reader.return_value = Mock() stream.open() reader.assert_called_with(stream, 1, "video/mp4") open_reader.open.assert_called_with() muxer.assert_not_called()
def test_parse_manifest_drm(self, mpdClass): mpd = mpdClass.return_value = Mock( periods=[Mock(adaptationSets=[Mock(contentProtection="DRM")])]) self.assertRaises(PluginError, DASHStream.parse_manifest, self.session, self.test_url) mpdClass.assert_called_with(ANY, base_url="http://test.bar", url="http://test.bar/foo.mpd")
def test_create_output_record_and_other_file_output(self): streamlink_cli.main.console = console = Mock() streamlink_cli.main.args = args = Mock() console.exit = Mock() args.output = None args.stdout = True args.record_and_pipe = True create_output(FakePlugin) console.exit.assert_called_with( "Cannot use record options with other file output options.")
def test_check_file_output_exists_notty(self): tmpfile = tempfile.NamedTemporaryFile() try: streamlink_cli.main.console = Mock() streamlink_cli.main.sys.stdin = stdin = Mock() stdin.isatty.return_value = False self.assertTrue(os.path.exists(tmpfile.name)) self.assertRaises(SystemExit, check_file_output, tmpfile.name, False) finally: tmpfile.close()
def test_create_output_no_file_output_options(self): streamlink_cli.main.console = Mock() streamlink_cli.main.args = args = Mock() args.output = None args.stdout = None args.record = None args.record_and_pipe = None args.title = None args.player = "mpv" self.assertIsInstance(create_output(FakePlugin), PlayerOutput)
def test_create_output_file_output(self): tmpfile = tempfile.NamedTemporaryFile() try: streamlink_cli.main.args = args = Mock() streamlink_cli.main.console = Mock() args.output = tmpfile.name self.assertTrue(os.path.exists(tmpfile.name)) self.assertIsInstance(create_output(FakePlugin), FileOutput) finally: tmpfile.close()
def test_check_file_output_exists(self): tmpfile = tempfile.NamedTemporaryFile() try: streamlink_cli.main.console = console = Mock() streamlink_cli.main.sys.stdin = stdin = Mock() stdin.isatty.return_value = True console.ask.return_value = "y" self.assertTrue(os.path.exists(tmpfile.name)) self.assertIsInstance(check_file_output(tmpfile.name, False), FileOutput) finally: tmpfile.close()
def test_create_output_record_and_pipe(self): tmpfile = tempfile.NamedTemporaryFile() try: streamlink_cli.main.console = Mock() streamlink_cli.main.args = args = Mock() args.output = None args.stdout = None args.record_and_pipe = tmpfile.name self.assertIsInstance(create_output(FakePlugin), FileOutput) finally: tmpfile.close()
def test_run_task_with_requirements(self): f = Mock() f2 = Mock() task = Task("fooqaaz") task.requires("foobar") task << Chute.create(f)() task2 = Task("foobar") task2 << Chute.create(f2)() Hellbox.add_task(task) Hellbox.add_task(task2) Hellbox.run_task("fooqaaz") assert f2.called
def testOnRegisteredAck(self): registeredHandler = Mock() timerProvider = Mock() registerAck = SCCPRegisterAck() registerAck.keepAliveInterval = 25 self.sccpPhone.setTimerProvider(timerProvider) self.sccpPhone.setRegisteredHandler(registeredHandler) self.sccpPhone.onRegisteredAck(registerAck) timerProvider.createTimer.assert_called_with( 25, self.sccpPhone.onKeepAliveTimer) registeredHandler.onRegistered.assert_called_with()
def test_stream_open_video_audio(self, muxer, reader): stream = DASHStream(self.session, Mock(), Mock(id=1, mimeType="video/mp4"), Mock(id=2, mimeType="audio/mp3", lang='en')) open_reader = reader.return_value = Mock() stream.open() self.assertSequenceEqual(reader.mock_calls, [call(stream, 1, "video/mp4"), call().open(), call(stream, 2, "audio/mp3"), call().open()]) self.assertSequenceEqual(muxer.mock_calls, [call(self.session, open_reader, open_reader, copyts=True), call().open()])
def test_stream_open_video_audio(self, muxer, reader): stream = DASHStream(self.session, Mock(), Mock(id=1), Mock(id=2)) open_reader = reader.return_value = Mock() stream.open() self.assertSequenceEqual(reader.mock_calls, [call(stream, 1), call().open(), call(stream, 2), call().open()]) self.assertSequenceEqual(muxer.mock_calls, [call(self.session, open_reader, open_reader, copyts=True), call().open()])
class TestNamedPipe(unittest.TestCase): @patch("streamlink.utils.named_pipe._id", 0) @patch("streamlink.utils.named_pipe.os.getpid", Mock(return_value=12345)) @patch("streamlink.utils.named_pipe.random.randint", Mock(return_value=67890)) @patch("streamlink.utils.named_pipe.NamedPipe._create", Mock(return_value=None)) @patch("streamlink.utils.named_pipe.log") def test_name(self, mock_log): NamedPipe() NamedPipe() self.assertEqual(mock_log.info.mock_calls, [ call("Creating pipe streamlinkpipe-12345-1-67890"), call("Creating pipe streamlinkpipe-12345-2-67890") ])
def test_cookie_store_load(self): session = Mock() session.http.cookies = requests.cookies.RequestsCookieJar() Plugin.bind(session, 'tests.test_plugin') Plugin.cache = Mock() Plugin.cache.get_all.return_value = { "__cookie:test-name:test.se:80:/": self._create_cookie_dict("test-name", "test-value", None) } plugin = Plugin("http://test.se") self.assertSequenceEqual( list(map(self._cookie_to_dict, session.http.cookies)), [self._cookie_to_dict(requests.cookies.create_cookie("test-name", "test-value", domain="test.se"))] )
def test_find_missing_task(self): task = Hellbox.find_task_by_name("bazzio") Hellbox._warn = Hellbox.warn Hellbox.warn = Mock() task.run() assert Hellbox.warn.called Hellbox.warn = Hellbox._warn
def testOnKeepAliveTimer(self): networkClient = Mock() self.sccpPhone.client = networkClient keepaliveMessage = SCCPMessage(SCCPMessageType.KeepAliveMessage) self.sccpPhone.onKeepAliveTimer() networkClient.sendSccpMessage.assert_called_with(keepaliveMessage)
def test_resolve_stream_name(self): high = Mock() medium = Mock() low = Mock() streams = { "low": low, "medium": medium, "high": high, "worst": low, "best": high } self.assertEqual("high", resolve_stream_name(streams, "best")) self.assertEqual("low", resolve_stream_name(streams, "worst")) self.assertEqual("medium", resolve_stream_name(streams, "medium")) self.assertEqual("high", resolve_stream_name(streams, "high")) self.assertEqual("low", resolve_stream_name(streams, "low"))
def test_create_output_record(self): tmpfile = tempfile.NamedTemporaryFile() try: streamlink_cli.main.console = Mock() streamlink_cli.main.args = args = Mock() args.output = None args.stdout = None args.record = tmpfile.name args.record_and_pipe = None args.title = None args.player = "mpv" args.player_args = "" args.player_fifo = None self.assertIsInstance(create_output(FakePlugin), PlayerOutput) finally: tmpfile.close()
def test_run_task(self): f = Mock() task = Task("foobaz") task << Chute.create(f)() Hellbox.add_task(task) Hellbox.run_task("foobaz") assert f.called
class TestCLIMainOutputStream(unittest.TestCase): @patch("streamlink_cli.main.args", Mock(retry_open=2)) @patch("streamlink_cli.main.log") @patch("streamlink_cli.main.console") def test_stream_failure_no_output_open(self, mock_console, mock_log): # type: (Mock, Mock) output = Mock() stream = Mock(__str__=lambda _: "fake-stream", open=Mock(side_effect=StreamError("failure"))) formatter = Formatter({}) with patch("streamlink_cli.main.output", Mock()), \ patch("streamlink_cli.main.create_output", return_value=output): output_stream(formatter, stream, True) self.assertEqual(mock_log.error.call_args_list, [ call( "Try 1/2: Could not open stream fake-stream (Could not open stream: failure)" ), call( "Try 2/2: Could not open stream fake-stream (Could not open stream: failure)" ), ]) self.assertEqual(mock_console.exit.call_args_list, [ call("Could not open stream fake-stream, tried 2 times, exiting") ]) self.assertFalse(output.open.called, "Does not open the output on stream error")
def testOnDialPadStarButtonPushed(self): networkClient = Mock() self.sccpPhone.client = networkClient dialPadMessage = SCCPKeyPadButton(14) self.sccpPhone.onDialPadButtonPushed('*') networkClient.sendSccpMessage.assert_called_with(dialPadMessage)
def test_priority(url, priority): session = Mock() MPEGDASH.bind(session, "tests.plugins.test_dash") assert next( (matcher.priority for matcher in MPEGDASH.matchers if matcher.pattern.match(url)), NO_PRIORITY) == priority
def test_run(self): f = Mock() task = Task("foo") task << Chute.create(f)() task.run() assert f.called assert f.args == ([],)
def test_cookie_store_save(self): session = Mock() session.http.cookies = [ requests.cookies.create_cookie("test-name", "test-value", domain="test.se") ] Plugin.bind(session, 'tests.test_plugin') Plugin.cache = Mock() Plugin.cache.get_all.return_value = {} plugin = Plugin("http://test.se") plugin.save_cookies(default_expires=3600) Plugin.cache.set.assert_called_with("__cookie:test-name:test.se:80:/", self._create_cookie_dict("test-name", "test-value", None), 3600)
def testOnSoftKeyNewCall(self): networkClient = Mock() self.sccpPhone.client = networkClient newCallMessage = SCCPSoftKeyEvent(SKINNY_LBL_NEWCALL) self.sccpPhone.onSoftKey(SKINNY_LBL_NEWCALL) networkClient.sendSccpMessage.assert_was_called_with(newCallMessage)
def testOnConnectSuccess(self): networkClient = Mock() self.sccpPhone.client = networkClient self.sccpPhone.on_sccp_connect_success() registerMessage = SCCPRegister('SEP001166554433', "1.1.1.1") networkClient.sendSccpMessage.assert_called_with(registerMessage)
def test_set_defaults(self): session = Mock() plugin = Mock() parser = Mock() session.plugins = {"mock": plugin} plugin.arguments = Arguments( Argument("test1", default="default1"), Argument("test2", default="default2"), Argument("test3") ) setup_plugin_args(session, parser) self.assertEqual(plugin.options.get("test1"), "default1") self.assertEqual(plugin.options.get("test2"), "default2") self.assertEqual(plugin.options.get("test3"), None)
def test_check_file_output_exists_force(self): tmpfile = tempfile.NamedTemporaryFile() try: streamlink_cli.main.console = Mock() self.assertTrue(os.path.exists(tmpfile.name)) self.assertIsInstance(check_file_output(tmpfile.name, True), FileOutput) finally: tmpfile.close()
def test_static(self, sleep): reader = MagicMock() worker = DASHStreamWorker(reader) reader.representation_id = 1 representation = Mock(id=1, mimeType="video/mp4", height=720) segments = [Mock(url="init_segment"), Mock(url="first_segment"), Mock(url="second_segment")] representation.segments.return_value = [segments[0]] worker.mpd = Mock(dynamic=False, publishTime=1, periods=[ Mock(adaptationSets=[ Mock(contentProtection=None, representations=[ representation ]) ]) ]) worker.mpd.type = "static" worker.mpd.minimumUpdatePeriod.total_seconds.return_value = 0 worker.mpd.periods[0].duration.total_seconds.return_value = 0 representation.segments.return_value = segments self.assertSequenceEqual(list(worker.iter_segments()), segments) representation.segments.assert_called_with(init=True)
def test_dynamic_reload(self, mpdClass, sleep): reader = MagicMock() worker = DASHStreamWorker(reader) reader.representation_id = 1 representation = Mock(id=1, mimeType="video/mp4", height=720) segments = [Mock(url="init_segment"), Mock(url="first_segment"), Mock(url="second_segment")] representation.segments.return_value = [segments[0]] mpdClass.return_value = worker.mpd = Mock(dynamic=True, publishTime=1, periods=[ Mock(adaptationSets=[ Mock(contentProtection=None, representations=[ representation ]) ]) ]) worker.mpd.type = "dynamic" worker.mpd.minimumUpdatePeriod.total_seconds.return_value = 0 worker.mpd.periods[0].duration.total_seconds.return_value = 0 segment_iter = worker.iter_segments() representation.segments.return_value = segments[:1] self.assertEqual(next(segment_iter), segments[0]) representation.segments.assert_called_with(init=True) representation.segments.return_value = segments[1:] self.assertSequenceEqual([next(segment_iter), next(segment_iter)], segments[1:]) representation.segments.assert_called_with(init=False)
def test_get_invalid_page(self): page_resp = Mock() page_resp.text = u""" var validate = "foo"; var resourceId = "1234"; """ self.session.http.get.return_value = page_resp TVPlayer.bind(self.session, "test.tvplayer") plugin = TVPlayer("http://tvplayer.com/watch/dave") streams = plugin.streams() self.assertEqual({}, streams) # test the url is used correctly self.session.http.get.assert_called_with("http://tvplayer.com/watch/dave")