def test_crypto_cmd(mock_requests):
    init_response(mock_requests)

    conn = MagicMock()
    conn.config = {}
    conn.bot = None

    event = CommandEvent(
        text="BTC USD",
        cmd_prefix=".",
        triggered_command="crypto",
        hook=MagicMock(),
        bot=conn.bot,
        conn=conn,
        channel="#foo",
        nick="foobaruser",
    )
    res = wrap_hook_response(cryptocurrency.crypto_command, event)

    assert res == [
        HookResult(
            return_type="return",
            value="BTC (bitcoin) // \x0307$50,000,000,000.00\x0f USD - 2.0000000 BTC // \x0303+18.9%\x0f change",
        )
    ]
Exemple #2
0
def test_crypto_cmd(mock_requests):
    init_response(mock_requests)

    conn = MagicMock()
    conn.config = {}
    conn.bot = None

    event = CommandEvent(
        text='BTC USD',
        cmd_prefix='.',
        triggered_command='crypto',
        hook=MagicMock(),
        bot=conn.bot,
        conn=conn,
        channel='#foo',
        nick='foobaruser',
    )
    res = wrap_hook_response(cryptocurrency.crypto_command, event)

    assert res == [
        HookResult(
            return_type='return',
            value=
            'BTC (bitcoin) // \x0307$50,000,000,000.00\x0f USD - 2.0000000 BTC // \x0303+18.9%\x0f change',
        )
    ]
Exemple #3
0
 def _run(self):
     conn = MagicMock()
     event = Event(hook=MagicMock(),
                   bot=conn.bot,
                   conn=conn,
                   channel='#foo',
                   nick='foobaruser')
     return wrap_hook_response(herald.welcome, event)
Exemple #4
0
 def call(self, text: str, results=None):
     event = CommandEvent(
         cmd_prefix=".",
         hook=MagicMock(),
         text=text,
         triggered_command="tv",
         conn=MagicMock(),
         channel="#foo",
         nick="nick",
     )
     return wrap_hook_response(self.get_func(), event, results=results)
def test_cmd_api_error(mock_requests):
    init_response(mock_requests, error_msg="FooBar")
    conn = MagicMock()
    conn.config = {}
    conn.bot = None

    event = CommandEvent(
        text="BTC USD",
        cmd_prefix=".",
        triggered_command="crypto",
        hook=MagicMock(),
        bot=conn.bot,
        conn=conn,
        channel="#foo",
        nick="foobaruser",
    )
    res = []
    with pytest.raises(cryptocurrency.APIError, match="FooBar"):
        wrap_hook_response(cryptocurrency.crypto_command, event, res)

    assert res == [HookResult("message", ("#foo", "(foobaruser) Unknown API error"))]
Exemple #6
0
def test_cmd_api_error(mock_requests):
    init_response(mock_requests, error_msg='FooBar')
    conn = MagicMock()
    conn.config = {}
    conn.bot = None

    event = CommandEvent(
        text='BTC USD',
        cmd_prefix='.',
        triggered_command='crypto',
        hook=MagicMock(),
        bot=conn.bot,
        conn=conn,
        channel='#foo',
        nick='foobaruser',
    )
    res = []
    with pytest.raises(cryptocurrency.APIError, match='FooBar'):
        wrap_hook_response(cryptocurrency.crypto_command, event, res)

    assert res == [
        HookResult('message', ('#foo', '(foobaruser) Unknown API error'))
    ]
def do_search(query, results=None):
    conn = MagicMock()
    conn.config = {}
    conn.bot = None

    cmd_event = CommandEvent(text=query,
                             cmd_prefix='.',
                             triggered_command='wiki',
                             hook=MagicMock(),
                             bot=conn.bot,
                             conn=conn,
                             channel='#foo',
                             nick='foobaruser')

    return wrap_hook_response(wikipedia.wiki, cmd_event, results=results)
def _run_alias():
    conn = MagicMock()
    conn.config = {}
    conn.bot = None

    event = CommandEvent(
        text="",
        cmd_prefix=".",
        triggered_command="btc",
        hook=MagicMock(),
        bot=conn.bot,
        conn=conn,
        channel="#foo",
        nick="foobaruser",
    )
    return wrap_hook_response(cryptocurrency.btc_alias, event)
Exemple #9
0
def _run_alias():
    conn = MagicMock()
    conn.config = {}
    conn.bot = None

    event = CommandEvent(
        text='',
        cmd_prefix='.',
        triggered_command='btc',
        hook=MagicMock(),
        bot=conn.bot,
        conn=conn,
        channel='#foo',
        nick='foobaruser',
    )
    return wrap_hook_response(cryptocurrency.btc_alias, event)
Exemple #10
0
def test_rounding(mock_requests, patch_try_shorten, mock_db):
    from plugins import weather
    bot = setup_api(mock_requests, mock_db)

    conn = MagicMock()
    conn.config = {}

    conn.bot = bot

    cmd_event = CommandEvent(text='',
                             cmd_prefix='.',
                             triggered_command='we',
                             hook=MagicMock(),
                             bot=bot,
                             conn=conn,
                             channel='#foo',
                             nick='foobar')

    weather.location_cache.append(('foobar', 'test location'))

    new_data = deepcopy(FIO_DATA)

    new_data['json']['currently']['temperature'] = 31.9

    mock_requests.add(mock_requests.GET,
                      re.compile(r'^https://api\.darksky\.net/forecast/.*'),
                      **new_data)

    out_text = (
        '(foobar) \x02Current\x02: foobar, 32F/0C\x0f; \x02High\x02: 64F/18C\x0f; '
        '\x02Low\x02: 57F/14C\x0f; \x02Humidity\x02: 45%\x0f; '
        '\x02Wind\x02: 12MPH/20KPH SE\x0f '
        '-- 123 Test St, Example City, CA - '
        '\x1fhttps://darksky.net/forecast/30.123,123.456\x0f '
        '(\x1dTo get a forecast, use .fc\x1d)')

    calls = [(
        'message',
        (
            '#foo',
            out_text,
        ),
        {},
    )]

    assert wrap_hook_response(weather.weather, cmd_event) == calls
Exemple #11
0
def test_parse_no_results(mock_requests, patch_try_shorten, mock_db):
    mock_requests.add('GET',
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      json={
                          'status': 'OK',
                          'results': [],
                      })

    from plugins import weather

    weather.table.create(mock_db.engine, True)

    bot = MockBot(
        {
            'api_keys': {
                'google_dev_key': 'AIzatestapikey',
                'darksky': 'abc12345' * 4,
            }
        }, mock_db)

    weather.create_maps_api(bot)

    conn = MagicMock()
    conn.config = {}

    conn.bot = bot

    cmd_event = CommandEvent(text='myloc',
                             cmd_prefix='.',
                             triggered_command='we',
                             hook=MagicMock(),
                             bot=bot,
                             conn=conn,
                             channel='#foo',
                             nick='foobaruser')

    cmd_event.hook.required_args = ['event', 'db']
    cmd_event.hook.doc = "- foobar"

    cmd_event.prepare_threaded()

    res = wrap_hook_response(weather.check_and_parse, cmd_event)
    assert res == [('return', (None, "Unable to find location 'myloc'"))]
def test_crypto_cmd_bad_fiat(mock_requests):
    init_response(mock_requests, quote=False)

    conn = MagicMock()
    conn.config = {}
    conn.bot = None

    event = CommandEvent(
        text="BTC ABC",
        cmd_prefix=".",
        triggered_command="crypto",
        hook=MagicMock(),
        bot=conn.bot,
        conn=conn,
        channel="#foo",
        nick="foobaruser",
    )
    res = wrap_hook_response(cryptocurrency.crypto_command, event)

    assert res == [HookResult("return", "Unknown fiat currency 'ABC'")]
Exemple #13
0
def test_crypto_cmd_bad_fiat(mock_requests):
    init_response(mock_requests, quote=False)

    conn = MagicMock()
    conn.config = {}
    conn.bot = None

    event = CommandEvent(
        text='BTC ABC',
        cmd_prefix='.',
        triggered_command='crypto',
        hook=MagicMock(),
        bot=conn.bot,
        conn=conn,
        channel='#foo',
        nick='foobaruser',
    )
    res = wrap_hook_response(cryptocurrency.crypto_command, event)

    assert res == [HookResult('return', "Unknown fiat currency 'ABC'")]
Exemple #14
0
def test_find_location(mock_requests, patch_try_shorten, mock_db):
    from plugins import weather
    bot = MockBot({}, mock_db)
    weather.create_maps_api(bot)
    weather.location_cache.clear()
    assert weather.data.maps_api is None

    bot = setup_api(mock_requests, mock_db)

    assert weather.find_location('Foo Bar') == {
        'lat': 30.123,
        'lng': 123.456,
        'address': '123 Test St, Example City, CA',
    }

    conn = MagicMock()
    conn.config = {}

    conn.bot = bot

    cmd_event = CommandEvent(text='',
                             cmd_prefix='.',
                             triggered_command='we',
                             hook=MagicMock(),
                             bot=bot,
                             conn=conn,
                             channel='#foo',
                             nick='foobar')

    cmd_event.hook.required_args = ['db']
    cmd_event.hook.doc = "- foobar"

    cmd_event.prepare_threaded()

    assert wrap_hook_response(weather.weather, cmd_event) == [
        ('notice', ('foobar', '.we - foobar'), {})
    ]
    weather.location_cache.append(('foobar', 'test location'))

    mock_requests.add(mock_requests.GET,
                      re.compile(r'^https://api\.darksky\.net/forecast/.*'),
                      **FIO_DATA)
    assert wrap_hook_response(weather.weather, cmd_event) == [(
        'message',
        (
            '#foo',
            '(foobar) \x02Current\x02: foobar, 68F/20C\x0f; \x02High\x02: 64F/18C\x0f; '
            '\x02Low\x02: 57F/14C\x0f; \x02Humidity\x02: 45%\x0f; '
            '\x02Wind\x02: 12MPH/20KPH SE\x0f '
            '-- 123 Test St, Example City, CA - '
            '\x1fhttps://darksky.net/forecast/30.123,123.456\x0f '
            '(\x1dTo get a forecast, use .fc\x1d)',
        ),
        {},
    )]
    assert wrap_hook_response(weather.forecast, cmd_event) == [(
        'message',
        (
            '#foo',
            '(foobar) \x02Today\x02: foobar; High: 64F/18C; Low: 57F/14C; '
            'Humidity: 45%; Wind: 15MPH/24KPH SE | '
            '\x02Tomorrow\x02: foobar; High: 64F/18C; '
            'Low: 57F/14C; Humidity: 45%; Wind: 15MPH/24KPH SE '
            '-- 123 Test St, Example City, CA - '
            '\x1fhttps://darksky.net/forecast/30.123,123.456\x0f',
        ),
        {},
    )]

    mock_requests.reset()
    mock_requests.add(mock_requests.GET,
                      'https://maps.googleapis.com/maps/api/geocode/json',
                      json={'status': 'foobar'})

    response = []
    with pytest.raises(ApiError):
        wrap_hook_response(weather.weather, cmd_event, response)

    assert response == [('message', ('#foo', '(foobar) API Error occurred.'),
                         {})]

    bot.config['api_keys']['google_dev_key'] = None
    bot.config.load_config()
    weather.create_maps_api(bot)
    assert wrap_hook_response(weather.weather, cmd_event) == [
        ('return',
         'This command requires a Google Developers Console API key.')
    ]
    assert wrap_hook_response(weather.forecast, cmd_event) == [
        ('return',
         'This command requires a Google Developers Console API key.')
    ]

    bot.config['api_keys']['darksky'] = None
    bot.config.load_config()
    weather.create_maps_api(bot)
    assert wrap_hook_response(weather.weather, cmd_event) == [
        ('return', 'This command requires a DarkSky API key.')
    ]
    assert wrap_hook_response(weather.forecast, cmd_event) == [
        ('return', 'This command requires a DarkSky API key.')
    ]

    # Test DB storage
    bot.config.update({
        'api_keys': {
            'google_dev_key': 'AIzatestapikey',
            'darksky': 'abc12345' * 4,
        }
    })
    bot.config.load_config()
    weather.create_maps_api(bot)
    weather.table.create(mock_db.engine, checkfirst=True)
    cmd_event.db = mock_db.session()
    cmd_event.text = 'my location'

    weather.load_cache(mock_db.session())
    mock_requests.reset()
    setup_api(mock_requests, mock_db)
    mock_requests.add(mock_requests.GET,
                      re.compile(r'^https://api\.darksky\.net/forecast/.*'),
                      **FIO_DATA)

    (loc, data), err = call_with_args(weather.check_and_parse, cmd_event)
    assert loc == {
        'address': '123 Test St, Example City, CA',
        'lat': 30.123,
        'lng': 123.456
    }
    assert data is not None
    assert err is None

    assert weather.location_cache == [(cmd_event.nick, cmd_event.text)]

    db_data = mock_db.session().execute(weather.table.select()).fetchall()
    assert len(db_data) == 1
    assert list(db_data[0]) == [cmd_event.nick, cmd_event.text]
Exemple #15
0
 def check(ev):
     return wrap_hook_response(herald.welcome, ev)