Files
devplacepy/tests/api/nodes/me.py
T
retoor 6f80a820da
DevPlace CI / test (push) Failing after 3m13s
Zo doen wijdat in oost
2026-10-16 07:20:44 +02:00

85 lines
4.0 KiB
Python

# retoor <retoor@molodetz.nl>
from devplacepy.database import set_consent
from devplacepy.database.nodes import record_job, save_node_settings
from tests.api.nodes.conftest import make_member
def test_guests_get_401(client):
assert client.get("/nodes/me").status_code == 401
assert client.post("/nodes/settings", json={"enabled": False}).status_code == 401
def test_me_reports_status_and_never_caches(client, member):
response = client.get("/nodes/me", headers=member["headers"])
assert response.status_code == 200
assert response.headers["cache-control"] == "no-store"
body = response.json()
assert body["consent"] is True and body["can_donate"] is True
assert body["settings"]["enabled"] is True
assert body["devices"] == [] and body["max_devices"] > 0
assert body["donated"]["lifetime_requests"] == 0
assert "reddit" in body["platforms"]
def test_me_explains_why_a_member_cannot_donate(client):
who = make_member(consent=False, enabled=False)
body = client.get("/nodes/me", headers=who["headers"]).json()
assert body["can_donate"] is False
assert body["reason"] and body["message"]
def test_settings_are_clamped_and_unsent_fields_keep_their_value(client, member):
response = client.post(
"/nodes/settings",
json={"bandwidth_mb_day": 100, "active_from": "08:00", "active_to": "22:00", "timezone": "Europe/Amsterdam"},
headers=member["headers"],
)
assert response.status_code == 200
settings = response.json()["settings"]
assert settings["bandwidth_mb_day"] == 100 and settings["active_from"] == "08:00"
assert settings["enabled"] is True
again = client.post("/nodes/settings", json={"max_concurrent": 1}, headers=member["headers"]).json()
assert again["settings"]["bandwidth_mb_day"] == 100 and again["settings"]["max_concurrent"] == 1
too_big = client.post("/nodes/settings", json={"bandwidth_mb_day": 10**9}, headers=member["headers"])
assert too_big.status_code in (400, 422)
def test_switching_on_needs_the_consent(client):
who = make_member(consent=False, enabled=False)
refused = client.post("/nodes/settings", json={"enabled": True}, headers=who["headers"])
assert refused.status_code == 403
assert refused.json()["error"]["reason"] == "no_consent"
set_consent("user", who["uid"], "bandwidth_sharing", True)
assert client.post("/nodes/settings", json={"enabled": True}, headers=who["headers"]).status_code == 200
def test_the_assistant_can_switch_off_but_never_on(client):
who = make_member(enabled=False)
headers = {**who["headers"], "X-Devii-Agent": "1"}
refused = client.post("/nodes/settings", json={"enabled": True}, headers=headers)
assert refused.status_code == 403
save_node_settings(who["uid"], {"enabled": True})
off = client.post("/nodes/settings", json={"enabled": False}, headers=headers)
assert off.status_code == 200 and off.json()["settings"]["enabled"] is False
def test_stats_are_aggregate_only(client, member):
device = client.post("/nodes/devices", json={"label": "x"}, headers=member["headers"]).json()["device"]
record_job(device["uid"], member["uid"], ok=True, bytes_up=10, bytes_down=90)
body = client.get("/nodes/stats").json()
assert set(body) == {"requests", "bytes", "donors"}
assert body["requests"] >= 1 and body["bytes"] >= 100 and body["donors"] >= 1
assert member["username"] not in str(body)
def test_leaderboard_lists_only_members_who_opted_in(client):
hidden = make_member()
shown = make_member()
for who, public in ((hidden, False), (shown, True)):
save_node_settings(who["uid"], {"public_display": public})
device = client.post("/nodes/devices", json={"label": "x"}, headers=who["headers"]).json()["device"]
record_job(device["uid"], who["uid"], ok=True, bytes_up=1, bytes_down=5)
names = [item["username"] for item in client.get("/nodes/leaderboard").json()["donors"]]
assert shown["username"] in names and hidden["username"] not in names