From b876e645dedbedfdf5d4a76e9889261397398324 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 1 Apr 2023 14:57:41 +0200 Subject: [PATCH] [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"