From 0f4b25fcde319726a359ccb737f450db6905805e Mon Sep 17 00:00:00 2001 From: missytake Date: Sun, 19 Mar 2023 17:25:37 +0100 Subject: [PATCH] [tests] Make Mastodon tests a bit more readable --- backend/tests/tests_mastodon/conftest.py | 12 ++++++--- .../test_api_mastodon_delete_bot.py | 16 ++++++++---- .../test_api_mastodon_get_bots.py | 26 +++++++++---------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/backend/tests/tests_mastodon/conftest.py b/backend/tests/tests_mastodon/conftest.py index 2e93e27..ea31bbb 100644 --- a/backend/tests/tests_mastodon/conftest.py +++ b/backend/tests/tests_mastodon/conftest.py @@ -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", diff --git a/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py b/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py index 6375b5f..1bebac3 100644 --- a/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py +++ b/backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py @@ -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 diff --git a/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py b/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py index 0417f9e..d3da4ac 100644 --- a/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py +++ b/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py @@ -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