From 3f68b65e17295638397d4b4b4fbf93117b69735f Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 18 Mar 2023 21:59:23 +0100 Subject: [PATCH] [mastodon] Return 422 error for invalid input when creating mastodon bot --- backend/setup.cfg | 1 + .../src/kibicara/platforms/mastodon/webapi.py | 24 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/backend/setup.cfg b/backend/setup.cfg index 8230773..e283b44 100644 --- a/backend/setup.cfg +++ b/backend/setup.cfg @@ -35,6 +35,7 @@ install_requires = requests scrypt Mastodon.py + pydantic[email] [options.packages.find] where = src diff --git a/backend/src/kibicara/platforms/mastodon/webapi.py b/backend/src/kibicara/platforms/mastodon/webapi.py index 6647633..7753081 100644 --- a/backend/src/kibicara/platforms/mastodon/webapi.py +++ b/backend/src/kibicara/platforms/mastodon/webapi.py @@ -5,14 +5,14 @@ from fastapi import APIRouter, Depends, HTTPException, Response, status from ormantic.exceptions import NoMatch -from pydantic import BaseModel +from pydantic import BaseModel, validate_email, validator from kibicara.config import config from kibicara.platforms.mastodon.bot import spawner from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance from kibicara.webapi.hoods import get_hood, get_hood_unauthorized -from mastodon import Mastodon, MastodonError +from mastodon import Mastodon, MastodonError, MastodonNetworkError from logging import getLogger @@ -29,6 +29,14 @@ class BodyMastodonAccount(BaseModel): instance_url: str password: str + @validator("email") + def validate_email(cls, value): + return validate_email(value) + + +class HTTPError(BaseModel): + status_code: int + async def get_mastodon(mastodon_id, hood=Depends(get_hood)): try: @@ -141,7 +149,10 @@ async def mastodon_stop(mastodon=Depends(get_mastodon)): @router.post( "/", status_code=status.HTTP_201_CREATED, - # TODO response_model + responses={ + 201: {"model": MastodonAccount}, + 422: {"model": HTTPError, "description": "Invalid Input"}, + }, operation_id="create_mastodon", ) async def mastodon_create(values: BodyMastodonAccount, hood=Depends(get_hood)): @@ -153,7 +164,10 @@ async def mastodon_create(values: BodyMastodonAccount, hood=Depends(get_hood)): :param: values: a BodyMastodonAccount object in json :param: hood: the hood ORM object """ - instance = await get_mastodon_instance(values.instance_url) + try: + instance = await get_mastodon_instance(values.instance_url) + except MastodonNetworkError: + raise HTTPException(422, "Invalid Mastodon Instance") account = Mastodon( instance.client_id, instance.client_secret, api_base_url=values.instance_url ) @@ -161,7 +175,7 @@ async def mastodon_create(values: BodyMastodonAccount, hood=Depends(get_hood)): access_token = account.log_in(values.email, values.password) except MastodonError: logger.warning("Login to Mastodon failed.", exc_info=True) - return # show error to user + return HTTPException(422, "Login to Mastodon failed") return await MastodonAccount.objects.create( hood=hood, instance=instance,