Beispiel #1
0
 async def test_returns_original_url_if_one_song_404(self, bot):
     """Return original URL if one of the songs not found."""
     url1 = 'https://deezer.com/track/1'
     url2 = 'https://deezer.com/track/2'
     message = make_mock_message(text=f'check these: {url1} and {url2}',
                                 chat_type=ChatType.GROUP)
     reply_text = ('<b>@test_user wrote:</b> check these: [1] and [2]\n'
                   '\n'
                   '1. https://deezer.com/track/1\n'
                   '2. Test Artist 1 - Test Title 1\n'
                   '<a href="https://www.test.com/d">Deezer</a> | '
                   '<a href="https://www.test.com/g">Google Music</a> | '
                   '<a href="https://www.test.com/sc">SoundCloud</a> | '
                   '<a href="https://www.test.com/yn">Yandex Music</a> | '
                   '<a href="https://www.test.com/s">Spotify</a> | '
                   '<a href="https://www.test.com/ym">YouTube Music</a> | '
                   '<a href="https://www.test.com/y">YouTube</a> | '
                   '<a href="https://www.test.com/am">Apple Music</a> | '
                   '<a href="https://www.test.com/t">Tidal</a>')
     api_url1 = f'{bot.config.ODESLI_API_URL}?url={url1}'
     api_url2 = f'{bot.config.ODESLI_API_URL}?url={url2}'
     payload = make_response()
     with aioresponses() as m:
         m.get(api_url1, status=HTTPStatus.NOT_FOUND)
         m.get(api_url2, status=HTTPStatus.OK, payload=payload)
         await bot.dispatcher.message_handlers.notify(message)
         assert message.reply.called
         assert message.reply.called_with_text == reply_text
Beispiel #2
0
 async def test_replies_if_some_urls_not_found(self, bot):
     """Send a reply to a private message if song not found in some
     platforms.
     """
     url = 'https://www.deezer.com/track/1'
     message = make_mock_message(text=f'check this one: {url}',
                                 chat_type=ChatType.PRIVATE)
     reply_text = ('check this one: [1]\n'
                   '\n'
                   '1. Test Artist 1 - Test Title 1\n'
                   '<a href="https://www.test.com/g">Google Music</a> | '
                   '<a href="https://www.test.com/sc">SoundCloud</a> | '
                   '<a href="https://www.test.com/yn">Yandex Music</a> | '
                   '<a href="https://www.test.com/s">Spotify</a> | '
                   '<a href="https://www.test.com/ym">YouTube Music</a> | '
                   '<a href="https://www.test.com/y">YouTube</a> | '
                   '<a href="https://www.test.com/am">Apple Music</a> | '
                   '<a href="https://www.test.com/t">Tidal</a>')
     api_url = f'{bot.config.ODESLI_API_URL}?url={url}'
     payload = make_response(song_id=1)
     # Remove Deezer data from the payload
     del payload['linksByPlatform']['deezer']
     with aioresponses() as m:
         m.get(api_url, status=HTTPStatus.OK, payload=payload)
         await bot.dispatcher.message_handlers.notify(message)
         assert message.reply.called
         assert message.reply.called_with_text == reply_text
Beispiel #3
0
def test_get_projects(metadata_api_client):
    projects = ["ioc-1000", "ioc-1099"]
    with patch.object(
            metadata_api_client,
            "get",
            return_value=make_response(content={"projects": projects})):
        res = metadata_api_client.get_projects()
    assert res == projects
Beispiel #4
0
def test_send_time_series_id_metadata(metadata_api_client):
    ts_id_metadata = TimeSeriesIdMetadataFactory()
    expected = ts_id_metadata.__dict__
    expected["model_id"] = 1226976
    expected = dump_any_dict(expected)

    with patch.object(metadata_api_client,
                      "post",
                      return_value=make_response(dumped_data=expected)):
        res = metadata_api_client.send_time_series_id_metadata(ts_id_metadata)
    assert res.content == expected
Beispiel #5
0
 async def test_replies_to_private_message_if_only_urls(self, bot):
     """Send a reply to a private message without text if message consists of
     song URLs only.
     """
     url1 = 'https://www.deezer.com/track/1'
     url2 = 'https://soundcloud.com/2'
     message = make_mock_message(text=f'{url1}\n{url2}',
                                 chat_type=ChatType.PRIVATE)
     reply_text = ('1. Test Artist 1 - Test Title 1\n'
                   '<a href="https://www.test.com/d">Deezer</a> | '
                   '<a href="https://www.test.com/g">Google Music</a> | '
                   '<a href="https://www.test.com/sc">SoundCloud</a> | '
                   '<a href="https://www.test.com/yn">Yandex Music</a> | '
                   '<a href="https://www.test.com/s">Spotify</a> | '
                   '<a href="https://www.test.com/ym">YouTube Music</a> | '
                   '<a href="https://www.test.com/y">YouTube</a> | '
                   '<a href="https://www.test.com/am">Apple Music</a> | '
                   '<a href="https://www.test.com/t">Tidal</a>\n'
                   '2. Test Artist 2 - Test Title 2\n'
                   '<a href="https://www.test.com/d">Deezer</a> | '
                   '<a href="https://www.test.com/g">Google Music</a> | '
                   '<a href="https://www.test.com/sc">SoundCloud</a> | '
                   '<a href="https://www.test.com/yn">Yandex Music</a> | '
                   '<a href="https://www.test.com/s">Spotify</a> | '
                   '<a href="https://www.test.com/ym">YouTube Music</a> | '
                   '<a href="https://www.test.com/y">YouTube</a> | '
                   '<a href="https://www.test.com/am">Apple Music</a> | '
                   '<a href="https://www.test.com/t">Tidal</a>')
     api_url1 = f'{bot.config.ODESLI_API_URL}?url={url1}'
     api_url2 = f'{bot.config.ODESLI_API_URL}?url={url2}'
     payload1 = make_response(song_id=1)
     payload2 = make_response(song_id=2)
     with aioresponses() as m:
         m.get(api_url1, status=HTTPStatus.OK, payload=payload1)
         m.get(api_url2, status=HTTPStatus.OK, payload=payload2)
         await bot.dispatcher.message_handlers.notify(message)
         assert message.reply.called
         assert message.reply.called_with_text == reply_text
Beispiel #6
0
 async def test_throttles_requests_if_429(self, caplog, bot):
     """Bot throttles requests if API returns 429 TOO_MANY_REQUESTS."""
     bot.API_RETRY_TIME = 1
     message1 = make_mock_message(
         text='check this one: https://deezer.com/track/1',
         chat_type=ChatType.PRIVATE,
     )
     message2 = make_mock_message(
         text='check this one: https://deezer.com/track/2',
         chat_type=ChatType.PRIVATE,
     )
     reply_text = ('check this one: [1]\n'
                   '\n'
                   '1. Test Artist 1 - Test Title 1\n'
                   '<a href="https://www.test.com/d">Deezer</a> | '
                   '<a href="https://www.test.com/g">Google Music</a> | '
                   '<a href="https://www.test.com/sc">SoundCloud</a> | '
                   '<a href="https://www.test.com/yn">Yandex Music</a> | '
                   '<a href="https://www.test.com/s">Spotify</a> | '
                   '<a href="https://www.test.com/ym">YouTube Music</a> | '
                   '<a href="https://www.test.com/y">YouTube</a> | '
                   '<a href="https://www.test.com/am">Apple Music</a> | '
                   '<a href="https://www.test.com/t">Tidal</a>')
     url1 = f'{bot.config.ODESLI_API_URL}?url=https://deezer.com/track/1'
     url2 = f'{bot.config.ODESLI_API_URL}?url=https://deezer.com/track/2'
     payload = make_response()
     with aioresponses() as m:
         m.get(url1, status=HTTPStatus.TOO_MANY_REQUESTS)
         m.get(url1, status=HTTPStatus.OK, payload=payload)
         m.get(url2, status=HTTPStatus.OK, payload=payload)
         tasks = [
             asyncio.create_task(
                 bot.dispatcher.message_handlers.notify(message1)),
             asyncio.create_task(
                 bot.dispatcher.message_handlers.notify(message2)),
         ]
         await asyncio.sleep(1)
         assert 'Too many requests, retrying' in caplog.text
         assert 'Waiting for the API' in caplog.text
         await asyncio.gather(*tasks)
         assert message1.reply.called
         assert message1.reply.called_with_text == reply_text
         assert message2.reply.called
         assert message2.reply.called_with_text == reply_text
Beispiel #7
0
 async def test_replies_to_private_message_for_single_url(self, bot):
     """Send a reply to a private message without an index number if incoming
     message consists only of one URL.
     """
     url = 'https://www.deezer.com/track/1'
     message = make_mock_message(text=url, chat_type=ChatType.PRIVATE)
     reply_text = ('Test Artist 1 - Test Title 1\n'
                   '<a href="https://www.test.com/d">Deezer</a> | '
                   '<a href="https://www.test.com/g">Google Music</a> | '
                   '<a href="https://www.test.com/sc">SoundCloud</a> | '
                   '<a href="https://www.test.com/yn">Yandex Music</a> | '
                   '<a href="https://www.test.com/s">Spotify</a> | '
                   '<a href="https://www.test.com/ym">YouTube Music</a> | '
                   '<a href="https://www.test.com/y">YouTube</a> | '
                   '<a href="https://www.test.com/am">Apple Music</a> | '
                   '<a href="https://www.test.com/t">Tidal</a>')
     api_url = f'{bot.config.ODESLI_API_URL}?url={url}'
     payload = make_response(song_id=1)
     with aioresponses() as m:
         m.get(api_url, status=HTTPStatus.OK, payload=payload)
         await bot.dispatcher.message_handlers.notify(message)
         assert message.reply.called
         assert message.reply.called_with_text == reply_text