Compare commits

...

17 commits

Author SHA1 Message Date
missytake 51929f8071 [doc] Fix ending of a file in CONTRIBUTING.md 2023-04-09 10:31:07 +00:00
missytake c773308a28 [doc] Users -> hood admins 2023-04-09 10:31:07 +00:00
missytake 7468344d81 [doc] Document how to implement the frontend for a platform 2023-04-09 10:31:07 +00:00
missytake 061d29325f [mastodon] Dismiss old notifications in the migration script, so they aren't reposted by ticketfrei3 2023-04-08 16:53:21 +00:00
missytake e9eb98dba8 [misc] Migration script creates nice output now 2023-04-08 16:53:21 +00:00
missytake ea58f4a150 [misc] Wrote migration script ticketfrei 2 -> 3 2023-04-08 16:53:21 +00:00
missytake 91e93b3e20 [mastodon] Fix instance load which broke with #17 2023-04-01 21:25:49 +02:00
Thomas Lindner 5bff62487b [misc] Migrate to TortoiseORM 2023-04-01 19:41:53 +02:00
Thomas Lindner 26818da430 [doc] Avoid global angular installation 2023-04-01 19:32:44 +02:00
missytake c468543fa5 [tests] Rename asyncclient into client 2023-04-01 15:32:10 +02:00
missytake f829bf8694 [tests] Get rid of non-async test client 2023-04-01 15:30:42 +02:00
missytake 16c4ebc84b [tests] Ported telegram tests to async 2023-04-01 15:23:33 +02:00
missytake f0e15f1d37 [tests] Ported mastodon tests to async 2023-04-01 15:13:50 +02:00
missytake b876e645de [tests] Ported email tests to async 2023-04-01 14:57:41 +02:00
missytake 6b6a2777bb [tests] Ported other hood webAPI tests to async 2023-04-01 14:51:32 +02:00
missytake c1b8ad2984 [tests] Disable trio backend for AnyIO 2023-04-01 14:40:02 +02:00
missytake 48e3b6f6bc [tests] Port first test to async #23 2023-04-01 14:18:24 +02:00
48 changed files with 1305 additions and 873 deletions

View file

@ -29,13 +29,13 @@
1. Install node.js (e.g. via
[nvm](https://github.com/nvm-sh/nvm#installation-and-update))
2. `cd kibicara-frontend`
2. `cd frontend`
3. Install the dependencies with `npm i`
4. Install Angular with `npm i -g @angular/cli`
4. Install Angular with `npm i @angular/cli`
5. Turn off production mode if you have not already (see above in backend).
6. Start the backend in a different terminal
7. To serve and open the application, run `ng s -o`. The application will open
under [http://127.0.0.1:4200](http://127.0.0.1:4200).
7. To serve and open the application, run `node_modules/@angular/cli/bin/ng.js s -o`.
The application will open under [http://127.0.0.1:4200](http://127.0.0.1:4200).
### Creating an account
@ -128,17 +128,29 @@ development team.
## How to implement a new Platform/Social Network
### tl;dr
For transferring messages, Kibicara supports a range of platforms/social
networks, e.g. Mastodon, E-Mail, and Telegram - but more can be added easily.
This guide explains what you need to do to add another platform, e.g. Matrix or
XMPP.
1. Implement the following modules in `platforms/<your-platform>/`:
### Overview:
1. Implement the backend modules in `platforms/<your-platform>/`:
- `bot.py`
- `model.py`
- `webapi.py`
2. Import your bot in `kibicara/webapi/__init__.py`.
3. Generate the FastAPI stuff
4. Generate the angular components for the kibicara-frontend from the FastAPI stuff
3. Generate the FastAPI boilerplate code
4. Generate the angular boilerplate code
5. Copy-paste frontend components from other bots into the angular boilerplate
and adjust them to your needs
### Explanation
At the bottom you can find a checklist what your pull request needs to be
merged into kibicara.
### Step by step
#### Implement the backend modules
In `kibicara/platforms/<your-platform>/bot.py`, you write the functions through
which the platform asks the social network for new messages, and publishes
@ -154,14 +166,90 @@ You will probably need to store the following things:
* platform-specific settings
* anything else your platform needs
In `kibicara/platforms/<your-platform>/webapi.py`, you can define HTTP routes.
You will need them to:
In `kibicara/platforms/<your-platform>/webapi.py`, you can define REST API
routes. You will need them to:
* let admins authenticate to the social network in the kibicara web interface
* update platform-specific settings
#### Import your bot into the kibicara REST API
To run the platform, you need to import the bot in
`kibicara/webapi/__init__.py`.
`kibicara/webapi/__init__.py`. You can see how the other platforms did it.
#### Generate the FastAPI boilerplate code
Whenever you changed the REST API in the backend, you need to re-generate the
FastAPI boilerplate code:
1. Start backend with `kibicara > /dev/null 2>&1 &`
2. Go to the frontend directory: `cd frontend`
3. Use this command to download the openapi.json from the backend and
generate the boilerplate: `npm run openapi-generator`
4. (Now you can stop the backend again, e.g. with `pkill kibicara`)
5. Append `/api` to all relevant URLs:
`find src/app/core/api/ -type f -exec sed -i "s#{this.configuration.basePath}#{this.configuration.basePath}/api#g" {} +`
6. Check if everything is okay (e.g. all api calls need the `/api` prefix)
#### Generate the Angular boilerplate code
##### Generate the platform-specific "public facing" page
Generate boilerplate "card" for myplatform:
```
ng generate component platforms/myplatform/myplatform-bot-card
```
Generate boilerplate for the popup that shows the users the guide on how to use
the bot:
```
ng generate component platforms/myplatform/myplatform-bot-card/myplatform-bot-info-dialog
```
##### Generate the platform-specific "settings" page
Generate bot card for settings page:
```
ng generate component platforms/myplatform/myplatform-settings
```
Generate popup that will show when you click on "Add" to add the myplatform
credentials (e.g. login data, api tokens etc):
```
ng generate component platforms/myplatform/myplatform-settings/mastodon-dialog
```
If something does not work, try to check `platforms.module.ts` and check if the
module was imported there. Every module needs to be imported there
#### Adjust the Angular code for your specific platform
Every frontend part for a bot has a similar structure. Basically copy the
content of the other files e.g. the telegram bot into the generated boilerplate
above and search and replace all occurrences with `myplatform`. You can see
the UI with `ng s -o`, it will auto-update on code change.
A component in angular has 3-4 files, only these ones ending with
`*.component.ts` (typescript) and `*.component.html`(html) are important for
us. Basically the typescript controls what is shown in the html. Please correct
every error that stops the angular page build or shows up on the page while you
go, otherwise this can become a mess.
With that in mind, first write the logic to call the /create endpoint:
- `src/app/platforms/myplatform/myplatform-settings/myplatform-dialog/myplatform-dialog.component.ts`:
implement the form to take the user inputs and the onSubmit() function
- `src/app/platforms/myplatform/myplatform-settings/myplatform-dialog/myplatform-dialog.component.html`:
implement the html skeleton that takes the form from the user
Then, fix up the public user facing page:
- `src/app/platforms/myplatform/myplatform-bot-card/myplatform-bot-info-dialog/myplatform-bot-info-dialog.component.html`
Finally, check the other typescript and html pages and adjust e.g. the tutorial
text for the users.
### Acceptance criteria for bots (Checklist)
@ -200,4 +288,10 @@ A bot should have at least this functionality:
- e.g. Telegram via direct message from the bot
- e.g. E-Mail via e-mail to the user's address
- Web Interface (hood admins)
- Web Interface (hood admins and users)
- A card which allows hood admins to add, configure, start, stop, and
delete a platform to their hood
- A pop-up which explains to hood admins how to configure the platform
- A card which allows users to subscribe on a platform or links to the
platform's account
- A pop-up which explains to users how to use the platform

View file

@ -26,16 +26,15 @@ install_requires =
fastapi
httpx
hypercorn
ormantic @ https://github.com/dl6tom/ormantic/tarball/master#egg=ormantic-0.0.32
Mastodon.py
passlib
peony-twitter[all]
pydantic[email]
pynacl
python-multipart
pytoml
requests
scrypt
Mastodon.py
pydantic[email]
tortoise-orm
[options.packages.find]
where = src
@ -44,6 +43,7 @@ where = src
console_scripts =
kibicara = kibicara.kibicara:Main
kibicara_mda = kibicara.platforms.email.mda:Main
migrate_from_ticketfrei2 = kibicara.migratefromticketfrei:Main
[tox:tox]
envlist = lint, py310
@ -54,19 +54,19 @@ skip_install = True
deps =
black
flake8
mypy
types-requests
commands =
black --check --diff src tests
flake8 src tests
# not yet
#mypy --ignore-missing-imports src tests
[testenv]
deps =
mypy
pytest
pytest-asyncio
types-requests
commands =
# not yet
#mypy --ignore-missing-imports src tests
pytest tests
[flake8]

View file

@ -4,17 +4,13 @@
#
# SPDX-License-Identifier: 0BSD
from nacl.secret import SecretBox
from nacl.utils import random
"""Default configuration.
The default configuration gets overwritten by a configuration file if one exists.
"""
config = {
"database_connection": "sqlite:////tmp/kibicara.sqlite",
"database_connection": "sqlite://:memory:",
"frontend_url": "http://localhost:4200", # url of frontend, change in prod
"secret": random(SecretBox.KEY_SIZE).hex(), # generate with: openssl rand -hex 32
# production params
"frontend_path": None, # required, path to frontend html/css/js files
"production": True,

View file

@ -16,9 +16,9 @@ from fastapi.staticfiles import StaticFiles
from hypercorn.asyncio import serve
from hypercorn.config import Config
from pytoml import load
from tortoise import Tortoise
from kibicara.config import config
from kibicara.model import Mapping
from kibicara.platformapi import Spawner
from kibicara.webapi import router
@ -66,12 +66,26 @@ class Main:
format="%(asctime)s %(name)s %(message)s",
)
getLogger("aiosqlite").setLevel(WARNING)
Mapping.create_all()
asyncio_run(self.__run())
async def __run(self):
await Tortoise.init(
db_url=config["database_connection"],
modules={
"models": [
"kibicara.model",
"kibicara.platforms.email.model",
"kibicara.platforms.mastodon.model",
"kibicara.platforms.telegram.model",
"kibicara.platforms.test.model",
"kibicara.platforms.twitter.model",
]
},
)
await Tortoise.generate_schemas()
await Spawner.init_all()
await self.__start_webserver()
await Tortoise.close_connections()
async def __start_webserver(self):
class SinglePageApplication(StaticFiles):

View file

@ -0,0 +1,225 @@
import sqlite3
import argparse
from time import time, sleep
from os import urandom
from tortoise import Tortoise
from tortoise.exceptions import DoesNotExist
import asyncio
from mastodon import Mastodon
from kibicara.model import Admin, Hood, IncludePattern, ExcludePattern
from kibicara.platforms.mastodon.model import MastodonInstance, MastodonAccount
from kibicara.platforms.telegram.model import Telegram, TelegramSubscriber
from kibicara.platforms.email.model import Email, EmailSubscriber
from kibicara.platforms.twitter.model import Twitter
class OldDataBase:
def __init__(self, old_database_path):
self.conn = sqlite3.connect(old_database_path)
self.cur = self.conn.cursor()
def execute(self, *args, **kwargs):
return self.cur.execute(*args, **kwargs)
def commit(self):
start_time = time()
while 1:
try:
self.conn.commit()
break
except sqlite3.OperationalError:
# another thread may be writing, give it a chance to finish
sleep(0.1)
if time() - start_time > 5:
# if it takes this long, something is wrong
raise
def close(self):
self.conn.close()
class Main:
def __init__(self):
parser = argparse.ArgumentParser()
parser.add_argument(
"old_database_path", help="path to the ticketfrei2 sqlite3 database"
)
parser.add_argument(
"new_database_path",
help="path to the ticketfrei3 sqlite3 database",
default="ticketfrei3.sqlite",
)
args = parser.parse_args()
# open old database
self.old_db = OldDataBase(args.old_database_path)
# open new database
asyncio.run(self.new_database(args.new_database_path))
async def new_database(self, new_database_path):
await Tortoise.init(
db_url=f"sqlite://{new_database_path}",
modules={
"models": [
"kibicara.model",
"kibicara.platforms.email.model",
"kibicara.platforms.mastodon.model",
"kibicara.platforms.telegram.model",
"kibicara.platforms.test.model",
"kibicara.platforms.twitter.model",
]
},
)
await Tortoise.generate_schemas()
# read table per table and write it to new database.
# mastodon instances
old_mastodon_instances = self.old_db.execute(
"SELECT * FROM mastodon_instances"
).fetchall()
for instance in old_mastodon_instances:
url = instance[1]
client_id = instance[2]
client_secret = instance[3]
await MastodonInstance.create(
name=url, client_id=client_id, client_secret=client_secret
)
print(f"Created Mastodon Instance: {url}")
old_users = self.old_db.execute("SELECT * FROM user;").fetchall()
for user in old_users:
user_id = user[0]
user_passhash = user[1]
user_enabled = user[2]
if user_enabled == 0:
print(f"skipping user {user_id}, inactive")
email = self.old_db.execute(
"SELECT email FROM email WHERE user_id=?", (user_id,)
).fetchone()[0]
try:
await Admin.get(email=email)
continue # skip if the user already exists.
except DoesNotExist:
admin = await Admin.create(email=email, passhash=user_passhash)
city = self.old_db.execute(
"SELECT * FROM cities WHERE user_id=?", (user_id,)
).fetchone()
city_name = city[2]
city_markdown = city[3]
hood = await Hood.create(name=city_name, landingpage=city_markdown)
await hood.admins.add(admin)
print()
print(
"Migrated user %s with email %s for the city %s "
"with the following accounts:" % (user_id, email, city_name)
)
patterns = self.old_db.execute(
"SELECT patterns FROM triggerpatterns WHERE user_id=?", (user_id,)
).fetchone()[0]
for pattern in patterns.splitlines():
await IncludePattern.create(hood=hood, pattern=pattern)
badwords = self.old_db.execute(
"SELECT words FROM badwords WHERE user_id=?", (user_id,)
).fetchone()[0]
for badword in badwords.splitlines():
await ExcludePattern.create(hood=hood, pattern=badword)
mastodon_account = self.old_db.execute(
"SELECT * FROM mastodon_accounts WHERE user_id=?", (user_id,)
).fetchone()
if mastodon_account:
instance_url = self.old_db.execute(
"SELECT instance FROM mastodon_instances WHERE id=?",
(mastodon_account[3],),
).fetchone()[0]
new_instance = await MastodonInstance.get(name=instance_url)
access_token = mastodon_account[2]
mastodon_enabled = mastodon_account[4]
await MastodonAccount.create(
hood=hood,
instance=new_instance,
access_token=access_token,
enabled=mastodon_enabled,
)
await dismiss_notifications(hood)
print(f"Mastodon: {instance_url}, {access_token}")
telegram_account = self.old_db.execute(
"SELECT apikey, active FROM telegram_accounts WHERE user_id=?",
(user_id,),
).fetchone()
if telegram_account[0] != "":
telegram_apikey = telegram_account[0]
telegram_enabled = telegram_account[1]
telegram = await Telegram.create(
hood=hood,
api_token=telegram_apikey,
enabled=telegram_enabled,
welcome_message="",
)
telegram_subscribers = self.old_db.execute(
"SELECT subscriber_id FROM telegram_subscribers WHERE user_id=?",
(user_id,),
).fetchall()
for subscriber in telegram_subscribers:
subscriber_id = subscriber[0]
await TelegramSubscriber.create(bot=telegram, user_id=subscriber_id)
print(f"Telegram: {len(telegram_subscribers)} subscribers")
mail_subscribers = self.old_db.execute(
"SELECT email FROM mailinglist WHERE user_id=?", (user_id,)
).fetchall()
if mail_subscribers is not []:
email_name = f"kibicara-{city_name}"
email = await Email.create(
hood=hood, name=email_name, secret=urandom(32).hex()
)
for subscriber in mail_subscribers:
subscriber_email = subscriber[0]
await EmailSubscriber.create(email=subscriber_email, hood=hood)
print(f"E-Mail: {len(mail_subscribers)} subscribers")
twitter = self.old_db.execute(
"SELECT * FROM twitter_accounts WHERE user_id=?", (user_id,)
).fetchone()
if twitter:
client_id = twitter[2]
client_secret = twitter[3]
seen_tweet = self.old_db.execute(
"SELECT tweet_id FROM seen_tweets WHERE user_id=?", (user_id,)
).fetchone()[0]
await Twitter.create(
hood=hood,
dms_since_id=1, # didn't work in ticketfrei2 anyway
mentions_since_id=seen_tweet,
access_token=client_id,
access_token_secret=client_secret,
username=" ",
verified=False,
enabled=True,
)
print(f"Twitter: last ID: {seen_tweet}")
await Tortoise.close_connections()
async def dismiss_notifications(hood):
model = await MastodonAccount.get(hood=hood)
await model.fetch_related("instance")
account = Mastodon(
client_id=model.instance.client_id,
client_secret=model.instance.client_secret,
api_base_url=model.instance.name,
access_token=model.access_token,
)
notifications = account.notifications()
for status in notifications:
account.notifications_dismiss(status["id"])
print(f"Dismissed {len(notifications)} notifications on Mastodon.")

View file

@ -1,4 +1,4 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
@ -6,69 +6,52 @@
"""ORM Models for core."""
from databases import Database
from ormantic import Boolean, ForeignKey, Integer, Model, Text
from sqlalchemy import MetaData, create_engine
from kibicara.config import config
class Mapping:
database = Database(config["database_connection"])
metadata = MetaData()
@classmethod
def create_all(cls):
engine = create_engine(str(cls.database.url))
cls.metadata.create_all(engine)
@classmethod
def drop_all(cls):
engine = create_engine(str(cls.database.url))
cls.metadata.drop_all(engine)
from tortoise import fields
from tortoise.models import Model
class Admin(Model):
id: Integer(primary_key=True) = None
email: Text(unique=True)
passhash: Text()
id = fields.IntField(pk=True)
email = fields.CharField(64, unique=True)
passhash = fields.TextField()
hoods: fields.ManyToManyRelation["Hood"] = fields.ManyToManyField(
"models.Hood", related_name="admins", through="admin_hood_relations"
)
class Mapping(Mapping):
table_name = "admins"
class Meta:
table = "admins"
class Hood(Model):
id: Integer(primary_key=True) = None
name: Text(unique=True)
landingpage: Text()
email_enabled: Boolean() = True
id = fields.IntField(pk=True)
name = fields.CharField(64, unique=True)
landingpage = fields.TextField()
email_enabled = fields.BooleanField(default=True)
admins: fields.ManyToManyRelation[Admin]
include_patterns: fields.ReverseRelation["IncludePattern"]
exclude_patterns: fields.ReverseRelation["ExcludePattern"]
class Mapping(Mapping):
table_name = "hoods"
class Meta:
table = "hoods"
class AdminHoodRelation(Model):
id: Integer(primary_key=True) = None
admin: ForeignKey(Admin)
hood: ForeignKey(Hood)
class IncludePattern(Model):
id = fields.IntField(pk=True)
hood: fields.ForeignKeyRelation[Hood] = fields.ForeignKeyField(
"models.Hood", related_name="include_patterns"
)
pattern = fields.TextField()
class Mapping(Mapping):
table_name = "admin_hood_relations"
class Meta:
table = "include_patterns"
class Trigger(Model):
id: Integer(primary_key=True) = None
hood: ForeignKey(Hood)
pattern: Text()
class ExcludePattern(Model):
id = fields.IntField(pk=True)
hood: fields.ForeignKeyRelation[Hood] = fields.ForeignKeyField(
"models.Hood", related_name="exclude_patterns"
)
pattern = fields.TextField()
class Mapping(Mapping):
table_name = "triggers"
class BadWord(Model):
id: Integer(primary_key=True) = None
hood: ForeignKey(Hood)
pattern: Text()
class Mapping(Mapping):
table_name = "badwords"
class Meta:
table = "exclude_patterns"

View file

@ -6,12 +6,15 @@
"""API classes for implementing bots for platforms."""
from asyncio import Queue, create_task
from asyncio import Queue, Task, create_task
from enum import Enum, auto
from logging import getLogger
from re import IGNORECASE, search
from typing import Generic, Optional, Type, TypeVar
from kibicara.model import BadWord, Trigger
from tortoise.models import Model
from kibicara.model import ExcludePattern, Hood, IncludePattern
logger = getLogger(__name__)
@ -29,7 +32,7 @@ class Message:
**kwargs (object, optional): Other platform-specific data.
"""
def __init__(self, text: str, **kwargs):
def __init__(self, text: str, **kwargs) -> None:
self.text = text
self.__dict__.update(kwargs)
@ -73,11 +76,11 @@ class Censor:
__instances: dict[int, list["Censor"]] = {}
def __init__(self, hood):
def __init__(self, hood: Hood) -> None:
self.hood = hood
self.enabled = True
self._inbox = Queue()
self.__task = None
self._inbox: Queue[Message] = Queue()
self.__task: Optional[Task[None]] = None
self.__hood_censors = self.__instances.setdefault(hood.id, [])
self.__hood_censors.append(self)
self.status = BotStatus.INSTANTIATED
@ -93,7 +96,8 @@ class Censor:
self.__task.cancel()
async def __run(self) -> None:
await self.hood.load()
assert self.__task is not None
await self.hood.refresh_from_db()
self.__task.set_name("{0} {1}".format(self.__class__.__name__, self.hood.name))
try:
self.status = BotStatus.RUNNING
@ -112,7 +116,7 @@ class Censor:
pass
@classmethod
async def destroy_hood(cls, hood) -> None:
async def destroy_hood(cls, hood: Hood) -> None:
"""Remove all of its database entries.
Note: Override this in the derived bot class.
@ -140,25 +144,29 @@ class Censor:
return await self._inbox.get()
async def __is_appropriate(self, message: Message) -> bool:
for badword in await BadWord.objects.filter(hood=self.hood).all():
if search(badword.pattern, message.text, IGNORECASE):
logger.debug(
for exclude in await ExcludePattern.filter(hood=self.hood):
if search(exclude.pattern, message.text, IGNORECASE):
logger.info(
"Matched bad word - dropped message: {0}".format(message.text)
)
return False
for trigger in await Trigger.objects.filter(hood=self.hood).all():
if search(trigger.pattern, message.text, IGNORECASE):
logger.debug(
for include in await IncludePattern.filter(hood=self.hood):
if search(include.pattern, message.text, IGNORECASE):
logger.info(
"Matched trigger - passed message: {0}".format(message.text)
)
return True
logger.debug(
logger.info(
"Did not match any trigger - dropped message: {0}".format(message.text)
)
return False
class Spawner:
ORMClass = TypeVar("ORMClass", bound=Model)
BotClass = TypeVar("BotClass", bound=Censor)
class Spawner(Generic[ORMClass, BotClass]):
"""Spawns a bot with a specific bot model.
Examples:
@ -179,10 +187,10 @@ class Spawner:
__instances: list["Spawner"] = []
def __init__(self, ORMClass, BotClass):
self.ORMClass = ORMClass
self.BotClass = BotClass
self.__bots = {}
def __init__(self, orm_class: Type[ORMClass], bot_class: Type[BotClass]) -> None:
self.ORMClass = orm_class
self.BotClass = bot_class
self.__bots: dict[int, BotClass] = {}
self.__instances.append(self)
@classmethod
@ -192,7 +200,7 @@ class Spawner:
await spawner._init()
@classmethod
async def destroy_hood(cls, hood) -> None:
async def destroy_hood(cls, hood: Hood) -> None:
for spawner in cls.__instances:
for pk in list(spawner.__bots):
bot = spawner.__bots[pk]
@ -202,15 +210,15 @@ class Spawner:
await spawner.BotClass.destroy_hood(hood)
async def _init(self) -> None:
for item in await self.ORMClass.objects.all():
async for item in self.ORMClass.all():
self.start(item)
def start(self, item) -> None:
def start(self, item: ORMClass) -> None:
"""Instantiate and start a bot with the provided ORM object.
Example:
```
xyz = await XYZ.objects.create(hood=hood, **values.__dict__)
xyz = await XYZ.create(hood=hood, **values.__dict__)
spawner.start(xyz)
```
@ -221,7 +229,7 @@ class Spawner:
if bot.enabled:
bot.start()
def stop(self, item) -> None:
def stop(self, item: ORMClass) -> None:
"""Stop and delete a bot.
Args:
@ -231,10 +239,10 @@ class Spawner:
if bot is not None:
bot.stop()
def get(self, item) -> Censor:
def get(self, item: ORMClass) -> BotClass:
"""Get a running bot.
Args:
item (ORM Model object): ORM object corresponding to bot.
"""
return self.__bots.get(item.pk)
return self.__bots[item.pk]

View file

@ -1,6 +1,6 @@
# Copyright (C) 2020 by Maike <maike@systemli.org>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
@ -12,7 +12,7 @@ from kibicara import email
from kibicara.config import config
from kibicara.model import Hood
from kibicara.platformapi import Censor, Spawner
from kibicara.platforms.email.model import Email, EmailSubscribers
from kibicara.platforms.email.model import Email, EmailSubscriber
from kibicara.webapi.admin import to_token
logger = getLogger(__name__)
@ -26,9 +26,9 @@ class EmailBot(Censor):
@classmethod
async def destroy_hood(cls, hood):
"""Removes all its database entries."""
for inbox in await Email.objects.filter(hood=hood).all():
for inbox in await Email.filter(hood=hood).all():
await inbox.delete()
for subscriber in await EmailSubscribers.objects.filter(hood=hood).all():
for subscriber in await EmailSubscriber.filter(hood=hood).all():
await subscriber.delete()
async def run(self):
@ -40,9 +40,7 @@ class EmailBot(Censor):
self.hood.name, message.text
)
)
for subscriber in await EmailSubscribers.objects.filter(
hood=self.hood
).all():
for subscriber in await EmailSubscriber.filter(hood=self.hood).all():
token = to_token(email=subscriber.email, hood=self.hood.id)
body = (
"{0}\n\n--\n"

View file

@ -20,7 +20,7 @@ from pytoml import load
from requests import post
from kibicara.config import config
from kibicara.platforms.email.model import Email, EmailSubscribers
from kibicara.platforms.email.model import Email, EmailSubscriber
logger = getLogger(__name__)
@ -53,7 +53,7 @@ class Main:
async def __run(self, email_name):
try:
email = await Email.objects.get(name=email_name)
email = await Email.get(name=email_name)
except NoMatch:
logger.error("No recipient with this name")
exit(1)
@ -75,7 +75,7 @@ class Main:
logger.error("Could not parse sender")
exit(1)
maybe_subscriber = await EmailSubscribers.objects.filter(email=sender).all()
maybe_subscriber = await EmailSubscriber.filter(email=sender).all()
if len(maybe_subscriber) != 1 or maybe_subscriber[0].hood.id != email.hood.id:
logger.error("Not a subscriber")
exit(1)

View file

@ -1,33 +1,38 @@
# Copyright (C) 2020 by Maike <maike@systemli.org>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
from ormantic import ForeignKey, Integer, Model, Text
from tortoise import fields
from tortoise.models import Model
from kibicara.model import Hood, Mapping
from kibicara.model import Hood
class Email(Model):
"""This table is used to track the names. It also stores the token secret."""
id: Integer(primary_key=True) = None
hood: ForeignKey(Hood)
name: Text(unique=True)
secret: Text()
id = fields.IntField(pk=True)
hood: fields.ForeignKeyRelation[Hood] = fields.ForeignKeyField(
"models.Hood", related_name="platforms_email", unique=True
)
name = fields.CharField(32, unique=True)
secret = fields.TextField()
class Mapping(Mapping):
table_name = "email"
class Meta:
table = "platforms_email"
class EmailSubscribers(Model):
class EmailSubscriber(Model):
"""This table stores all subscribers, who want to receive messages via email."""
id: Integer(primary_key=True) = None
hood: ForeignKey(Hood)
email: Text(unique=True)
id = fields.IntField(pk=True)
hood: fields.ForeignKeyRelation[Hood] = fields.ForeignKeyField(
"models.Hood", related_name="platforms_email_subscribers"
)
email = fields.CharField(64, unique=True)
class Mapping(Mapping):
table_name = "email_subscribers"
class Meta:
table = "platforms_email_subscribers"

View file

@ -1,6 +1,6 @@
# Copyright (C) 2020 by Maike <maike@systemli.org>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
@ -8,18 +8,18 @@
from logging import getLogger
from os import urandom
from smtplib import SMTPException
from sqlite3 import IntegrityError
from fastapi import APIRouter, Depends, HTTPException, Response, status
from nacl import exceptions
from ormantic.exceptions import NoMatch
from pydantic import BaseModel, validator
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara import email
from kibicara.config import config
from kibicara.model import Hood
from kibicara.platformapi import Message
from kibicara.platforms.email.bot import spawner
from kibicara.platforms.email.model import Email, EmailSubscribers
from kibicara.platforms.email.model import Email, EmailSubscriber
from kibicara.webapi.admin import from_token, to_token
from kibicara.webapi.hoods import get_hood, get_hood_unauthorized
@ -53,7 +53,7 @@ class BodySubscriber(BaseModel):
email: str
async def get_email(email_id: int, hood=Depends(get_hood)):
async def get_email(email_id: int, hood: Hood = Depends(get_hood)):
"""Get Email row by hood.
You can specify an email_id to nail it down, but it works without as well.
@ -62,16 +62,16 @@ async def get_email(email_id: int, hood=Depends(get_hood)):
:return: Email row of the found email bot.
"""
try:
return await Email.objects.get(id=email_id, hood=hood)
except NoMatch:
return HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return await Email.get(id=email_id, hood=hood)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
async def get_subscriber(subscriber_id: int, hood=Depends(get_hood)):
async def get_subscriber(subscriber_id: int, hood: Hood = Depends(get_hood)):
try:
return await EmailSubscribers.objects.get(id=subscriber_id, hood=hood)
except NoMatch:
return HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return await EmailSubscriber.get(id=subscriber_id, hood=hood)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
# registers the routes, gets imported in /kibicara/webapi/__init__.py
@ -83,9 +83,9 @@ router = APIRouter()
# TODO response_model
operation_id="get_emails_public",
)
async def email_read_all_public(hood=Depends(get_hood_unauthorized)):
async def email_read_all_public(hood: Hood = Depends(get_hood_unauthorized)):
if hood.email_enabled:
emails = await Email.objects.filter(hood=hood).all()
emails = await Email.filter(hood=hood)
return [BodyEmailPublic(name=email.name) for email in emails]
return []
@ -95,8 +95,8 @@ async def email_read_all_public(hood=Depends(get_hood_unauthorized)):
# TODO response_model
operation_id="get_emails",
)
async def email_read_all(hood=Depends(get_hood)):
return await Email.objects.filter(hood=hood).select_related("hood").all()
async def email_read_all(hood: Hood = Depends(get_hood)):
return await Email.filter(hood=hood)
@router.post(
@ -112,7 +112,7 @@ async def email_create(values: BodyEmail, response: Response, hood=Depends(get_h
:return: Email row of the new email bot.
"""
try:
email = await Email.objects.create(
email = await Email.create(
hood=hood, secret=urandom(32).hex(), **values.__dict__
)
response.headers["Location"] = str(hood.id)
@ -200,7 +200,7 @@ async def email_subscribe(
token,
)
try:
subs = await EmailSubscribers.objects.filter(email=subscriber.email).all()
subs = await EmailSubscriber.filter(email=subscriber.email).all()
if subs:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
email.send_email(
@ -239,7 +239,7 @@ async def email_subscribe_confirm(token, hood=Depends(get_hood_unauthorized)):
if hood.id is not payload["hood"]:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
try:
await EmailSubscribers.objects.create(hood=hood.id, email=payload["email"])
await EmailSubscriber.create(hood=hood, email=payload["email"])
return {}
except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
@ -262,12 +262,12 @@ async def email_unsubscribe(token, hood=Depends(get_hood_unauthorized)):
# If token.hood and url.hood are different, raise an error:
if hood.id is not payload["hood"]:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
subscriber = await EmailSubscribers.objects.filter(
subscriber = await EmailSubscriber.filter(
hood=payload["hood"], email=payload["email"]
).get()
await subscriber.delete()
return Response(status_code=status.HTTP_204_NO_CONTENT)
except NoMatch:
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
except exceptions.CryptoError:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
@ -279,7 +279,7 @@ async def email_unsubscribe(token, hood=Depends(get_hood_unauthorized)):
operation_id="get_subscribers",
)
async def subscribers_read_all(hood=Depends(get_hood)):
return await EmailSubscribers.objects.filter(hood=hood).all()
return await EmailSubscriber.filter(hood=hood).all()
@router.get(
@ -306,7 +306,7 @@ async def email_message_create(
:param hood: Hood the Email bot belongs to.
:return: returns status code 201 if the message is accepted by the censor.
"""
for receiver in await Email.objects.filter(hood=hood).all():
for receiver in await Email.filter(hood=hood).all():
if message.secret == receiver.secret:
# pass message.text to bot.py
if await spawner.get(hood).publish(Message(message.text)):

View file

@ -1,17 +1,17 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
from asyncio import get_event_loop, sleep
from kibicara.platformapi import Censor, Spawner, Message
from kibicara.platforms.mastodon.model import MastodonAccount
from asyncio import gather, get_event_loop, sleep
from logging import getLogger
import re
from mastodon import Mastodon, MastodonError
from asyncio import gather
import re
from kibicara.platformapi import Censor, Spawner, Message
from kibicara.platforms.mastodon.model import MastodonAccount
logger = getLogger(__name__)
@ -26,12 +26,12 @@ class MastodonBot(Censor):
@classmethod
async def destroy_hood(cls, hood):
"""Removes all its database entries."""
for mastodon in await MastodonAccount.objects.filter(hood=hood).all():
for mastodon in await MastodonAccount.filter(hood=hood).all():
await mastodon.delete()
async def run(self):
try:
await self.model.instance.load()
await self.model.fetch_related("instance")
self.account = Mastodon(
client_id=self.model.instance.client_id,
client_secret=self.model.instance.client_secret,

View file

@ -1,30 +1,36 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
from ormantic import ForeignKey, Integer, Text, Boolean, Model
from tortoise import fields
from tortoise.models import Model
from kibicara.model import Hood, Mapping
from kibicara.model import Hood
class MastodonInstance(Model):
id: Integer(primary_key=True) = None
name: Text()
client_id: Text()
client_secret: Text()
id = fields.IntField(pk=True)
name = fields.TextField()
client_id = fields.TextField()
client_secret = fields.TextField()
accounts: fields.ReverseRelation["MastodonAccount"]
class Mapping(Mapping):
table_name = "mastodoninstances"
class Meta:
table = "platforms_mastodon_instances"
class MastodonAccount(Model):
id: Integer(primary_key=True) = None
hood: ForeignKey(Hood)
instance: ForeignKey(MastodonInstance)
access_token: Text()
username: Text(allow_null=True) = None
enabled: Boolean() = False
id = fields.IntField(pk=True)
hood: fields.ForeignKeyRelation[Hood] = fields.ForeignKeyField(
"models.Hood", related_name="platforms_mastodon"
)
instance: fields.ForeignKeyRelation[MastodonInstance] = fields.ForeignKeyField(
"models.MastodonInstance", related_name="accounts"
)
access_token = fields.TextField()
username = fields.TextField(null=True)
enabled = fields.BooleanField()
class Mapping(Mapping):
table_name = "mastodonaccounts"
class Meta:
table = "platforms_mastodon_accounts"

View file

@ -1,24 +1,24 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from asyncio import get_event_loop
from logging import getLogger
from fastapi import APIRouter, Depends, HTTPException, Response, status
from ormantic.exceptions import NoMatch
from mastodon import Mastodon, MastodonNetworkError
from mastodon.errors import MastodonIllegalArgumentError
from pydantic import BaseModel, validate_email, validator
from sqlite3 import IntegrityError
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara.config import config
from kibicara.model import Hood
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, MastodonNetworkError
from mastodon.errors import MastodonIllegalArgumentError
from logging import getLogger
logger = getLogger(__name__)
@ -37,10 +37,12 @@ class BodyMastodonAccount(BaseModel):
return validate_email(value)
async def get_mastodon(mastodon_id, hood=Depends(get_hood)):
async def get_mastodon(
mastodon_id: int, hood: Hood = Depends(get_hood)
) -> MastodonAccount:
try:
return await MastodonAccount.objects.get(id=mastodon_id, hood=hood)
except NoMatch:
return await MastodonAccount.get(id=mastodon_id, hood=hood)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@ -51,16 +53,16 @@ async def get_mastodon_instance(instance_url: str) -> MastodonInstance:
:return the MastodonInstance ORM object
"""
try:
return await MastodonInstance.objects.get(name=instance_url)
except NoMatch:
return await MastodonInstance.get(name=instance_url)
except DoesNotExist:
app_name = config.get("frontend_url")
client_id, client_secret = Mastodon.create_app(
app_name, api_base_url=instance_url
)
await MastodonInstance.objects.create(
await MastodonInstance.create(
name=instance_url, client_id=client_id, client_secret=client_secret
)
return await MastodonInstance.objects.get(name=instance_url)
return await MastodonInstance.get(name=instance_url)
router = APIRouter()
@ -73,13 +75,11 @@ twitter_callback_router = APIRouter()
operation_id="get_mastodons_public",
)
async def mastodon_read_all_public(hood=Depends(get_hood_unauthorized)):
mastodonbots = await MastodonAccount.objects.filter(hood=hood).all()
mbots = []
for mbot in mastodonbots:
async for mbot in MastodonAccount.filter(hood=hood).prefetch_related("instance"):
if mbot.enabled == 1 and mbot.username:
instance = await MastodonInstance.objects.get(id=mbot.instance)
mbots.append(
BodyMastodonPublic(username=mbot.username, instance=instance.name)
BodyMastodonPublic(username=mbot.username, instance=mbot.instance.name)
)
return mbots
@ -90,7 +90,7 @@ async def mastodon_read_all_public(hood=Depends(get_hood_unauthorized)):
operation_id="get_mastodons",
)
async def mastodon_read_all(hood=Depends(get_hood)):
return await MastodonAccount.objects.filter(hood=hood).all()
return await MastodonAccount.filter(hood=hood).all()
@router.delete(
@ -101,8 +101,8 @@ async def mastodon_read_all(hood=Depends(get_hood)):
)
async def mastodon_delete(mastodon=Depends(get_mastodon)):
spawner.stop(mastodon)
await mastodon.instance.load()
object_with_instance = await MastodonAccount.objects.filter(
await mastodon.fetch_related("instance")
object_with_instance = await MastodonAccount.filter(
instance=mastodon.instance
).all()
if len(object_with_instance) == 1 and object_with_instance[0] == mastodon:
@ -172,7 +172,7 @@ async def mastodon_create(values: BodyMastodonAccount, hood=Depends(get_hood)):
None, account.log_in, values.email, values.password
)
logger.debug(f"{access_token}")
mastodon = await MastodonAccount.objects.create(
mastodon = await MastodonAccount.create(
hood=hood, instance=instance, access_token=access_token, enabled=True
)
spawner.start(mastodon)
@ -181,4 +181,5 @@ async def mastodon_create(values: BodyMastodonAccount, hood=Depends(get_hood)):
logger.warning("Login to Mastodon failed.", exc_info=True)
raise HTTPException(status_code=status.HTTP_422_INVALID_INPUT)
except IntegrityError:
logger.warning("Login to Mastodon failed.", exc_info=True)
raise HTTPException(status_code=status.HTTP_409_CONFLICT)

View file

@ -1,17 +1,17 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from asyncio import CancelledError, gather, sleep
from logging import getLogger
from sqlite3 import IntegrityError
from aiogram import Bot, Dispatcher, exceptions, types
from ormantic.exceptions import NoMatch
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara.platformapi import Censor, Message, Spawner
from kibicara.platforms.telegram.model import Telegram, TelegramUser
from kibicara.platforms.telegram.model import Telegram, TelegramSubscriber
logger = getLogger(__name__)
@ -25,8 +25,8 @@ class TelegramBot(Censor):
@classmethod
async def destroy_hood(cls, hood):
"""Removes all its database entries."""
for telegram in await Telegram.objects.filter(hood=hood).all():
for user in await TelegramUser.objects.filter(bot=telegram).all():
for telegram in await Telegram.filter(hood=hood).all():
for user in await TelegramSubscriber.filter(bot=telegram).all():
await user.delete()
await telegram.delete()
@ -69,9 +69,7 @@ class TelegramBot(Censor):
self.telegram_model.hood.name, message.text
)
)
for user in await TelegramUser.objects.filter(
bot=self.telegram_model
).all():
for user in await TelegramSubscriber.filter(bot=self.telegram_model).all():
await self._send_message(user.user_id, message.text)
async def _send_message(self, user_id, message):
@ -116,7 +114,7 @@ class TelegramBot(Censor):
if message.from_user.is_bot:
await message.reply("Error: Bots can not join here.")
return
await TelegramUser.objects.create(
await TelegramSubscriber.create(
user_id=message.from_user.id, bot=self.telegram_model
)
await message.reply(self.telegram_model.welcome_message)
@ -125,12 +123,12 @@ class TelegramBot(Censor):
async def _remove_user(self, message: types.Message):
try:
telegram_user = await TelegramUser.objects.get(
telegram_user = await TelegramSubscriber.get(
user_id=message.from_user.id, bot=self.telegram_model
)
await telegram_user.delete()
await message.reply("You were removed successfully from this bot.")
except NoMatch:
except DoesNotExist:
await message.reply("Error: You are not subscribed to this bot.")
async def _send_help(self, message: types.Message):

View file

@ -1,30 +1,36 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from ormantic import Boolean, ForeignKey, Integer, Model, Text
from tortoise import fields
from tortoise.models import Model
from kibicara.model import Hood, Mapping
from kibicara.model import Hood
class Telegram(Model):
id: Integer(primary_key=True) = None
hood: ForeignKey(Hood)
api_token: Text(unique=True)
welcome_message: Text()
username: Text(allow_null=True) = None
enabled: Boolean() = True
id = fields.IntField(pk=True)
hood: fields.ForeignKeyRelation[Hood] = fields.ForeignKeyField(
"models.Hood", related_name="platforms_telegram"
)
api_token = fields.CharField(64, unique=True)
welcome_message = fields.TextField()
username = fields.TextField(null=True)
enabled = fields.BooleanField(default=True)
subscribers: fields.ReverseRelation["TelegramSubscriber"]
class Mapping(Mapping):
table_name = "telegrambots"
class Meta:
table = "platforms_telegram"
class TelegramUser(Model):
id: Integer(primary_key=True) = None
user_id: Integer(unique=True)
# TODO unique
bot: ForeignKey(Telegram)
class TelegramSubscriber(Model):
id = fields.IntField(pk=True)
bot: fields.ForeignKeyRelation[Telegram] = fields.ForeignKeyField(
"models.Telegram", related_name="subscribers"
)
user_id = fields.IntField()
class Mapping(Mapping):
table_name = "telegramusers"
class Meta:
table = "platforms_telegram_subscribers"

View file

@ -1,19 +1,19 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from logging import getLogger
from sqlite3 import IntegrityError
from aiogram import exceptions
from aiogram.bot.api import check_token
from fastapi import APIRouter, Depends, HTTPException, Response, status
from ormantic.exceptions import NoMatch
from pydantic import BaseModel, validator
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara.platforms.telegram.bot import spawner
from kibicara.platforms.telegram.model import Telegram, TelegramUser
from kibicara.platforms.telegram.model import Telegram, TelegramSubscriber
from kibicara.webapi.hoods import get_hood, get_hood_unauthorized
logger = getLogger(__name__)
@ -38,8 +38,8 @@ class BodyTelegramPublic(BaseModel):
async def get_telegram(telegram_id: int, hood=Depends(get_hood)):
try:
return await Telegram.objects.get(id=telegram_id, hood=hood)
except NoMatch:
return await Telegram.get(id=telegram_id, hood=hood)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@ -53,7 +53,7 @@ telegram_callback_router = APIRouter()
operation_id="get_telegrams_public",
)
async def telegram_read_all_public(hood=Depends(get_hood_unauthorized)):
telegrambots = await Telegram.objects.filter(hood=hood).all()
telegrambots = await Telegram.filter(hood=hood).all()
return [
BodyTelegramPublic(username=telegrambot.username)
for telegrambot in telegrambots
@ -67,7 +67,7 @@ async def telegram_read_all_public(hood=Depends(get_hood_unauthorized)):
operation_id="get_telegrams",
)
async def telegram_read_all(hood=Depends(get_hood)):
return await Telegram.objects.filter(hood=hood).all()
return await Telegram.filter(hood=hood).all()
@router.get(
@ -86,7 +86,7 @@ async def telegram_read(telegram=Depends(get_telegram)):
)
async def telegram_delete(telegram=Depends(get_telegram)):
spawner.stop(telegram)
for user in await TelegramUser.objects.filter(bot=telegram).all():
for user in await TelegramSubscriber.filter(bot=telegram).all():
await user.delete()
await telegram.delete()
return Response(status_code=status.HTTP_204_NO_CONTENT)
@ -102,7 +102,7 @@ async def telegram_create(
response: Response, values: BodyTelegram, hood=Depends(get_hood)
):
try:
telegram = await Telegram.objects.create(hood=hood, **values.__dict__)
telegram = await Telegram.create(hood=hood, **values.__dict__)
spawner.start(telegram)
response.headers["Location"] = str(telegram.id)
return telegram

View file

@ -1,17 +1,17 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
from kibicara.platformapi import Censor, Spawner
from kibicara.platformapi import Censor, Message, Spawner
from kibicara.platforms.test.model import Test
class TestBot(Censor):
def __init__(self, test):
def __init__(self, test: Test):
super().__init__(test.hood)
self.messages = []
self.messages: list[Message] = []
async def run(self):
while True:

View file

@ -1,16 +1,19 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
from ormantic import ForeignKey, Integer, Model
from tortoise import fields
from tortoise.models import Model
from kibicara.model import Hood, Mapping
from kibicara.model import Hood
class Test(Model):
id: Integer(primary_key=True) = None
hood: ForeignKey(Hood)
id = fields.IntField(pk=True)
hood: fields.ForeignKeyRelation[Hood] = fields.ForeignKeyField(
"models.Hood", related_name="platforms_test"
)
class Mapping(Mapping):
table_name = "testapi"
class Meta:
table = "platforms_test"

View file

@ -1,15 +1,14 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
from sqlite3 import IntegrityError
from fastapi import APIRouter, Depends, HTTPException, Response, status
from ormantic.exceptions import NoMatch
from pydantic import BaseModel
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara.model import Hood
from kibicara.platformapi import Message
from kibicara.platforms.test.bot import spawner
from kibicara.platforms.test.model import Test
@ -20,10 +19,10 @@ class BodyMessage(BaseModel):
text: str
async def get_test(test_id: int, hood=Depends(get_hood)):
async def get_test(test_id: int, hood: Hood = Depends(get_hood)) -> Test:
try:
return await Test.objects.get(id=test_id, hood=hood)
except NoMatch:
return await Test.get(id=test_id, hood=hood)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@ -31,14 +30,14 @@ router = APIRouter()
@router.get("/")
async def test_read_all(hood=Depends(get_hood)):
return await Test.objects.filter(hood=hood).all()
async def test_read_all(hood: Hood = Depends(get_hood)):
return await Test.filter(hood=hood)
@router.post("/", status_code=status.HTTP_201_CREATED)
async def test_create(response: Response, hood=Depends(get_hood)):
async def test_create(response: Response, hood: Hood = Depends(get_hood)):
try:
test = await Test.objects.create(hood=hood)
test = await Test.create(hood=hood)
spawner.start(test)
response.headers["Location"] = str(test.id)
return test
@ -47,22 +46,22 @@ async def test_create(response: Response, hood=Depends(get_hood)):
@router.get("/{test_id}")
async def test_read(test=Depends(get_test)):
async def test_read(test: Test = Depends(get_test)):
return test
@router.delete("/{test_id}", status_code=status.HTTP_204_NO_CONTENT)
async def test_delete(test=Depends(get_test)):
async def test_delete(test: Test = Depends(get_test)):
spawner.stop(test)
await test.delete()
@router.get("/{test_id}/messages/")
async def test_message_read_all(test=Depends(get_test)):
async def test_message_read_all(test: Test = Depends(get_test)):
return spawner.get(test).messages
@router.post("/{test_id}/messages/")
async def test_message_create(message: BodyMessage, test=Depends(get_test)):
async def test_message_create(message: BodyMessage, test: Test = Depends(get_test)):
await spawner.get(test).publish(Message(message.text))
return {}

View file

@ -1,5 +1,6 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
@ -27,7 +28,7 @@ class TwitterBot(Censor):
@classmethod
async def destroy_hood(cls, hood):
"""Removes all its database entries."""
for twitter in await Twitter.objects.filter(hood=hood).all():
for twitter in await Twitter.filter(hood=hood).all():
await twitter.delete()
async def run(self):

View file

@ -1,23 +1,27 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from ormantic import Boolean, ForeignKey, Integer, Model, Text
from tortoise import fields
from tortoise.models import Model
from kibicara.model import Hood, Mapping
from kibicara.model import Hood
class Twitter(Model):
id: Integer(primary_key=True) = None
hood: ForeignKey(Hood)
dms_since_id: Integer(allow_null=True) = None
mentions_since_id: Integer(allow_null=True) = None
access_token: Text()
access_token_secret: Text()
username: Text(allow_null=True) = None
verified: Boolean() = False
enabled: Boolean() = False
id = fields.IntField(pk=True)
hood: fields.ForeignKeyRelation[Hood] = fields.ForeignKeyField(
"models.Hood", related_name="platforms_twitter"
)
dms_since_id = fields.IntField()
mentions_since_id = fields.IntField()
access_token = fields.TextField()
access_token_secret = fields.TextField()
username = fields.TextField()
verified = fields.BooleanField(default=False)
enabled = fields.BooleanField(default=False)
class Mapping(Mapping):
table_name = "twitterbots"
class Meta:
table = "platforms_twitter"

View file

@ -1,16 +1,16 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from logging import getLogger
from sqlite3 import IntegrityError
from fastapi import APIRouter, Depends, HTTPException, Response, status
from ormantic.exceptions import NoMatch
from peony.exceptions import NotAuthenticated
from peony.oauth_dance import get_access_token, get_oauth_token
from pydantic import BaseModel
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara.config import config
from kibicara.platforms.twitter.bot import spawner
@ -26,8 +26,8 @@ class BodyTwitterPublic(BaseModel):
async def get_twitter(twitter_id: int, hood=Depends(get_hood)):
try:
return await Twitter.objects.get(id=twitter_id, hood=hood)
except NoMatch:
return await Twitter.get(id=twitter_id, hood=hood)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
@ -41,7 +41,7 @@ twitter_callback_router = APIRouter()
operation_id="get_twitters_public",
)
async def twitter_read_all_public(hood=Depends(get_hood_unauthorized)):
twitterbots = await Twitter.objects.filter(hood=hood).all()
twitterbots = await Twitter.filter(hood=hood).all()
return [
BodyTwitterPublic(username=twitterbot.username)
for twitterbot in twitterbots
@ -55,7 +55,7 @@ async def twitter_read_all_public(hood=Depends(get_hood_unauthorized)):
operation_id="get_twitters",
)
async def twitter_read_all(hood=Depends(get_hood)):
return await Twitter.objects.filter(hood=hood).all()
return await Twitter.filter(hood=hood).all()
@router.get(
@ -125,7 +125,7 @@ async def twitter_create(response: Response, hood=Depends(get_hood)):
"""
try:
# Purge Twitter corpses
for corpse in await Twitter.objects.filter(hood=hood, verified=False).all():
for corpse in await Twitter.filter(hood=hood, verified=False).all():
await corpse.delete()
# Create Twitter
request_token = await get_oauth_token(
@ -137,7 +137,7 @@ async def twitter_create(response: Response, hood=Depends(get_hood)):
)
if request_token["oauth_callback_confirmed"] != "true":
raise HTTPException(status_code=status.HTTP_503_SERVICE_UNAVAILABLE)
twitter = await Twitter.objects.create(
twitter = await Twitter.create(
hood=hood,
access_token=request_token["oauth_token"],
access_token_secret=request_token["oauth_token_secret"],
@ -159,7 +159,7 @@ async def twitter_read_callback(
oauth_token: str, oauth_verifier: str, hood=Depends(get_hood)
):
try:
twitter = await Twitter.objects.filter(access_token=oauth_token).get()
twitter = await Twitter.filter(access_token=oauth_token).get()
access_token = await get_access_token(
config["twitter"]["consumer_key"],
config["twitter"]["consumer_secret"],
@ -167,7 +167,7 @@ async def twitter_read_callback(
twitter.access_token_secret,
oauth_verifier,
)
await twitter.update(
await Twitter.filter(id=twitter).update(
access_token=access_token["oauth_token"],
access_token_secret=access_token["oauth_token_secret"],
verified=True,
@ -177,7 +177,7 @@ async def twitter_read_callback(
return {}
except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
except NoMatch:
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
except (KeyError, ValueError, NotAuthenticated):
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR)

View file

@ -1,4 +1,4 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
@ -20,16 +20,20 @@ from kibicara.platforms.twitter.webapi import router as twitter_router
from kibicara.platforms.twitter.webapi import twitter_callback_router
from kibicara.webapi.admin import router as admin_router
from kibicara.webapi.hoods import router as hoods_router
from kibicara.webapi.hoods.badwords import router as badwords_router
from kibicara.webapi.hoods.triggers import router as triggers_router
from kibicara.webapi.hoods.exclude_patterns import router as exclude_patterns_router
from kibicara.webapi.hoods.include_patterns import router as include_patterns_router
router = APIRouter()
router.include_router(admin_router, prefix="/admin", tags=["admin"])
hoods_router.include_router(
triggers_router, prefix="/{hood_id}/triggers", tags=["triggers"]
include_patterns_router,
prefix="/{hood_id}/triggers",
tags=["include_patterns"],
)
hoods_router.include_router(
badwords_router, prefix="/{hood_id}/badwords", tags=["badwords"]
exclude_patterns_router,
prefix="/{hood_id}/badwords",
tags=["exclude_patterns"],
)
hoods_router.include_router(test_router, prefix="/{hood_id}/test", tags=["test"])
hoods_router.include_router(

View file

@ -1,4 +1,4 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Christian Hagenest <c.hagenest@pm.me>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
@ -11,20 +11,20 @@ from datetime import datetime, timedelta
from logging import getLogger
from pickle import dumps, loads
from smtplib import SMTPException
from sqlite3 import IntegrityError
from fastapi import APIRouter, Depends, HTTPException, Response, status
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from nacl.encoding import URLSafeBase64Encoder
from nacl.exceptions import CryptoError
from nacl.secret import SecretBox
from ormantic.exceptions import NoMatch
from nacl.utils import random
from passlib.hash import argon2
from pydantic import BaseModel, validator
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara import email
from kibicara.config import config
from kibicara.model import Admin, AdminHoodRelation, Hood
from kibicara.model import Admin, Hood
from kibicara.webapi.utils import delete_hood
logger = getLogger(__name__)
@ -54,41 +54,42 @@ class BodyAccessToken(BaseModel):
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/admin/login")
secret_box = SecretBox(bytes.fromhex(config["secret"]))
secret_box = SecretBox(
bytes.fromhex(str(config.get("secret", random(SecretBox.KEY_SIZE).hex())))
)
def to_token(**kwargs):
def to_token(**kwargs) -> str:
return secret_box.encrypt(dumps(kwargs), encoder=URLSafeBase64Encoder).decode(
"ascii"
)
def from_token(token):
def from_token(token: str) -> dict:
return loads(
secret_box.decrypt(token.encode("ascii"), encoder=URLSafeBase64Encoder)
)
async def get_auth(email, password):
async def get_auth(email: str, password: str) -> Admin:
try:
admin = await Admin.objects.get(email=email)
admin = await Admin.get(email=email)
if argon2.verify(password, admin.passhash):
return admin
raise ValueError
except NoMatch:
except DoesNotExist:
raise ValueError
async def get_admin(access_token=Depends(oauth2_scheme)):
async def get_admin(access_token: str = Depends(oauth2_scheme)) -> Admin:
try:
admin = await get_auth(**from_token(access_token))
return await get_auth(**from_token(access_token))
except (CryptoError, ValueError):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid authentication credentials",
headers={"WWW-Authenticate": "Bearer"},
)
return admin
router = APIRouter()
@ -109,9 +110,9 @@ async def admin_register(values: BodyAdmin):
register_token = to_token(**values.__dict__)
logger.debug("register_token={0}".format(register_token))
try:
admin = await Admin.objects.filter(email=values.email).all()
if admin:
if await Admin.exists(email=values.email):
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
# link goes to frontend. this is not the confirm API endpoint below!
body = "{0}/confirm?token={1}".format(config["frontend_url"], register_token)
logger.debug(body)
email.send_email(
@ -138,7 +139,8 @@ async def admin_confirm(register_token: str):
try:
values = from_token(register_token)
passhash = argon2.hash(values["password"])
await Admin.objects.create(email=values["email"], passhash=passhash)
await Admin.create(email=values["email"], passhash=passhash)
# XXX login and registration tokens are exchangeable. does this hurt?
return BodyAccessToken(access_token=register_token)
except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
@ -178,14 +180,14 @@ async def admin_reset_password(values: BodyEmail):
- **email**: E-Mail Address of new hood admin
- **password**: Password of new hood admin
"""
register_token = to_token(datetime=datetime.now().isoformat(), **values.__dict__)
logger.debug("register_token={0}".format(register_token))
reset_token = to_token(datetime=datetime.now().isoformat(), **values.__dict__)
logger.debug("reset_token={0}".format(reset_token))
try:
admin = await Admin.objects.filter(email=values.email).all()
if not admin:
if not await Admin.exists(email=values.email):
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
# link goes to frontend. this is not the reset API endpoint below!
body = "{0}/password-reset?token={1}".format(
config["frontend_url"], register_token
config["frontend_url"], reset_token
)
logger.debug(body)
email.send_email(
@ -213,11 +215,10 @@ async def admin_confirm_reset(reset_token: str, values: BodyPassword):
):
raise HTTPException(status_code=status.HTTP_410_GONE)
passhash = argon2.hash(values.password)
admins = await Admin.objects.filter(email=token_values["email"]).all()
if len(admins) != 1:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
await admins[0].update(passhash=passhash)
await Admin.filter(email=token_values["email"]).update(passhash=passhash)
return BodyAccessToken(access_token=reset_token)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
except CryptoError:
@ -229,11 +230,9 @@ async def admin_confirm_reset(reset_token: str, values: BodyPassword):
# TODO response_model,
operation_id="get_hoods_admin",
)
async def admin_hood_read_all(admin=Depends(get_admin)):
async def admin_hood_read_all(admin: Admin = Depends(get_admin)):
"""Get a list of all hoods of a given admin."""
return (
await AdminHoodRelation.objects.select_related("hood").filter(admin=admin).all()
)
return await Hood.filter(admins=admin)
@router.get(
@ -241,12 +240,8 @@ async def admin_hood_read_all(admin=Depends(get_admin)):
# TODO response_model,
operation_id="get_admin",
)
async def admin_read(admin=Depends(get_admin)):
"""Get a list of all hoods of a given admin."""
admin = await Admin.objects.filter(email=admin.email).all()
if len(admin) != 1:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
return BodyEmail(email=admin[0].email)
async def admin_read(admin: Admin = Depends(get_admin)):
return BodyEmail(email=admin.email)
@router.delete(
@ -254,18 +249,10 @@ async def admin_read(admin=Depends(get_admin)):
status_code=status.HTTP_204_NO_CONTENT,
operation_id="delete_admin",
)
async def admin_delete(admin=Depends(get_admin)):
hood_relations = (
await AdminHoodRelation.objects.select_related("hood").filter(admin=admin).all()
)
for hood in hood_relations:
admins = (
await AdminHoodRelation.objects.select_related("admin")
.filter(hood=hood.id)
.all()
)
if len(admins) == 1 and admins[0].id == admin.id:
actual_hood = await Hood.objects.filter(id=hood.id).all()
await delete_hood(actual_hood[0])
async def admin_delete(admin: Admin = Depends(get_admin)):
async for hood in Hood.filter(admins__contains=admin):
await hood.admins.remove(admin)
await hood.fetch_related("admins")
if len(hood.admins) == 0:
await delete_hood(hood)
await admin.delete()
return Response(status_code=status.HTTP_204_NO_CONTENT)

View file

@ -1,4 +1,4 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
@ -6,13 +6,11 @@
"""REST API Endpoints for managing hoods."""
from sqlite3 import IntegrityError
from fastapi import APIRouter, Depends, HTTPException, Response, status
from ormantic.exceptions import NoMatch
from pydantic import BaseModel
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara.model import AdminHoodRelation, Hood, Trigger
from kibicara.model import Admin, Hood, IncludePattern
from kibicara.platforms.email.bot import spawner
from kibicara.webapi.admin import get_admin
from kibicara.webapi.utils import delete_hood
@ -20,28 +18,26 @@ from kibicara.webapi.utils import delete_hood
class BodyHood(BaseModel):
name: str
landingpage: str = """
Default Landing Page
"""
landingpage: str = "Default Landing Page"
async def get_hood_unauthorized(hood_id: int):
async def get_hood_unauthorized(hood_id: int) -> Hood:
try:
hood = await Hood.objects.get(id=hood_id)
except NoMatch:
return await Hood.get(id=hood_id)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
async def get_hood(
hood: Hood = Depends(get_hood_unauthorized), admin: Admin = Depends(get_admin)
) -> Hood:
await hood.fetch_related("admins")
if admin in hood.admins:
return hood
async def get_hood(hood=Depends(get_hood_unauthorized), admin=Depends(get_admin)):
try:
await AdminHoodRelation.objects.get(admin=admin, hood=hood)
except NoMatch:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
headers={"WWW-Authenticate": "Bearer"},
)
return hood
router = APIRouter()
@ -55,7 +51,7 @@ router = APIRouter()
)
async def hood_read_all():
"""Get all existing hoods."""
return await Hood.objects.all()
return await Hood.all()
@router.post(
@ -65,19 +61,21 @@ async def hood_read_all():
operation_id="create_hood",
tags=["hoods"],
)
async def hood_create(values: BodyHood, response: Response, admin=Depends(get_admin)):
async def hood_create(
values: BodyHood, response: Response, admin: Admin = Depends(get_admin)
):
"""Creates a hood.
- **name**: Name of the hood
- **landingpage**: Markdown formatted description of the hood
"""
try:
hood = await Hood.objects.create(**values.__dict__)
await AdminHoodRelation.objects.create(admin=admin.id, hood=hood.id)
hood = await Hood.create(**values.__dict__)
await admin.hoods.add(hood)
spawner.start(hood)
# Initialize Triggers to match all
await Trigger.objects.create(hood=hood, pattern=".")
await IncludePattern.create(hood=hood, pattern=".")
response.headers["Location"] = str(hood.id)
return hood
@ -91,25 +89,24 @@ async def hood_create(values: BodyHood, response: Response, admin=Depends(get_ad
operation_id="get_hood",
tags=["hoods"],
)
async def hood_read(hood=Depends(get_hood_unauthorized)):
async def hood_read(hood: Hood = Depends(get_hood_unauthorized)):
"""Get hood with id **hood_id**."""
return hood
@router.put(
"/{hood_id}",
status_code=status.HTTP_204_NO_CONTENT,
operation_id="update_hood",
tags=["hoods"],
)
async def hood_update(values: BodyHood, hood=Depends(get_hood)):
async def hood_update(values: BodyHood, hood: Hood = Depends(get_hood)):
"""Updates hood with id **hood_id**.
- **name**: New name of the hood
- **landingpage**: New Markdown formatted description of the hood
"""
await hood.update(**values.__dict__)
return Response(status_code=status.HTTP_204_NO_CONTENT)
await Hood.filter(id=hood).update(**values.__dict__)
return hood
@router.delete(
@ -121,4 +118,3 @@ async def hood_update(values: BodyHood, hood=Depends(get_hood)):
async def hood_delete(hood=Depends(get_hood)):
"""Deletes hood with id **hood_id**."""
await delete_hood(hood)
return Response(status_code=status.HTTP_204_NO_CONTENT)

View file

@ -1,106 +0,0 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
"""REST API endpoints for managing badwords.
Provides API endpoints for adding, removing and reading regular expressions that block a
received message to be dropped by a censor. This provides a message filter customizable
by the hood admins.
"""
from re import compile as regex_compile
from re import error as RegexError
from sqlite3 import IntegrityError
from fastapi import APIRouter, Depends, HTTPException, Response, status
from ormantic.exceptions import NoMatch
from pydantic import BaseModel
from kibicara.model import BadWord
from kibicara.webapi.hoods import get_hood
class BodyBadWord(BaseModel):
pattern: str
async def get_badword(badword_id: int, hood=Depends(get_hood)):
try:
return await BadWord.objects.get(id=badword_id, hood=hood)
except NoMatch:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
router = APIRouter()
@router.get(
"/",
# TODO response_model,
operation_id="get_badwords",
)
async def badword_read_all(hood=Depends(get_hood)):
"""Get all badwords of hood with id **hood_id**."""
return await BadWord.objects.filter(hood=hood).all()
@router.post(
"/",
status_code=status.HTTP_201_CREATED,
# TODO response_model,
operation_id="create_badword",
)
async def badword_create(
values: BodyBadWord, response: Response, hood=Depends(get_hood)
):
"""Creates a new badword for hood with id **hood_id**.
- **pattern**: Regular expression which is used to match a badword.
"""
try:
regex_compile(values.pattern)
badword = await BadWord.objects.create(hood=hood, **values.__dict__)
response.headers["Location"] = str(badword.id)
return badword
except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
except RegexError:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
@router.get(
"/{badword_id}",
# TODO response_model,
operation_id="get_badword",
)
async def badword_read(badword=Depends(get_badword)):
"""Reads badword with id **badword_id** for hood with id **hood_id**."""
return badword
@router.put(
"/{badword_id}",
status_code=status.HTTP_204_NO_CONTENT,
operation_id="update_badword",
)
async def badword_update(values: BodyBadWord, badword=Depends(get_badword)):
"""Updates badword with id **badword_id** for hood with id **hood_id**.
- **pattern**: Regular expression which is used to match a badword
"""
await badword.update(**values.__dict__)
return Response(status_code=status.HTTP_204_NO_CONTENT)
@router.delete(
"/{badword_id}",
status_code=status.HTTP_204_NO_CONTENT,
operation_id="delete_badword",
)
async def badword_delete(badword=Depends(get_badword)):
"""Deletes badword with id **badword_id** for hood with id **hood_id**."""
await badword.delete()
return Response(status_code=status.HTTP_204_NO_CONTENT)

View file

@ -0,0 +1,116 @@
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
"""REST API endpoints for managing exclude_patterns.
Provides API endpoints for adding, removing and reading regular expressions that block a
received message to be dropped by a censor. This provides a message filter customizable
by the hood admins.
"""
from re import compile as regex_compile, error as RegexError
from fastapi import APIRouter, Depends, HTTPException, Response, status
from pydantic import BaseModel
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara.model import ExcludePattern, Hood
from kibicara.webapi.hoods import get_hood
class BodyExcludePattern(BaseModel):
pattern: str
async def get_exclude_pattern(
exclude_pattern_id: int, hood: Hood = Depends(get_hood)
) -> ExcludePattern:
try:
return await ExcludePattern.get(id=exclude_pattern_id, hood=hood)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
router = APIRouter()
@router.get(
"/",
# TODO response_model,
operation_id="get_exclude_patterns",
)
async def exclude_pattern_read_all(hood: Hood = Depends(get_hood)):
"""Get all exclude_patterns of hood with id **hood_id**."""
return await ExcludePattern.filter(hood=hood)
@router.post(
"/",
status_code=status.HTTP_201_CREATED,
# TODO response_model,
operation_id="create_exclude_pattern",
)
async def exclude_pattern_create(
values: BodyExcludePattern, response: Response, hood: Hood = Depends(get_hood)
):
"""Creates a new exclude_pattern for hood with id **hood_id**.
- **pattern**: Regular expression which is used to match a exclude_pattern.
"""
try:
regex_compile(values.pattern)
exclude_pattern = await ExcludePattern.create(hood=hood, **values.__dict__)
response.headers["Location"] = str(exclude_pattern.id)
return exclude_pattern
except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
except RegexError:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
@router.get(
"/{exclude_pattern_id}",
# TODO response_model,
operation_id="get_exclude_pattern",
)
async def exclude_pattern_read(
exclude_pattern: ExcludePattern = Depends(get_exclude_pattern),
):
"""
Reads exclude_pattern with id **exclude_pattern_id** for hood with id **hood_id**.
"""
return exclude_pattern
@router.put(
"/{exclude_pattern_id}",
operation_id="update_exclude_pattern",
)
async def exclude_pattern_update(
values: BodyExcludePattern,
exclude_pattern: ExcludePattern = Depends(get_exclude_pattern),
):
"""
Updates exclude_pattern with id **exclude_pattern_id** for hood with id **hood_id**.
- **pattern**: Regular expression which is used to match a exclude_pattern
"""
await ExcludePattern.filter(id=exclude_pattern).update(**values.__dict__)
return exclude_pattern
@router.delete(
"/{exclude_pattern_id}",
status_code=status.HTTP_204_NO_CONTENT,
operation_id="delete_exclude_pattern",
)
async def exclude_pattern_delete(
exclude_pattern: ExcludePattern = Depends(get_exclude_pattern),
):
"""
Deletes exclude_pattern with id **exclude_pattern_id** for hood with id **hood_id**.
"""
await exclude_pattern.delete()

View file

@ -0,0 +1,115 @@
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
"""REST API endpoints for managing include_patterns.
Provides API endpoints for adding, removing and reading regular expressions that allow a
message to be passed through by a censor. A published message must match one of these
regular expressions otherwise it gets dropped by the censor. This provides a message
filter customizable by the hood admins.
"""
from re import compile as regex_compile, error as RegexError
from fastapi import APIRouter, Depends, HTTPException, Response, status
from pydantic import BaseModel
from tortoise.exceptions import DoesNotExist, IntegrityError
from kibicara.model import IncludePattern, Hood
from kibicara.webapi.hoods import get_hood
class BodyIncludePattern(BaseModel):
pattern: str
async def get_include_pattern(include_pattern_id: int, hood: Hood = Depends(get_hood)):
try:
return await IncludePattern.get(id=include_pattern_id, hood=hood)
except DoesNotExist:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
router = APIRouter()
@router.get(
"/",
# TODO response_model,
operation_id="get_include_patterns",
)
async def include_pattern_read_all(hood: Hood = Depends(get_hood)):
"""Get all include_patterns of hood with id **hood_id**."""
return await IncludePattern.filter(hood=hood)
@router.post(
"/",
status_code=status.HTTP_201_CREATED,
# TODO response_model,
operation_id="create_include_pattern",
)
async def include_pattern_create(
values: BodyIncludePattern, response: Response, hood: Hood = Depends(get_hood)
):
"""Creates a new include_pattern for hood with id **hood_id**.
- **pattern**: Regular expression which is used to match a include_pattern.
"""
try:
regex_compile(values.pattern)
include_pattern = await IncludePattern.create(hood=hood, **values.__dict__)
response.headers["Location"] = str(include_pattern.id)
return include_pattern
except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
except RegexError:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
@router.get(
"/{include_pattern_id}",
# TODO response_model,
operation_id="get_include_pattern",
)
async def include_pattern_read(
include_pattern: IncludePattern = Depends(get_include_pattern),
):
"""
Reads include_pattern with id **include_pattern_id** for hood with id **hood_id**.
"""
return include_pattern
@router.put(
"/{include_pattern_id}",
operation_id="update_include_pattern",
)
async def include_pattern_update(
values: BodyIncludePattern,
include_pattern: IncludePattern = Depends(get_include_pattern),
):
"""
Updates include_pattern with id **include_pattern_id** for hood with id **hood_id**.
- **pattern**: Regular expression which is used to match a include_pattern
"""
await IncludePattern.filter(id=include_pattern).update(**values.__dict__)
return include_pattern
@router.delete(
"/{include_pattern_id}",
status_code=status.HTTP_204_NO_CONTENT,
operation_id="delete_include_pattern",
)
async def include_pattern_delete(
include_pattern: IncludePattern = Depends(get_include_pattern),
):
"""
Deletes include_pattern with id **include_pattern_id** for hood with id **hood_id**.
"""
await include_pattern.delete()

View file

@ -1,107 +0,0 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
"""REST API endpoints for managing triggers.
Provides API endpoints for adding, removing and reading regular expressions that allow a
message to be passed through by a censor. A published message must match one of these
regular expressions otherwise it gets dropped by the censor. This provides a message
filter customizable by the hood admins.
"""
from re import compile as regex_compile
from re import error as RegexError
from sqlite3 import IntegrityError
from fastapi import APIRouter, Depends, HTTPException, Response, status
from ormantic.exceptions import NoMatch
from pydantic import BaseModel
from kibicara.model import Trigger
from kibicara.webapi.hoods import get_hood
class BodyTrigger(BaseModel):
pattern: str
async def get_trigger(trigger_id: int, hood=Depends(get_hood)):
try:
return await Trigger.objects.get(id=trigger_id, hood=hood)
except NoMatch:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)
router = APIRouter()
@router.get(
"/",
# TODO response_model,
operation_id="get_triggers",
)
async def trigger_read_all(hood=Depends(get_hood)):
"""Get all triggers of hood with id **hood_id**."""
return await Trigger.objects.filter(hood=hood).all()
@router.post(
"/",
status_code=status.HTTP_201_CREATED,
# TODO response_model,
operation_id="create_trigger",
)
async def trigger_create(
values: BodyTrigger, response: Response, hood=Depends(get_hood)
):
"""Creates a new trigger for hood with id **hood_id**.
- **pattern**: Regular expression which is used to match a trigger.
"""
try:
regex_compile(values.pattern)
trigger = await Trigger.objects.create(hood=hood, **values.__dict__)
response.headers["Location"] = str(trigger.id)
return trigger
except IntegrityError:
raise HTTPException(status_code=status.HTTP_409_CONFLICT)
except RegexError:
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY)
@router.get(
"/{trigger_id}",
# TODO response_model,
operation_id="get_trigger",
)
async def trigger_read(trigger=Depends(get_trigger)):
"""Reads trigger with id **trigger_id** for hood with id **hood_id**."""
return trigger
@router.put(
"/{trigger_id}",
status_code=status.HTTP_204_NO_CONTENT,
operation_id="update_trigger",
)
async def trigger_update(values: BodyTrigger, trigger=Depends(get_trigger)):
"""Updates trigger with id **trigger_id** for hood with id **hood_id**.
- **pattern**: Regular expression which is used to match a trigger
"""
await trigger.update(**values.__dict__)
return Response(status_code=status.HTTP_204_NO_CONTENT)
@router.delete(
"/{trigger_id}",
status_code=status.HTTP_204_NO_CONTENT,
operation_id="delete_trigger",
)
async def trigger_delete(trigger=Depends(get_trigger)):
"""Deletes trigger with id **trigger_id** for hood with id **hood_id**."""
await trigger.delete()
return Response(status_code=status.HTTP_204_NO_CONTENT)

View file

@ -1,17 +1,14 @@
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
#
# SPDX-License-Identifier: 0BSD
from kibicara.model import AdminHoodRelation, BadWord, Trigger
from kibicara.model import ExcludePattern, Hood, IncludePattern
from kibicara.platformapi import Spawner
async def delete_hood(hood):
async def delete_hood(hood: Hood) -> None:
await Spawner.destroy_hood(hood)
for trigger in await Trigger.objects.filter(hood=hood).all():
await trigger.delete()
for badword in await BadWord.objects.filter(hood=hood).all():
await badword.delete()
for relation in await AdminHoodRelation.objects.filter(hood=hood).all():
await relation.delete()
await IncludePattern.filter(hood=hood).delete()
await ExcludePattern.filter(hood=hood).delete()
await hood.delete()

View file

@ -1,4 +1,4 @@
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020, 2023 by Thomas Lindner <tom@dl6tom.de>
# Copyright (C) 2020 by Christian Hagenest <c.hagenest@pm.me>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
@ -7,24 +7,43 @@
from urllib.parse import urlparse
from fastapi import FastAPI, status
from fastapi.testclient import TestClient
from pytest import fixture
from httpx import AsyncClient
import pytest
from tortoise import Tortoise
from kibicara import email
from kibicara.model import Mapping
from kibicara.webapi import router
@fixture(scope="module")
def client():
Mapping.drop_all()
Mapping.create_all()
@pytest.fixture(scope="session")
def anyio_backend():
return "asyncio"
@pytest.fixture(scope="session")
@pytest.mark.anyio
async def client():
await Tortoise.init(
db_url="sqlite://:memory:",
modules={
"models": [
"kibicara.model",
"kibicara.platforms.email.model",
"kibicara.platforms.mastodon.model",
"kibicara.platforms.telegram.model",
"kibicara.platforms.test.model",
"kibicara.platforms.twitter.model",
]
},
)
await Tortoise.generate_schemas()
app = FastAPI()
app.include_router(router, prefix="/api")
return TestClient(app)
yield AsyncClient(app=app, base_url="http://test")
await Tortoise.close_connections()
@fixture(scope="module")
@pytest.fixture(scope="session")
def monkeymodule():
from _pytest.monkeypatch import MonkeyPatch
@ -33,7 +52,7 @@ def monkeymodule():
mpatch.undo()
@fixture(scope="module")
@pytest.fixture(scope="session")
def receive_email(monkeymodule):
mailbox = []
@ -47,47 +66,54 @@ def receive_email(monkeymodule):
return mock_receive_email
@fixture(scope="module")
def register_token(client, receive_email):
response = client.post(
@pytest.fixture(scope="session")
@pytest.mark.anyio
async def register_token(client, receive_email):
response = await client.post(
"/api/admin/register/", json={"email": "user", "password": "password"}
)
assert response.status_code == status.HTTP_202_ACCEPTED
return urlparse(receive_email()["body"]).query.split("=", 1)[1]
@fixture(scope="module")
def register_confirmed(client, register_token):
response = client.post("/api/admin/confirm/{0}".format(register_token))
@pytest.fixture(scope="session")
@pytest.mark.anyio
async def register_confirmed(client, register_token):
response = await client.post("/api/admin/confirm/{0}".format(register_token))
assert response.status_code == status.HTTP_200_OK
@fixture(scope="module")
def access_token(client, register_confirmed):
response = client.post(
@pytest.fixture(scope="session")
@pytest.mark.anyio
async def access_token(client, register_confirmed):
response = await client.post(
"/api/admin/login/", data={"username": "user", "password": "password"}
)
assert response.status_code == status.HTTP_200_OK
return response.json()["access_token"]
@fixture(scope="module")
@pytest.fixture(scope="session")
def auth_header(access_token):
return {"Authorization": "Bearer {0}".format(access_token)}
@fixture(scope="function")
def hood_id(client, auth_header):
response = client.post("/api/hoods/", json={"name": "hood"}, headers=auth_header)
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def hood_id(client, auth_header):
response = await client.post(
"/api/hoods/", json={"name": "hood"}, headers=auth_header
)
assert response.status_code == status.HTTP_201_CREATED
hood_id = int(response.headers["Location"])
yield hood_id
client.delete("/api/hoods/{0}".format(hood_id), headers=auth_header)
await client.delete("/api/hoods/{0}".format(hood_id), headers=auth_header)
@fixture(scope="function")
def trigger_id(client, hood_id, auth_header):
response = client.post(
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def trigger_id(client, hood_id, auth_header):
response = await client.post(
"/api/hoods/{0}/triggers/".format(hood_id),
json={"pattern": "test"},
headers=auth_header,
@ -95,14 +121,15 @@ def trigger_id(client, hood_id, auth_header):
assert response.status_code == status.HTTP_201_CREATED
trigger_id = int(response.headers["Location"])
yield trigger_id
client.delete(
await client.delete(
"/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id), headers=auth_header
)
@fixture(scope="function")
def badword_id(client, hood_id, auth_header):
response = client.post(
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def badword_id(client, hood_id, auth_header):
response = await client.post(
"/api/hoods/{0}/badwords/".format(hood_id),
json={"pattern": ""},
headers=auth_header,
@ -110,19 +137,20 @@ def badword_id(client, hood_id, auth_header):
assert response.status_code == status.HTTP_201_CREATED
badword_id = int(response.headers["Location"])
yield badword_id
client.delete(
await client.delete(
"/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id), headers=auth_header
)
@fixture(scope="function")
def test_id(client, hood_id, auth_header):
response = client.post(
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def test_id(client, hood_id, auth_header):
response = await client.post(
"/api/hoods/{0}/test/".format(hood_id), json={}, headers=auth_header
)
assert response.status_code == status.HTTP_201_CREATED
test_id = int(response.headers["Location"])
yield test_id
client.delete(
await client.delete(
"/api/hoods/{0}/test/{1}".format(hood_id, test_id), headers=auth_header
)

View file

@ -3,13 +3,16 @@
# SPDX-License-Identifier: 0BSD
from fastapi import status
import pytest
def test_hoods_unauthorized(client):
response = client.get("/api/admin/hoods/")
@pytest.mark.anyio
async def test_hoods_unauthorized(client):
response = await client.get("/api/admin/hoods/")
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_hoods_success(client, auth_header):
response = client.get("/api/admin/hoods/", headers=auth_header)
@pytest.mark.anyio
async def test_hoods_success(client, auth_header):
response = await client.get("/api/admin/hoods/", headers=auth_header)
assert response.status_code == status.HTTP_200_OK

View file

@ -3,80 +3,107 @@
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
import pytest
from fastapi import status
def test_hood_read_all(client):
response = client.get("/api/hoods/")
@pytest.mark.anyio
async def test_hood_read_all(client):
response = await client.get("/api/hoods/")
assert response.status_code == status.HTTP_200_OK
def test_hood_create_unauthorized(client, hood_id):
response = client.post("/api/hoods/")
@pytest.mark.anyio
async def test_hood_create_unauthorized(client, hood_id):
response = await client.post("/api/hoods/")
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_hood_read(client, hood_id):
response = client.get("/api/hoods/{0}".format(hood_id))
@pytest.mark.anyio
async def test_hood_read(client, hood_id):
response = await client.get("/api/hoods/{0}".format(hood_id))
assert response.status_code == status.HTTP_200_OK
def test_hood_update_unauthorized(client, hood_id):
response = client.put("/api/hoods/{0}".format(hood_id))
@pytest.mark.anyio
async def test_hood_update_unauthorized(client, hood_id):
response = await client.put("/api/hoods/{0}".format(hood_id))
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_hood_delete_unauthorized(client, hood_id):
response = client.delete("/api/hoods/{0}".format(hood_id))
@pytest.mark.anyio
async def test_hood_delete_unauthorized(client, hood_id):
response = await client.delete("/api/hoods/{0}".format(hood_id))
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_trigger_read_all_unauthorized(client, hood_id):
response = client.get("/api/hoods/{0}/triggers/".format(hood_id))
@pytest.mark.anyio
async def test_trigger_read_all_unauthorized(client, hood_id):
response = await client.get("/api/hoods/{0}/triggers/".format(hood_id))
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_trigger_create_unauthorized(client, hood_id):
response = client.post("/api/hoods/{0}/triggers/".format(hood_id))
@pytest.mark.anyio
async def test_trigger_create_unauthorized(client, hood_id):
response = await client.post("/api/hoods/{0}/triggers/".format(hood_id))
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_trigger_read_unauthorized(client, hood_id, trigger_id):
response = client.get("/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id))
@pytest.mark.anyio
async def test_trigger_read_unauthorized(client, hood_id, trigger_id):
response = await client.get(
"/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_trigger_update_unauthorized(client, hood_id, trigger_id):
response = client.put("/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id))
@pytest.mark.anyio
async def test_trigger_update_unauthorized(client, hood_id, trigger_id):
response = await client.put(
"/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_trigger_delete_unauthorized(client, hood_id, trigger_id):
response = client.delete("/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id))
@pytest.mark.anyio
async def test_trigger_delete_unauthorized(client, hood_id, trigger_id):
response = await client.delete(
"/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_badword_read_all_unauthorized(client, hood_id):
response = client.get("/api/hoods/{0}/badwords/".format(hood_id))
@pytest.mark.anyio
async def test_badword_read_all_unauthorized(client, hood_id):
response = await client.get("/api/hoods/{0}/badwords/".format(hood_id))
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_badword_create_unauthorized(client, hood_id):
response = client.post("/api/hoods/{0}/badwords/".format(hood_id))
@pytest.mark.anyio
async def test_badword_create_unauthorized(client, hood_id):
response = await client.post("/api/hoods/{0}/badwords/".format(hood_id))
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_badword_read_unauthorized(client, hood_id, badword_id):
response = client.get("/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id))
@pytest.mark.anyio
async def test_badword_read_unauthorized(client, hood_id, badword_id):
response = await client.get(
"/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_badword_update_unauthorized(client, hood_id, badword_id):
response = client.put("/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id))
@pytest.mark.anyio
async def test_badword_update_unauthorized(client, hood_id, badword_id):
response = await client.put(
"/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_badword_delete_unauthorized(client, hood_id, badword_id):
response = client.delete("/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id))
@pytest.mark.anyio
async def test_badword_delete_unauthorized(client, hood_id, badword_id):
response = await client.delete(
"/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -6,12 +6,13 @@
from fastapi import status
from pytest import fixture
import pytest
@fixture(scope="function")
def email_row(client, hood_id, auth_header):
response = client.post(
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def email_row(client, hood_id, auth_header):
response = await client.post(
"/api/hoods/{0}/email/".format(hood_id),
json={"name": "kibicara-test"},
headers=auth_header,
@ -19,6 +20,6 @@ def email_row(client, hood_id, auth_header):
assert response.status_code == status.HTTP_201_CREATED
email_id = int(response.headers["Location"])
yield response.json()
client.delete(
await client.delete(
"/api/hoods/{0}/email/{1}".format(hood_id, email_id), headers=auth_header
)

View file

@ -8,13 +8,14 @@ from re import findall
from urllib.parse import urlparse
from fastapi import status
from pytest import skip
import pytest
from kibicara.webapi.admin import to_token
def test_email_subscribe_unsubscribe(client, hood_id, receive_email):
response = client.post(
@pytest.mark.anyio
async def test_email_subscribe_unsubscribe(client, hood_id, receive_email):
response = await client.post(
"/api/hoods/{0}/email/subscribe/".format(hood_id),
json={"email": "test@localhost"},
)
@ -27,37 +28,41 @@ def test_email_subscribe_unsubscribe(client, hood_id, receive_email):
body,
)[0]
start = len("token=")
response = client.post(
response = await client.post(
"/api/hoods/{0}/email/subscribe/confirm/{1}".format(
hood_id, urlparse(confirm_url).query[start:]
)
)
assert response.status_code == status.HTTP_201_CREATED
response = client.post(
response = await client.post(
"/api/hoods/{0}/email/subscribe/confirm/{1}".format(
hood_id, urlparse(confirm_url).query[start:]
)
)
assert response.status_code == status.HTTP_409_CONFLICT
token = to_token(email=mail["to"], hood=hood_id)
response = client.delete(
response = await client.delete(
"/api/hoods/{0}/email/unsubscribe/{1}".format(hood_id, token)
)
assert response.status_code == status.HTTP_204_NO_CONTENT
def test_email_message(client, hood_id, trigger_id, email_row):
@pytest.mark.anyio
async def test_email_message(client, hood_id, trigger_id, email_row):
body = {
"text": "test",
"author": "test@localhost",
"secret": email_row["secret"],
}
response = client.post("/api/hoods/{0}/email/messages/".format(hood_id), json=body)
response = await client.post(
"/api/hoods/{0}/email/messages/".format(hood_id), json=body
)
assert response.status_code == status.HTTP_201_CREATED
def test_email_send_mda(trigger_id, email_row):
skip("Only works if kibicara is listening on port 8000, and only sometimes")
@pytest.mark.anyio
async def test_email_send_mda(trigger_id, email_row):
pytest.skip("Only works if kibicara is listening on port 8000, and only sometimes")
mail = """From test@example.com Tue Jun 16 15:33:19 2020
Return-path: <test@example.com>
Envelope-to: hood@localhost

View file

@ -5,21 +5,27 @@
# SPDX-License-Identifier: 0BSD
from fastapi import status
import pytest
def test_email_create_unauthorized(client, hood_id):
response = client.post("/api/hoods/{0}/email/".format(hood_id))
@pytest.mark.anyio
async def test_email_create_unauthorized(client, hood_id):
response = await client.post("/api/hoods/{0}/email/".format(hood_id))
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_email_delete_unauthorized(client, hood_id, email_row):
response = client.delete(
@pytest.mark.anyio
async def test_email_delete_unauthorized(client, hood_id, email_row):
response = await client.delete(
"/api/hoods/{0}/email/{1}".format(hood_id, email_row["id"])
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_email_message_unauthorized(client, hood_id, email_row):
@pytest.mark.anyio
async def test_email_message_unauthorized(client, hood_id, email_row):
body = {"text": "test", "author": "author", "secret": "wrong"}
response = client.post("/api/hoods/{0}/email/messages/".format(hood_id), json=body)
response = await client.post(
"/api/hoods/{0}/email/messages/".format(hood_id), json=body
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -5,16 +5,19 @@
from fastapi import status
from nacl.exceptions import CryptoError
import pytest
def test_email_subscribe_empty(client, hood_id):
response = client.post("/api/hoods/{0}/email/subscribe/".format(hood_id))
@pytest.mark.anyio
async def test_email_subscribe_empty(client, hood_id):
response = await client.post("/api/hoods/{0}/email/subscribe/".format(hood_id))
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_email_subscribe_confirm_wrong_token(client, hood_id):
@pytest.mark.anyio
async def test_email_subscribe_confirm_wrong_token(client, hood_id):
try:
response = client.post(
response = await client.post(
"/api/hoods/{0}/email/subscribe/confirm/".format(hood_id)
+ "asdfasdfasdfasdfasdfasdfasdfasdf"
)
@ -23,26 +26,31 @@ def test_email_subscribe_confirm_wrong_token(client, hood_id):
pass
def test_email_subscribe_confirm_wrong_hood(client):
response = client.delete(
@pytest.mark.anyio
async def test_email_subscribe_confirm_wrong_hood(client):
response = await client.delete(
"/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf"
)
assert response.json()["detail"] == "Not Found"
def test_email_message_wrong(client, hood_id, email_row):
@pytest.mark.anyio
async def test_email_message_wrong(client, hood_id, email_row):
body = {
"text": "",
"author": "test@localhost",
"secret": email_row["secret"],
}
response = client.post("/api/hoods/{0}/email/messages/".format(hood_id), json=body)
response = await client.post(
"/api/hoods/{0}/email/messages/".format(hood_id), json=body
)
assert response.status_code == status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS
def test_email_unsubscribe_wrong_token(client, hood_id):
@pytest.mark.anyio
async def test_email_unsubscribe_wrong_token(client, hood_id):
try:
client.delete(
await client.delete(
"/api/hoods/{0}/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf".format(
hood_id
)
@ -51,8 +59,9 @@ def test_email_unsubscribe_wrong_token(client, hood_id):
pass
def test_email_unsubscribe_wrong_hood(client):
response = client.delete(
@pytest.mark.anyio
async def test_email_unsubscribe_wrong_hood(client):
response = await client.delete(
"/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf"
)
assert response.json()["detail"] == "Not Found"

View file

View file

@ -1,34 +1,33 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from pytest import fixture
import pytest
from kibicara.model import Hood
from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
@fixture(scope="function")
def mastodon_instance(event_loop):
return event_loop.run_until_complete(
MastodonInstance.objects.create(
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def mastodon_instance():
return await MastodonInstance.create(
name="inst4nce",
client_id="cl13nt_id",
client_secret="cl13nt_s3cr3t",
)
)
@fixture(scope="function")
def mastodon_account(event_loop, hood_id, mastodon_instance):
hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id))
return event_loop.run_until_complete(
MastodonAccount.objects.create(
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def mastodon_account(hood_id, mastodon_instance):
hood = await Hood.get(id=hood_id)
return await MastodonAccount.create(
hood=hood,
instance=mastodon_instance,
access_token="t0k3n",
enabled=True,
username="us3r",
)
)

View file

@ -1,17 +1,18 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from fastapi import status
from pytest import fixture, mark
import pytest
from mastodon.Mastodon import Mastodon
from kibicara.platforms import mastodon
from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
from kibicara.platforms.mastodon.model import MastodonAccount
@fixture(scope="function")
@pytest.fixture(scope="function")
def disable_spawner(monkeypatch):
class DoNothing:
def start(self, bot):
@ -20,7 +21,7 @@ def disable_spawner(monkeypatch):
monkeypatch.setattr(mastodon.webapi, "spawner", DoNothing())
@mark.parametrize(
@pytest.mark.parametrize(
"body",
[
{
@ -30,8 +31,8 @@ def disable_spawner(monkeypatch):
}
],
)
def test_mastodon_create_bot(
event_loop,
@pytest.mark.anyio
async def test_mastodon_create_bot(
client,
disable_spawner,
hood_id,
@ -44,7 +45,7 @@ def test_mastodon_create_bot(
monkeypatch.setattr(Mastodon, "log_in", log_in_mock)
response = client.post(
response = await client.post(
"/api/hoods/{0}/mastodon/".format(hood_id),
json=body,
headers=auth_header,
@ -52,33 +53,20 @@ def test_mastodon_create_bot(
print(response.json())
assert response.status_code == status.HTTP_201_CREATED
bot_id = response.json()["id"]
mastodon_obj = event_loop.run_until_complete(MastodonAccount.objects.get(id=bot_id))
mastodon_obj = await MastodonAccount.get(id=bot_id)
assert response.json()["access_token"] == mastodon_obj.access_token
mastodon_instance = event_loop.run_until_complete(
MastodonInstance.objects.get(id=mastodon_obj.instance.id)
)
assert (
response.json()["instance"]["name"]
== body["instance_url"]
== mastodon_instance.name
)
assert response.json()["hood"]["id"] == mastodon_obj.hood.id
assert response.json()["instance"]["client_id"] == mastodon_instance.client_id
assert (
response.json()["instance"]["client_secret"] == mastodon_instance.client_secret
)
assert mastodon_obj.enabled
@mark.parametrize(
@pytest.mark.parametrize(
"body",
[
{"instance_url": "botsin.space", "email": "notanemail", "password": "asdf1234"},
{"instance_url": "wrong", "email": "asdf@example.org", "password": "asdf1234"},
],
)
def test_mastodon_invalid_input(
event_loop,
@pytest.mark.anyio
async def test_mastodon_invalid_input(
client,
disable_spawner,
hood_id,
@ -86,7 +74,7 @@ def test_mastodon_invalid_input(
monkeypatch,
body,
):
response = client.post(
response = await client.post(
"/api/hoods/{0}/mastodon/".format(hood_id),
json=body,
headers=auth_header,
@ -94,13 +82,15 @@ def test_mastodon_invalid_input(
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_mastodon_create_mastodon_invalid_id(client, auth_header):
response = client.post("/api/hoods/1337/mastodon/", headers=auth_header)
@pytest.mark.anyio
async def test_mastodon_create_mastodon_invalid_id(client, auth_header):
response = await client.post("/api/hoods/1337/mastodon/", headers=auth_header)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.post("/api/hoods/wrong/mastodon/", headers=auth_header)
response = await client.post("/api/hoods/wrong/mastodon/", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_mastodon_create_unauthorized(client, hood_id):
response = client.post("/api/hoods/{hood_id}/mastodon/")
@pytest.mark.anyio
async def test_mastodon_create_unauthorized(client, hood_id):
response = await client.post("/api/hoods/{hood_id}/mastodon/")
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -1,46 +1,48 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from fastapi import status
from ormantic.exceptions import NoMatch
from pytest import raises
import pytest
from tortoise.exceptions import DoesNotExist
from kibicara.platforms.mastodon.model import MastodonAccount
def test_mastodon_delete_bot(client, event_loop, mastodon_account, auth_header):
response = client.delete(
@pytest.mark.anyio
async def test_mastodon_delete_bot(client, mastodon_account, auth_header):
response = await client.delete(
"/api/hoods/{0}/mastodon/{1}".format(
mastodon_account.hood.id, mastodon_account.id
),
headers=auth_header,
)
assert response.status_code == status.HTTP_204_NO_CONTENT
with raises(NoMatch):
event_loop.run_until_complete(
MastodonAccount.objects.get(id=mastodon_account.id)
)
with pytest.raises(DoesNotExist):
await MastodonAccount.get(id=mastodon_account.id)
def test_mastodon_delete_bot_invalid_id(client, auth_header, hood_id):
response = client.delete("/api/hoods/1337/mastodon/123", headers=auth_header)
@pytest.mark.anyio
async def test_mastodon_delete_bot_invalid_id(client, auth_header, hood_id):
response = await client.delete("/api/hoods/1337/mastodon/123", headers=auth_header)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.delete("/api/hoods/wrong/mastodon/123", headers=auth_header)
response = await client.delete("/api/hoods/wrong/mastodon/123", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
response = client.delete(
response = await client.delete(
"/api/hoods/{0}/mastodon/7331".format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.delete(
response = await client.delete(
"/api/hoods/{0}/mastodon/wrong".format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_404_NOT_FOUND
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_mastodon_delete_bot_unauthorized(client, mastodon_account):
response = client.delete(
@pytest.mark.anyio
async def test_mastodon_delete_bot_unauthorized(client, mastodon_account):
response = await client.delete(
"/api/hoods/{0}/mastodon/{1}".format(
mastodon_account.hood.id, mastodon_account.id
)

View file

@ -1,28 +1,30 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from fastapi import status
import pytest
from kibicara.platforms.mastodon.model import MastodonAccount
def test_mastodon_get_bots(
client, auth_header, event_loop, hood_id, mastodon_account, mastodon_instance
@pytest.mark.anyio
async def test_mastodon_get_bots(
client, auth_header, hood_id, mastodon_account, mastodon_instance
):
mastodon2 = event_loop.run_until_complete(
MastodonAccount.objects.create(
mastodon2 = await MastodonAccount.create(
hood=mastodon_account.hood,
instance=mastodon_instance,
access_token="4cc3ss",
enabled=True,
username="us4r",
)
response = await client.get(
"/api/hoods/{0}/mastodon/".format(mastodon_account.hood.id), headers=auth_header
)
response = client.get(
"/api/hoods/{0}/mastodon".format(mastodon_account.hood.id), headers=auth_header
)
print(response.headers)
assert response.status_code == status.HTTP_200_OK
assert response.json()[0]["id"] == mastodon_account.id
assert response.json()[0]["access_token"] == mastodon_account.access_token
@ -30,20 +32,23 @@ def test_mastodon_get_bots(
assert response.json()[1]["access_token"] == mastodon2.access_token
def test_mastodon_get_bots_invalid_id(client, auth_header, hood_id):
response = client.get("/api/hoods/1337/mastodon", headers=auth_header)
@pytest.mark.anyio
async def test_mastodon_get_bots_invalid_id(client, auth_header, hood_id):
response = await client.get("/api/hoods/1337/mastodon/", headers=auth_header)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.get("/api/hoods/wrong/mastodon", headers=auth_header)
response = await client.get("/api/hoods/wrong/mastodon/", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_mastodon_get_bots_unauthorized(client, hood_id):
response = client.get("/api/hoods/{0}/mastodon".format(hood_id))
@pytest.mark.anyio
async def test_mastodon_get_bots_unauthorized(client, hood_id):
response = await client.get("/api/hoods/{0}/mastodon/".format(hood_id))
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_mastodon_public(client, mastodon_account, mastodon_instance, event_loop):
response = client.get(
@pytest.mark.anyio
async def test_mastodon_public(client, mastodon_account, mastodon_instance):
response = await client.get(
"/api/hoods/{0}/mastodon/public".format(mastodon_account.hood.id)
)
assert response.json()[0]["username"] == mastodon_account.username

View file

@ -1,21 +1,21 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from pytest import fixture
import pytest
from kibicara.model import Hood
from kibicara.platforms.telegram.model import Telegram
@fixture(scope="function")
def telegram(event_loop, hood_id, bot):
hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id))
return event_loop.run_until_complete(
Telegram.objects.create(
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def telegram(hood_id, bot):
hood = await Hood.get(id=hood_id)
return await Telegram.create(
hood=hood,
api_token=bot["api_token"],
welcome_message=bot["welcome_message"],
)
)

View file

@ -1,16 +1,17 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from fastapi import status
from pytest import fixture, mark
import pytest
from kibicara.platforms import telegram
from kibicara.platforms.telegram.model import Telegram
@fixture(scope="function")
@pytest.fixture(scope="function")
def disable_spawner(monkeypatch):
class DoNothing:
def start(self, bot):
@ -19,9 +20,9 @@ def disable_spawner(monkeypatch):
monkeypatch.setattr(telegram.webapi, "spawner", DoNothing())
@mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
def test_telegram_create_bot(
event_loop,
@pytest.mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
@pytest.mark.anyio
async def test_telegram_create_bot(
client,
disable_spawner,
hood_id,
@ -34,27 +35,26 @@ def test_telegram_create_bot(
monkeypatch.setattr(telegram.webapi, "check_token", check_token_mock)
response = client.post(
response = await client.post(
"/api/hoods/{0}/telegram/".format(hood_id),
json=body,
headers=auth_header,
)
assert response.status_code == status.HTTP_201_CREATED
bot_id = response.json()["id"]
telegram_obj = event_loop.run_until_complete(Telegram.objects.get(id=bot_id))
telegram_obj = await Telegram.get(id=bot_id)
assert response.json()["api_token"] == body["api_token"] == telegram_obj.api_token
assert (
response.json()["welcome_message"]
== body["welcome_message"]
== telegram_obj.welcome_message
)
assert response.json()["hood"]["id"] == telegram_obj.hood.id
assert telegram_obj.enabled
@mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
def test_telegram_invalid_api_token(
event_loop,
@pytest.mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
@pytest.mark.anyio
async def test_telegram_invalid_api_token(
client,
disable_spawner,
hood_id,
@ -62,7 +62,7 @@ def test_telegram_invalid_api_token(
monkeypatch,
body,
):
response = client.post(
response = await client.post(
"/api/hoods/{0}/telegram/".format(hood_id),
json=body,
headers=auth_header,
@ -70,13 +70,15 @@ def test_telegram_invalid_api_token(
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_telegram_create_telegram_invalid_id(client, auth_header):
response = client.post("/api/hoods/1337/telegram/", headers=auth_header)
@pytest.mark.anyio
async def test_telegram_create_telegram_invalid_id(client, auth_header):
response = await client.post("/api/hoods/1337/telegram/", headers=auth_header)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.post("/api/hoods/wrong/telegram/", headers=auth_header)
response = await client.post("/api/hoods/wrong/telegram/", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_telegram_create_unauthorized(client, hood_id):
response = client.post("/api/hoods/{hood_id}/telegram/")
@pytest.mark.anyio
async def test_telegram_create_unauthorized(client, hood_id):
response = await client.post("/api/hoods/{hood_id}/telegram/")
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -1,52 +1,56 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from fastapi import status
from ormantic.exceptions import NoMatch
from pytest import mark, raises
import pytest
from tortoise.exceptions import DoesNotExist
from kibicara.platforms.telegram.model import Telegram, TelegramUser
from kibicara.platforms.telegram.model import Telegram, TelegramSubscriber
@mark.parametrize("bot", [{"api_token": "apitoken123", "welcome_message": "msg"}])
def test_telegram_delete_bot(client, event_loop, bot, telegram, auth_header):
event_loop.run_until_complete(
TelegramUser.objects.create(user_id=1234, bot=telegram.id)
)
event_loop.run_until_complete(
TelegramUser.objects.create(user_id=5678, bot=telegram.id)
)
response = client.delete(
@pytest.mark.parametrize(
"bot", [{"api_token": "apitoken123", "welcome_message": "msg"}]
)
@pytest.mark.anyio
async def test_telegram_delete_bot(client, bot, telegram, auth_header):
await TelegramSubscriber.create(user_id=1234, bot=telegram)
await TelegramSubscriber.create(user_id=5678, bot=telegram)
response = await client.delete(
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id),
headers=auth_header,
)
assert response.status_code == status.HTTP_204_NO_CONTENT
with raises(NoMatch):
event_loop.run_until_complete(Telegram.objects.get(id=telegram.id))
with raises(NoMatch):
event_loop.run_until_complete(TelegramUser.objects.get(id=telegram.id))
with pytest.raises(DoesNotExist):
await Telegram.get(id=telegram.id)
with pytest.raises(DoesNotExist):
await TelegramSubscriber.get(id=telegram.id)
def test_telegram_delete_bot_invalid_id(client, auth_header, hood_id):
response = client.delete("/api/hoods/1337/telegram/123", headers=auth_header)
@pytest.mark.anyio
async def test_telegram_delete_bot_invalid_id(client, auth_header, hood_id):
response = await client.delete("/api/hoods/1337/telegram/123", headers=auth_header)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.delete("/api/hoods/wrong/telegram/123", headers=auth_header)
response = await client.delete("/api/hoods/wrong/telegram/123", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
response = client.delete(
response = await client.delete(
"/api/hoods/{0}/telegram/7331".format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.delete(
response = await client.delete(
"/api/hoods/{0}/telegram/wrong".format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
@mark.parametrize("bot", [{"api_token": "apitoken123", "welcome_message": "msg"}])
def test_telegram_delete_bot_unauthorized(client, bot, telegram):
response = client.delete(
@pytest.mark.parametrize(
"bot", [{"api_token": "apitoken123", "welcome_message": "msg"}]
)
@pytest.mark.anyio
async def test_telegram_delete_bot_unauthorized(client, bot, telegram):
response = await client.delete(
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -4,12 +4,15 @@
# SPDX-License-Identifier: 0BSD
from fastapi import status
from pytest import mark
import pytest
@mark.parametrize("bot", [{"api_token": "apitoken123", "welcome_message": "msg"}])
def test_telegram_get_bot(client, auth_header, event_loop, bot, telegram):
response = client.get(
@pytest.mark.parametrize(
"bot", [{"api_token": "apitoken123", "welcome_message": "msg"}]
)
@pytest.mark.anyio
async def test_telegram_get_bot(client, auth_header, bot, telegram):
response = await client.get(
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id),
headers=auth_header,
)
@ -19,24 +22,28 @@ def test_telegram_get_bot(client, auth_header, event_loop, bot, telegram):
assert response.json()["welcome_message"] == telegram.welcome_message
def test_telegram_get_bot_invalid_id(client, auth_header, hood_id):
response = client.get("/api/hoods/1337/telegram/123", headers=auth_header)
@pytest.mark.anyio
async def test_telegram_get_bot_invalid_id(client, auth_header, hood_id):
response = await client.get("/api/hoods/1337/telegram/123", headers=auth_header)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.get("/api/hoods/wrong/telegram/123", headers=auth_header)
response = await client.get("/api/hoods/wrong/telegram/123", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
response = client.get(
response = await client.get(
"/api/hoods/{0}/telegram/7331".format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.get(
response = await client.get(
"/api/hoods/{0}/telegram/wrong".format(hood_id), headers=auth_header
)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
@mark.parametrize("bot", [{"api_token": "apitoken456", "welcome_message": "msg"}])
def test_telegram_get_bot_unauthorized(client, bot, telegram):
response = client.get(
@pytest.mark.parametrize(
"bot", [{"api_token": "apitoken456", "welcome_message": "msg"}]
)
@pytest.mark.anyio
async def test_telegram_get_bot_unauthorized(client, bot, telegram):
response = await client.get(
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -1,32 +1,31 @@
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from fastapi import status
import pytest
from kibicara.model import Hood
from kibicara.platforms.telegram.model import Telegram
def test_telegram_get_bots(client, auth_header, event_loop, hood_id):
hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id))
telegram0 = event_loop.run_until_complete(
Telegram.objects.create(
@pytest.mark.anyio
async def test_telegram_get_bots(client, auth_header, hood_id):
hood = await Hood.get(id=hood_id)
telegram0 = await Telegram.create(
hood=hood,
api_token="api_token123",
welcome_message="welcome_message123",
)
)
telegram1 = event_loop.run_until_complete(
Telegram.objects.create(
telegram1 = await Telegram.create(
hood=hood,
api_token="api_token456",
welcome_message="welcome_message123",
)
)
response = client.get(
"/api/hoods/{0}/telegram".format(telegram0.hood.id), headers=auth_header
response = await client.get(
"/api/hoods/{0}/telegram/".format(telegram0.hood.id), headers=auth_header
)
assert response.status_code == status.HTTP_200_OK
assert response.json()[0]["id"] == telegram0.id
@ -35,13 +34,15 @@ def test_telegram_get_bots(client, auth_header, event_loop, hood_id):
assert response.json()[1]["api_token"] == telegram1.api_token
def test_telegram_get_bots_invalid_id(client, auth_header, hood_id):
response = client.get("/api/hoods/1337/telegram", headers=auth_header)
@pytest.mark.anyio
async def test_telegram_get_bots_invalid_id(client, auth_header, hood_id):
response = await client.get("/api/hoods/1337/telegram/", headers=auth_header)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.get("/api/hoods/wrong/telegram", headers=auth_header)
response = await client.get("/api/hoods/wrong/telegram/", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_telegram_get_bots_unauthorized(client, hood_id):
response = client.get("/api/hoods/{0}/telegram".format(hood_id))
@pytest.mark.anyio
async def test_telegram_get_bots_unauthorized(client, hood_id):
response = await client.get("/api/hoods/{0}/telegram/".format(hood_id))
assert response.status_code == status.HTTP_401_UNAUTHORIZED