Files
devplacepy/tests/api/messages/delete.py
T
2026-11-05 17:59:21 +01:00

123 lines
3.9 KiB
Python

# retoor <retoor@molodetz.nl>
import time
import pytest
import requests
from tests.conftest import BASE_URL
from devplacepy.database import get_table, refresh_snapshot, set_setting
JSON = {"Accept": "application/json"}
@pytest.fixture(scope="module", autouse=True)
def _delete_settings(app_server):
for key, value in {
"rate_limit_per_minute": "1000000",
"rate_limit_window_seconds": "60",
"registration_open": "1",
"maintenance_mode": "0",
}.items():
set_setting(key, value)
yield
def _login(seeded_db, who):
session = requests.Session()
creds = seeded_db[who]
session.post(
f"{BASE_URL}/auth/login",
data={"email": creds["email"], "password": creds["password"]},
allow_redirects=True,
)
return session
def _uid(username):
refresh_snapshot()
return get_table("users").find_one(username=username)["uid"]
def _send(session, receiver_uid, text):
response = session.post(
f"{BASE_URL}/messages/send",
data={"receiver_uid": receiver_uid, "content": text},
headers=JSON,
)
assert response.status_code == 200, response.text
return response.json()["data"]["uid"]
def _thread_text(session, with_uid):
response = session.get(f"{BASE_URL}/messages", params={"with_uid": with_uid}, headers=JSON)
assert response.status_code == 200, response.text
return [m["message"]["content"] for m in response.json()["messages"]]
def test_admin_deletes_own_message(seeded_db):
alice = _login(seeded_db, "alice")
bob = _login(seeded_db, "bob")
bob_uid = _uid("bob_test")
text = f"admin own delete {int(time.time() * 1000)}"
uid = _send(alice, bob_uid, text)
assert text in _thread_text(alice, bob_uid)
response = alice.post(f"{BASE_URL}/messages/{uid}/delete", headers=JSON)
assert response.status_code == 200, response.text
assert response.json()["data"]["deleted_uid"] == uid
assert text not in _thread_text(alice, bob_uid)
assert text not in _thread_text(bob, _uid("alice_test"))
refresh_snapshot()
row = get_table("messages").find_one(uid=uid)
assert row["deleted_at"] and row["deleted_by"] == _uid("alice_test")
def test_member_cannot_delete_own_message(seeded_db):
alice = _login(seeded_db, "alice")
bob = _login(seeded_db, "bob")
alice_uid = _uid("alice_test")
text = f"member cannot delete {int(time.time() * 1000)}"
uid = _send(bob, alice_uid, text)
response = bob.post(f"{BASE_URL}/messages/{uid}/delete", headers=JSON)
assert response.status_code == 403
assert text in _thread_text(alice, _uid("bob_test"))
def test_admin_cannot_delete_other_users_message(seeded_db):
alice = _login(seeded_db, "alice")
bob = _login(seeded_db, "bob")
alice_uid = _uid("alice_test")
text = f"admin cannot delete theirs {int(time.time() * 1000)}"
uid = _send(bob, alice_uid, text)
response = alice.post(f"{BASE_URL}/messages/{uid}/delete", headers=JSON)
assert response.status_code == 403
assert text in _thread_text(bob, alice_uid)
def test_delete_unknown_message_is_404(seeded_db):
alice = _login(seeded_db, "alice")
response = alice.post(f"{BASE_URL}/messages/does-not-exist/delete", headers=JSON)
assert response.status_code == 404
def test_delete_requires_login(app_server):
response = requests.post(
f"{BASE_URL}/messages/anything/delete", headers=JSON, allow_redirects=False
)
assert response.status_code in (401, 303)
def test_deleted_message_leaves_conversation_preview(seeded_db):
alice = _login(seeded_db, "alice")
bob_uid = _uid("bob_test")
text = f"preview delete {int(time.time() * 1000)}"
uid = _send(alice, bob_uid, text)
alice.post(f"{BASE_URL}/messages/{uid}/delete", headers=JSON)
response = alice.get(f"{BASE_URL}/messages/conversations")
previews = [c["last_message"] for c in response.json()["conversations"]]
assert text not in previews