99 lines
2.5 KiB
Python
99 lines
2.5 KiB
Python
# retoor <retoor@molodetz.nl>
|
|
|
|
import time
|
|
|
|
import pytest
|
|
from fastapi.testclient import TestClient
|
|
|
|
from devplacepy.database import get_setting, get_table, set_setting
|
|
from devplacepy.main import app
|
|
from devplacepy.utils import register_account
|
|
|
|
_counter = [0]
|
|
|
|
|
|
def unique(prefix: str) -> str:
|
|
_counter[0] += 1
|
|
return f"{prefix}{int(time.time() * 1000)}{_counter[0]}"
|
|
|
|
|
|
def make_account(role: str = "Member") -> dict:
|
|
name = unique("syncmb")
|
|
uid, _, _ = register_account(
|
|
name, f"{name}@t.dev", "secret123", age_band="adult", accepted_terms=True
|
|
)
|
|
if role != "Member":
|
|
get_table("users").update({"uid": uid, "role": role}, ["uid"])
|
|
row = get_table("users").find_one(uid=uid)
|
|
return {
|
|
"uid": uid,
|
|
"username": name,
|
|
"headers": {"X-API-KEY": row["api_key"], "Accept": "application/json"},
|
|
}
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
def client():
|
|
previous = {key: get_setting(key, "") for key in ("sync_enabled", "sync_origins")}
|
|
set_setting("sync_enabled", "1")
|
|
set_setting("sync_origins", "")
|
|
with TestClient(app) as test_client:
|
|
yield test_client
|
|
for key, value in previous.items():
|
|
set_setting(key, value)
|
|
|
|
|
|
@pytest.fixture
|
|
def admin(client):
|
|
return make_account("Admin")
|
|
|
|
|
|
@pytest.fixture
|
|
def origin():
|
|
return f"{unique('o')}.example.org"
|
|
|
|
|
|
def sync(client, admin, kind, origin, records, **extra):
|
|
response = client.post(
|
|
f"/sync/{kind}",
|
|
json={"origin": origin, "records": records, **extra},
|
|
headers=admin["headers"],
|
|
)
|
|
return response
|
|
|
|
|
|
def sync_ok(client, admin, kind, origin, records, **extra) -> dict:
|
|
response = sync(client, admin, kind, origin, records, **extra)
|
|
assert response.status_code == 200, response.text
|
|
return response.json()
|
|
|
|
|
|
def user_record(uid="u1", username="syncuser", **extra) -> dict:
|
|
return {
|
|
"origin_uid": uid,
|
|
"username": username,
|
|
"created_at": "2025-01-01T00:00:00Z",
|
|
**extra,
|
|
}
|
|
|
|
|
|
def post_record(uid="p1", author="u1", **extra) -> dict:
|
|
return {
|
|
"origin_uid": uid,
|
|
"author_uid": author,
|
|
"content": "a synced post with enough content",
|
|
"created_at": "2025-01-02T00:00:00Z",
|
|
**extra,
|
|
}
|
|
|
|
|
|
def comment_record(uid="c1", post="p1", author="u1", **extra) -> dict:
|
|
return {
|
|
"origin_uid": uid,
|
|
"post_uid": post,
|
|
"author_uid": author,
|
|
"content": "a synced comment",
|
|
"created_at": "2025-01-03T00:00:00Z",
|
|
**extra,
|
|
}
|