2023-03-18 21:00:17 +00:00
|
|
|
# Copyright (C) 2020 by Cathy Hu <cathy.hu@fau.de>
|
|
|
|
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
|
2023-03-19 18:03:25 +00:00
|
|
|
# Copyright (C) 2023 by Thomas Lindner <tom@dl6tom.de>
|
2023-03-18 21:00:17 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
|
|
|
from fastapi import status
|
2023-04-01 13:13:50 +00:00
|
|
|
import pytest
|
2023-03-18 21:00:17 +00:00
|
|
|
from mastodon.Mastodon import Mastodon
|
|
|
|
|
|
|
|
from kibicara.platforms import mastodon
|
2023-03-19 18:03:25 +00:00
|
|
|
from kibicara.platforms.mastodon.model import MastodonAccount
|
2023-03-18 21:00:17 +00:00
|
|
|
|
|
|
|
|
2023-04-01 13:13:50 +00:00
|
|
|
@pytest.fixture(scope="function")
|
2023-03-18 21:00:17 +00:00
|
|
|
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(
|
2023-03-18 21:00:17 +00:00
|
|
|
"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,
|
2023-03-18 21:00:17 +00:00
|
|
|
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(
|
2023-03-18 21:00:17 +00:00
|
|
|
"/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-03-19 18:03:25 +00:00
|
|
|
mastodon_obj = await MastodonAccount.get(id=bot_id)
|
2023-03-18 21:00:17 +00:00
|
|
|
assert response.json()["access_token"] == mastodon_obj.access_token
|
|
|
|
assert mastodon_obj.enabled
|
|
|
|
|
|
|
|
|
2023-04-01 13:13:50 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-03-18 21:00:17 +00:00
|
|
|
"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,
|
2023-03-18 21:00:17 +00:00
|
|
|
disable_spawner,
|
|
|
|
hood_id,
|
|
|
|
auth_header,
|
|
|
|
monkeypatch,
|
|
|
|
body,
|
|
|
|
):
|
2023-04-01 13:32:10 +00:00
|
|
|
response = await client.post(
|
2023-03-18 21:00:17 +00:00
|
|
|
"/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)
|
2023-03-18 21:00:17 +00:00
|
|
|
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)
|
2023-03-18 21:00:17 +00:00
|
|
|
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/")
|
2023-03-18 21:00:17 +00:00
|
|
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|