From 48e3b6f6bc18b7a15b4f9e8737c58e4547215eff Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 1 Apr 2023 14:18:24 +0200 Subject: [PATCH 1/8] [tests] Port first test to async #23 --- backend/setup.cfg | 1 + backend/tests/conftest.py | 10 ++++++++++ backend/tests/tests_core/test_api_admin.py | 6 ++++-- setup.sh | 2 +- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/backend/setup.cfg b/backend/setup.cfg index 6f11b83..7a4f884 100644 --- a/backend/setup.cfg +++ b/backend/setup.cfg @@ -66,6 +66,7 @@ commands = deps = pytest pytest-asyncio + trio commands = pytest tests diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 6c7cd08..f715e13 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -8,6 +8,7 @@ from urllib.parse import urlparse from fastapi import FastAPI, status from fastapi.testclient import TestClient +from httpx import AsyncClient from pytest import fixture from kibicara import email @@ -24,6 +25,15 @@ def client(): return TestClient(app) +@fixture(scope="module") +def asyncclient(): + Mapping.drop_all() + Mapping.create_all() + app = FastAPI() + app.include_router(router, prefix="/api") + return AsyncClient(app=app, base_url="http://test") + + @fixture(scope="module") def monkeymodule(): from _pytest.monkeypatch import MonkeyPatch diff --git a/backend/tests/tests_core/test_api_admin.py b/backend/tests/tests_core/test_api_admin.py index 167a664..aec3989 100644 --- a/backend/tests/tests_core/test_api_admin.py +++ b/backend/tests/tests_core/test_api_admin.py @@ -3,10 +3,12 @@ # SPDX-License-Identifier: 0BSD from fastapi import status +import pytest -def test_hoods_unauthorized(client): - response = client.get("/api/admin/hoods/") +@pytest.mark.anyio +async def test_hoods_unauthorized(asyncclient): + response = await asyncclient.get("/api/admin/hoods/") assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/setup.sh b/setup.sh index 13b674f..553e0f8 100755 --- a/setup.sh +++ b/setup.sh @@ -6,4 +6,4 @@ ln -sf ../../git-hooks/commit-msg .git/hooks/commit-msg # create virtualenv virtualenv -p $(which python3.10) backend/.venv -backend/.venv/bin/pip install tox black pytest pytest-aiohttp +backend/.venv/bin/pip install tox black pytest pytest-aiohttp trio -- 2.43.5 From c1b8ad29846cd4b50f69122cdc4efa7cf59c0804 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 1 Apr 2023 14:40:02 +0200 Subject: [PATCH 2/8] [tests] Disable trio backend for AnyIO --- backend/setup.cfg | 1 - backend/tests/conftest.py | 5 +++++ setup.sh | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/backend/setup.cfg b/backend/setup.cfg index 7a4f884..6f11b83 100644 --- a/backend/setup.cfg +++ b/backend/setup.cfg @@ -66,7 +66,6 @@ commands = deps = pytest pytest-asyncio - trio commands = pytest tests diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index f715e13..68d48ef 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -25,6 +25,11 @@ def client(): return TestClient(app) +@fixture(scope="module") +def anyio_backend(): + return "asyncio" + + @fixture(scope="module") def asyncclient(): Mapping.drop_all() diff --git a/setup.sh b/setup.sh index 553e0f8..13b674f 100755 --- a/setup.sh +++ b/setup.sh @@ -6,4 +6,4 @@ ln -sf ../../git-hooks/commit-msg .git/hooks/commit-msg # create virtualenv virtualenv -p $(which python3.10) backend/.venv -backend/.venv/bin/pip install tox black pytest pytest-aiohttp trio +backend/.venv/bin/pip install tox black pytest pytest-aiohttp -- 2.43.5 From 6b6a2777bb6a70b8496b9f79cd989c8e13cf3111 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 1 Apr 2023 14:41:13 +0200 Subject: [PATCH 3/8] [tests] Ported other hood webAPI tests to async --- backend/tests/tests_core/test_api_admin.py | 5 +- backend/tests/tests_core/test_api_hoods.py | 89 ++++++++++++++-------- 2 files changed, 61 insertions(+), 33 deletions(-) diff --git a/backend/tests/tests_core/test_api_admin.py b/backend/tests/tests_core/test_api_admin.py index aec3989..821a4bd 100644 --- a/backend/tests/tests_core/test_api_admin.py +++ b/backend/tests/tests_core/test_api_admin.py @@ -12,6 +12,7 @@ async def test_hoods_unauthorized(asyncclient): assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_hoods_success(client, auth_header): - response = client.get("/api/admin/hoods/", headers=auth_header) +@pytest.mark.anyio +async def test_hoods_success(asyncclient, auth_header): + response = await asyncclient.get("/api/admin/hoods/", headers=auth_header) assert response.status_code == status.HTTP_200_OK diff --git a/backend/tests/tests_core/test_api_hoods.py b/backend/tests/tests_core/test_api_hoods.py index 33421d6..3fca51c 100644 --- a/backend/tests/tests_core/test_api_hoods.py +++ b/backend/tests/tests_core/test_api_hoods.py @@ -3,80 +3,107 @@ # Copyright (C) 2020 by Martin Rey # # SPDX-License-Identifier: 0BSD - +import pytest from fastapi import status -def test_hood_read_all(client): - response = client.get("/api/hoods/") +@pytest.mark.anyio +async def test_hood_read_all(asyncclient): + response = await asyncclient.get("/api/hoods/") assert response.status_code == status.HTTP_200_OK -def test_hood_create_unauthorized(client, hood_id): - response = client.post("/api/hoods/") +@pytest.mark.anyio +async def test_hood_create_unauthorized(asyncclient, hood_id): + response = await asyncclient.post("/api/hoods/") assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_hood_read(client, hood_id): - response = client.get("/api/hoods/{0}".format(hood_id)) +@pytest.mark.anyio +async def test_hood_read(asyncclient, hood_id): + response = await asyncclient.get("/api/hoods/{0}".format(hood_id)) assert response.status_code == status.HTTP_200_OK -def test_hood_update_unauthorized(client, hood_id): - response = client.put("/api/hoods/{0}".format(hood_id)) +@pytest.mark.anyio +async def test_hood_update_unauthorized(asyncclient, hood_id): + response = await asyncclient.put("/api/hoods/{0}".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_hood_delete_unauthorized(client, hood_id): - response = client.delete("/api/hoods/{0}".format(hood_id)) +@pytest.mark.anyio +async def test_hood_delete_unauthorized(asyncclient, hood_id): + response = await asyncclient.delete("/api/hoods/{0}".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_trigger_read_all_unauthorized(client, hood_id): - response = client.get("/api/hoods/{0}/triggers/".format(hood_id)) +@pytest.mark.anyio +async def test_trigger_read_all_unauthorized(asyncclient, hood_id): + response = await asyncclient.get("/api/hoods/{0}/triggers/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_trigger_create_unauthorized(client, hood_id): - response = client.post("/api/hoods/{0}/triggers/".format(hood_id)) +@pytest.mark.anyio +async def test_trigger_create_unauthorized(asyncclient, hood_id): + response = await asyncclient.post("/api/hoods/{0}/triggers/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_trigger_read_unauthorized(client, hood_id, trigger_id): - response = client.get("/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id)) +@pytest.mark.anyio +async def test_trigger_read_unauthorized(asyncclient, hood_id, trigger_id): + response = await asyncclient.get( + "/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id) + ) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_trigger_update_unauthorized(client, hood_id, trigger_id): - response = client.put("/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id)) +@pytest.mark.anyio +async def test_trigger_update_unauthorized(asyncclient, hood_id, trigger_id): + response = await asyncclient.put( + "/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id) + ) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_trigger_delete_unauthorized(client, hood_id, trigger_id): - response = client.delete("/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id)) +@pytest.mark.anyio +async def test_trigger_delete_unauthorized(asyncclient, hood_id, trigger_id): + response = await asyncclient.delete( + "/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id) + ) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_badword_read_all_unauthorized(client, hood_id): - response = client.get("/api/hoods/{0}/badwords/".format(hood_id)) +@pytest.mark.anyio +async def test_badword_read_all_unauthorized(asyncclient, hood_id): + response = await asyncclient.get("/api/hoods/{0}/badwords/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_badword_create_unauthorized(client, hood_id): - response = client.post("/api/hoods/{0}/badwords/".format(hood_id)) +@pytest.mark.anyio +async def test_badword_create_unauthorized(asyncclient, hood_id): + response = await asyncclient.post("/api/hoods/{0}/badwords/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_badword_read_unauthorized(client, hood_id, badword_id): - response = client.get("/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id)) +@pytest.mark.anyio +async def test_badword_read_unauthorized(asyncclient, hood_id, badword_id): + response = await asyncclient.get( + "/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id) + ) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_badword_update_unauthorized(client, hood_id, badword_id): - response = client.put("/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id)) +@pytest.mark.anyio +async def test_badword_update_unauthorized(asyncclient, hood_id, badword_id): + response = await asyncclient.put( + "/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id) + ) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_badword_delete_unauthorized(client, hood_id, badword_id): - response = client.delete("/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id)) +@pytest.mark.anyio +async def test_badword_delete_unauthorized(asyncclient, hood_id, badword_id): + response = await asyncclient.delete( + "/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id) + ) assert response.status_code == status.HTTP_401_UNAUTHORIZED -- 2.43.5 From b876e645dedbedfdf5d4a76e9889261397398324 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 1 Apr 2023 14:57:41 +0200 Subject: [PATCH 4/8] [tests] Ported email tests to async --- backend/tests/tests_email/conftest.py | 11 ++++--- .../tests_email/test_api_email_happy_path.py | 25 ++++++++------ .../test_api_email_unauthorized.py | 18 ++++++---- .../tests/tests_email/test_api_email_wrong.py | 33 ++++++++++++------- 4 files changed, 54 insertions(+), 33 deletions(-) diff --git a/backend/tests/tests_email/conftest.py b/backend/tests/tests_email/conftest.py index dde5ac2..60d2868 100644 --- a/backend/tests/tests_email/conftest.py +++ b/backend/tests/tests_email/conftest.py @@ -6,12 +6,13 @@ from fastapi import status -from pytest import fixture +import pytest -@fixture(scope="function") -def email_row(client, hood_id, auth_header): - response = client.post( +@pytest.fixture(scope="function") +@pytest.mark.anyio +async def email_row(asyncclient, hood_id, auth_header): + response = await asyncclient.post( "/api/hoods/{0}/email/".format(hood_id), json={"name": "kibicara-test"}, headers=auth_header, @@ -19,6 +20,6 @@ def email_row(client, hood_id, auth_header): assert response.status_code == status.HTTP_201_CREATED email_id = int(response.headers["Location"]) yield response.json() - client.delete( + await asyncclient.delete( "/api/hoods/{0}/email/{1}".format(hood_id, email_id), headers=auth_header ) diff --git a/backend/tests/tests_email/test_api_email_happy_path.py b/backend/tests/tests_email/test_api_email_happy_path.py index d220288..eea483e 100644 --- a/backend/tests/tests_email/test_api_email_happy_path.py +++ b/backend/tests/tests_email/test_api_email_happy_path.py @@ -8,13 +8,14 @@ from re import findall from urllib.parse import urlparse from fastapi import status -from pytest import skip +import pytest from kibicara.webapi.admin import to_token -def test_email_subscribe_unsubscribe(client, hood_id, receive_email): - response = client.post( +@pytest.mark.anyio +async def test_email_subscribe_unsubscribe(asyncclient, hood_id, receive_email): + response = await asyncclient.post( "/api/hoods/{0}/email/subscribe/".format(hood_id), json={"email": "test@localhost"}, ) @@ -27,37 +28,41 @@ def test_email_subscribe_unsubscribe(client, hood_id, receive_email): body, )[0] start = len("token=") - response = client.post( + response = await asyncclient.post( "/api/hoods/{0}/email/subscribe/confirm/{1}".format( hood_id, urlparse(confirm_url).query[start:] ) ) assert response.status_code == status.HTTP_201_CREATED - response = client.post( + response = await asyncclient.post( "/api/hoods/{0}/email/subscribe/confirm/{1}".format( hood_id, urlparse(confirm_url).query[start:] ) ) assert response.status_code == status.HTTP_409_CONFLICT token = to_token(email=mail["to"], hood=hood_id) - response = client.delete( + response = await asyncclient.delete( "/api/hoods/{0}/email/unsubscribe/{1}".format(hood_id, token) ) assert response.status_code == status.HTTP_204_NO_CONTENT -def test_email_message(client, hood_id, trigger_id, email_row): +@pytest.mark.anyio +async def test_email_message(asyncclient, hood_id, trigger_id, email_row): body = { "text": "test", "author": "test@localhost", "secret": email_row["secret"], } - response = client.post("/api/hoods/{0}/email/messages/".format(hood_id), json=body) + response = await asyncclient.post( + "/api/hoods/{0}/email/messages/".format(hood_id), json=body + ) assert response.status_code == status.HTTP_201_CREATED -def test_email_send_mda(trigger_id, email_row): - skip("Only works if kibicara is listening on port 8000, and only sometimes") +@pytest.mark.anyio +async def test_email_send_mda(trigger_id, email_row): + pytest.skip("Only works if kibicara is listening on port 8000, and only sometimes") mail = """From test@example.com Tue Jun 16 15:33:19 2020 Return-path: Envelope-to: hood@localhost diff --git a/backend/tests/tests_email/test_api_email_unauthorized.py b/backend/tests/tests_email/test_api_email_unauthorized.py index f86f20c..c029d3a 100644 --- a/backend/tests/tests_email/test_api_email_unauthorized.py +++ b/backend/tests/tests_email/test_api_email_unauthorized.py @@ -5,21 +5,27 @@ # SPDX-License-Identifier: 0BSD from fastapi import status +import pytest -def test_email_create_unauthorized(client, hood_id): - response = client.post("/api/hoods/{0}/email/".format(hood_id)) +@pytest.mark.anyio +async def test_email_create_unauthorized(asyncclient, hood_id): + response = await asyncclient.post("/api/hoods/{0}/email/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_email_delete_unauthorized(client, hood_id, email_row): - response = client.delete( +@pytest.mark.anyio +async def test_email_delete_unauthorized(asyncclient, hood_id, email_row): + response = await asyncclient.delete( "/api/hoods/{0}/email/{1}".format(hood_id, email_row["id"]) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_email_message_unauthorized(client, hood_id, email_row): +@pytest.mark.anyio +async def test_email_message_unauthorized(asyncclient, hood_id, email_row): body = {"text": "test", "author": "author", "secret": "wrong"} - response = client.post("/api/hoods/{0}/email/messages/".format(hood_id), json=body) + response = await asyncclient.post( + "/api/hoods/{0}/email/messages/".format(hood_id), json=body + ) assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/backend/tests/tests_email/test_api_email_wrong.py b/backend/tests/tests_email/test_api_email_wrong.py index 69566c6..5c164bb 100644 --- a/backend/tests/tests_email/test_api_email_wrong.py +++ b/backend/tests/tests_email/test_api_email_wrong.py @@ -5,16 +5,19 @@ from fastapi import status from nacl.exceptions import CryptoError +import pytest -def test_email_subscribe_empty(client, hood_id): - response = client.post("/api/hoods/{0}/email/subscribe/".format(hood_id)) +@pytest.mark.anyio +async def test_email_subscribe_empty(asyncclient, hood_id): + response = await asyncclient.post("/api/hoods/{0}/email/subscribe/".format(hood_id)) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY -def test_email_subscribe_confirm_wrong_token(client, hood_id): +@pytest.mark.anyio +async def test_email_subscribe_confirm_wrong_token(asyncclient, hood_id): try: - response = client.post( + response = await asyncclient.post( "/api/hoods/{0}/email/subscribe/confirm/".format(hood_id) + "asdfasdfasdfasdfasdfasdfasdfasdf" ) @@ -23,26 +26,31 @@ def test_email_subscribe_confirm_wrong_token(client, hood_id): pass -def test_email_subscribe_confirm_wrong_hood(client): - response = client.delete( +@pytest.mark.anyio +async def test_email_subscribe_confirm_wrong_hood(asyncclient): + response = await asyncclient.delete( "/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf" ) assert response.json()["detail"] == "Not Found" -def test_email_message_wrong(client, hood_id, email_row): +@pytest.mark.anyio +async def test_email_message_wrong(asyncclient, hood_id, email_row): body = { "text": "", "author": "test@localhost", "secret": email_row["secret"], } - response = client.post("/api/hoods/{0}/email/messages/".format(hood_id), json=body) + response = await asyncclient.post( + "/api/hoods/{0}/email/messages/".format(hood_id), json=body + ) assert response.status_code == status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS -def test_email_unsubscribe_wrong_token(client, hood_id): +@pytest.mark.anyio +async def test_email_unsubscribe_wrong_token(asyncclient, hood_id): try: - client.delete( + await asyncclient.delete( "/api/hoods/{0}/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf".format( hood_id ) @@ -51,8 +59,9 @@ def test_email_unsubscribe_wrong_token(client, hood_id): pass -def test_email_unsubscribe_wrong_hood(client): - response = client.delete( +@pytest.mark.anyio +async def test_email_unsubscribe_wrong_hood(asyncclient): + response = await asyncclient.delete( "/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf" ) assert response.json()["detail"] == "Not Found" -- 2.43.5 From f0e15f1d37784e3eeb3dfdb0d9db54619044ce52 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 1 Apr 2023 15:13:50 +0200 Subject: [PATCH 5/8] [tests] Ported mastodon tests to async --- backend/tests/tests_mastodon/conftest.py | 38 ++++++++--------- .../test_api_mastodon_create_bot.py | 40 +++++++++--------- .../test_api_mastodon_delete_bot.py | 33 ++++++++------- .../test_api_mastodon_get_bots.py | 42 ++++++++++--------- 4 files changed, 81 insertions(+), 72 deletions(-) diff --git a/backend/tests/tests_mastodon/conftest.py b/backend/tests/tests_mastodon/conftest.py index ea31bbb..a6eaa87 100644 --- a/backend/tests/tests_mastodon/conftest.py +++ b/backend/tests/tests_mastodon/conftest.py @@ -3,32 +3,30 @@ # # SPDX-License-Identifier: 0BSD -from pytest import fixture +import pytest from kibicara.model import Hood from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance -@fixture(scope="function") -def mastodon_instance(event_loop): - return event_loop.run_until_complete( - MastodonInstance.objects.create( - name="inst4nce", - client_id="cl13nt_id", - client_secret="cl13nt_s3cr3t", - ) +@pytest.fixture(scope="function") +@pytest.mark.anyio +async def mastodon_instance(event_loop): + return await MastodonInstance.objects.create( + name="inst4nce", + client_id="cl13nt_id", + client_secret="cl13nt_s3cr3t", ) -@fixture(scope="function") -def mastodon_account(event_loop, hood_id, mastodon_instance): - hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id)) - return event_loop.run_until_complete( - MastodonAccount.objects.create( - hood=hood, - instance=mastodon_instance, - access_token="t0k3n", - enabled=True, - username="us3r", - ) +@pytest.fixture(scope="function") +@pytest.mark.anyio +async def mastodon_account(event_loop, hood_id, mastodon_instance): + hood = await Hood.objects.get(id=hood_id) + return await MastodonAccount.objects.create( + hood=hood, + instance=mastodon_instance, + access_token="t0k3n", + enabled=True, + username="us3r", ) diff --git a/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py b/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py index f5a885f..6525ffe 100644 --- a/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py +++ b/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py @@ -4,14 +4,14 @@ # SPDX-License-Identifier: 0BSD from fastapi import status -from pytest import fixture, mark +import pytest from mastodon.Mastodon import Mastodon from kibicara.platforms import mastodon from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance -@fixture(scope="function") +@pytest.fixture(scope="function") def disable_spawner(monkeypatch): class DoNothing: def start(self, bot): @@ -20,7 +20,7 @@ def disable_spawner(monkeypatch): monkeypatch.setattr(mastodon.webapi, "spawner", DoNothing()) -@mark.parametrize( +@pytest.mark.parametrize( "body", [ { @@ -30,9 +30,10 @@ def disable_spawner(monkeypatch): } ], ) -def test_mastodon_create_bot( +@pytest.mark.anyio +async def test_mastodon_create_bot( event_loop, - client, + asyncclient, disable_spawner, hood_id, auth_header, @@ -44,7 +45,7 @@ def test_mastodon_create_bot( monkeypatch.setattr(Mastodon, "log_in", log_in_mock) - response = client.post( + response = await asyncclient.post( "/api/hoods/{0}/mastodon/".format(hood_id), json=body, headers=auth_header, @@ -52,11 +53,9 @@ def test_mastodon_create_bot( print(response.json()) assert response.status_code == status.HTTP_201_CREATED bot_id = response.json()["id"] - mastodon_obj = event_loop.run_until_complete(MastodonAccount.objects.get(id=bot_id)) + mastodon_obj = await MastodonAccount.objects.get(id=bot_id) assert response.json()["access_token"] == mastodon_obj.access_token - mastodon_instance = event_loop.run_until_complete( - MastodonInstance.objects.get(id=mastodon_obj.instance.id) - ) + mastodon_instance = await MastodonInstance.objects.get(id=mastodon_obj.instance.id) assert ( response.json()["instance"]["name"] == body["instance_url"] @@ -70,23 +69,24 @@ def test_mastodon_create_bot( assert mastodon_obj.enabled -@mark.parametrize( +@pytest.mark.parametrize( "body", [ {"instance_url": "botsin.space", "email": "notanemail", "password": "asdf1234"}, {"instance_url": "wrong", "email": "asdf@example.org", "password": "asdf1234"}, ], ) -def test_mastodon_invalid_input( +@pytest.mark.anyio +async def test_mastodon_invalid_input( event_loop, - client, + asyncclient, disable_spawner, hood_id, auth_header, monkeypatch, body, ): - response = client.post( + response = await asyncclient.post( "/api/hoods/{0}/mastodon/".format(hood_id), json=body, headers=auth_header, @@ -94,13 +94,15 @@ def test_mastodon_invalid_input( assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY -def test_mastodon_create_mastodon_invalid_id(client, auth_header): - response = client.post("/api/hoods/1337/mastodon/", headers=auth_header) +@pytest.mark.anyio +async def test_mastodon_create_mastodon_invalid_id(asyncclient, auth_header): + response = await asyncclient.post("/api/hoods/1337/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_404_NOT_FOUND - response = client.post("/api/hoods/wrong/mastodon/", headers=auth_header) + response = await asyncclient.post("/api/hoods/wrong/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY -def test_mastodon_create_unauthorized(client, hood_id): - response = client.post("/api/hoods/{hood_id}/mastodon/") +@pytest.mark.anyio +async def test_mastodon_create_unauthorized(asyncclient, hood_id): + response = await asyncclient.post("/api/hoods/{hood_id}/mastodon/") assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py b/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py index 1bebac3..55365d8 100644 --- a/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py +++ b/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py @@ -5,42 +5,47 @@ from fastapi import status from ormantic.exceptions import NoMatch -from pytest import raises +import pytest from kibicara.platforms.mastodon.model import MastodonAccount -def test_mastodon_delete_bot(client, event_loop, mastodon_account, auth_header): - response = client.delete( +@pytest.mark.anyio +async def test_mastodon_delete_bot(asyncclient, mastodon_account, auth_header): + response = await asyncclient.delete( "/api/hoods/{0}/mastodon/{1}".format( mastodon_account.hood.id, mastodon_account.id ), headers=auth_header, ) assert response.status_code == status.HTTP_204_NO_CONTENT - with raises(NoMatch): - event_loop.run_until_complete( - MastodonAccount.objects.get(id=mastodon_account.id) - ) + with pytest.raises(NoMatch): + await MastodonAccount.objects.get(id=mastodon_account.id) -def test_mastodon_delete_bot_invalid_id(client, auth_header, hood_id): - response = client.delete("/api/hoods/1337/mastodon/123", headers=auth_header) +@pytest.mark.anyio +async def test_mastodon_delete_bot_invalid_id(asyncclient, auth_header, hood_id): + response = await asyncclient.delete( + "/api/hoods/1337/mastodon/123", headers=auth_header + ) assert response.status_code == status.HTTP_404_NOT_FOUND - response = client.delete("/api/hoods/wrong/mastodon/123", headers=auth_header) + response = await asyncclient.delete( + "/api/hoods/wrong/mastodon/123", headers=auth_header + ) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY - response = client.delete( + response = await asyncclient.delete( "/api/hoods/{0}/mastodon/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}/mastodon/wrong".format(hood_id), headers=auth_header ) assert response.status_code == status.HTTP_404_NOT_FOUND -def test_mastodon_delete_bot_unauthorized(client, mastodon_account): - response = client.delete( +@pytest.mark.anyio +async def test_mastodon_delete_bot_unauthorized(asyncclient, mastodon_account): + response = await asyncclient.delete( "/api/hoods/{0}/mastodon/{1}".format( mastodon_account.hood.id, mastodon_account.id ) diff --git a/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py b/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py index d3da4ac..a242e68 100644 --- a/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py +++ b/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py @@ -4,25 +4,26 @@ # SPDX-License-Identifier: 0BSD from fastapi import status +import pytest from kibicara.platforms.mastodon.model import MastodonAccount -def test_mastodon_get_bots( - client, auth_header, event_loop, hood_id, mastodon_account, mastodon_instance +@pytest.mark.anyio +async def test_mastodon_get_bots( + asyncclient, auth_header, hood_id, mastodon_account, mastodon_instance ): - mastodon2 = event_loop.run_until_complete( - MastodonAccount.objects.create( - hood=mastodon_account.hood, - instance=mastodon_instance, - access_token="4cc3ss", - enabled=True, - username="us4r", - ) + mastodon2 = await MastodonAccount.objects.create( + hood=mastodon_account.hood, + instance=mastodon_instance, + access_token="4cc3ss", + enabled=True, + username="us4r", ) - response = client.get( - "/api/hoods/{0}/mastodon".format(mastodon_account.hood.id), headers=auth_header + response = await asyncclient.get( + "/api/hoods/{0}/mastodon/".format(mastodon_account.hood.id), headers=auth_header ) + print(response.headers) assert response.status_code == status.HTTP_200_OK assert response.json()[0]["id"] == mastodon_account.id assert response.json()[0]["access_token"] == mastodon_account.access_token @@ -30,20 +31,23 @@ def test_mastodon_get_bots( assert response.json()[1]["access_token"] == mastodon2.access_token -def test_mastodon_get_bots_invalid_id(client, auth_header, hood_id): - response = client.get("/api/hoods/1337/mastodon", headers=auth_header) +@pytest.mark.anyio +async def test_mastodon_get_bots_invalid_id(asyncclient, auth_header, hood_id): + response = await asyncclient.get("/api/hoods/1337/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_404_NOT_FOUND - response = client.get("/api/hoods/wrong/mastodon", headers=auth_header) + response = await asyncclient.get("/api/hoods/wrong/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY -def test_mastodon_get_bots_unauthorized(client, hood_id): - response = client.get("/api/hoods/{0}/mastodon".format(hood_id)) +@pytest.mark.anyio +async def test_mastodon_get_bots_unauthorized(asyncclient, hood_id): + response = await asyncclient.get("/api/hoods/{0}/mastodon/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED -def test_mastodon_public(client, mastodon_account, mastodon_instance, event_loop): - response = client.get( +@pytest.mark.anyio +async def test_mastodon_public(asyncclient, mastodon_account, mastodon_instance): + response = await asyncclient.get( "/api/hoods/{0}/mastodon/public".format(mastodon_account.hood.id) ) assert response.json()[0]["username"] == mastodon_account.username -- 2.43.5 From 16c4ebc84beee359bff511696712b7a9fa86b260 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 1 Apr 2023 15:23:33 +0200 Subject: [PATCH 6/8] [tests] Ported telegram tests to async --- backend/tests/tests_telegram/conftest.py | 19 ++++--- .../test_api_telegram_create_bot.py | 36 +++++++------ .../test_api_telegram_delete_bot.py | 51 +++++++++++-------- .../test_api_telegram_get_bot.py | 35 ++++++++----- .../test_api_telegram_get_bots.py | 42 +++++++-------- 5 files changed, 102 insertions(+), 81 deletions(-) diff --git a/backend/tests/tests_telegram/conftest.py b/backend/tests/tests_telegram/conftest.py index d68dbd9..1e58b5c 100644 --- a/backend/tests/tests_telegram/conftest.py +++ b/backend/tests/tests_telegram/conftest.py @@ -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( - hood=hood, - api_token=bot["api_token"], - welcome_message=bot["welcome_message"], - ) +@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"], ) diff --git a/backend/tests/tests_telegram/test_api_telegram_create_bot.py b/backend/tests/tests_telegram/test_api_telegram_create_bot.py index 7391789..2f14d89 100644 --- a/backend/tests/tests_telegram/test_api_telegram_create_bot.py +++ b/backend/tests/tests_telegram/test_api_telegram_create_bot.py @@ -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 diff --git a/backend/tests/tests_telegram/test_api_telegram_delete_bot.py b/backend/tests/tests_telegram/test_api_telegram_delete_bot.py index 2b407b3..26f899b 100644 --- a/backend/tests/tests_telegram/test_api_telegram_delete_bot.py +++ b/backend/tests/tests_telegram/test_api_telegram_delete_bot.py @@ -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) - ) - event_loop.run_until_complete( - TelegramUser.objects.create(user_id=5678, bot=telegram.id) - ) - response = client.delete( +@pytest.mark.parametrize( + "bot", [{"api_token": "apitoken123", "welcome_message": "msg"}] +) +@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 diff --git a/backend/tests/tests_telegram/test_api_telegram_get_bot.py b/backend/tests/tests_telegram/test_api_telegram_get_bot.py index 154f796..cc87eb7 100644 --- a/backend/tests/tests_telegram/test_api_telegram_get_bot.py +++ b/backend/tests/tests_telegram/test_api_telegram_get_bot.py @@ -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 diff --git a/backend/tests/tests_telegram/test_api_telegram_get_bots.py b/backend/tests/tests_telegram/test_api_telegram_get_bots.py index 7879602..d816511 100644 --- a/backend/tests/tests_telegram/test_api_telegram_get_bots.py +++ b/backend/tests/tests_telegram/test_api_telegram_get_bots.py @@ -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( - hood=hood, - api_token="api_token123", - welcome_message="welcome_message123", - ) +@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( - hood=hood, - api_token="api_token456", - welcome_message="welcome_message123", - ) + 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 -- 2.43.5 From f829bf869409757f7ef49644ca43741001376178 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 1 Apr 2023 15:30:42 +0200 Subject: [PATCH 7/8] [tests] Get rid of non-async test client --- backend/tests/conftest.py | 81 +++++++++---------- backend/tests/tests_mastodon/conftest.py | 4 +- .../test_api_mastodon_create_bot.py | 2 - .../test_api_telegram_create_bot.py | 2 - .../test_api_telegram_delete_bot.py | 2 +- .../test_api_telegram_get_bot.py | 2 +- .../test_api_telegram_get_bots.py | 2 +- 7 files changed, 45 insertions(+), 50 deletions(-) diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 68d48ef..4dd78ff 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -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 ) diff --git a/backend/tests/tests_mastodon/conftest.py b/backend/tests/tests_mastodon/conftest.py index a6eaa87..d09732a 100644 --- a/backend/tests/tests_mastodon/conftest.py +++ b/backend/tests/tests_mastodon/conftest.py @@ -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, diff --git a/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py b/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py index 6525ffe..ba926fa 100644 --- a/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py +++ b/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py @@ -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, diff --git a/backend/tests/tests_telegram/test_api_telegram_create_bot.py b/backend/tests/tests_telegram/test_api_telegram_create_bot.py index 2f14d89..3f5daf4 100644 --- a/backend/tests/tests_telegram/test_api_telegram_create_bot.py +++ b/backend/tests/tests_telegram/test_api_telegram_create_bot.py @@ -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, diff --git a/backend/tests/tests_telegram/test_api_telegram_delete_bot.py b/backend/tests/tests_telegram/test_api_telegram_delete_bot.py index 26f899b..241fa0f 100644 --- a/backend/tests/tests_telegram/test_api_telegram_delete_bot.py +++ b/backend/tests/tests_telegram/test_api_telegram_delete_bot.py @@ -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( diff --git a/backend/tests/tests_telegram/test_api_telegram_get_bot.py b/backend/tests/tests_telegram/test_api_telegram_get_bot.py index cc87eb7..c1a097d 100644 --- a/backend/tests/tests_telegram/test_api_telegram_get_bot.py +++ b/backend/tests/tests_telegram/test_api_telegram_get_bot.py @@ -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, diff --git a/backend/tests/tests_telegram/test_api_telegram_get_bots.py b/backend/tests/tests_telegram/test_api_telegram_get_bots.py index d816511..a109503 100644 --- a/backend/tests/tests_telegram/test_api_telegram_get_bots.py +++ b/backend/tests/tests_telegram/test_api_telegram_get_bots.py @@ -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, -- 2.43.5 From c468543fa5472d0cf6e7eb43d2c6c58bfaa22b63 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 1 Apr 2023 15:32:10 +0200 Subject: [PATCH 8/8] [tests] Rename asyncclient into client --- backend/tests/conftest.py | 38 ++++++------ backend/tests/tests_core/test_api_admin.py | 8 +-- backend/tests/tests_core/test_api_hoods.py | 60 +++++++++---------- backend/tests/tests_email/conftest.py | 6 +- .../tests_email/test_api_email_happy_path.py | 14 ++--- .../test_api_email_unauthorized.py | 12 ++-- .../tests/tests_email/test_api_email_wrong.py | 24 ++++---- .../test_api_mastodon_create_bot.py | 18 +++--- .../test_api_mastodon_delete_bot.py | 22 +++---- .../test_api_mastodon_get_bots.py | 18 +++--- .../test_api_telegram_create_bot.py | 18 +++--- .../test_api_telegram_delete_bot.py | 22 +++---- .../test_api_telegram_get_bot.py | 22 +++---- .../test_api_telegram_get_bots.py | 14 ++--- 14 files changed, 142 insertions(+), 154 deletions(-) diff --git a/backend/tests/conftest.py b/backend/tests/conftest.py index 4dd78ff..3030c4c 100644 --- a/backend/tests/conftest.py +++ b/backend/tests/conftest.py @@ -21,7 +21,7 @@ def anyio_backend(): @pytest.fixture(scope="module") -def asyncclient(): +def client(): Mapping.drop_all() Mapping.create_all() app = FastAPI() @@ -54,8 +54,8 @@ def receive_email(monkeymodule): @pytest.fixture(scope="module") @pytest.mark.anyio -async def register_token(asyncclient, receive_email): - response = await asyncclient.post( +async def register_token(client, receive_email): + response = await client.post( "/api/admin/register/", json={"email": "user", "password": "password"} ) assert response.status_code == status.HTTP_202_ACCEPTED @@ -64,15 +64,15 @@ async def register_token(asyncclient, receive_email): @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)) +async def register_confirmed(client, register_token): + response = await client.post("/api/admin/confirm/{0}".format(register_token)) assert response.status_code == status.HTTP_200_OK @pytest.fixture(scope="module") @pytest.mark.anyio -async def access_token(asyncclient, register_confirmed): - response = await asyncclient.post( +async def access_token(client, register_confirmed): + response = await client.post( "/api/admin/login/", data={"username": "user", "password": "password"} ) assert response.status_code == status.HTTP_200_OK @@ -86,20 +86,20 @@ def auth_header(access_token): @pytest.fixture(scope="function") @pytest.mark.anyio -async def hood_id(asyncclient, auth_header): - response = await asyncclient.post( +async def hood_id(client, auth_header): + response = await client.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 - await asyncclient.delete("/api/hoods/{0}".format(hood_id), headers=auth_header) + await client.delete("/api/hoods/{0}".format(hood_id), headers=auth_header) @pytest.fixture(scope="function") @pytest.mark.anyio -async def trigger_id(asyncclient, hood_id, auth_header): - response = await asyncclient.post( +async def trigger_id(client, hood_id, auth_header): + response = await client.post( "/api/hoods/{0}/triggers/".format(hood_id), json={"pattern": "test"}, headers=auth_header, @@ -107,15 +107,15 @@ async def trigger_id(asyncclient, hood_id, auth_header): assert response.status_code == status.HTTP_201_CREATED trigger_id = int(response.headers["Location"]) yield trigger_id - await asyncclient.delete( + await client.delete( "/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id), headers=auth_header ) @pytest.fixture(scope="function") @pytest.mark.anyio -async def badword_id(asyncclient, hood_id, auth_header): - response = await asyncclient.post( +async def badword_id(client, hood_id, auth_header): + response = await client.post( "/api/hoods/{0}/badwords/".format(hood_id), json={"pattern": ""}, headers=auth_header, @@ -123,20 +123,20 @@ async def badword_id(asyncclient, hood_id, auth_header): assert response.status_code == status.HTTP_201_CREATED badword_id = int(response.headers["Location"]) yield badword_id - await asyncclient.delete( + await client.delete( "/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id), headers=auth_header ) @pytest.fixture(scope="function") @pytest.mark.anyio -async def test_id(asyncclient, hood_id, auth_header): - response = await asyncclient.post( +async def test_id(client, hood_id, auth_header): + response = await client.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 - await asyncclient.delete( + await client.delete( "/api/hoods/{0}/test/{1}".format(hood_id, test_id), headers=auth_header ) diff --git a/backend/tests/tests_core/test_api_admin.py b/backend/tests/tests_core/test_api_admin.py index 821a4bd..b7c4834 100644 --- a/backend/tests/tests_core/test_api_admin.py +++ b/backend/tests/tests_core/test_api_admin.py @@ -7,12 +7,12 @@ import pytest @pytest.mark.anyio -async def test_hoods_unauthorized(asyncclient): - response = await asyncclient.get("/api/admin/hoods/") +async def test_hoods_unauthorized(client): + response = await client.get("/api/admin/hoods/") assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_hoods_success(asyncclient, auth_header): - response = await asyncclient.get("/api/admin/hoods/", headers=auth_header) +async def test_hoods_success(client, auth_header): + response = await client.get("/api/admin/hoods/", headers=auth_header) assert response.status_code == status.HTTP_200_OK diff --git a/backend/tests/tests_core/test_api_hoods.py b/backend/tests/tests_core/test_api_hoods.py index 3fca51c..813fb53 100644 --- a/backend/tests/tests_core/test_api_hoods.py +++ b/backend/tests/tests_core/test_api_hoods.py @@ -8,102 +8,102 @@ from fastapi import status @pytest.mark.anyio -async def test_hood_read_all(asyncclient): - response = await asyncclient.get("/api/hoods/") +async def test_hood_read_all(client): + response = await client.get("/api/hoods/") assert response.status_code == status.HTTP_200_OK @pytest.mark.anyio -async def test_hood_create_unauthorized(asyncclient, hood_id): - response = await asyncclient.post("/api/hoods/") +async def test_hood_create_unauthorized(client, hood_id): + response = await client.post("/api/hoods/") assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_hood_read(asyncclient, hood_id): - response = await asyncclient.get("/api/hoods/{0}".format(hood_id)) +async def test_hood_read(client, hood_id): + response = await client.get("/api/hoods/{0}".format(hood_id)) assert response.status_code == status.HTTP_200_OK @pytest.mark.anyio -async def test_hood_update_unauthorized(asyncclient, hood_id): - response = await asyncclient.put("/api/hoods/{0}".format(hood_id)) +async def test_hood_update_unauthorized(client, hood_id): + response = await client.put("/api/hoods/{0}".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_hood_delete_unauthorized(asyncclient, hood_id): - response = await asyncclient.delete("/api/hoods/{0}".format(hood_id)) +async def test_hood_delete_unauthorized(client, hood_id): + response = await client.delete("/api/hoods/{0}".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_trigger_read_all_unauthorized(asyncclient, hood_id): - response = await asyncclient.get("/api/hoods/{0}/triggers/".format(hood_id)) +async def test_trigger_read_all_unauthorized(client, hood_id): + response = await client.get("/api/hoods/{0}/triggers/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_trigger_create_unauthorized(asyncclient, hood_id): - response = await asyncclient.post("/api/hoods/{0}/triggers/".format(hood_id)) +async def test_trigger_create_unauthorized(client, hood_id): + response = await client.post("/api/hoods/{0}/triggers/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_trigger_read_unauthorized(asyncclient, hood_id, trigger_id): - response = await asyncclient.get( +async def test_trigger_read_unauthorized(client, hood_id, trigger_id): + response = await client.get( "/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_trigger_update_unauthorized(asyncclient, hood_id, trigger_id): - response = await asyncclient.put( +async def test_trigger_update_unauthorized(client, hood_id, trigger_id): + response = await client.put( "/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_trigger_delete_unauthorized(asyncclient, hood_id, trigger_id): - response = await asyncclient.delete( +async def test_trigger_delete_unauthorized(client, hood_id, trigger_id): + response = await client.delete( "/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_badword_read_all_unauthorized(asyncclient, hood_id): - response = await asyncclient.get("/api/hoods/{0}/badwords/".format(hood_id)) +async def test_badword_read_all_unauthorized(client, hood_id): + response = await client.get("/api/hoods/{0}/badwords/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_badword_create_unauthorized(asyncclient, hood_id): - response = await asyncclient.post("/api/hoods/{0}/badwords/".format(hood_id)) +async def test_badword_create_unauthorized(client, hood_id): + response = await client.post("/api/hoods/{0}/badwords/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_badword_read_unauthorized(asyncclient, hood_id, badword_id): - response = await asyncclient.get( +async def test_badword_read_unauthorized(client, hood_id, badword_id): + response = await client.get( "/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_badword_update_unauthorized(asyncclient, hood_id, badword_id): - response = await asyncclient.put( +async def test_badword_update_unauthorized(client, hood_id, badword_id): + response = await client.put( "/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_badword_delete_unauthorized(asyncclient, hood_id, badword_id): - response = await asyncclient.delete( +async def test_badword_delete_unauthorized(client, hood_id, badword_id): + response = await client.delete( "/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/backend/tests/tests_email/conftest.py b/backend/tests/tests_email/conftest.py index 60d2868..9d6bbfb 100644 --- a/backend/tests/tests_email/conftest.py +++ b/backend/tests/tests_email/conftest.py @@ -11,8 +11,8 @@ import pytest @pytest.fixture(scope="function") @pytest.mark.anyio -async def email_row(asyncclient, hood_id, auth_header): - response = await asyncclient.post( +async def email_row(client, hood_id, auth_header): + response = await client.post( "/api/hoods/{0}/email/".format(hood_id), json={"name": "kibicara-test"}, headers=auth_header, @@ -20,6 +20,6 @@ async def email_row(asyncclient, hood_id, auth_header): assert response.status_code == status.HTTP_201_CREATED email_id = int(response.headers["Location"]) yield response.json() - await asyncclient.delete( + await client.delete( "/api/hoods/{0}/email/{1}".format(hood_id, email_id), headers=auth_header ) diff --git a/backend/tests/tests_email/test_api_email_happy_path.py b/backend/tests/tests_email/test_api_email_happy_path.py index eea483e..f6ce23c 100644 --- a/backend/tests/tests_email/test_api_email_happy_path.py +++ b/backend/tests/tests_email/test_api_email_happy_path.py @@ -14,8 +14,8 @@ from kibicara.webapi.admin import to_token @pytest.mark.anyio -async def test_email_subscribe_unsubscribe(asyncclient, hood_id, receive_email): - response = await asyncclient.post( +async def test_email_subscribe_unsubscribe(client, hood_id, receive_email): + response = await client.post( "/api/hoods/{0}/email/subscribe/".format(hood_id), json={"email": "test@localhost"}, ) @@ -28,33 +28,33 @@ async def test_email_subscribe_unsubscribe(asyncclient, hood_id, receive_email): body, )[0] start = len("token=") - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/email/subscribe/confirm/{1}".format( hood_id, urlparse(confirm_url).query[start:] ) ) assert response.status_code == status.HTTP_201_CREATED - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/email/subscribe/confirm/{1}".format( hood_id, urlparse(confirm_url).query[start:] ) ) assert response.status_code == status.HTTP_409_CONFLICT token = to_token(email=mail["to"], hood=hood_id) - response = await asyncclient.delete( + response = await client.delete( "/api/hoods/{0}/email/unsubscribe/{1}".format(hood_id, token) ) assert response.status_code == status.HTTP_204_NO_CONTENT @pytest.mark.anyio -async def test_email_message(asyncclient, hood_id, trigger_id, email_row): +async def test_email_message(client, hood_id, trigger_id, email_row): body = { "text": "test", "author": "test@localhost", "secret": email_row["secret"], } - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/email/messages/".format(hood_id), json=body ) assert response.status_code == status.HTTP_201_CREATED diff --git a/backend/tests/tests_email/test_api_email_unauthorized.py b/backend/tests/tests_email/test_api_email_unauthorized.py index c029d3a..c934066 100644 --- a/backend/tests/tests_email/test_api_email_unauthorized.py +++ b/backend/tests/tests_email/test_api_email_unauthorized.py @@ -9,23 +9,23 @@ import pytest @pytest.mark.anyio -async def test_email_create_unauthorized(asyncclient, hood_id): - response = await asyncclient.post("/api/hoods/{0}/email/".format(hood_id)) +async def test_email_create_unauthorized(client, hood_id): + response = await client.post("/api/hoods/{0}/email/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_email_delete_unauthorized(asyncclient, hood_id, email_row): - response = await asyncclient.delete( +async def test_email_delete_unauthorized(client, hood_id, email_row): + response = await client.delete( "/api/hoods/{0}/email/{1}".format(hood_id, email_row["id"]) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_email_message_unauthorized(asyncclient, hood_id, email_row): +async def test_email_message_unauthorized(client, hood_id, email_row): body = {"text": "test", "author": "author", "secret": "wrong"} - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/email/messages/".format(hood_id), json=body ) assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/backend/tests/tests_email/test_api_email_wrong.py b/backend/tests/tests_email/test_api_email_wrong.py index 5c164bb..3735840 100644 --- a/backend/tests/tests_email/test_api_email_wrong.py +++ b/backend/tests/tests_email/test_api_email_wrong.py @@ -9,15 +9,15 @@ import pytest @pytest.mark.anyio -async def test_email_subscribe_empty(asyncclient, hood_id): - response = await asyncclient.post("/api/hoods/{0}/email/subscribe/".format(hood_id)) +async def test_email_subscribe_empty(client, hood_id): + response = await client.post("/api/hoods/{0}/email/subscribe/".format(hood_id)) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY @pytest.mark.anyio -async def test_email_subscribe_confirm_wrong_token(asyncclient, hood_id): +async def test_email_subscribe_confirm_wrong_token(client, hood_id): try: - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/email/subscribe/confirm/".format(hood_id) + "asdfasdfasdfasdfasdfasdfasdfasdf" ) @@ -27,30 +27,30 @@ async def test_email_subscribe_confirm_wrong_token(asyncclient, hood_id): @pytest.mark.anyio -async def test_email_subscribe_confirm_wrong_hood(asyncclient): - response = await asyncclient.delete( +async def test_email_subscribe_confirm_wrong_hood(client): + response = await client.delete( "/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf" ) assert response.json()["detail"] == "Not Found" @pytest.mark.anyio -async def test_email_message_wrong(asyncclient, hood_id, email_row): +async def test_email_message_wrong(client, hood_id, email_row): body = { "text": "", "author": "test@localhost", "secret": email_row["secret"], } - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/email/messages/".format(hood_id), json=body ) assert response.status_code == status.HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS @pytest.mark.anyio -async def test_email_unsubscribe_wrong_token(asyncclient, hood_id): +async def test_email_unsubscribe_wrong_token(client, hood_id): try: - await asyncclient.delete( + await client.delete( "/api/hoods/{0}/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf".format( hood_id ) @@ -60,8 +60,8 @@ async def test_email_unsubscribe_wrong_token(asyncclient, hood_id): @pytest.mark.anyio -async def test_email_unsubscribe_wrong_hood(asyncclient): - response = await asyncclient.delete( +async def test_email_unsubscribe_wrong_hood(client): + response = await client.delete( "/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf" ) assert response.json()["detail"] == "Not Found" diff --git a/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py b/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py index ba926fa..68fdaf4 100644 --- a/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py +++ b/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py @@ -32,7 +32,7 @@ def disable_spawner(monkeypatch): ) @pytest.mark.anyio async def test_mastodon_create_bot( - asyncclient, + client, disable_spawner, hood_id, auth_header, @@ -44,7 +44,7 @@ async def test_mastodon_create_bot( monkeypatch.setattr(Mastodon, "log_in", log_in_mock) - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/mastodon/".format(hood_id), json=body, headers=auth_header, @@ -77,14 +77,14 @@ async def test_mastodon_create_bot( ) @pytest.mark.anyio async def test_mastodon_invalid_input( - asyncclient, + client, disable_spawner, hood_id, auth_header, monkeypatch, body, ): - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/mastodon/".format(hood_id), json=body, headers=auth_header, @@ -93,14 +93,14 @@ async def test_mastodon_invalid_input( @pytest.mark.anyio -async def test_mastodon_create_mastodon_invalid_id(asyncclient, auth_header): - response = await asyncclient.post("/api/hoods/1337/mastodon/", headers=auth_header) +async def test_mastodon_create_mastodon_invalid_id(client, auth_header): + response = await client.post("/api/hoods/1337/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_404_NOT_FOUND - response = await asyncclient.post("/api/hoods/wrong/mastodon/", headers=auth_header) + response = await client.post("/api/hoods/wrong/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY @pytest.mark.anyio -async def test_mastodon_create_unauthorized(asyncclient, hood_id): - response = await asyncclient.post("/api/hoods/{hood_id}/mastodon/") +async def test_mastodon_create_unauthorized(client, hood_id): + response = await client.post("/api/hoods/{hood_id}/mastodon/") assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py b/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py index 55365d8..03cca3e 100644 --- a/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py +++ b/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py @@ -11,8 +11,8 @@ from kibicara.platforms.mastodon.model import MastodonAccount @pytest.mark.anyio -async def test_mastodon_delete_bot(asyncclient, mastodon_account, auth_header): - response = await asyncclient.delete( +async def test_mastodon_delete_bot(client, mastodon_account, auth_header): + response = await client.delete( "/api/hoods/{0}/mastodon/{1}".format( mastodon_account.hood.id, mastodon_account.id ), @@ -24,28 +24,24 @@ async def test_mastodon_delete_bot(asyncclient, mastodon_account, auth_header): @pytest.mark.anyio -async def test_mastodon_delete_bot_invalid_id(asyncclient, auth_header, hood_id): - response = await asyncclient.delete( - "/api/hoods/1337/mastodon/123", headers=auth_header - ) +async def test_mastodon_delete_bot_invalid_id(client, auth_header, hood_id): + response = await client.delete("/api/hoods/1337/mastodon/123", headers=auth_header) assert response.status_code == status.HTTP_404_NOT_FOUND - response = await asyncclient.delete( - "/api/hoods/wrong/mastodon/123", headers=auth_header - ) + response = await client.delete("/api/hoods/wrong/mastodon/123", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY - response = await asyncclient.delete( + response = await client.delete( "/api/hoods/{0}/mastodon/7331".format(hood_id), headers=auth_header ) assert response.status_code == status.HTTP_404_NOT_FOUND - response = await asyncclient.delete( + response = await client.delete( "/api/hoods/{0}/mastodon/wrong".format(hood_id), headers=auth_header ) assert response.status_code == status.HTTP_404_NOT_FOUND @pytest.mark.anyio -async def test_mastodon_delete_bot_unauthorized(asyncclient, mastodon_account): - response = await asyncclient.delete( +async def test_mastodon_delete_bot_unauthorized(client, mastodon_account): + response = await client.delete( "/api/hoods/{0}/mastodon/{1}".format( mastodon_account.hood.id, mastodon_account.id ) diff --git a/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py b/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py index a242e68..9ff9087 100644 --- a/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py +++ b/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py @@ -11,7 +11,7 @@ from kibicara.platforms.mastodon.model import MastodonAccount @pytest.mark.anyio async def test_mastodon_get_bots( - asyncclient, auth_header, hood_id, mastodon_account, mastodon_instance + client, auth_header, hood_id, mastodon_account, mastodon_instance ): mastodon2 = await MastodonAccount.objects.create( hood=mastodon_account.hood, @@ -20,7 +20,7 @@ async def test_mastodon_get_bots( enabled=True, username="us4r", ) - response = await asyncclient.get( + response = await client.get( "/api/hoods/{0}/mastodon/".format(mastodon_account.hood.id), headers=auth_header ) print(response.headers) @@ -32,22 +32,22 @@ async def test_mastodon_get_bots( @pytest.mark.anyio -async def test_mastodon_get_bots_invalid_id(asyncclient, auth_header, hood_id): - response = await asyncclient.get("/api/hoods/1337/mastodon/", headers=auth_header) +async def test_mastodon_get_bots_invalid_id(client, auth_header, hood_id): + response = await client.get("/api/hoods/1337/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_404_NOT_FOUND - response = await asyncclient.get("/api/hoods/wrong/mastodon/", headers=auth_header) + response = await client.get("/api/hoods/wrong/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY @pytest.mark.anyio -async def test_mastodon_get_bots_unauthorized(asyncclient, hood_id): - response = await asyncclient.get("/api/hoods/{0}/mastodon/".format(hood_id)) +async def test_mastodon_get_bots_unauthorized(client, hood_id): + response = await client.get("/api/hoods/{0}/mastodon/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED @pytest.mark.anyio -async def test_mastodon_public(asyncclient, mastodon_account, mastodon_instance): - response = await asyncclient.get( +async def test_mastodon_public(client, mastodon_account, mastodon_instance): + response = await client.get( "/api/hoods/{0}/mastodon/public".format(mastodon_account.hood.id) ) assert response.json()[0]["username"] == mastodon_account.username diff --git a/backend/tests/tests_telegram/test_api_telegram_create_bot.py b/backend/tests/tests_telegram/test_api_telegram_create_bot.py index 3f5daf4..12c688e 100644 --- a/backend/tests/tests_telegram/test_api_telegram_create_bot.py +++ b/backend/tests/tests_telegram/test_api_telegram_create_bot.py @@ -22,7 +22,7 @@ def disable_spawner(monkeypatch): @pytest.mark.parametrize("body", [{"api_token": "string", "welcome_message": "string"}]) @pytest.mark.anyio async def test_telegram_create_bot( - asyncclient, + client, disable_spawner, hood_id, auth_header, @@ -34,7 +34,7 @@ async def test_telegram_create_bot( monkeypatch.setattr(telegram.webapi, "check_token", check_token_mock) - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/telegram/".format(hood_id), json=body, headers=auth_header, @@ -55,14 +55,14 @@ 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( - asyncclient, + client, disable_spawner, hood_id, auth_header, monkeypatch, body, ): - response = await asyncclient.post( + response = await client.post( "/api/hoods/{0}/telegram/".format(hood_id), json=body, headers=auth_header, @@ -71,14 +71,14 @@ async def test_telegram_invalid_api_token( @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) +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 - response = await asyncclient.post("/api/hoods/wrong/telegram/", headers=auth_header) + response = await client.post("/api/hoods/wrong/telegram/", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY @pytest.mark.anyio -async def test_telegram_create_unauthorized(asyncclient, hood_id): - response = await asyncclient.post("/api/hoods/{hood_id}/telegram/") +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 diff --git a/backend/tests/tests_telegram/test_api_telegram_delete_bot.py b/backend/tests/tests_telegram/test_api_telegram_delete_bot.py index 241fa0f..c561846 100644 --- a/backend/tests/tests_telegram/test_api_telegram_delete_bot.py +++ b/backend/tests/tests_telegram/test_api_telegram_delete_bot.py @@ -14,10 +14,10 @@ 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, bot, telegram, auth_header): +async def test_telegram_delete_bot(client, 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( + response = await client.delete( "/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id), headers=auth_header, ) @@ -29,20 +29,16 @@ async def test_telegram_delete_bot(asyncclient, bot, telegram, 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 - ) +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) assert response.status_code == status.HTTP_404_NOT_FOUND - response = await asyncclient.delete( - "/api/hoods/wrong/telegram/123", headers=auth_header - ) + response = await client.delete("/api/hoods/wrong/telegram/123", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY - response = await asyncclient.delete( + response = await client.delete( "/api/hoods/{0}/telegram/7331".format(hood_id), headers=auth_header ) assert response.status_code == status.HTTP_404_NOT_FOUND - response = await asyncclient.delete( + response = await client.delete( "/api/hoods/{0}/telegram/wrong".format(hood_id), headers=auth_header ) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY @@ -52,8 +48,8 @@ async def test_telegram_delete_bot_invalid_id(asyncclient, auth_header, hood_id) "bot", [{"api_token": "apitoken123", "welcome_message": "msg"}] ) @pytest.mark.anyio -async def test_telegram_delete_bot_unauthorized(asyncclient, bot, telegram): - response = await asyncclient.delete( +async def test_telegram_delete_bot_unauthorized(client, bot, telegram): + response = await client.delete( "/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/backend/tests/tests_telegram/test_api_telegram_get_bot.py b/backend/tests/tests_telegram/test_api_telegram_get_bot.py index c1a097d..c808609 100644 --- a/backend/tests/tests_telegram/test_api_telegram_get_bot.py +++ b/backend/tests/tests_telegram/test_api_telegram_get_bot.py @@ -11,8 +11,8 @@ import pytest "bot", [{"api_token": "apitoken123", "welcome_message": "msg"}] ) @pytest.mark.anyio -async def test_telegram_get_bot(asyncclient, auth_header, bot, telegram): - response = await asyncclient.get( +async def test_telegram_get_bot(client, auth_header, bot, telegram): + response = await client.get( "/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id), headers=auth_header, ) @@ -23,20 +23,16 @@ async def test_telegram_get_bot(asyncclient, auth_header, bot, telegram): @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 - ) +async def test_telegram_get_bot_invalid_id(client, auth_header, hood_id): + response = await client.get("/api/hoods/1337/telegram/123", headers=auth_header) assert response.status_code == status.HTTP_404_NOT_FOUND - response = await asyncclient.get( - "/api/hoods/wrong/telegram/123", headers=auth_header - ) + response = await client.get("/api/hoods/wrong/telegram/123", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY - response = await asyncclient.get( + response = await client.get( "/api/hoods/{0}/telegram/7331".format(hood_id), headers=auth_header ) assert response.status_code == status.HTTP_404_NOT_FOUND - response = await asyncclient.get( + response = await client.get( "/api/hoods/{0}/telegram/wrong".format(hood_id), headers=auth_header ) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY @@ -46,8 +42,8 @@ async def test_telegram_get_bot_invalid_id(asyncclient, auth_header, hood_id): "bot", [{"api_token": "apitoken456", "welcome_message": "msg"}] ) @pytest.mark.anyio -async def test_telegram_get_bot_unauthorized(asyncclient, bot, telegram): - response = await asyncclient.get( +async def test_telegram_get_bot_unauthorized(client, bot, telegram): + response = await client.get( "/api/hoods/{0}/telegram/{1}".format(telegram.hood.id, telegram.id) ) assert response.status_code == status.HTTP_401_UNAUTHORIZED diff --git a/backend/tests/tests_telegram/test_api_telegram_get_bots.py b/backend/tests/tests_telegram/test_api_telegram_get_bots.py index a109503..c3b3c78 100644 --- a/backend/tests/tests_telegram/test_api_telegram_get_bots.py +++ b/backend/tests/tests_telegram/test_api_telegram_get_bots.py @@ -11,7 +11,7 @@ from kibicara.platforms.telegram.model import Telegram @pytest.mark.anyio -async def test_telegram_get_bots(asyncclient, auth_header, hood_id): +async def test_telegram_get_bots(client, auth_header, hood_id): hood = await Hood.objects.get(id=hood_id) telegram0 = await Telegram.objects.create( hood=hood, @@ -23,7 +23,7 @@ async def test_telegram_get_bots(asyncclient, auth_header, hood_id): api_token="api_token456", welcome_message="welcome_message123", ) - response = await asyncclient.get( + response = await client.get( "/api/hoods/{0}/telegram/".format(telegram0.hood.id), headers=auth_header ) assert response.status_code == status.HTTP_200_OK @@ -34,14 +34,14 @@ async def test_telegram_get_bots(asyncclient, auth_header, hood_id): @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) +async def test_telegram_get_bots_invalid_id(client, auth_header, hood_id): + response = await client.get("/api/hoods/1337/telegram/", headers=auth_header) assert response.status_code == status.HTTP_404_NOT_FOUND - response = await asyncclient.get("/api/hoods/wrong/telegram/", headers=auth_header) + response = await client.get("/api/hoods/wrong/telegram/", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY @pytest.mark.anyio -async def test_telegram_get_bots_unauthorized(asyncclient, hood_id): - response = await asyncclient.get("/api/hoods/{0}/telegram/".format(hood_id)) +async def test_telegram_get_bots_unauthorized(client, hood_id): + response = await client.get("/api/hoods/{0}/telegram/".format(hood_id)) assert response.status_code == status.HTTP_401_UNAUTHORIZED -- 2.43.5