[tests] Make Mastodon tests a bit more readable

This commit is contained in:
missytake 2023-03-19 17:25:37 +01:00
parent 5d9f7cbe63
commit f97e041284
3 changed files with 31 additions and 23 deletions

View file

@ -10,19 +10,23 @@ from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
@fixture(scope="function")
def mastodon(event_loop, hood_id):
hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id))
instance = event_loop.run_until_complete(
def mastodon_instance(event_loop):
return event_loop.run_until_complete(
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=instance,
instance=mastodon_instance,
access_token="t0k3n",
enabled=True,
username="us3r",

View file

@ -10,14 +10,18 @@ from pytest import raises
from kibicara.platforms.mastodon.model import MastodonAccount
def test_mastodon_delete_bot(client, event_loop, mastodon, auth_header):
def test_mastodon_delete_bot(client, event_loop, mastodon_account, auth_header):
response = client.delete(
"/api/hoods/{0}/mastodon/{1}".format(mastodon.hood.id, mastodon.id),
"/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.id))
event_loop.run_until_complete(
MastodonAccount.objects.get(id=mastodon_account.id)
)
def test_mastodon_delete_bot_invalid_id(client, auth_header, hood_id):
@ -35,8 +39,10 @@ def test_mastodon_delete_bot_invalid_id(client, auth_header, hood_id):
assert response.status_code == status.HTTP_404_NOT_FOUND
def test_mastodon_delete_bot_unauthorized(client, mastodon):
def test_mastodon_delete_bot_unauthorized(client, mastodon_account):
response = client.delete(
"/api/hoods/{0}/mastodon/{1}".format(mastodon.hood.id, mastodon.id)
"/api/hoods/{0}/mastodon/{1}".format(
mastodon_account.hood.id, mastodon_account.id
)
)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -5,16 +5,15 @@
from fastapi import status
from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
from kibicara.platforms.mastodon.model import MastodonAccount
def test_mastodon_get_bots(client, auth_header, event_loop, hood_id, mastodon):
mastodon_instance = event_loop.run_until_complete(
MastodonInstance.objects.get(id=mastodon.instance)
)
def test_mastodon_get_bots(
client, auth_header, event_loop, hood_id, mastodon_account, mastodon_instance
):
mastodon2 = event_loop.run_until_complete(
MastodonAccount.objects.create(
hood=mastodon.hood,
hood=mastodon_account.hood,
instance=mastodon_instance,
access_token="4cc3ss",
enabled=True,
@ -22,11 +21,11 @@ def test_mastodon_get_bots(client, auth_header, event_loop, hood_id, mastodon):
)
)
response = client.get(
"/api/hoods/{0}/mastodon".format(mastodon.hood.id), headers=auth_header
"/api/hoods/{0}/mastodon".format(mastodon_account.hood.id), headers=auth_header
)
assert response.status_code == status.HTTP_200_OK
assert response.json()[0]["id"] == mastodon.id
assert response.json()[0]["access_token"] == mastodon.access_token
assert response.json()[0]["id"] == mastodon_account.id
assert response.json()[0]["access_token"] == mastodon_account.access_token
assert response.json()[1]["id"] == mastodon2.id
assert response.json()[1]["access_token"] == mastodon2.access_token
@ -43,10 +42,9 @@ def test_mastodon_get_bots_unauthorized(client, hood_id):
assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_mastodon_public(client, mastodon, event_loop):
mastodon_instance = event_loop.run_until_complete(
MastodonInstance.objects.get(id=mastodon.instance)
def test_mastodon_public(client, mastodon_account, mastodon_instance, event_loop):
response = client.get(
"/api/hoods/{0}/mastodon/public".format(mastodon_account.hood.id)
)
response = client.get("/api/hoods/{0}/mastodon/public".format(mastodon.hood.id))
assert response.json()[0]["username"] == mastodon.username
assert response.json()[0]["username"] == mastodon_account.username
assert response.json()[0]["instance"] == mastodon_instance.name