ticketfrei3/backend/tests/tests_telegram/test_api_telegram_create_bot.py

85 lines
2.5 KiB
Python
Raw Permalink Normal View History

# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
2023-03-19 18:03:25 +00:00
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
#
# SPDX-License-Identifier: 0BSD
from fastapi import status
2023-04-01 13:23:33 +00:00
import pytest
2020-10-13 08:35:20 +00:00
from kibicara.platforms import telegram
from kibicara.platforms.telegram.model import Telegram
2023-04-01 13:23:33 +00:00
@pytest.fixture(scope="function")
def disable_spawner(monkeypatch):
class DoNothing:
def start(self, bot):
assert bot is not None
monkeypatch.setattr(telegram.webapi, "spawner", DoNothing())
2023-04-01 13:23:33 +00:00
@pytest.mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
@pytest.mark.anyio
async def test_telegram_create_bot(
2023-04-01 13:32:10 +00:00
client,
2020-09-12 16:42:52 +00:00
disable_spawner,
hood_id,
auth_header,
monkeypatch,
body,
):
def check_token_mock(token):
return True
monkeypatch.setattr(telegram.webapi, "check_token", check_token_mock)
2023-04-01 13:32:10 +00:00
response = await client.post(
"/api/hoods/{0}/telegram/".format(hood_id),
2020-09-12 16:42:52 +00:00
json=body,
headers=auth_header,
)
assert response.status_code == status.HTTP_201_CREATED
bot_id = response.json()["id"]
2023-03-19 18:03:25 +00:00
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 telegram_obj.enabled
2023-04-01 13:23:33 +00:00
@pytest.mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
@pytest.mark.anyio
async def test_telegram_invalid_api_token(
2023-04-01 13:32:10 +00:00
client,
2020-09-12 16:42:52 +00:00
disable_spawner,
hood_id,
auth_header,
monkeypatch,
body,
):
2023-04-01 13:32:10 +00:00
response = await client.post(
"/api/hoods/{0}/telegram/".format(hood_id),
2020-09-12 16:42:52 +00:00
json=body,
headers=auth_header,
)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
2023-04-01 13:23:33 +00:00
@pytest.mark.anyio
2023-04-01 13:32:10 +00:00
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
2023-04-01 13:32:10 +00:00
response = await client.post("/api/hoods/wrong/telegram/", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
2023-04-01 13:23:33 +00:00
@pytest.mark.anyio
2023-04-01 13:32:10 +00:00
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