diff --git a/backend/src/kibicara/platforms/mastodon/webapi.py b/backend/src/kibicara/platforms/mastodon/webapi.py index 025acb0..a71f1fd 100644 --- a/backend/src/kibicara/platforms/mastodon/webapi.py +++ b/backend/src/kibicara/platforms/mastodon/webapi.py @@ -74,11 +74,14 @@ twitter_callback_router = APIRouter() ) async def mastodon_read_all_public(hood=Depends(get_hood_unauthorized)): mastodonbots = await MastodonAccount.objects.filter(hood=hood).all() - return [ - BodyMastodonPublic(username=mbot.username, instance=mbot.model.instance.name) - for mbot in mastodonbots - if mbot.enabled == 1 and mbot.username - ] + mbots = [] + for mbot in mastodonbots: + if mbot.enabled == 1 and mbot.username: + instance = await MastodonInstance.objects.get(id=mbot.instance) + mbots.append( + BodyMastodonPublic(username=mbot.username, instance=instance.name) + ) + return mbots @router.get( diff --git a/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py b/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py new file mode 100644 index 0000000..0417f9e --- /dev/null +++ b/backend/tests/tests_mastodon/test_api_mastodon_get_bots.py @@ -0,0 +1,52 @@ +# Copyright (C) 2020 by Cathy Hu +# Copyright (C) 2020 by Martin Rey +# +# SPDX-License-Identifier: 0BSD + +from fastapi import status + +from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance + + +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) + ) + mastodon2 = event_loop.run_until_complete( + MastodonAccount.objects.create( + hood=mastodon.hood, + instance=mastodon_instance, + access_token="4cc3ss", + enabled=True, + username="us4r", + ) + ) + response = client.get( + "/api/hoods/{0}/mastodon".format(mastodon.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()[1]["id"] == mastodon2.id + 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) + assert response.status_code == status.HTTP_404_NOT_FOUND + response = client.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)) + 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) + ) + response = client.get("/api/hoods/{0}/mastodon/public".format(mastodon.hood.id)) + assert response.json()[0]["username"] == mastodon.username + assert response.json()[0]["instance"] == mastodon_instance.name