[tests] Test mastodon_delete route
This commit is contained in:
parent
1a639548f0
commit
1bee3a8042
|
@ -15,16 +15,16 @@ from kibicara.model import Mapping
|
||||||
from kibicara.webapi import router
|
from kibicara.webapi import router
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='module')
|
@fixture(scope="module")
|
||||||
def client():
|
def client():
|
||||||
Mapping.drop_all()
|
Mapping.drop_all()
|
||||||
Mapping.create_all()
|
Mapping.create_all()
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.include_router(router, prefix='/api')
|
app.include_router(router, prefix="/api")
|
||||||
return TestClient(app)
|
return TestClient(app)
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='module')
|
@fixture(scope="module")
|
||||||
def monkeymodule():
|
def monkeymodule():
|
||||||
from _pytest.monkeypatch import MonkeyPatch
|
from _pytest.monkeypatch import MonkeyPatch
|
||||||
|
|
||||||
|
@ -33,96 +33,96 @@ def monkeymodule():
|
||||||
mpatch.undo()
|
mpatch.undo()
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='module')
|
@fixture(scope="module")
|
||||||
def receive_email(monkeymodule):
|
def receive_email(monkeymodule):
|
||||||
mailbox = []
|
mailbox = []
|
||||||
|
|
||||||
def mock_send_email(to, subject, sender='kibicara', body=''):
|
def mock_send_email(to, subject, sender="kibicara", body=""):
|
||||||
mailbox.append(dict(to=to, subject=subject, sender=sender, body=body))
|
mailbox.append(dict(to=to, subject=subject, sender=sender, body=body))
|
||||||
|
|
||||||
def mock_receive_email():
|
def mock_receive_email():
|
||||||
return mailbox.pop()
|
return mailbox.pop()
|
||||||
|
|
||||||
monkeymodule.setattr(email, 'send_email', mock_send_email)
|
monkeymodule.setattr(email, "send_email", mock_send_email)
|
||||||
return mock_receive_email
|
return mock_receive_email
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='module')
|
@fixture(scope="module")
|
||||||
def register_token(client, receive_email):
|
def register_token(client, receive_email):
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'/api/admin/register/', json={'email': 'user', 'password': 'password'}
|
"/api/admin/register/", json={"email": "user", "password": "password"}
|
||||||
)
|
)
|
||||||
assert response.status_code == status.HTTP_202_ACCEPTED
|
assert response.status_code == status.HTTP_202_ACCEPTED
|
||||||
return urlparse(receive_email()['body']).query.split('=', 1)[1]
|
return urlparse(receive_email()["body"]).query.split("=", 1)[1]
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='module')
|
@fixture(scope="module")
|
||||||
def register_confirmed(client, register_token):
|
def register_confirmed(client, register_token):
|
||||||
response = client.post('/api/admin/confirm/{0}'.format(register_token))
|
response = client.post("/api/admin/confirm/{0}".format(register_token))
|
||||||
assert response.status_code == status.HTTP_200_OK
|
assert response.status_code == status.HTTP_200_OK
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='module')
|
@fixture(scope="module")
|
||||||
def access_token(client, register_confirmed):
|
def access_token(client, register_confirmed):
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'/api/admin/login/', data={'username': 'user', 'password': 'password'}
|
"/api/admin/login/", data={"username": "user", "password": "password"}
|
||||||
)
|
)
|
||||||
assert response.status_code == status.HTTP_200_OK
|
assert response.status_code == status.HTTP_200_OK
|
||||||
return response.json()['access_token']
|
return response.json()["access_token"]
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='module')
|
@fixture(scope="module")
|
||||||
def auth_header(access_token):
|
def auth_header(access_token):
|
||||||
return {'Authorization': 'Bearer {0}'.format(access_token)}
|
return {"Authorization": "Bearer {0}".format(access_token)}
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='function')
|
@fixture(scope="function")
|
||||||
def hood_id(client, auth_header):
|
def hood_id(client, auth_header):
|
||||||
response = client.post('/api/hoods/', json={'name': 'hood'}, headers=auth_header)
|
response = client.post("/api/hoods/", json={"name": "hood"}, headers=auth_header)
|
||||||
assert response.status_code == status.HTTP_201_CREATED
|
assert response.status_code == status.HTTP_201_CREATED
|
||||||
hood_id = int(response.headers['Location'])
|
hood_id = int(response.headers["Location"])
|
||||||
yield hood_id
|
yield hood_id
|
||||||
client.delete('/api/hoods/{0}'.format(hood_id), headers=auth_header)
|
client.delete("/api/hoods/{0}".format(hood_id), headers=auth_header)
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='function')
|
@fixture(scope="function")
|
||||||
def trigger_id(client, hood_id, auth_header):
|
def trigger_id(client, hood_id, auth_header):
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'/api/hoods/{0}/triggers/'.format(hood_id),
|
"/api/hoods/{0}/triggers/".format(hood_id),
|
||||||
json={'pattern': 'test'},
|
json={"pattern": "test"},
|
||||||
headers=auth_header,
|
headers=auth_header,
|
||||||
)
|
)
|
||||||
assert response.status_code == status.HTTP_201_CREATED
|
assert response.status_code == status.HTTP_201_CREATED
|
||||||
trigger_id = int(response.headers['Location'])
|
trigger_id = int(response.headers["Location"])
|
||||||
yield trigger_id
|
yield trigger_id
|
||||||
client.delete(
|
client.delete(
|
||||||
'/api/hoods/{0}/triggers/{1}'.format(hood_id, trigger_id), headers=auth_header
|
"/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id), headers=auth_header
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='function')
|
@fixture(scope="function")
|
||||||
def badword_id(client, hood_id, auth_header):
|
def badword_id(client, hood_id, auth_header):
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'/api/hoods/{0}/badwords/'.format(hood_id),
|
"/api/hoods/{0}/badwords/".format(hood_id),
|
||||||
json={'pattern': ''},
|
json={"pattern": ""},
|
||||||
headers=auth_header,
|
headers=auth_header,
|
||||||
)
|
)
|
||||||
assert response.status_code == status.HTTP_201_CREATED
|
assert response.status_code == status.HTTP_201_CREATED
|
||||||
badword_id = int(response.headers['Location'])
|
badword_id = int(response.headers["Location"])
|
||||||
yield badword_id
|
yield badword_id
|
||||||
client.delete(
|
client.delete(
|
||||||
'/api/hoods/{0}/badwords/{1}'.format(hood_id, badword_id), headers=auth_header
|
"/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id), headers=auth_header
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope='function')
|
@fixture(scope="function")
|
||||||
def test_id(client, hood_id, auth_header):
|
def test_id(client, hood_id, auth_header):
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'/api/hoods/{0}/test/'.format(hood_id), json={}, headers=auth_header
|
"/api/hoods/{0}/test/".format(hood_id), json={}, headers=auth_header
|
||||||
)
|
)
|
||||||
assert response.status_code == status.HTTP_201_CREATED
|
assert response.status_code == status.HTTP_201_CREATED
|
||||||
test_id = int(response.headers['Location'])
|
test_id = int(response.headers["Location"])
|
||||||
yield test_id
|
yield test_id
|
||||||
client.delete(
|
client.delete(
|
||||||
'/api/hoods/{0}/test/{1}'.format(hood_id, test_id), headers=auth_header
|
"/api/hoods/{0}/test/{1}".format(hood_id, test_id), headers=auth_header
|
||||||
)
|
)
|
||||||
|
|
30
backend/tests/tests_mastodon/conftest.py
Normal file
30
backend/tests/tests_mastodon/conftest.py
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
# 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 pytest import fixture
|
||||||
|
|
||||||
|
from kibicara.model import Hood
|
||||||
|
from kibicara.platforms.mastodon.model import MastodonAccount, MastodonInstance
|
||||||
|
|
||||||
|
|
||||||
|
@fixture(scope='function')
|
||||||
|
def mastodon(event_loop, hood_id):
|
||||||
|
hood = event_loop.run_until_complete(Hood.objects.get(id=hood_id))
|
||||||
|
instance = event_loop.run_until_complete(
|
||||||
|
MastodonInstance.objects.create(
|
||||||
|
name="inst4nce",
|
||||||
|
client_id="cl13nt_id",
|
||||||
|
client_secret="cl13nt_s3cr3t",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return event_loop.run_until_complete(
|
||||||
|
MastodonAccount.objects.create(
|
||||||
|
hood=hood,
|
||||||
|
instance=instance,
|
||||||
|
access_token="t0k3n",
|
||||||
|
enabled=True,
|
||||||
|
username="us3r",
|
||||||
|
)
|
||||||
|
)
|
42
backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py
Normal file
42
backend/tests/tests_mastodon/test_api_mastodon_delete_bot.py
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
# 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 ormantic.exceptions import NoMatch
|
||||||
|
from pytest import raises
|
||||||
|
|
||||||
|
from kibicara.platforms.mastodon.model import MastodonAccount
|
||||||
|
|
||||||
|
|
||||||
|
def test_mastodon_delete_bot(client, event_loop, mastodon, auth_header):
|
||||||
|
response = client.delete(
|
||||||
|
'/api/hoods/{0}/mastodon/{1}'.format(mastodon.hood.id, mastodon.id),
|
||||||
|
headers=auth_header,
|
||||||
|
)
|
||||||
|
assert response.status_code == status.HTTP_204_NO_CONTENT
|
||||||
|
with raises(NoMatch):
|
||||||
|
event_loop.run_until_complete(MastodonAccount.objects.get(id=mastodon.id))
|
||||||
|
|
||||||
|
|
||||||
|
def test_mastodon_delete_bot_invalid_id(client, auth_header, hood_id):
|
||||||
|
response = client.delete('/api/hoods/1337/mastodon/123', headers=auth_header)
|
||||||
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||||
|
response = client.delete('/api/hoods/wrong/mastodon/123', headers=auth_header)
|
||||||
|
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
||||||
|
response = client.delete(
|
||||||
|
'/api/hoods/{0}/mastodon/7331'.format(hood_id), headers=auth_header
|
||||||
|
)
|
||||||
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||||
|
response = client.delete(
|
||||||
|
'/api/hoods/{0}/mastodon/wrong'.format(hood_id), headers=auth_header
|
||||||
|
)
|
||||||
|
assert response.status_code == status.HTTP_404_NOT_FOUND
|
||||||
|
|
||||||
|
|
||||||
|
def test_mastodon_delete_bot_unauthorized(client, mastodon):
|
||||||
|
response = client.delete(
|
||||||
|
'/api/hoods/{0}/mastodon/{1}'.format(mastodon.hood.id, mastodon.id)
|
||||||
|
)
|
||||||
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
Loading…
Reference in a new issue