[tests] Testing the mastodon_create API endpoint

This commit is contained in:
missytake 2023-03-18 22:00:17 +01:00
parent f533efee4f
commit a548c2febc

View file

@ -0,0 +1,106 @@
# 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
from pytest import fixture, mark
from mastodon.Mastodon import Mastodon
from kibicara.platforms import mastodon
from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
@fixture(scope="function")
def disable_spawner(monkeypatch):
class DoNothing:
def start(self, bot):
assert bot is not None
monkeypatch.setattr(mastodon.webapi, "spawner", DoNothing())
@mark.parametrize(
"body",
[
{
"instance_url": "botsin.space",
"email": "test@example.org",
"password": "string",
}
],
)
def test_mastodon_create_bot(
event_loop,
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)
response = 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"]
mastodon_obj = event_loop.run_until_complete(MastodonAccount.objects.get(id=bot_id))
assert response.json()["access_token"] == mastodon_obj.access_token
mastodon_instance = event_loop.run_until_complete(
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
@mark.parametrize(
"body",
[
{"instance_url": "botsin.space", "email": "notanemail", "password": "asdf1234"},
{"instance_url": "wrong", "email": "asdf@example.org", "password": "asdf1234"},
],
)
def test_mastodon_invalid_input(
event_loop,
client,
disable_spawner,
hood_id,
auth_header,
monkeypatch,
body,
):
response = client.post(
"/api/hoods/{0}/mastodon/".format(hood_id),
json=body,
headers=auth_header,
)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_mastodon_create_mastodon_invalid_id(client, auth_header):
response = client.post("/api/hoods/1337/mastodon/", headers=auth_header)
assert response.status_code == status.HTTP_404_NOT_FOUND
response = client.post("/api/hoods/wrong/mastodon/", headers=auth_header)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_mastodon_create_unauthorized(client, hood_id):
response = client.post("/api/hoods/{hood_id}/mastodon/")
assert response.status_code == status.HTTP_401_UNAUTHORIZED