summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/tests/test_moderation.py
blob: 68e08c9fb6d8a69a882c7b297f8a59a86443a3a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""Tests for moderation — reports + blocklist."""

import pytest
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from cryptography.hazmat.primitives.asymmetric.x25519 import X25519PrivateKey
from meshbay_common.crypto import pk_to_b64


FAKE_HASH = "a" * 64   # valid blake3 hex


@pytest.fixture
async def auth_headers(client):
    sk_ed = Ed25519PrivateKey.generate()
    sk_x  = X25519PrivateKey.generate()
    await client.post("/v1/users/register", json={
        "username": "mod_admin", "email": "m@t.com", "password": "modpass99",
        "pk_user_ed25519": pk_to_b64(sk_ed.public_key()),
        "pk_user_x25519":  pk_to_b64(sk_x.public_key()),
    })
    r = await client.post("/v1/users/login",
                          json={"username": "mod_admin", "password": "modpass99"})
    return {"Authorization": f"Bearer {r.json()['access_token']}"}


@pytest.mark.asyncio
async def test_report_content_logged(client):
    r = await client.post("/v1/reports", json={
        "content_hash": FAKE_HASH, "reason": "illegal"})
    assert r.status_code == 201
    data = r.json()
    assert data["report_count"] == 1
    assert data["status"] == "logged"


@pytest.mark.asyncio
async def test_auto_block_on_threshold(client):
    """Second report triggers auto-block."""
    hash2 = "b" * 64
    await client.post("/v1/reports", json={"content_hash": hash2, "reason": "spam"})
    r = await client.post("/v1/reports", json={"content_hash": hash2, "reason": "spam"})
    assert r.json()["status"] == "auto_blocked"
    assert r.json()["report_count"] == 2


@pytest.mark.asyncio
async def test_blocklist_check(client):
    hash3 = "c" * 64
    # Not blocked yet
    r = await client.get(f"/v1/blocklist/check?hash={hash3}")
    assert r.json()["blocked"] is False

    # Report twice to auto-block
    await client.post("/v1/reports", json={"content_hash": hash3, "reason": "illegal"})
    await client.post("/v1/reports", json={"content_hash": hash3, "reason": "illegal"})

    r = await client.get(f"/v1/blocklist/check?hash={hash3}")
    assert r.json()["blocked"] is True


@pytest.mark.asyncio
async def test_admin_add_remove_blocklist(client, auth_headers):
    hash4 = "d" * 64

    r = await client.post("/v1/admin/blocklist",
                          json={"content_hash": hash4, "reason": "csam"},
                          headers=auth_headers)
    assert r.status_code == 201

    r = await client.get(f"/v1/blocklist/check?hash={hash4}")
    assert r.json()["blocked"] is True

    r = await client.delete(f"/v1/admin/blocklist/{hash4}", headers=auth_headers)
    assert r.status_code == 200

    r = await client.get(f"/v1/blocklist/check?hash={hash4}")
    assert r.json()["blocked"] is False


@pytest.mark.asyncio
async def test_invalid_hash_rejected(client):
    r = await client.post("/v1/reports", json={
        "content_hash": "not-a-valid-blake3-hash", "reason": "test"})
    assert r.status_code == 422


@pytest.mark.asyncio
async def test_full_blocklist(client, auth_headers):
    hash5 = "e" * 64
    await client.post("/v1/admin/blocklist",
                      json={"content_hash": hash5, "reason": "test"},
                      headers=auth_headers)
    r = await client.get("/v1/blocklist")
    assert r.status_code == 200
    assert hash5 in r.json()["hashes"]