Files
devplacepy/tests/api/notifications_grouping.py
T

101 lines
2.8 KiB
Python
Raw Normal View History

# retoor <retoor@molodetz.nl>
import time
from datetime import datetime, timezone
import requests
from tests.conftest import BASE_URL
from devplacepy.database import get_table, refresh_snapshot
from devplacepy.utils import generate_uid
_counter = [0]
JSON = {"Accept": "application/json"}
def _signup():
_counter[0] += 1
name = f"ngrp{int(time.time() * 1000)}{_counter[0]}"
session = requests.Session()
session.post(
f"{BASE_URL}/auth/signup",
data={
"username": name,
"email": f"{name}@t.dev",
"password": "secret123",
"confirm_password": "secret123",
"birth_date": "1990-01-01",
"accept_terms": "1",
},
allow_redirects=True,
)
return session, name
def _seed(session, name, count=5):
refresh_snapshot()
user = get_table("users").find_one(username=name)
now = datetime.now(timezone.utc).isoformat()
table = get_table("notifications")
for i in range(count):
table.insert(
{
"uid": generate_uid(),
"user_uid": user["uid"],
"type": "vote",
"message": f"actor-{i} ++'d your post",
"related_uid": user["uid"],
"target_url": "/posts/seeded",
"read": False,
"created_at": now,
}
)
refresh_snapshot()
return user
def _wait_counts(session, expected):
deadline = time.time() + 20
counts = {}
while time.time() < deadline:
counts = session.get(f"{BASE_URL}/notifications/counts").json()
if counts.get("notifications") == expected:
return counts
time.sleep(0.5)
return counts
def test_grouped_page_and_counts(app_server):
session, name = _signup()
_seed(session, name)
counts = _wait_counts(session, 1)
assert counts["notifications"] == 1, counts
page = session.get(f"{BASE_URL}/notifications", headers=JSON)
data = page.json()
assert data["grouping"] is True
events = [
entry
for group in data["notification_groups"]
for entry in group["event_entries"]
]
assert len(events) == 1
assert events[0]["count"] == 5
html = session.get(f"{BASE_URL}/notifications")
assert "&times;5" in html.text or "×5" in html.text
def test_grouping_off_restores_row_counts(app_server):
session, name = _signup()
_seed(session, name)
resp = session.post(
f"{BASE_URL}/profile/{name}/notifications/grouping",
data={"value": "false"},
headers=JSON,
)
assert resp.status_code == 200
counts = _wait_counts(session, 5)
assert counts["notifications"] == 5, counts
page = session.get(f"{BASE_URL}/notifications", headers=JSON)
data = page.json()
assert data["grouping"] is False