# Copyright (C) 2020 by Cathy Hu # Copyright (C) 2020 by Martin Rey # # SPDX-License-Identifier: 0BSD from fastapi import status import pytest from mastodon.Mastodon import Mastodon from kibicara.platforms import mastodon from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance @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()) @pytest.mark.parametrize( "body", [ { "instance_url": "botsin.space", "email": "test@example.org", "password": "string", } ], ) @pytest.mark.anyio async def test_mastodon_create_bot( asyncclient, 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 = await asyncclient.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 = await MastodonAccount.objects.get(id=bot_id) assert response.json()["access_token"] == mastodon_obj.access_token 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 @pytest.mark.parametrize( "body", [ {"instance_url": "botsin.space", "email": "notanemail", "password": "asdf1234"}, {"instance_url": "wrong", "email": "asdf@example.org", "password": "asdf1234"}, ], ) @pytest.mark.anyio async def test_mastodon_invalid_input( asyncclient, disable_spawner, hood_id, auth_header, monkeypatch, body, ): response = await asyncclient.post( "/api/hoods/{0}/mastodon/".format(hood_id), json=body, headers=auth_header, ) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY @pytest.mark.anyio async def test_mastodon_create_mastodon_invalid_id(asyncclient, auth_header): response = await asyncclient.post("/api/hoods/1337/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_404_NOT_FOUND response = await asyncclient.post("/api/hoods/wrong/mastodon/", headers=auth_header) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY @pytest.mark.anyio async def test_mastodon_create_unauthorized(asyncclient, hood_id): response = await asyncclient.post("/api/hoods/{hood_id}/mastodon/") assert response.status_code == status.HTTP_401_UNAUTHORIZED