[tests] Get rid of non-async test client

This commit is contained in:
missytake 2023-04-01 15:30:42 +02:00
parent 16c4ebc84b
commit f829bf8694
7 changed files with 45 additions and 50 deletions

View file

@ -7,30 +7,20 @@
from urllib.parse import urlparse
from fastapi import FastAPI, status
from fastapi.testclient import TestClient
from httpx import AsyncClient
from pytest import fixture
import pytest
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()
app = FastAPI()
app.include_router(router, prefix="/api")
return TestClient(app)
@fixture(scope="module")
@pytest.fixture(scope="module")
def anyio_backend():
return "asyncio"
@fixture(scope="module")
@pytest.fixture(scope="module")
def asyncclient():
Mapping.drop_all()
Mapping.create_all()
@ -39,7 +29,7 @@ def asyncclient():
return AsyncClient(app=app, base_url="http://test")
@fixture(scope="module")
@pytest.fixture(scope="module")
def monkeymodule():
from _pytest.monkeypatch import MonkeyPatch
@ -48,7 +38,7 @@ def monkeymodule():
mpatch.undo()
@fixture(scope="module")
@pytest.fixture(scope="module")
def receive_email(monkeymodule):
mailbox = []
@ -62,47 +52,54 @@ def receive_email(monkeymodule):
return mock_receive_email
@fixture(scope="module")
def register_token(client, receive_email):
response = client.post(
@pytest.fixture(scope="module")
@pytest.mark.anyio
async def register_token(asyncclient, receive_email):
response = await asyncclient.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="module")
@pytest.mark.anyio
async def register_confirmed(asyncclient, register_token):
response = await asyncclient.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="module")
@pytest.mark.anyio
async def access_token(asyncclient, register_confirmed):
response = await asyncclient.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="module")
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(asyncclient, auth_header):
response = await asyncclient.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 asyncclient.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(asyncclient, hood_id, auth_header):
response = await asyncclient.post(
"/api/hoods/{0}/triggers/".format(hood_id),
json={"pattern": "test"},
headers=auth_header,
@ -110,14 +107,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 asyncclient.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(asyncclient, hood_id, auth_header):
response = await asyncclient.post(
"/api/hoods/{0}/badwords/".format(hood_id),
json={"pattern": ""},
headers=auth_header,
@ -125,19 +123,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 asyncclient.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(asyncclient, hood_id, auth_header):
response = await asyncclient.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 asyncclient.delete(
"/api/hoods/{0}/test/{1}".format(hood_id, test_id), headers=auth_header
)

View file

@ -11,7 +11,7 @@ from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def mastodon_instance(event_loop):
async def mastodon_instance():
return await MastodonInstance.objects.create(
name="inst4nce",
client_id="cl13nt_id",
@ -21,7 +21,7 @@ async def mastodon_instance(event_loop):
@pytest.fixture(scope="function")
@pytest.mark.anyio
async def mastodon_account(event_loop, hood_id, mastodon_instance):
async def mastodon_account(hood_id, mastodon_instance):
hood = await Hood.objects.get(id=hood_id)
return await MastodonAccount.objects.create(
hood=hood,

View file

@ -32,7 +32,6 @@ def disable_spawner(monkeypatch):
)
@pytest.mark.anyio
async def test_mastodon_create_bot(
event_loop,
asyncclient,
disable_spawner,
hood_id,
@ -78,7 +77,6 @@ async def test_mastodon_create_bot(
)
@pytest.mark.anyio
async def test_mastodon_invalid_input(
event_loop,
asyncclient,
disable_spawner,
hood_id,

View file

@ -22,7 +22,6 @@ def disable_spawner(monkeypatch):
@pytest.mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
@pytest.mark.anyio
async def test_telegram_create_bot(
event_loop,
asyncclient,
disable_spawner,
hood_id,
@ -56,7 +55,6 @@ async def test_telegram_create_bot(
@pytest.mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}])
@pytest.mark.anyio
async def test_telegram_invalid_api_token(
event_loop,
asyncclient,
disable_spawner,
hood_id,

View file

@ -14,7 +14,7 @@ from kibicara.platforms.telegram.model import Telegram, TelegramUser
"bot", [{"api_token": "apitoken123", "welcome_message": "msg"}]
)
@pytest.mark.anyio
async def test_telegram_delete_bot(asyncclient, event_loop, bot, telegram, auth_header):
async def test_telegram_delete_bot(asyncclient, 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(

View file

@ -11,7 +11,7 @@ import pytest
"bot", [{"api_token": "apitoken123", "welcome_message": "msg"}]
)
@pytest.mark.anyio
async def test_telegram_get_bot(asyncclient, auth_header, event_loop, bot, telegram):
async def test_telegram_get_bot(asyncclient, auth_header, bot, telegram):
response = await asyncclient.get(
"/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id),
headers=auth_header,

View file

@ -11,7 +11,7 @@ from kibicara.platforms.telegram.model import Telegram
@pytest.mark.anyio
async def test_telegram_get_bots(asyncclient, auth_header, event_loop, hood_id):
async def test_telegram_get_bots(asyncclient, auth_header, hood_id):
hood = await Hood.objects.get(id=hood_id)
telegram0 = await Telegram.objects.create(
hood=hood,