ticketfrei3/backend/tests/tests_mastodon/test_api_mastodon_create_bot.py

107 lines
3.1 KiB
Python
Raw Normal View History

# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
#
# SPDX-License-Identifier: 0BSD
from fastapi import status
2023-04-01 13:13:50 +00:00
import pytest
from mastodon.Mastodon import Mastodon
from kibicara.platforms import mastodon
from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
2023-04-01 13:13:50 +00:00
@pytest.fixture(scope="function")
def disable_spawner(monkeypatch):
class DoNothing:
def start(self, bot):
assert bot is not None
monkeypatch.setattr(mastodon.webapi, "spawner", DoNothing())
2023-04-01 13:13:50 +00:00
@pytest.mark.parametrize(
"body",
[
{
"instance_url": "botsin.space",
"email": "test@example.org",
"password": "string",
}
],
)
2023-04-01 13:13:50 +00:00
@pytest.mark.anyio
async def test_mastodon_create_bot(
2023-04-01 13:32:10 +00:00
client,
disable_spawner,
hood_id,
auth_header,
monkeypatch,
body,
):
def log_in_mock(self, username, password):
return "acc3ss_t0ken"
monkeypatch.setattr(Mastodon, "log_in", log_in_mock)
2023-04-01 13:32:10 +00:00
response = await client.post(
"/api/hoods/{0}/mastodon/".format(hood_id),
json=body,
headers=auth_header,
)
print(response.json())
assert response.status_code == status.HTTP_201_CREATED
bot_id = response.json()["id"]
2023-04-01 13:13:50 +00:00
mastodon_obj = await MastodonAccount.objects.get(id=bot_id)
assert response.json()["access_token"] == mastodon_obj.access_token
2023-04-01 13:13:50 +00:00
mastodon_instance = await MastodonInstance.objects.get(id=mastodon_obj.instance.id)
assert (
response.json()["instance"]["name"]
== body["instance_url"]
== mastodon_instance.name
)
assert response.json()["hood"]["id"] == mastodon_obj.hood.id
assert response.json()["instance"]["client_id"] == mastodon_instance.client_id
assert (
response.json()["instance"]["client_secret"] == mastodon_instance.client_secret
)
assert mastodon_obj.enabled
2023-04-01 13:13:50 +00:00
@pytest.mark.parametrize(
"body",
[
{"instance_url": "botsin.space", "email": "notanemail", "password": "asdf1234"},
{"instance_url": "wrong", "email": "asdf@example.org", "password": "asdf1234"},
],
)
2023-04-01 13:13:50 +00:00
@pytest.mark.anyio
async def test_mastodon_invalid_input(
2023-04-01 13:32:10 +00:00
client,
disable_spawner,
hood_id,
auth_header,
monkeypatch,
body,
):
2023-04-01 13:32:10 +00:00
response = await client.post(
"/api/hoods/{0}/mastodon/".format(hood_id),
json=body,
headers=auth_header,
)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
2023-04-01 13:13:50 +00:00
@pytest.mark.anyio
2023-04-01 13:32:10 +00:00
async def test_mastodon_create_mastodon_invalid_id(client, auth_header):
response = await client.post("/api/hoods/1337/mastodon/", headers=auth_header)
assert response.status_code == status.HTTP_404_NOT_FOUND
2023-04-01 13:32:10 +00:00
response = await client.post("/api/hoods/wrong/mastodon/", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
2023-04-01 13:13:50 +00:00
@pytest.mark.anyio
2023-04-01 13:32:10 +00:00
async def test_mastodon_create_unauthorized(client, hood_id):
response = await client.post("/api/hoods/{hood_id}/mastodon/")
assert response.status_code == status.HTTP_401_UNAUTHORIZED