def mock_api_keys():
    try:
        bot.set(MagicMock())
        bot.config.get_api_key.return_value = "APIKEY"
        yield bot.config.get_api_key
    finally:
        bot.set(None)
Exemple #2
0
def test_api(unset_bot):
    bot.set(MockBot({"api_keys": {"lastfm": "hunter20"}}))

    with RequestsMock() as reqs:
        with pytest.raises(requests.ConnectionError):
            api_request('track.getTopTags')

        reqs.add(reqs.GET,
                 "http://ws.audioscrobbler.com/2.0/",
                 json={"data": "thing"})

        res, _ = api_request('track.getTopTags')

        assert res["data"] == "thing"

    with RequestsMock() as reqs:
        reqs.add(reqs.GET,
                 "http://ws.audioscrobbler.com/2.0/",
                 body="<html></html>")

        with pytest.raises(JSONDecodeError):
            api_request("track.getTopTags")

    with RequestsMock() as reqs:
        reqs.add(reqs.GET,
                 "http://ws.audioscrobbler.com/2.0/",
                 body="<html></html>",
                 status=403)

        with pytest.raises(HTTPError):
            api_request("track.getTopTags")
Exemple #3
0
def unset_bot():
    try:
        yield
    finally:
        from cloudbot.bot import bot

        bot.set(None)
def test_twitter_url(mock_requests, unset_bot):
    from cloudbot.bot import bot

    bot.set(MagicMock())

    mock_conn = MagicMock()
    mock_conn.config = {}
    mock_conn.bot = bot.get()
    bot.get().config = MockConfig(bot.get())

    from plugins import twitter

    result = twitter.twitter_url(
        twitter.TWITTER_RE.search("twitter.com/FakeUser/status/11235"),
        mock_conn,
    )

    assert result is None

    twitter.set_api()

    with pytest.raises(tweepy.TweepError):
        twitter.twitter_url(
            twitter.TWITTER_RE.search("twitter.com/FakeUser/status/11235"),
            mock_conn,
        )

    mock_requests.add(
        "GET",
        "https://api.twitter.com/1.1/statuses/show.json"
        "?id=11235&tweet_mode=extended",
        json={
            "errors": [{
                "message": "No status found with that ID.",
                "code": 144,
            }],
        },
        status=404,
    )

    result = twitter.twitter_url(
        twitter.TWITTER_RE.search("twitter.com/FakeUser/status/11235"),
        mock_conn,
    )

    assert result is None
Exemple #5
0
def test_no_results(mock_requests, unset_bot):
    from cloudbot.bot import bot

    bot.set(MockBot({"api_keys": {"brewerydb": "APIKEY"}}))
    mock_requests.add(
        'GET',
        'http://api.brewerydb.com/v2/search'
        '?format=json&key=APIKEY&type=beer&withBreweries=Y&q=some+text',
        match_querystring=True,
        json={"totalResults": 0},
    )
    from plugins import brew

    reply = MagicMock()
    result = brew.brew('some text', reply)

    reply.assert_not_called()

    assert result == 'No results found.'
Exemple #6
0
def setup_api(unset_bot, mock_requests):
    from cloudbot.bot import bot

    bot.set(
        MockBot({
            "api_keys": {
                "spotify_client_id": "APIKEY",
                "spotify_client_secret": "APIKEY",
            }
        }))
    mock_requests.add(
        "POST",
        "https://accounts.spotify.com/api/token",
        json={
            "access_token": "foo",
            "expires_in": 3600
        },
    )
    from plugins import spotify
    importlib.reload(spotify)
    spotify.set_keys()

    yield
Exemple #7
0
def unset_bot():
    yield
    from cloudbot.bot import bot
    bot.set(None)