[tests] Ported telegram tests to async
This commit is contained in:
parent
f0e15f1d37
commit
16c4ebc84b
|
@ -3,19 +3,18 @@
|
|||
#
|
||||
# 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.objects.get(id=hood_id)
|
||||
return await Telegram.objects.create(
|
||||
hood=hood,
|
||||
api_token=bot["api_token"],
|
||||
welcome_message=bot["welcome_message"],
|
||||
)
|
||||
)
|
||||
|
|
|
@ -4,13 +4,13 @@
|
|||
# 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,10 +19,11 @@ def disable_spawner(monkeypatch):
|
|||
monkeypatch.setattr(telegram.webapi, "spawner", DoNothing())
|
||||
|
||||
|
||||
@mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
|
||||
def test_telegram_create_bot(
|
||||
@pytest.mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
|
||||
@pytest.mark.anyio
|
||||
async def test_telegram_create_bot(
|
||||
event_loop,
|
||||
client,
|
||||
asyncclient,
|
||||
disable_spawner,
|
||||
hood_id,
|
||||
auth_header,
|
||||
|
@ -34,14 +35,14 @@ def test_telegram_create_bot(
|
|||
|
||||
monkeypatch.setattr(telegram.webapi, "check_token", check_token_mock)
|
||||
|
||||
response = client.post(
|
||||
response = await asyncclient.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.objects.get(id=bot_id)
|
||||
assert response.json()["api_token"] == body["api_token"] == telegram_obj.api_token
|
||||
assert (
|
||||
response.json()["welcome_message"]
|
||||
|
@ -52,17 +53,18 @@ def test_telegram_create_bot(
|
|||
assert telegram_obj.enabled
|
||||
|
||||
|
||||
@mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
|
||||
def test_telegram_invalid_api_token(
|
||||
@pytest.mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
|
||||
@pytest.mark.anyio
|
||||
async def test_telegram_invalid_api_token(
|
||||
event_loop,
|
||||
client,
|
||||
asyncclient,
|
||||
disable_spawner,
|
||||
hood_id,
|
||||
auth_header,
|
||||
monkeypatch,
|
||||
body,
|
||||
):
|
||||
response = client.post(
|
||||
response = await asyncclient.post(
|
||||
"/api/hoods/{0}/telegram/".format(hood_id),
|
||||
json=body,
|
||||
headers=auth_header,
|
||||
|
@ -70,13 +72,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(asyncclient, auth_header):
|
||||
response = await asyncclient.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 asyncclient.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(asyncclient, hood_id):
|
||||
response = await asyncclient.post("/api/hoods/{hood_id}/telegram/")
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
|
|
@ -5,48 +5,55 @@
|
|||
|
||||
from fastapi import status
|
||||
from ormantic.exceptions import NoMatch
|
||||
from pytest import mark, raises
|
||||
import pytest
|
||||
|
||||
from kibicara.platforms.telegram.model import Telegram, TelegramUser
|
||||
|
||||
|
||||
@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)
|
||||
@pytest.mark.parametrize(
|
||||
"bot", [{"api_token": "apitoken123", "welcome_message": "msg"}]
|
||||
)
|
||||
event_loop.run_until_complete(
|
||||
TelegramUser.objects.create(user_id=5678, bot=telegram.id)
|
||||
)
|
||||
response = client.delete(
|
||||
@pytest.mark.anyio
|
||||
async def test_telegram_delete_bot(asyncclient, event_loop, bot, telegram, auth_header):
|
||||
await TelegramUser.objects.create(user_id=1234, bot=telegram.id)
|
||||
await TelegramUser.objects.create(user_id=5678, bot=telegram.id)
|
||||
response = await asyncclient.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(NoMatch):
|
||||
await Telegram.objects.get(id=telegram.id)
|
||||
with pytest.raises(NoMatch):
|
||||
await TelegramUser.objects.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(asyncclient, auth_header, hood_id):
|
||||
response = await asyncclient.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 asyncclient.delete(
|
||||
"/api/hoods/wrong/telegram/123", headers=auth_header
|
||||
)
|
||||
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
||||
response = client.delete(
|
||||
response = await asyncclient.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 asyncclient.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(asyncclient, bot, telegram):
|
||||
response = await asyncclient.delete(
|
||||
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id)
|
||||
)
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
|
|
@ -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(asyncclient, auth_header, event_loop, bot, telegram):
|
||||
response = await asyncclient.get(
|
||||
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id),
|
||||
headers=auth_header,
|
||||
)
|
||||
|
@ -19,24 +22,32 @@ 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(asyncclient, auth_header, hood_id):
|
||||
response = await asyncclient.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 asyncclient.get(
|
||||
"/api/hoods/wrong/telegram/123", headers=auth_header
|
||||
)
|
||||
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
||||
response = client.get(
|
||||
response = await asyncclient.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 asyncclient.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(asyncclient, bot, telegram):
|
||||
response = await asyncclient.get(
|
||||
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id)
|
||||
)
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
|
|
@ -4,29 +4,27 @@
|
|||
# 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(asyncclient, auth_header, event_loop, hood_id):
|
||||
hood = await Hood.objects.get(id=hood_id)
|
||||
telegram0 = await Telegram.objects.create(
|
||||
hood=hood,
|
||||
api_token="api_token123",
|
||||
welcome_message="welcome_message123",
|
||||
)
|
||||
)
|
||||
telegram1 = event_loop.run_until_complete(
|
||||
Telegram.objects.create(
|
||||
telegram1 = await Telegram.objects.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 asyncclient.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 +33,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(asyncclient, auth_header, hood_id):
|
||||
response = await asyncclient.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 asyncclient.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(asyncclient, hood_id):
|
||||
response = await asyncclient.get("/api/hoods/{0}/telegram/".format(hood_id))
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
|
Loading…
Reference in a new issue