def test_play_video(self, mock_listitem, mock_ticket): escaped_auth_url = re.escape( config.AUTH_URL).replace('\\{', '{').replace('\\}', '}') auth_url = re.compile(escaped_auth_url.format('.*', '.*', '.*')) responses.add(responses.GET, auth_url, body=self.AUTH_JSON, status=200) escaped_embed_url = re.escape( config.EMBED_TOKEN_URL).replace('\\{', '{').replace('\\}', '}') embed_url = re.compile(escaped_embed_url.format('.*')) responses.add(responses.GET, embed_url, body=self.EMBED_TOKEN_XML, status=200) escaped_stream_url = re.escape( config.STREAM_API_URL).replace('\\{', '{').replace('\\}', '}') escaped_stream_url = escaped_stream_url.replace('\\_', '_') stream_url = re.compile(escaped_stream_url.format(video_id='.*')) responses.add(responses.GET, stream_url, body=self.STREAM_API_JSON, status=200) responses.add(responses.GET, config.MEDIA_AUTH_URL.format(embed_code='123456'), body=self.VIDEOTOKEN_JSON) mock_ticket.return_value = 'foobar123456' mock_listitem.side_effect = fakes.FakeListItem params = dict(parse_qsl(sys.argv[2][1:])) mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.play as play play.play_video(params) self.assertEqual(fakes.M3U8_URL.decode(), mock_plugin.resolved[2].getPath())
def test_list_matches_live(self, mock_listitem): responses.add(responses.GET, config.HOME_URL, body=self.HOME_BOX_XML, status=200) escaped_box_url = re.escape(config.BOX_URL).replace('\\{', '{').replace( '\\}', '}') box_url = re.compile(escaped_box_url.format('.*')) responses.add(responses.GET, box_url, body=self.BOX_XML, status=200) responses.add(responses.GET, config.SCORE_URL, body=self.SCORE_XML, status=200) mock_listitem.side_effect = fakes.FakeListItem params = dict(parse_qsl(sys.argv[2][1:])) mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.menu as menu menu.list_matches(params, live=True) for index, expected in enumerate(fakes.EXPECTED_LIVE_TITLES): url = mock_plugin.directory[index].get('url') url_query = dict(parse_qsl(urlparse(url)[4])) observed = url_query.get('title') self.assertEqual(expected, observed)
def test_make_seasons_list(self, mock_listitem): responses.add(responses.POST, config.TOKEN_URL, body=json.dumps({'token': 'abcdef'}), status=200) responses.add(responses.GET, config.SEASONS_URL, body=self.SEASONS_JSON, status=200) mock_listitem.side_effect = fakes.FakeListItem mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.index as index index.make_seasons_list() expected_url = 'plugin://{addonid}/?{params}'.format( addonid='plugin.video.afl-video', params=unquote_plus( urlencode({ 'season': 'CD_S2020014', 'current_round': 'CD_R202001401', 'name': 'AFL Premiership 2020' }))) observed_url = mock_plugin.directory[0].get('url') expected = urlparse(expected_url) observed = urlparse(observed_url) for x in range(6): if x == 4: self.assertEqual(dict(parse_qsl(expected[x])), dict(parse_qsl(observed[x]))) else: self.assertEqual(expected[x], observed[x])
def test_play_video_live(self, mock_listitem, mock_ticket): params = dict(parse_qsl(sys.argv[2][1:])) escaped_bc_url = re.escape(config.BC_URL).replace('\\{', '{').replace( '\\}', '}') bc_url = re.compile(escaped_bc_url.format('.*', '.*')) responses.add(responses.GET, bc_url, body=self.BC_EDGE_JSON, status=200) responses.add(responses.GET, config.MEDIA_AUTH_URL.format(video_id='6112170884001'), body=self.VIDEO_TOKEN_JSON, status=200) responses.add(responses.GET, config.SIGN_URL.format( quote_plus('https://foo.bar/video.m3u8')), body=self.SIGN_JSON, status=200) mock_ticket.return_value = json.dumps({ 'pai': fakes.FAKE_UUID[0], 'bearer': 'abc123' }) mock_listitem.side_effect = fakes.FakeListItem mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.play as play play.play_video(params) self.assertEqual('https://foo.bar/index.m3u8?signed', mock_plugin.resolved[2].getPath())
def setUp(self): super(SearchTests, self).setUp() self.mock_plugin = fakes.FakePlugin() self.patcher = mock.patch.dict('sys.modules', xbmcplugin=self.mock_plugin) self.patcher.start() self.addCleanup(self.patcher.stop) global search search = importlib.import_module('resources.lib.search')
def setUp(self): super(CategoriesTests, self).setUp() self.mock_plugin = fakes.FakePlugin() self.patcher = mock.patch.dict('sys.modules', xbmcplugin=self.mock_plugin) self.patcher.start() self.addCleanup(self.patcher.stop) global categories categories = importlib.import_module('resources.lib.categories')
def test_play(self, mock_listitem, mock_token, mock_version): mock_version.return_value = 18 mock_token.return_value = 'foo' mock_plugin = fakes.FakePlugin() mock_listitem.side_effect = fakes.FakeListItem url = re.compile('^https://www.sbs.com.au/api/v3/video_stream') responses.add(responses.GET, url, body=self.VIDEO_STREAM_JSON) with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.play as play play.play(sys.argv[2][1:]) expected = 'New Girl - S02 - Re-Launch' observed = mock_plugin.resolved[2].getLabel() self.assertEqual(expected, observed)
def test_make_search_history_list(self, mock_listitem, mock_version, mock_history): mock_history.return_value = ['Foo', 'Bar', 'foobar'] mock_version.return_value = 18 mock_plugin = fakes.FakePlugin() mock_listitem.side_effect = fakes.FakeListItem with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.index as index index.make_search_history_list() observed = [] for res in mock_plugin.directory: observed.append(res.get('listitem').getLabel()) self.assertEqual(['New Search', 'Foo', 'Bar', 'foobar'], observed)
def test_make_favourites_categories_list( self, mock_listitem, mock_version, mock_favourites): mock_favourites.return_value = json.loads(self.VIDEO_FAV_ALL_JSON) mock_version.return_value = 18 mock_plugin = fakes.FakePlugin() mock_listitem.side_effect = fakes.FakeListItem responses.add(responses.GET, config.CONFIG_URL, body=self.VIDEO_CONFIG_JSON) with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.index as index index.make_favourites_categories_list() observed = [] for res in mock_plugin.directory: observed.append(res.get('listitem').getLabel()) self.assertEqual(['Programs'], observed)
def test_make_rounds(self, mock_listitem): responses.add(responses.POST, config.TOKEN_URL, body=json.dumps({'token': 'abcdef'}), status=200) responses.add(responses.GET, config.SEASONS_URL, body=self.SEASONS_JSON, status=200) mock_listitem.side_effect = fakes.FakeListItem mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.rounds as rounds rounds.make_rounds({'season': 'CD_S2020014'}) self.assertEqual(23, len(mock_plugin.directory))
def test_list_videos_listitem_labels(self, mock_listitem): responses.add(responses.GET, config.VIDEO_URL, body=self.VIDEO_XML, status=200) mock_listitem.side_effect = fakes.FakeListItem params = dict(parse_qsl(sys.argv[2][1:])) mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.menu as menu menu.list_videos(params) for index, expected in enumerate(fakes.EXPECTED_VIDEO_TITLES): li = mock_plugin.directory[index].get('listitem') self.assertEqual(expected, li.getLabel())
def test_make_category_list(self, mock_listitem, mock_version): mock_version.return_value = 18 mock_plugin = fakes.FakePlugin() mock_listitem.side_effect = fakes.FakeListItem responses.add(responses.GET, config.CONFIG_URL, body=self.VIDEO_CONFIG_JSON) with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.index as index params = index.utils.get_url(sys.argv[2][1:]) index.make_category_list(params) observed = [] for res in mock_plugin.directory: observed.append(res.get('listitem').getLabel()) self.assertEqual(18, len(observed)) self.assertIn('Spy Series', observed)
def test_make_index_list_not_logged_in(self, mock_listitem, mock_version): mock_version.return_value = 18 mock_plugin = fakes.FakePlugin() mock_listitem.side_effect = fakes.FakeListItem responses.add(responses.GET, config.CONFIG_URL, body=self.VIDEO_CONFIG_JSON) with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.index as index index.make_index_list() observed = [] expected = ['Featured', 'Programs', 'Movies', 'Catchup', 'Search', 'Favourites', 'Settings', 'Login'] for res in mock_plugin.directory: observed.append(res.get('listitem').getLabel()) self.assertEqual(expected, observed)
def setUp(self): super(DefaultTests, self).setUp() self.mock_plugin = fakes.FakePlugin() self.patcher = mock.patch.dict('sys.modules', xbmcplugin=self.mock_plugin) self.patcher.start() self.addCleanup(self.patcher.stop) for module in ['index', 'play']: setattr( self, module, importlib.import_module('resources.lib.{0}'.format(module))) self.assertEqual(self.mock_plugin, getattr(self, module).xbmcplugin) global default global classes default = importlib.import_module('default') classes = importlib.import_module('resources.lib.classes')
def test_make_entries_list(self, mock_listitem, mock_version): mock_version.return_value = 18 mock_plugin = fakes.FakePlugin() mock_listitem.side_effect = fakes.FakeListItem feed_url = 'http://foo.bar/api/v3/video_feed&range=1-50' responses.add(responses.GET, feed_url, body=self.VIDEO_FEED_JSON) with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.index as index params = index.utils.get_url(sys.argv[2][1:]) index.make_entries_list(params) observed = [] for res in mock_plugin.directory: observed.append(res.get('listitem').getLabel()) self.assertEqual(18, len(observed)) self.assertIn('New Girl - S02E12 - Cabin', observed)
def test_make_search_list(self, mock_listitem, mock_version): mock_version.return_value = 18 mock_plugin = fakes.FakePlugin() mock_listitem.side_effect = fakes.FakeListItem feed_url = re.compile('https://www.sbs.com.au/api/v3/video_search') responses.add(responses.GET, config.CONFIG_URL, body=self.VIDEO_CONFIG_JSON) responses.add(responses.GET, feed_url, body=self.VIDEO_SEARCH_MOVIES_JSON) with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.index as index params = index.utils.get_url(sys.argv[2][1:]) index.make_search_list(params) observed = [] for res in mock_plugin.directory: observed.append(res.get('listitem').getLabel()) self.assertEqual(['Live (3)', 'Programs (3)', 'Movies (3)', 'Clips (3)', 'Episodes (3)'], observed)
def test_make_list(self, mock_listitem): mock_listitem.side_effect = fakes.FakeListItem mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.index as index index.make_list() for ind, category in enumerate(config.CATEGORIES): expected_url = 'plugin://{addonid}/?{params}'.format( addonid='plugin.video.afl-video', params=unquote_plus(urlencode({'category': category}))) observed_url = mock_plugin.directory[ind].get('url') expected = urlparse(expected_url) observed = urlparse(observed_url) for x in range(6): if x == 4: self.assertEqual(dict(parse_qsl(expected[x])), dict(parse_qsl(observed[x]))) else: self.assertEqual(expected[x], observed[x])
def test_make_matches_list_live(self, mock_listitem): mock_listitem.side_effect = fakes.FakeListItem mock_plugin = fakes.FakePlugin() responses.add(responses.GET, config.SCORE_URL.format(mode='INTERNATIONAL'), body=self.SCORE_INTERNATIONAL_XML, status=200) responses.add(responses.GET, config.SCORE_URL.format(mode='SUPER_NETBALL'), body=self.SCORE_SUPER_XML, status=200) responses.add(responses.GET, config.BOX_URL.format('107250301'), body=self.BOX_XML, status=200) responses.add(responses.GET, config.LIVE_MEDIA_URL.format('107250301'), body=self.VIDEO_XML, status=200) for mode in ['SUPER_NETBALL', 'INTERNATIONAL']: responses.add(responses.GET, config.INDEX_URL.format(mode=mode), body=self.INDEX_XML, status=200) with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.matches as matches matches.make_matches_list({'category': 'livematches'}) expected_title = ('[COLOR green][LIVE NOW][/COLOR] Sunshine Coast ' 'Lightning v NSW Swifts [COLOR yellow]53 - ' '46[/COLOR]') expected = fakes.FakeListItem(expected_title) expected.setInfo('video', { 'plot': expected_title, 'plotoutline': expected_title }) expected.setProperty('IsPlayable', 'true') observed = mock_plugin.directory[0].get('listitem') for attrib in vars(observed): self.assertEqual(getattr(expected, attrib), getattr(observed, attrib))
def test_play_video_bc(self, mock_listitem, mock_drm): mock_drm.return_value = True responses.add(responses.POST, config.TOKEN_URL, body=json.dumps({'token': 'abcdef'}), status=200) edge_url = config.BC_EDGE_URL.format(account_id='foo', video_id='bar') responses.add(responses.GET, edge_url, body=self.BC_EDGE_JSON, status=200) responses.add(responses.GET, config.CONFIG_URL, body=self.CONFIG_JSON, status=200) mock_listitem.side_effect = fakes.FakeListItem mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.play as play play.play(sys.argv[2][1:]) self.assertEqual(fakes.M3U8_URL_BC.get('stream_url'), mock_plugin.resolved[2].getPath())
def test_play_video_live(self, mock_listitem, mock_ticket, mock_drm, mock_sub_type): escaped_auth_url = re.escape(config.AUTH_URL).replace('\\{', '{').replace( '\\}', '}') auth_url = re.compile(escaped_auth_url.format('.*', '.*', '.*')) responses.add(responses.GET, auth_url, body=self.AUTH_JSON, status=200) escaped_embed_url = re.escape(config.EMBED_TOKEN_URL).replace( '\\{', '{').replace('\\}', '}') embed_url = re.compile(escaped_embed_url.format('.*', '.*')) responses.add(responses.GET, embed_url, body=self.EMBED_TOKEN_JSON, status=200) responses.add(responses.POST, config.TOKEN_URL, body=json.dumps({'token': 'abcdef'}), status=200) responses.add(responses.GET, config.CONFIG_URL, body=self.CONFIG_JSON, status=200) edge_url = config.BC_EDGE_URL.format(account_id='foo', video_id='bar') responses.add(responses.GET, edge_url, body=self.BC_EDGE_JSON, status=200) mock_ticket.return_value = 'foobar123456' mock_listitem.side_effect = fakes.FakeListItem mock_drm.return_value = False mock_sub_type.return_value = '0' mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.play as play play.play(sys.argv[2][1:]) self.assertEqual(fakes.M3U8_URL_BC.get('stream_url'), mock_plugin.resolved[2].getPath())
def test_make_entries_list_context_items( self, mock_listitem, mock_version, mock_addon): mock_addon.return_value = fakes.FakeAddon(user_token='foo') mock_version.return_value = 18 mock_plugin = fakes.FakePlugin() mock_listitem.side_effect = fakes.FakeListItem feed_url = 'http://foo.bar/api/v3/video_program&range=1-50' responses.add(responses.GET, feed_url, body=self.VIDEO_PROGRAM_COLLECTION_JSON) with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.index as index params = index.utils.get_url(sys.argv[2][1:]) index.make_entries_list(params) observed = [] for res in mock_plugin.directory: if res.get('listitem').context_items: observed.extend(res.get('listitem').context_items) self.assertEqual(27, len(observed)) seen = False for item in observed: if '3236' in item[1]: seen = True self.assertIs(True, seen)
def test_make_matches_list(self, mock_listitem): mock_listitem.side_effect = fakes.FakeListItem mock_plugin = fakes.FakePlugin() for mode in ['SUPER_NETBALL', 'INTERNATIONAL']: responses.add(responses.GET, config.TAGGEDLIST_REPLAY_URL.format(mode=mode), body=self.TAGGEDLIST_REPLAY_XML, status=200) with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.matches as matches matches.make_matches_list({'category': 'Match Replays'}) expected_title = '2019 Grand Final: Lightning v Swifts (Replay)' expected = fakes.FakeListItem(expected_title) expected.setArt({'icon': 'example.jpg', 'thumb': 'example.jpg'}) expected.setInfo('video', { 'plot': expected_title, 'plotoutline': expected_title }) expected.setProperty('IsPlayable', 'true') observed = mock_plugin.directory[0].get('listitem') for attrib in vars(observed): self.assertEqual(getattr(expected, attrib), getattr(observed, attrib))
def test_list_categories(self, mock_listitem): mock_listitem.side_effect = fakes.FakeListItem mock_plugin = fakes.FakePlugin() with mock.patch.dict('sys.modules', xbmcplugin=mock_plugin): import resources.lib.categories as categories categories.list_categories() for index, category in enumerate(sorted(config.CATEGORIES.keys())): expected_url = 'plugin://{addonid}/?{params}'.format( addonid=config.ADDON_ID, params=unquote_plus( urlencode({ 'action': 'listcategories', 'category': config.CATEGORIES[category] }))) observed_url = mock_plugin.directory[index].get('url') expected = urlparse(expected_url) observed = urlparse(observed_url) for x in range(6): if x == 4: self.assertEqual(dict(parse_qsl(expected[x])), dict(parse_qsl(observed[x]))) else: self.assertEqual(expected[x], observed[x])