2020-07-19 11:14:45 +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>
|
2020-07-19 11:14:45 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
|
|
|
from fastapi import status
|
2020-10-13 08:35:20 +00:00
|
|
|
from pytest import fixture, mark
|
|
|
|
|
2020-07-19 11:14:45 +00:00
|
|
|
from kibicara.platforms import telegram
|
|
|
|
from kibicara.platforms.telegram.model import Telegram
|
|
|
|
|
|
|
|
|
2023-03-18 17:47:02 +00:00
|
|
|
@fixture(scope="function")
|
2020-07-19 11:14:45 +00:00
|
|
|
def disable_spawner(monkeypatch):
|
|
|
|
class DoNothing:
|
|
|
|
def start(self, bot):
|
|
|
|
assert bot is not None
|
|
|
|
|
2023-03-18 17:47:02 +00:00
|
|
|
monkeypatch.setattr(telegram.webapi, "spawner", DoNothing())
|
2020-07-19 11:14:45 +00:00
|
|
|
|
|
|
|
|
2023-03-18 17:47:02 +00:00
|
|
|
@mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
|
2020-07-19 11:14:45 +00:00
|
|
|
def test_telegram_create_bot(
|
2020-09-12 16:42:52 +00:00
|
|
|
event_loop,
|
|
|
|
client,
|
|
|
|
disable_spawner,
|
|
|
|
hood_id,
|
|
|
|
auth_header,
|
|
|
|
monkeypatch,
|
|
|
|
body,
|
2020-07-19 11:14:45 +00:00
|
|
|
):
|
|
|
|
def check_token_mock(token):
|
|
|
|
return True
|
|
|
|
|
2023-03-18 17:47:02 +00:00
|
|
|
monkeypatch.setattr(telegram.webapi, "check_token", check_token_mock)
|
2020-07-19 11:14:45 +00:00
|
|
|
|
|
|
|
response = client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/telegram/".format(hood_id),
|
2020-09-12 16:42:52 +00:00
|
|
|
json=body,
|
|
|
|
headers=auth_header,
|
2020-07-19 11:14:45 +00:00
|
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
2023-03-18 17:47:02 +00:00
|
|
|
bot_id = response.json()["id"]
|
2020-07-19 11:14:45 +00:00
|
|
|
telegram_obj = event_loop.run_until_complete(Telegram.objects.get(id=bot_id))
|
2023-03-18 17:47:02 +00:00
|
|
|
assert response.json()["api_token"] == body["api_token"] == telegram_obj.api_token
|
2020-07-19 11:14:45 +00:00
|
|
|
assert (
|
2023-03-18 17:47:02 +00:00
|
|
|
response.json()["welcome_message"]
|
|
|
|
== body["welcome_message"]
|
2020-07-19 11:14:45 +00:00
|
|
|
== telegram_obj.welcome_message
|
|
|
|
)
|
2023-03-18 17:47:02 +00:00
|
|
|
assert response.json()["hood"]["id"] == telegram_obj.hood.id
|
2020-07-19 11:14:45 +00:00
|
|
|
assert telegram_obj.enabled
|
|
|
|
|
|
|
|
|
2023-03-18 17:47:02 +00:00
|
|
|
@mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
|
2020-07-19 11:14:45 +00:00
|
|
|
def test_telegram_invalid_api_token(
|
2020-09-12 16:42:52 +00:00
|
|
|
event_loop,
|
|
|
|
client,
|
|
|
|
disable_spawner,
|
|
|
|
hood_id,
|
|
|
|
auth_header,
|
|
|
|
monkeypatch,
|
|
|
|
body,
|
2020-07-19 11:14:45 +00:00
|
|
|
):
|
|
|
|
response = client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/telegram/".format(hood_id),
|
2020-09-12 16:42:52 +00:00
|
|
|
json=body,
|
|
|
|
headers=auth_header,
|
2020-07-19 11:14:45 +00:00
|
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
|
|
|
|
|
|
|
|
|
|
|
def test_telegram_create_telegram_invalid_id(client, auth_header):
|
2023-03-18 17:47:02 +00:00
|
|
|
response = client.post("/api/hoods/1337/telegram/", headers=auth_header)
|
2020-07-19 11:14:45 +00:00
|
|
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
2023-03-18 17:47:02 +00:00
|
|
|
response = client.post("/api/hoods/wrong/telegram/", headers=auth_header)
|
2020-07-19 11:14:45 +00:00
|
|
|
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
|
|
|
|
|
|
|
|
|
|
|
def test_telegram_create_unauthorized(client, hood_id):
|
2023-03-18 17:47:02 +00:00
|
|
|
response = client.post("/api/hoods/{hood_id}/telegram/")
|
2020-07-19 11:14:45 +00:00
|
|
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|