2020-07-07 01:16:23 +00:00
|
|
|
# Copyright (C) 2020 by Thomas Lindner <tom@dl6tom.de>
|
2020-07-16 10:12:03 +00:00
|
|
|
# Copyright (C) 2020 by Christian Hagenest <c.hagenest@pm.me>
|
2020-09-28 22:39:32 +00:00
|
|
|
# Copyright (C) 2020 by Martin Rey <martin.rey@mailbox.org>
|
2020-07-07 01:16:23 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: 0BSD
|
|
|
|
|
2020-10-13 08:35:20 +00:00
|
|
|
from urllib.parse import urlparse
|
|
|
|
|
2020-07-07 01:16:23 +00:00
|
|
|
from fastapi import FastAPI, status
|
2023-04-01 12:18:24 +00:00
|
|
|
from httpx import AsyncClient
|
2023-04-01 13:30:42 +00:00
|
|
|
import pytest
|
2020-10-13 08:35:20 +00:00
|
|
|
|
2020-07-11 16:23:27 +00:00
|
|
|
from kibicara import email
|
2020-07-19 12:13:15 +00:00
|
|
|
from kibicara.model import Mapping
|
2020-07-07 01:16:23 +00:00
|
|
|
from kibicara.webapi import router
|
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2023-04-01 12:40:02 +00:00
|
|
|
def anyio_backend():
|
|
|
|
return "asyncio"
|
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2023-04-01 13:32:10 +00:00
|
|
|
def client():
|
2023-04-01 12:18:24 +00:00
|
|
|
Mapping.drop_all()
|
|
|
|
Mapping.create_all()
|
|
|
|
app = FastAPI()
|
|
|
|
app.include_router(router, prefix="/api")
|
|
|
|
return AsyncClient(app=app, base_url="http://test")
|
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2020-07-11 16:23:27 +00:00
|
|
|
def monkeymodule():
|
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
|
|
|
|
|
|
|
mpatch = MonkeyPatch()
|
|
|
|
yield mpatch
|
|
|
|
mpatch.undo()
|
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2020-07-11 16:23:27 +00:00
|
|
|
def receive_email(monkeymodule):
|
|
|
|
mailbox = []
|
|
|
|
|
2023-03-18 17:47:02 +00:00
|
|
|
def mock_send_email(to, subject, sender="kibicara", body=""):
|
2020-07-11 16:23:27 +00:00
|
|
|
mailbox.append(dict(to=to, subject=subject, sender=sender, body=body))
|
|
|
|
|
|
|
|
def mock_receive_email():
|
|
|
|
return mailbox.pop()
|
2020-07-07 01:16:23 +00:00
|
|
|
|
2023-03-18 17:47:02 +00:00
|
|
|
monkeymodule.setattr(email, "send_email", mock_send_email)
|
2020-07-11 16:23:27 +00:00
|
|
|
return mock_receive_email
|
2020-07-07 01:16:23 +00:00
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def register_token(client, receive_email):
|
|
|
|
response = await client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/admin/register/", json={"email": "user", "password": "password"}
|
2020-07-11 16:23:27 +00:00
|
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_202_ACCEPTED
|
2023-03-18 17:47:02 +00:00
|
|
|
return urlparse(receive_email()["body"]).query.split("=", 1)[1]
|
2020-07-07 01:16:23 +00:00
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def register_confirmed(client, register_token):
|
|
|
|
response = await client.post("/api/admin/confirm/{0}".format(register_token))
|
2020-07-07 01:16:23 +00:00
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def access_token(client, register_confirmed):
|
|
|
|
response = await client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/admin/login/", data={"username": "user", "password": "password"}
|
2020-07-07 01:16:23 +00:00
|
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
2023-03-18 17:47:02 +00:00
|
|
|
return response.json()["access_token"]
|
2020-07-07 01:16:23 +00:00
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="module")
|
2020-07-07 01:16:23 +00:00
|
|
|
def auth_header(access_token):
|
2023-03-18 17:47:02 +00:00
|
|
|
return {"Authorization": "Bearer {0}".format(access_token)}
|
2020-07-07 01:16:23 +00:00
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def hood_id(client, auth_header):
|
|
|
|
response = await client.post(
|
2023-04-01 13:30:42 +00:00
|
|
|
"/api/hoods/", json={"name": "hood"}, headers=auth_header
|
|
|
|
)
|
2020-07-07 01:16:23 +00:00
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
2023-03-18 17:47:02 +00:00
|
|
|
hood_id = int(response.headers["Location"])
|
2020-07-07 01:16:23 +00:00
|
|
|
yield hood_id
|
2023-04-01 13:32:10 +00:00
|
|
|
await client.delete("/api/hoods/{0}".format(hood_id), headers=auth_header)
|
2020-07-07 01:16:23 +00:00
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def trigger_id(client, hood_id, auth_header):
|
|
|
|
response = await client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/triggers/".format(hood_id),
|
|
|
|
json={"pattern": "test"},
|
2020-07-13 01:21:11 +00:00
|
|
|
headers=auth_header,
|
2020-07-07 01:16:23 +00:00
|
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
2023-03-18 17:47:02 +00:00
|
|
|
trigger_id = int(response.headers["Location"])
|
2020-07-07 01:16:23 +00:00
|
|
|
yield trigger_id
|
2023-04-01 13:32:10 +00:00
|
|
|
await client.delete(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/triggers/{1}".format(hood_id, trigger_id), headers=auth_header
|
2020-07-07 01:16:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def badword_id(client, hood_id, auth_header):
|
|
|
|
response = await client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/badwords/".format(hood_id),
|
|
|
|
json={"pattern": ""},
|
2020-10-12 20:47:06 +00:00
|
|
|
headers=auth_header,
|
2020-07-07 01:16:23 +00:00
|
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
2023-03-18 17:47:02 +00:00
|
|
|
badword_id = int(response.headers["Location"])
|
2020-07-07 01:16:23 +00:00
|
|
|
yield badword_id
|
2023-04-01 13:32:10 +00:00
|
|
|
await client.delete(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/badwords/{1}".format(hood_id, badword_id), headers=auth_header
|
2020-07-07 01:16:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-04-01 13:30:42 +00:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
@pytest.mark.anyio
|
2023-04-01 13:32:10 +00:00
|
|
|
async def test_id(client, hood_id, auth_header):
|
|
|
|
response = await client.post(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/test/".format(hood_id), json={}, headers=auth_header
|
2020-07-07 01:16:23 +00:00
|
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_201_CREATED
|
2023-03-18 17:47:02 +00:00
|
|
|
test_id = int(response.headers["Location"])
|
2020-07-07 01:16:23 +00:00
|
|
|
yield test_id
|
2023-04-01 13:32:10 +00:00
|
|
|
await client.delete(
|
2023-03-18 17:47:02 +00:00
|
|
|
"/api/hoods/{0}/test/{1}".format(hood_id, test_id), headers=auth_header
|
2020-10-12 20:47:06 +00:00
|
|
|
)
|