2020-07-19 12:02:43 +00:00
|
|
|
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
|
2020-09-28 22:39:32 +00:00
|
|
|
# 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>
|
2020-07-19 12:02:43 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
|
|
|
from fastapi import status
|
2023-04-01 13:23:33 +00:00
|
|
|
import pytest
|
2023-03-19 18:03:25 +00:00
|
|
|
from tortoise.exceptions import DoesNotExist
|
2020-07-19 12:02:43 +00:00
|
|
|
|
2023-03-19 18:03:25 +00:00
|
|
|
from kibicara.platforms.telegram.model import Telegram, TelegramSubscriber
|
2020-10-13 08:35:20 +00:00
|
|
|
|
2020-07-19 12:02:43 +00:00
|
|
|
|
2023-04-01 13:23:33 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"bot", [{"api_token": "apitoken123", "welcome_message": "msg"}]
|
|
|
|
)
|
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def test_telegram_delete_bot(client, bot, telegram, auth_header):
|
2023-03-19 18:03:25 +00:00
|
|
|
await TelegramSubscriber.create(user_id=1234, bot=telegram)
|
|
|
|
await TelegramSubscriber.create(user_id=5678, bot=telegram)
|
2023-04-01 13:32:10 +00:00
|
|
|
response = await client.delete(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id),
|
2020-10-12 20:47:06 +00:00
|
|
|
headers=auth_header,
|
2020-07-19 12:02:43 +00:00
|
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
2023-03-19 18:03:25 +00:00
|
|
|
with pytest.raises(DoesNotExist):
|
|
|
|
await Telegram.get(id=telegram.id)
|
|
|
|
with pytest.raises(DoesNotExist):
|
|
|
|
await TelegramSubscriber.get(id=telegram.id)
|
2020-07-19 12:02:43 +00:00
|
|
|
|
|
|
|
|
2023-04-01 13:23:33 +00:00
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
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)
|
2020-07-19 12:02:43 +00:00
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
2023-04-01 13:32:10 +00:00
|
|
|
response = await client.delete("/api/hoods/wrong/telegram/123", headers=auth_header)
|
2020-07-19 12:02:43 +00:00
|
|
|
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
2023-04-01 13:32:10 +00:00
|
|
|
response = await client.delete(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/telegram/7331".format(hood_id), headers=auth_header
|
2020-10-12 20:47:06 +00:00
|
|
|
)
|
2020-07-19 12:02:43 +00:00
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
2023-04-01 13:32:10 +00:00
|
|
|
response = await client.delete(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/telegram/wrong".format(hood_id), headers=auth_header
|
2020-07-19 12:02:43 +00:00
|
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
|
|
|
|
|
|
|
|
2023-04-01 13:23:33 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"bot", [{"api_token": "apitoken123", "welcome_message": "msg"}]
|
|
|
|
)
|
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def test_telegram_delete_bot_unauthorized(client, bot, telegram):
|
|
|
|
response = await client.delete(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id)
|
2020-10-12 20:47:06 +00:00
|
|
|
)
|
2020-07-19 12:02:43 +00:00
|
|
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|