def test_upload_attachment_error_retry( mock_make_attachment, mock_upload_file_to_vk, mock_request, ): mock_request.side_effect = [ { "upload_url": "_" }, RequestException(None, None, None, {"error_code": 1}), { "upload_url": "_" }, "ok", ] mock_upload_file_to_vk.side_effect = [ "ok", "ok", ] mock_make_attachment.return_value = "ok" vkontakte = Vkontakte("token") result = asyncio.get_event_loop().run_until_complete( vkontakte.upload_attachment(Attachment.new(b""), peer_id=123)) assert result == "ok"
def test_perform_send_exception(): vkontakte = Vkontakte(token="token") attachment = vkontakte._make_attachment(ATTACHMENTS["image"]) attachment = attachment._replace(id=None) with pytest.raises(ValueError): asyncio.get_event_loop().run_until_complete( vkontakte.perform_send(1, "text", attachment, {}))
def test_perform_api_call(): vkontakte = Vkontakte(token="token") async def req(method, kwargs): assert method == "method" assert kwargs["arg"] == "val" return 1 vkontakte._request = req result = asyncio.get_event_loop().run_until_complete( vkontakte.perform_api_call("method", {"arg": "val"})) assert result == 1
def test_perform_send_string(): vkontakte = Vkontakte(token="token") async def req(method, kwargs): assert method == "messages.send" assert kwargs["attachment"] == "hey,hoy" return 1 vkontakte._request = req result = asyncio.get_event_loop().run_until_complete( vkontakte.perform_send(1, "text", ("hey", "hoy"), {})) assert result == 1
async def test(): vkontakte = Vkontakte(token="token", session=aiohttp.ClientSession()) vkontakte.longpoll_data = { "ts": "ts", "key": "key", "server": "server", } await vkontakte.perform_updates_request(None) with pytest.raises(asyncio.CancelledError): await vkontakte.perform_updates_request(None) await vkontakte.perform_updates_request(None)
async def test(): vkontakte = Vkontakte(token="token", session=aiohttp.ClientSession()) assert await vkontakte.raw_request("method1", {"arg": "val1"}) == 1 with pytest.raises(RequestException): await vkontakte.raw_request("method2", {"arg": "val2"})
async def test(): vkontakte = Vkontakte(token="token", session=aiohttp.ClientSession()) tasks = [VKRequest("m1", {}), VKRequest("m2", {})] await vkontakte._execute_loop_perform_execute("code", tasks) assert tasks[0].done() assert tasks[1].done() assert tasks[0].result() == 1 with pytest.raises(RequestException): assert tasks[1].result() tasks = [VKRequest("m1", {}), VKRequest("m2", {})] await vkontakte._execute_loop_perform_execute("code", tasks) assert tasks[0].done() assert tasks[1].done() assert tasks[0].result() == 1 with pytest.raises(RequestException): assert tasks[1].result() tasks = [VKRequest("m1", {}), VKRequest("m2", {})] tasks[1].set_exception(asyncio.CancelledError) await vkontakte._execute_loop_perform_execute("code", tasks) assert tasks[0].done() assert tasks[1].done() assert tasks[0].result() == 1 with pytest.raises(asyncio.CancelledError): assert tasks[1].result()
async def test(): fut = VKRequest("", "") fut.set_result("OK") mock.return_value = fut vk = Vkontakte("token") resp = await vk.request("method", arg1="val1") assert resp == "OK" mock.assert_awaited_with("method", {"arg1": "val1"}, None) resp = await vk.send_message("user1", "msg", arg1="val1", random_id="1") assert resp == "OK" mock.assert_awaited_with( "messages.send", { "peer_id": "user1", "message": "msg", "arg1": "val1", "random_id": "1" }, )
def test_perform_send_sticker(): vkontakte = Vkontakte(token="token") async def req(method, kwargs): assert method == "messages.send" assert "attachment" not in kwargs assert kwargs["sticker_id"] == "123" return 1 vkontakte._request = req sticker_attachment = Attachment.existing("123", "sticker") result = asyncio.get_event_loop().run_until_complete( vkontakte.perform_send(1, "text", sticker_attachment, {})) assert result == 1
async def test(): vkontakte = Vkontakte(token="token", session=aiohttp.ClientSession()) for k, v in ATTACHMENTS.items(): attachment = vkontakte._make_attachment(v) if k == "graffiti": assert attachment.file is None continue assert attachment.type == k assert attachment.file is None if k in ("video", "graffiti"): continue assert await attachment.get_file() == "content" assert next(mock_read.side_effect, None) is None
def test_upload_attachment_error_no_retry( mock_upload_file_to_vk, mock_request, ): mock_request.side_effect = [ { "upload_url": "_" }, RequestException(None, None, None, {"error_code": 1}), ] mock_upload_file_to_vk.side_effect = [ "ok", ] vkontakte = Vkontakte("token") with pytest.raises(RequestException): asyncio.get_event_loop().run_until_complete( vkontakte.upload_attachment(Attachment.new(b"")))
async def test(): vkontakte = Vkontakte(token="token", session=aiohttp.ClientSession()) assert await vkontakte.resolve_screen_name("durov") == data assert await vkontakte.resolve_screen_name("durov") == data NAIVE_CACHE.update({i: None for i in range(500_000)}) assert await vkontakte.resolve_screen_name("krukov") == data assert len(NAIVE_CACHE) == 1 assert next(mock_request.side_effect, None) is None
def test_perform_send_new(): vkontakte = Vkontakte(token="token") async def _upl_att(attachment, peer_id): return attachment._replace(id=1, raw={"ok": "ok"}) vkontakte.upload_attachment = _upl_att async def req(method, kwargs): assert method == "messages.send" assert kwargs["attachment"] == "1" return 1 vkontakte._request = req attachment = Attachment.new(b"content", "image") result = asyncio.get_event_loop().run_until_complete( vkontakte.perform_send(1, "text", attachment, {})) assert result == 1
def main(): app = Kutana() # set_logger_level(logging.DEBUG) backend = Vkontakte(SHUF_SETTINGS['TOKEN']) app.add_backend(backend) app.config['settings'] = SHUF_SETTINGS app.config['prefixes'] = ('еш ', 'есб ', 'esb ', 'ешаф ', 'eshuf ' ) # ('шаф ', 'sb ', 'шб ', 'shuf ', 'shufbot ') app.config['inform_time'] = time(20, 50) # 12:00 app.config['votekick_time'] = 5 * 60 init_db(app) app.add_plugins(load_plugins(os.path.join(os.curdir, 'bot', 'plugins'))) return app
def add_backends(app, backends): for backend in backends or []: kwargs = {k: v for k, v in backend.items() if k != "kind"} name = kwargs.get("name") if name and app.get_backend(name): logger.logger.warning(f"Duplicated backend name: {name}") if backend["kind"] == "vk" and "address" in backend: app.add_backend(VkontakteCallback(**kwargs)) elif backend["kind"] == "vk": app.add_backend(Vkontakte(**kwargs)) elif backend["kind"] == "tg": app.add_backend(Telegram(**kwargs)) else: logger.logger.warning(f"Unknown backend kind: {backend['kind']}")
import json from kutana import Kutana, load_plugins from kutana.backends import Vkontakte # Import configuration with open("config.json") as fh: config = json.load(fh) # Create application app = Kutana() # Add manager to application app.add_backend(Vkontakte(token=config["vk_token"])) # Load and register plugins app.add_plugins(load_plugins("plugins/")) if __name__ == "__main__": # Run application app.run()
from ai_dungeon import config from ai_dungeon.storage.redis import RedisStorage from kutana import Kutana, load_plugins from kutana.backends import Vkontakte, Telegram import logging logging.basicConfig(level=logging.DEBUG) # Create application if config.REDIS_URL is not None: app = Kutana(storage=RedisStorage(config.REDIS_URL)) else: app = Kutana() # Add manager to application if config.TELEGRAM_TOKEN: app.add_backend(Telegram(token=config.TELEGRAM_TOKEN)) if config.VK_TOKEN: app.add_backend(Vkontakte(token=config.VK_TOKEN)) # Load and register plugins app.add_plugins(load_plugins("plugins/")) if __name__ == "__main__": # Run application app.run()
def test_no_token(): with pytest.raises(ValueError): Vkontakte("")
async def test(): vkontakte = Vkontakte(token="token", session=aiohttp.ClientSession()) result = await vkontakte._upload_file_to_vk("url", {"file": "file"}) assert result == {"r": "ok"}