[tests] Ported email tests to async

This commit is contained in:
missytake 2023-04-01 14:57:41 +02:00
parent 6b6a2777bb
commit b876e645de
4 changed files with 54 additions and 33 deletions

View file

@ -6,12 +6,13 @@
from fastapi import status from fastapi import status
from pytest import fixture import pytest
@fixture(scope="function") @pytest.fixture(scope="function")
def email_row(client, hood_id, auth_header): @pytest.mark.anyio
response = client.post( async def email_row(asyncclient, hood_id, auth_header):
response = await asyncclient.post(
"/api/hoods/{0}/email/".format(hood_id), "/api/hoods/{0}/email/".format(hood_id),
json={"name": "kibicara-test"}, json={"name": "kibicara-test"},
headers=auth_header, headers=auth_header,
@ -19,6 +20,6 @@ def email_row(client, hood_id, auth_header):
assert response.status_code == status.HTTP_201_CREATED assert response.status_code == status.HTTP_201_CREATED
email_id = int(response.headers["Location"]) email_id = int(response.headers["Location"])
yield response.json() yield response.json()
client.delete( await asyncclient.delete(
"/api/hoods/{0}/email/{1}".format(hood_id, email_id), headers=auth_header "/api/hoods/{0}/email/{1}".format(hood_id, email_id), headers=auth_header
) )

View file

@ -8,13 +8,14 @@ from re import findall
from urllib.parse import urlparse from urllib.parse import urlparse
from fastapi import status from fastapi import status
from pytest import skip import pytest
from kibicara.webapi.admin import to_token from kibicara.webapi.admin import to_token
def test_email_subscribe_unsubscribe(client, hood_id, receive_email): @pytest.mark.anyio
response = client.post( async def test_email_subscribe_unsubscribe(asyncclient, hood_id, receive_email):
response = await asyncclient.post(
"/api/hoods/{0}/email/subscribe/".format(hood_id), "/api/hoods/{0}/email/subscribe/".format(hood_id),
json={"email": "test@localhost"}, json={"email": "test@localhost"},
) )
@ -27,37 +28,41 @@ def test_email_subscribe_unsubscribe(client, hood_id, receive_email):
body, body,
)[0] )[0]
start = len("token=") start = len("token=")
response = client.post( response = await asyncclient.post(
"/api/hoods/{0}/email/subscribe/confirm/{1}".format( "/api/hoods/{0}/email/subscribe/confirm/{1}".format(
hood_id, urlparse(confirm_url).query[start:] hood_id, urlparse(confirm_url).query[start:]
) )
) )
assert response.status_code == status.HTTP_201_CREATED assert response.status_code == status.HTTP_201_CREATED
response = client.post( response = await asyncclient.post(
"/api/hoods/{0}/email/subscribe/confirm/{1}".format( "/api/hoods/{0}/email/subscribe/confirm/{1}".format(
hood_id, urlparse(confirm_url).query[start:] hood_id, urlparse(confirm_url).query[start:]
) )
) )
assert response.status_code == status.HTTP_409_CONFLICT assert response.status_code == status.HTTP_409_CONFLICT
token = to_token(email=mail["to"], hood=hood_id) 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) "/api/hoods/{0}/email/unsubscribe/{1}".format(hood_id, token)
) )
assert response.status_code == status.HTTP_204_NO_CONTENT 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 = { body = {
"text": "test", "text": "test",
"author": "test@localhost", "author": "test@localhost",
"secret": email_row["secret"], "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 assert response.status_code == status.HTTP_201_CREATED
def test_email_send_mda(trigger_id, email_row): @pytest.mark.anyio
skip("Only works if kibicara is listening on port 8000, and only sometimes") 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 mail = """From test@example.com Tue Jun 16 15:33:19 2020
Return-path: <test@example.com> Return-path: <test@example.com>
Envelope-to: hood@localhost Envelope-to: hood@localhost

View file

@ -5,21 +5,27 @@
# SPDX-License-Identifier: 0BSD # SPDX-License-Identifier: 0BSD
from fastapi import status from fastapi import status
import pytest
def test_email_create_unauthorized(client, hood_id): @pytest.mark.anyio
response = client.post("/api/hoods/{0}/email/".format(hood_id)) 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 assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_email_delete_unauthorized(client, hood_id, email_row): @pytest.mark.anyio
response = client.delete( 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"]) "/api/hoods/{0}/email/{1}".format(hood_id, email_row["id"])
) )
assert response.status_code == status.HTTP_401_UNAUTHORIZED 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"} 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 assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -5,16 +5,19 @@
from fastapi import status from fastapi import status
from nacl.exceptions import CryptoError from nacl.exceptions import CryptoError
import pytest
def test_email_subscribe_empty(client, hood_id): @pytest.mark.anyio
response = client.post("/api/hoods/{0}/email/subscribe/".format(hood_id)) 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 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: try:
response = client.post( response = await asyncclient.post(
"/api/hoods/{0}/email/subscribe/confirm/".format(hood_id) "/api/hoods/{0}/email/subscribe/confirm/".format(hood_id)
+ "asdfasdfasdfasdfasdfasdfasdfasdf" + "asdfasdfasdfasdfasdfasdfasdfasdf"
) )
@ -23,26 +26,31 @@ def test_email_subscribe_confirm_wrong_token(client, hood_id):
pass pass
def test_email_subscribe_confirm_wrong_hood(client): @pytest.mark.anyio
response = client.delete( async def test_email_subscribe_confirm_wrong_hood(asyncclient):
response = await asyncclient.delete(
"/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf" "/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf"
) )
assert response.json()["detail"] == "Not Found" 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 = { body = {
"text": "", "text": "",
"author": "test@localhost", "author": "test@localhost",
"secret": email_row["secret"], "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 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: try:
client.delete( await asyncclient.delete(
"/api/hoods/{0}/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf".format( "/api/hoods/{0}/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf".format(
hood_id hood_id
) )
@ -51,8 +59,9 @@ def test_email_unsubscribe_wrong_token(client, hood_id):
pass pass
def test_email_unsubscribe_wrong_hood(client): @pytest.mark.anyio
response = client.delete( async def test_email_unsubscribe_wrong_hood(asyncclient):
response = await asyncclient.delete(
"/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf" "/api/hoods/99999/email/unsubscribe/asdfasdfasdfasdfasdfasdfasdfasdf"
) )
assert response.json()["detail"] == "Not Found" assert response.json()["detail"] == "Not Found"