Skip to content

Commit

Permalink
fix: linter and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Maximenyuk committed Apr 5, 2024
1 parent 3ef89fd commit e108d0f
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 34 deletions.
3 changes: 3 additions & 0 deletions app/api/dependencies/healthcheck.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ check_redis_connection_dependency = Depends(check_redis_connection)
async def check_worker_status() -> Optional[str]:
job = await queue.enqueue("healthcheck")

if not job:
return None

try:
await job.refresh(settings.WORKER_TIMEOUT_SEC)
except TimeoutError:
Expand Down
7 changes: 4 additions & 3 deletions app/caching/exception_handlers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Redis custom exception handlers."""

from redis.asyncio.client import PubSub
from redis.asyncio.client import PubSub, PubsubWorkerExceptionHandler

from app.logger import logger


async def pubsub_exception_handler(exc: BaseException, pubsub: PubSub) -> None:
logger.exception("Something went wrong in PubSub")
class PubsubExceptionHandler(PubsubWorkerExceptionHandler):
def __call__(self, exc: BaseException, pubsub: PubSub) -> None:
logger.exception("Something went wrong in PubSub")
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from app.api.routers import router
from app.bot.bot import get_bot
from app.caching.callback_redis_repo import CallbackRedisRepo
from app.caching.exception_handlers import pubsub_exception_handler
from app.caching.exception_handlers import PubsubExceptionHandler
from app.caching.redis_repo import RedisRepo
from app.db.sqlalchemy import build_db_session_factory, close_db_connections
from app.resources import strings
Expand All @@ -33,7 +33,7 @@ async def startup(application: FastAPI, raise_bot_exceptions: bool) -> None:
# -- Bot --
callback_repo = CallbackRedisRepo(redis_client)
process_callbacks_task = asyncio.create_task(
callback_repo.pubsub.run(exception_handler=pubsub_exception_handler)
callback_repo.pubsub.run(exception_handler=PubsubExceptionHandler())
)
bot = get_bot(callback_repo, raise_exceptions=raise_bot_exceptions)

Expand Down
10 changes: 8 additions & 2 deletions app/services/botx_user_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
from typing import Optional, Tuple
from uuid import UUID

from pybotx import Bot, BotAccount, UserFromSearch, UserKinds, UserNotFoundError
from pybotx import (
Bot,
BotAccountWithSecret,
UserFromSearch,
UserKinds,
UserNotFoundError,
)


class UserIsBotError(Exception):
Expand All @@ -12,7 +18,7 @@ class UserIsBotError(Exception):

async def search_user_on_each_cts(
bot: Bot, huid: UUID
) -> Optional[Tuple[UserFromSearch, BotAccount]]:
) -> Optional[Tuple[UserFromSearch, BotAccountWithSecret]]:
"""Search user by huid on all cts on which bot is registered.
return type: tuple of UserFromSearch instance and host.
Expand Down
8 changes: 6 additions & 2 deletions app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ def _build_credentials_from_string(
credentials_str = credentials_str.replace("|", "@")
assert credentials_str.count("@") == 2, "Have you forgot to add `bot_id`?"

host, secret_key, bot_id = [
cts_url, secret_key, bot_id = [
str_value.strip() for str_value in credentials_str.split("@")
]

if "://" not in cts_url:
cts_url = f"https://{cts_url}"

return BotAccountWithSecret(
id=UUID(bot_id), host=host, secret_key=secret_key
id=UUID(bot_id), cts_url=cts_url, secret_key=secret_key
)

BOT_CREDENTIALS: List[BotAccountWithSecret]
Expand Down
56 changes: 32 additions & 24 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ add-trailing-comma = "2.2.1"
autoflake = "1.4.0"
black = "22.3.0"
isort = "5.10.1"
mypy = "0.931.0"
mypy = "1.0.1"
wemake-python-styleguide = "0.16.0"
flake8-bandit = "2.1.2" # https://github.com/PyCQA/bandit/issues/837
Expand Down
3 changes: 3 additions & 0 deletions tests/endpoints/test_botx.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from http import HTTPStatus
from typing import Dict
from uuid import UUID
Expand Down Expand Up @@ -174,6 +175,8 @@ def test__web_app__bot_command_response_accepted(
headers=authorization_header,
)

time.sleep(0.1)

callback_response = test_client.post(
"/notification/callback",
json=callback_payload,
Expand Down

0 comments on commit e108d0f

Please sign in to comment.