async def test_Bot_with_autopost_error_listener( mocker: MockerFixture, capsys: CaptureFixture[str], session: ClientSession, exc: Exception, ) -> None: bot = Bot("") state = False @bot.listen() async def on_autopost_error(exc: Exception) -> None: nonlocal state state = True mocker.patch("topgg.DBLClient._auto_post", new_callable=mock.AsyncMock) # type: ignore DBLClient(bot, "", True, session=session) # await to make sure all the listeners were run before asserting # as <Bot>.dispatch schedules the events and will continue # to the assert line without finishing the event callbacks await bot.on_autopost_error(exc) await on_autopost_error(exc) assert "Ignoring exception in auto post loop" not in capsys.readouterr().err assert state
async def test_AutoPoster_breaks_autopost_loop_on_401( mocker: MockerFixture, session: ClientSession) -> None: response = mock.Mock("reason, status") response.reason = "Unauthorized" response.status = 401 mocker.patch("topgg.DBLClient.post_guild_count", side_effect=Unauthorized(response, {})) callback = mock.Mock() autopost = DBLClient("", session=session).autopost().stats(callback) assert isinstance(autopost, AutoPoster) assert not isinstance(autopost.stats()(callback), AutoPoster) with pytest.raises(Unauthorized): await autopost.start() callback.assert_called_once() assert not autopost.is_running
async def test_Client_with_default_autopost_error_handler( mocker: MockerFixture, capsys: CaptureFixture[str], session: ClientSession, exc: Exception, ) -> None: client = Client() mocker.patch("topgg.DBLClient._auto_post", new_callable=mock.AsyncMock) # type: ignore dbl = DBLClient(client, "", True, session=session) assert client.on_autopost_error == dbl.on_autopost_error await client.on_autopost_error(exc) assert "Ignoring exception in auto post loop" in capsys.readouterr().err
async def test_Client_with_custom_autopost_error_handler( mocker: MockerFixture, session: ClientSession, exc: Exception ) -> None: client = Client() state = False @client.event async def on_autopost_error(exc: Exception) -> None: nonlocal state state = True mocker.patch("topgg.DBLClient._auto_post", new_callable=mock.AsyncMock) # type: ignore DBLClient(client, "", True, session=session) await client.on_autopost_error(exc) assert state
def test_DBLClient_validates_constructor_and_fails_for_invalid_values( bot: Client, mocker: MockerFixture, autopost: bool, post_shard_count: bool, autopost_interval: Optional[int], session: ClientSession, ) -> None: with pytest.raises(ClientException): DBLClient( bot, "", session=session, autopost=autopost, post_shard_count=post_shard_count, autopost_interval=autopost_interval, )
async def test_DBLClient_validates_constructor_and_passes_for_valid_values( bot: Client, mocker: MockerFixture, autopost: bool, post_shard_count: bool, autopost_interval: Optional[int], session: ClientSession, ) -> None: mocker.patch("topgg.DBLClient._auto_post", new_callable=mock.AsyncMock) # type: ignore DBLClient( bot, "", session=session, autopost=autopost, post_shard_count=post_shard_count, autopost_interval=autopost_interval, )
async def test_DBLClient_breaks_autopost_loop_on_401( bot: Client, mocker: MockerFixture, session: ClientSession ) -> None: response = mock.Mock("reason, status") response.reason = "Unauthorized" response.status = 401 mocker.patch( "topgg.DBLClient.post_guild_count", side_effect=Unauthorized(response, {}) ) mocker.patch( "topgg.DBLClient._ensure_bot_user", new_callable=mock.AsyncMock, # type: ignore ) obj = DBLClient(bot, "", False, session=session) with pytest.raises(Unauthorized): await obj._auto_post()
async def connect_dbl(self, autopost: bool = None): try: if self.dbl: await self.dbl.close() token = self.auth.get("DBL_TOKEN") self.dbl = DBLClient(self, token, autopost=autopost) await self.dbl.post_guild_count() print("[DBL LOGIN] Logged in to DBL with token.") except DBLException: await self.dbl.close() self.auth["MWS_DBL_TOKEN"] = None self.dbl = None print( "[DBL ERROR] Login Failed: No token was provided or token provided was invalid." )
async def setup_hook(self) -> None: self.session: ClientSession = ClientSession() self.cache: Cache = Cache(self) self.pool: asyncpg.Pool = await asyncpg.create_pool( **self.settings["postgresql"]) self.topgg: DBLClient = DBLClient(self, self.api["TopGG"], autopost_interval=None, session=self.session) self.topgg_webhook: WebhookManager = WebhookManager(self).dbl_webhook( "/dbl", self.api["TopGGWH"]) self.gist: asyncgist.Client = asyncgist.Client(self.api["GitHub"], self.session) self.sr: sr_api.Client = sr_api.Client() self.dagpi: asyncdagpi.Client = asyncdagpi.Client(self.api["DagpiAPI"], session=self.session) self.myst: mystbin.Client = mystbin.Client(session=self.session) self.loop.create_task(self.cache.populate_cache()) self.loop.create_task(self.load_extensions()) self.loop.create_task(self.start_nodes()) self.loop.create_task(self.find_restart_message()) self.topgg_webhook.run(8025)
def autopost(session: ClientSession) -> AutoPoster: return AutoPoster(DBLClient("", session=session))