[tests] Make Mastodon tests a bit more readable

This commit is contained in:
missytake 2023-03-19 17:25:37 +01:00
parent 4f96dfbee7
commit 0f4b25fcde
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") @fixture(scope="function")
def mastodon(event_loop, hood_id): def mastodon_instance(event_loop):
hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id)) return event_loop.run_until_complete(
instance = event_loop.run_until_complete(
MastodonInstance.objects.create( 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")
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( return event_loop.run_until_complete(
MastodonAccount.objects.create( MastodonAccount.objects.create(
hood=hood, hood=hood,
instance=instance, instance=mastodon_instance,
access_token="t0k3n", access_token="t0k3n",
enabled=True, enabled=True,
username="us3r", username="us3r",

View file

@ -10,14 +10,18 @@ from pytest import raises
from kibicara.platforms.mastodon.model import MastodonAccount 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( 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, 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 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): 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 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( 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 assert response.status_code == status.HTTP_401_UNAUTHORIZED

View file

@ -5,16 +5,15 @@
from fastapi import status 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): def test_mastodon_get_bots(
mastodon_instance = event_loop.run_until_complete( client, auth_header, event_loop, hood_id, mastodon_account, mastodon_instance
MastodonInstance.objects.get(id=mastodon.instance) ):
)
mastodon2 = event_loop.run_until_complete( mastodon2 = event_loop.run_until_complete(
MastodonAccount.objects.create( MastodonAccount.objects.create(
hood=mastodon.hood, hood=mastodon_account.hood,
instance=mastodon_instance, instance=mastodon_instance,
access_token="4cc3ss", access_token="4cc3ss",
enabled=True, enabled=True,
@ -22,11 +21,11 @@ def test_mastodon_get_bots(client, auth_header, event_loop, hood_id, mastodon):
) )
) )
response = client.get( 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.status_code == status.HTTP_200_OK
assert response.json()[0]["id"] == mastodon.id assert response.json()[0]["id"] == mastodon_account.id
assert response.json()[0]["access_token"] == mastodon.access_token assert response.json()[0]["access_token"] == mastodon_account.access_token
assert response.json()[1]["id"] == mastodon2.id assert response.json()[1]["id"] == mastodon2.id
assert response.json()[1]["access_token"] == mastodon2.access_token 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 assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_mastodon_public(client, mastodon, event_loop): def test_mastodon_public(client, mastodon_account, mastodon_instance, event_loop):
mastodon_instance = event_loop.run_until_complete( response = client.get(
MastodonInstance.objects.get(id=mastodon.instance) "/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_account.username
assert response.json()[0]["username"] == mastodon.username
assert response.json()[0]["instance"] == mastodon_instance.name assert response.json()[0]["instance"] == mastodon_instance.name