Skip to content

Commit

Permalink
feature/skip_invalid_credentials (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kondrahin committed Aug 23, 2021
1 parent 036a844 commit 832c257
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
21 changes: 15 additions & 6 deletions botx/bots/bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,21 @@ async def execute_command(self, message: dict) -> None:

await self(msg)

async def authorize(self) -> None:
async def authorize(self, *args: Any) -> None:
"""Process auth for each bot account."""
for account in self.bot_accounts:
token = await self.get_token(
account.host,
account.bot_id,
account.signature,
)
try:
token = await self.get_token(
account.host,
account.bot_id,
account.signature,
)
except (exceptions.BotXAPIError, exceptions.BotXConnectError) as exc:
logger.bind(botx_bot=True).warning(
f"Credentials `host - {account.host}, " # noqa: WPS305
f"bot_id - {account.bot_id}` are invalid. "
f"Reason - {exc.message_template}",
)
continue

account.token = token
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.21.3 (Aug 17, 2021)

### Fixed

* Bot's method `authorize()` now not fall if cant take some tokens. Just logging and skip invalid credentials.


## 0.21.2 (Aug 3, 2021)

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "botx"
version = "0.21.2"
version = "0.21.3"
description = "A little python framework for building bots for eXpress"
license = "MIT"
authors = [
Expand Down
15 changes: 15 additions & 0 deletions tests/test_models/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,18 @@ async def test_auth_to_each_known_account(bot, client) -> None:

for account in bot.bot_accounts:
assert account.token is not None


@pytest.mark.asyncio()
async def test_auth_with_wrong_credentials(bot, host) -> None:
bot_id = UUID("8dada2c8-67a6-4434-9dec-570d244e78ee")
bot.bot_accounts = [
BotXCredentials(
host=host,
secret_key="wrong_secret",
bot_id=bot_id,
),
]
await bot.authorize()

assert bot.bot_accounts[0].token is None

1 comment on commit 832c257

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://pybotx.netlify.app as production
🚀 Deployed on https://61234488e1248b66c2fade63--pybotx.netlify.app

Please sign in to comment.