Exemple #1
0
def test_bot_init(monkeypatch):
    importlib = FakeImportLib()

    monkeypatch.setattr("importlib.import_module", importlib.import_module)

    config = AttrDict(copy.deepcopy(DEFAULT))
    config.DEBUG = True
    config.DATABASE_URL = 'sqlite:///'
    config.MODELS = ['yui.model1', 'yui.model2']
    config.HANDLERS = ['yui.handler1', 'yui.handler2']
    config['LOGGING']['loggers']['yui']['handlers'] = ['console']
    config.REGISTER_CRONTAB = False
    del config['LOGGING']['handlers']['file']
    bot = Bot(config)

    assert bot.config == config
    assert bot.channels == []
    assert bot.ims == []
    assert bot.groups == []
    assert bot.loop is None
    assert bot.restart is False
    assert isinstance(bot.api, SlackAPI)
    assert bot.box is box
    assert isinstance(bot.queue, asyncio.Queue)
    assert importlib.import_queue == [
        'yui.handler1',
        'yui.handler2',
        'yui.model1',
        'yui.model2',
    ]
Exemple #2
0
def fx_engine(request):
    try:
        database_url = request.config.getoption('--database-url')
    except ValueError:
        database_url = None
    config = gen_config()
    if database_url:
        config.DATABASE_URL = database_url
    bot = Bot(config, using_box=Box())
    engine = bot.config.DATABASE_ENGINE
    try:
        metadata = Base.metadata
        metadata.drop_all(bind=engine)
        metadata.create_all(bind=engine)
        yield engine
        metadata.drop_all(bind=engine)
    finally:
        engine.dispose()
Exemple #3
0
def test_bot_init(monkeypatch, fx_config):
    importlib = FakeImportLib()

    monkeypatch.setattr("importlib.import_module", importlib.import_module)

    fx_config.APPS = ['yui.app1', 'yui.app2']
    box = Box()
    bot = Bot(fx_config, using_box=box)

    assert bot.config == fx_config
    assert bot.channels == []
    assert bot.ims == []
    assert bot.groups == []
    assert bot.restart is False
    assert isinstance(bot.api, SlackAPI)
    assert bot.box is box
    assert isinstance(bot.queue, asyncio.Queue)
    assert importlib.import_queue == [
        'yui.app1',
        'yui.app2',
    ]
Exemple #4
0
def fx_engine(request):
    try:
        database_url = request.config.getoption('--database-url')
    except ValueError:
        database_url = None

    config = AttrDict(copy.deepcopy(DEFAULT))
    config.DEBUG = True
    config.DATABASE_URL = database_url
    config.MODELS = []
    config.HANDLERS = []
    config['LOGGING']['loggers']['yui']['handlers'] = ['console']
    config.REGISTER_CRONTAB = False
    del config['LOGGING']['handlers']['file']
    bot = Bot(config)
    engine = bot.config.DATABASE_ENGINE
    try:
        metadata = Base.metadata
        metadata.drop_all(bind=engine)
        metadata.create_all(bind=engine)
        yield engine
        metadata.drop_all(bind=engine)
    finally:
        engine.dispose()
Exemple #5
0
async def test_call(fx_config, response_mock):
    token = 'asdf1234'

    response_mock.post(
        'https://slack.com/api/test11',
        body=ujson.dumps({
            'res': 'hello world!',
        }),
        headers={'content-type': 'application/json'},
        status=200,
    )
    response_mock.post(
        'https://slack.com/api/test12',
        body=ujson.dumps({
            'res': 'hello world!',
            'data': {
                'extra': 'wow',
            },
        }),
        headers={'content-type': 'application/json'},
        status=200,
    )

    response_mock.post(
        'https://slack.com/api/test21',
        body=ujson.dumps({
            'error': 'aaa',
        }),
        headers={'content-type': 'application/json'},
        status=404,
    )
    response_mock.post(
        'https://slack.com/api/test22',
        body=ujson.dumps({
            'error': 'aaa',
        }),
        headers={'content-type': 'application/json'},
        status=404,
    )
    response_mock.post(
        'https://slack.com/api/test3',
        body=ujson.dumps({
            'res': 'hello world!',
        }),
        headers={'content-type': 'application/json'},
        status=200,
    )

    box = Box()
    bot = Bot(fx_config, using_box=box)

    res = await bot.call('test11')
    assert res == APIResponse(
        body={'res': 'hello world!'},
        status=200,
        headers={'content-type': 'application/json'},
    )

    res = await bot.call('test12', data={'extra': 'wow'})
    assert res == APIResponse(
        body={
            'res': 'hello world!',
            'data': {
                'extra': 'wow'
            }
        },
        status=200,
        headers={'content-type': 'application/json'},
    )

    res = await bot.call('test21')
    assert res == APIResponse(
        body={'error': 'aaa'},
        status=404,
        headers={'content-type': 'application/json'},
    )

    res = await bot.call('test22', data={'extra': 'wow'})
    assert res == APIResponse(
        body={'error': 'aaa'},
        status=404,
        headers={'content-type': 'application/json'},
    )

    res = await bot.call('test3', token=token)
    assert res == APIResponse(
        body={'res': 'hello world!'},
        status=200,
        headers={'content-type': 'application/json'},
    )
Exemple #6
0
async def test_call(event_loop, bot_config, response_mock):
    token = 'asdf1234'

    response_mock.post(
        'https://slack.com/api/test11',
        body=json.dumps({'res': 'hello world!'}),
        headers={'content-type': 'application/json'},
        status=200,
    )
    response_mock.post(
        'https://slack.com/api/test12',
        body=json.dumps({
            'res': 'hello world!',
            'data': {
                'extra': 'wow'
            }
        }),
        headers={'content-type': 'application/json'},
        status=200,
    )

    response_mock.post(
        'https://slack.com/api/test21',
        body=json.dumps({'error': 'aaa'}),
        headers={'content-type': 'application/json'},
        status=404,
    )
    response_mock.post(
        'https://slack.com/api/test22',
        body=json.dumps({'error': 'aaa'}),
        headers={'content-type': 'application/json'},
        status=404,
    )
    response_mock.post(
        'https://slack.com/api/test3',
        body=json.dumps({'res': 'hello world!'}),
        headers={'content-type': 'application/json'},
        status=200,
    )

    box = Box()
    bot = Bot(bot_config, event_loop, using_box=box)
    bot.api.throttle_interval = defaultdict(lambda: timedelta(0))

    res = await bot.call('test11')
    assert res == APIResponse(
        body={'res': 'hello world!'},
        status=200,
        headers={'content-type': 'application/json'},
    )

    res = await bot.call('test12', data={'extra': 'wow'})
    assert res == APIResponse(
        body={
            'res': 'hello world!',
            'data': {
                'extra': 'wow'
            }
        },
        status=200,
        headers={'content-type': 'application/json'},
    )

    res = await bot.call('test21')
    assert res == APIResponse(
        body={'error': 'aaa'},
        status=404,
        headers={'content-type': 'application/json'},
    )

    res = await bot.call('test22', data={'extra': 'wow'})
    assert res == APIResponse(
        body={'error': 'aaa'},
        status=404,
        headers={'content-type': 'application/json'},
    )

    res = await bot.call('test3', token=token)
    assert res == APIResponse(
        body={'res': 'hello world!'},
        status=200,
        headers={'content-type': 'application/json'},
    )
Exemple #7
0
async def test_call(response_mock):
    token = 'asdf1234'

    response_mock.post(
        'https://slack.com/api/test11',
        body=ujson.dumps({
            'res': 'hello world!',
        }),
        headers={'content-type': 'application/json'},
        status=200,
    )
    response_mock.post(
        'https://slack.com/api/test12',
        body=ujson.dumps({
            'res': 'hello world!',
            'data': {
                'extra': 'wow',
            },
        }),
        headers={'content-type': 'application/json'},
        status=200,
    )

    response_mock.post(
        'https://slack.com/api/test21',
        body=ujson.dumps({
            'error': 'aaa',
        }),
        headers={'content-type': 'application/json'},
        status=404,
    )
    response_mock.post(
        'https://slack.com/api/test22',
        body=ujson.dumps({
            'error': 'aaa',
        }),
        headers={'content-type': 'application/json'},
        status=404,
    )
    response_mock.post(
        'https://slack.com/api/test3',
        body=ujson.dumps({
            'res': 'hello world!',
        }),
        headers={'content-type': 'application/json'},
        status=200,
    )

    config = AttrDict(copy.deepcopy(DEFAULT))
    config.DATABASE_URL = 'sqlite:///'
    config.TOKEN = 'asdf1234'
    config['LOGGING']['loggers']['yui']['handlers'] = ['console']
    del config['LOGGING']['handlers']['file']
    config.REGISTER_CRONTAB = False
    bot = Bot(config)

    res = await bot.call('test11')
    assert res['res'] == 'hello world!'

    res = await bot.call('test12', data={'extra': 'wow'})
    assert res['res'] == 'hello world!'
    assert res['data']['extra'] == 'wow'

    with pytest.raises(APICallError) as e:
        await bot.call('test21')
    assert str(e.value) == 'fail to call test21 with None'
    assert e.value.status_code == 404
    assert e.value.result == {'error': 'aaa'}
    assert e.value.headers['Content-Type'] == 'application/json'

    with pytest.raises(APICallError) as e:
        await bot.call('test22', data={'extra': 'wow'})
    assert str(e.value) == "fail to call test22 with {'extra': 'wow'}"
    assert e.value.status_code == 404
    assert e.value.result == {'error': 'aaa'}
    assert e.value.headers['Content-Type'] == 'application/json'

    res = await bot.call('test3', token=token)
    assert res['res'] == 'hello world!'
Exemple #8
0
async def test_call(bot_config, response_mock):
    token = "asdf1234"

    response_mock.post(
        "https://slack.com/api/test11",
        body=json.dumps({"res": "hello world!"}),
        headers={"content-type": "application/json"},
        status=200,
    )
    response_mock.post(
        "https://slack.com/api/test12",
        body=json.dumps({
            "res": "hello world!",
            "data": {
                "extra": "wow"
            }
        }),
        headers={"content-type": "application/json"},
        status=200,
    )

    response_mock.post(
        "https://slack.com/api/test21",
        body=json.dumps({"error": "aaa"}),
        headers={"content-type": "application/json"},
        status=404,
    )
    response_mock.post(
        "https://slack.com/api/test22",
        body=json.dumps({"error": "aaa"}),
        headers={"content-type": "application/json"},
        status=404,
    )
    response_mock.post(
        "https://slack.com/api/test3",
        body=json.dumps({"res": "hello world!"}),
        headers={"content-type": "application/json"},
        status=200,
    )

    box = Box()
    bot = Bot(bot_config, using_box=box)
    bot.api.throttle_interval = defaultdict(lambda: timedelta(0))

    res = await bot.call("test11")
    assert res == APIResponse(
        body={"res": "hello world!"},
        status=200,
        headers={"content-type": "application/json"},
    )

    res = await bot.call("test12", data={"extra": "wow"})
    assert res == APIResponse(
        body={
            "res": "hello world!",
            "data": {
                "extra": "wow"
            }
        },
        status=200,
        headers={"content-type": "application/json"},
    )

    res = await bot.call("test21")
    assert res == APIResponse(
        body={"error": "aaa"},
        status=404,
        headers={"content-type": "application/json"},
    )

    res = await bot.call("test22", data={"extra": "wow"})
    assert res == APIResponse(
        body={"error": "aaa"},
        status=404,
        headers={"content-type": "application/json"},
    )

    res = await bot.call("test3", token=token)
    assert res == APIResponse(
        body={"res": "hello world!"},
        status=200,
        headers={"content-type": "application/json"},
    )