[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
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(
@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(
@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",
)
)

View file

@ -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

View file

@ -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)
@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
)
def test_mastodon_delete_bot_invalid_id(client, auth_header, hood_id):
response = client.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
)

View file

@ -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(
mastodon2 = await MastodonAccount.objects.create(
hood=mastodon_account.hood,
instance=mastodon_instance,
access_token="4cc3ss",
enabled=True,
username="us4r",
)
response = await asyncclient.get(
"/api/hoods/{0}/mastodon/".format(mastodon_account.hood.id), headers=auth_header
)
response = client.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