[tests] Ported mastodon tests to async

This commit is contained in:
missytake 2023-04-01 15:13:50 +02:00
parent b876e645de
commit f0e15f1d37
4 changed files with 81 additions and 72 deletions

View file

@ -3,32 +3,30 @@
# #
# SPDX-License-Identifier: 0BSD # SPDX-License-Identifier: 0BSD
from pytest import fixture import pytest
from kibicara.model import Hood from kibicara.model import Hood
from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
@fixture(scope="function") @pytest.fixture(scope="function")
def mastodon_instance(event_loop): @pytest.mark.anyio
return event_loop.run_until_complete( async def mastodon_instance(event_loop):
MastodonInstance.objects.create( return await MastodonInstance.objects.create(
name="inst4nce", name="inst4nce",
client_id="cl13nt_id", client_id="cl13nt_id",
client_secret="cl13nt_s3cr3t", client_secret="cl13nt_s3cr3t",
)
) )
@fixture(scope="function") @pytest.fixture(scope="function")
def mastodon_account(event_loop, hood_id, mastodon_instance): @pytest.mark.anyio
hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id)) async def mastodon_account(event_loop, hood_id, mastodon_instance):
return event_loop.run_until_complete( hood = await Hood.objects.get(id=hood_id)
MastodonAccount.objects.create( return await MastodonAccount.objects.create(
hood=hood, hood=hood,
instance=mastodon_instance, instance=mastodon_instance,
access_token="t0k3n", access_token="t0k3n",
enabled=True, enabled=True,
username="us3r", username="us3r",
)
) )

View file

@ -4,14 +4,14 @@
# SPDX-License-Identifier: 0BSD # SPDX-License-Identifier: 0BSD
from fastapi import status from fastapi import status
from pytest import fixture, mark import pytest
from mastodon.Mastodon import Mastodon from mastodon.Mastodon import Mastodon
from kibicara.platforms import mastodon from kibicara.platforms import mastodon
from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
@fixture(scope="function") @pytest.fixture(scope="function")
def disable_spawner(monkeypatch): def disable_spawner(monkeypatch):
class DoNothing: class DoNothing:
def start(self, bot): def start(self, bot):
@ -20,7 +20,7 @@ def disable_spawner(monkeypatch):
monkeypatch.setattr(mastodon.webapi, "spawner", DoNothing()) monkeypatch.setattr(mastodon.webapi, "spawner", DoNothing())
@mark.parametrize( @pytest.mark.parametrize(
"body", "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, event_loop,
client, asyncclient,
disable_spawner, disable_spawner,
hood_id, hood_id,
auth_header, auth_header,
@ -44,7 +45,7 @@ def test_mastodon_create_bot(
monkeypatch.setattr(Mastodon, "log_in", log_in_mock) monkeypatch.setattr(Mastodon, "log_in", log_in_mock)
response = client.post( response = await asyncclient.post(
"/api/hoods/{0}/mastodon/".format(hood_id), "/api/hoods/{0}/mastodon/".format(hood_id),
json=body, json=body,
headers=auth_header, headers=auth_header,
@ -52,11 +53,9 @@ def test_mastodon_create_bot(
print(response.json()) print(response.json())
assert response.status_code == status.HTTP_201_CREATED assert response.status_code == status.HTTP_201_CREATED
bot_id = response.json()["id"] 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 assert response.json()["access_token"] == mastodon_obj.access_token
mastodon_instance = event_loop.run_until_complete( mastodon_instance = await MastodonInstance.objects.get(id=mastodon_obj.instance.id)
MastodonInstance.objects.get(id=mastodon_obj.instance.id)
)
assert ( assert (
response.json()["instance"]["name"] response.json()["instance"]["name"]
== body["instance_url"] == body["instance_url"]
@ -70,23 +69,24 @@ def test_mastodon_create_bot(
assert mastodon_obj.enabled assert mastodon_obj.enabled
@mark.parametrize( @pytest.mark.parametrize(
"body", "body",
[ [
{"instance_url": "botsin.space", "email": "notanemail", "password": "asdf1234"}, {"instance_url": "botsin.space", "email": "notanemail", "password": "asdf1234"},
{"instance_url": "wrong", "email": "asdf@example.org", "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, event_loop,
client, asyncclient,
disable_spawner, disable_spawner,
hood_id, hood_id,
auth_header, auth_header,
monkeypatch, monkeypatch,
body, body,
): ):
response = client.post( response = await asyncclient.post(
"/api/hoods/{0}/mastodon/".format(hood_id), "/api/hoods/{0}/mastodon/".format(hood_id),
json=body, json=body,
headers=auth_header, headers=auth_header,
@ -94,13 +94,15 @@ def test_mastodon_invalid_input(
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_mastodon_create_mastodon_invalid_id(client, auth_header): @pytest.mark.anyio
response = client.post("/api/hoods/1337/mastodon/", headers=auth_header) 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 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 assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_mastodon_create_unauthorized(client, hood_id): @pytest.mark.anyio
response = client.post("/api/hoods/{hood_id}/mastodon/") 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 assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -5,42 +5,47 @@
from fastapi import status from fastapi import status
from ormantic.exceptions import NoMatch from ormantic.exceptions import NoMatch
from pytest import raises import pytest
from kibicara.platforms.mastodon.model import MastodonAccount from kibicara.platforms.mastodon.model import MastodonAccount
def test_mastodon_delete_bot(client, event_loop, mastodon_account, auth_header): @pytest.mark.anyio
response = client.delete( async def test_mastodon_delete_bot(asyncclient, mastodon_account, auth_header):
response = await asyncclient.delete(
"/api/hoods/{0}/mastodon/{1}".format( "/api/hoods/{0}/mastodon/{1}".format(
mastodon_account.hood.id, mastodon_account.id mastodon_account.hood.id, mastodon_account.id
), ),
headers=auth_header, headers=auth_header,
) )
assert response.status_code == status.HTTP_204_NO_CONTENT assert response.status_code == status.HTTP_204_NO_CONTENT
with raises(NoMatch): with pytest.raises(NoMatch):
event_loop.run_until_complete( await MastodonAccount.objects.get(id=mastodon_account.id)
MastodonAccount.objects.get(id=mastodon_account.id)
)
def test_mastodon_delete_bot_invalid_id(client, auth_header, hood_id): @pytest.mark.anyio
response = client.delete("/api/hoods/1337/mastodon/123", headers=auth_header) 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 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 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 "/api/hoods/{0}/mastodon/7331".format(hood_id), headers=auth_header
) )
assert response.status_code == status.HTTP_404_NOT_FOUND 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 "/api/hoods/{0}/mastodon/wrong".format(hood_id), headers=auth_header
) )
assert response.status_code == status.HTTP_404_NOT_FOUND assert response.status_code == status.HTTP_404_NOT_FOUND
def test_mastodon_delete_bot_unauthorized(client, mastodon_account): @pytest.mark.anyio
response = client.delete( async def test_mastodon_delete_bot_unauthorized(asyncclient, mastodon_account):
response = await asyncclient.delete(
"/api/hoods/{0}/mastodon/{1}".format( "/api/hoods/{0}/mastodon/{1}".format(
mastodon_account.hood.id, mastodon_account.id mastodon_account.hood.id, mastodon_account.id
) )

View file

@ -4,25 +4,26 @@
# SPDX-License-Identifier: 0BSD # SPDX-License-Identifier: 0BSD
from fastapi import status from fastapi import status
import pytest
from kibicara.platforms.mastodon.model import MastodonAccount from kibicara.platforms.mastodon.model import MastodonAccount
def test_mastodon_get_bots( @pytest.mark.anyio
client, auth_header, event_loop, hood_id, mastodon_account, mastodon_instance async def test_mastodon_get_bots(
asyncclient, auth_header, hood_id, mastodon_account, mastodon_instance
): ):
mastodon2 = event_loop.run_until_complete( mastodon2 = await MastodonAccount.objects.create(
MastodonAccount.objects.create( hood=mastodon_account.hood,
hood=mastodon_account.hood, instance=mastodon_instance,
instance=mastodon_instance, access_token="4cc3ss",
access_token="4cc3ss", enabled=True,
enabled=True, username="us4r",
username="us4r",
)
) )
response = client.get( response = await asyncclient.get(
"/api/hoods/{0}/mastodon".format(mastodon_account.hood.id), headers=auth_header "/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.status_code == status.HTTP_200_OK
assert response.json()[0]["id"] == mastodon_account.id assert response.json()[0]["id"] == mastodon_account.id
assert response.json()[0]["access_token"] == mastodon_account.access_token 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 assert response.json()[1]["access_token"] == mastodon2.access_token
def test_mastodon_get_bots_invalid_id(client, auth_header, hood_id): @pytest.mark.anyio
response = client.get("/api/hoods/1337/mastodon", headers=auth_header) 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 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 assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_mastodon_get_bots_unauthorized(client, hood_id): @pytest.mark.anyio
response = client.get("/api/hoods/{0}/mastodon".format(hood_id)) 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 assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_mastodon_public(client, mastodon_account, mastodon_instance, event_loop): @pytest.mark.anyio
response = client.get( async def test_mastodon_public(asyncclient, mastodon_account, mastodon_instance):
response = await asyncclient.get(
"/api/hoods/{0}/mastodon/public".format(mastodon_account.hood.id) "/api/hoods/{0}/mastodon/public".format(mastodon_account.hood.id)
) )
assert response.json()[0]["username"] == mastodon_account.username assert response.json()[0]["username"] == mastodon_account.username