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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
"""
The operator's MusicBrainz User-Agent contact string — docs/musicbay.md
§3.2/§6. Same shape as test_tmdb_config_policy.py: a signed operator
instruction, node-wide (group_id="") rather than per-group, stored via
roster.py's group_settings table.
Unlike TMDB's token, a contact string is not a secret — MusicBrainz's usage
policy expects it to be visible to the service it's sent to — but the
subject signed/audited still only ever says whether one was configured
(never the address itself), the same "yes/no" shape as tmdb_config's
subject, to keep a personal contact out of the audit log as free text.
"""
from pathlib import Path
import pytest
from meshbay_common.adminop import OP_MUSICBRAINZ_CONFIG
from meshbay_node.indexer.group_index import GroupIndex
from meshbay_node.roster import Roster
from meshbay_node.transport.webrtc_server import WebRTCPeerSession
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from conftest import one_root
pytestmark = pytest.mark.asyncio
def _session(tmp_path: Path, user_id: str, *, operator: str | None = None) -> WebRTCPeerSession:
shared_root = tmp_path / "shared"
shared_root.mkdir(exist_ok=True)
index = GroupIndex(group_id="g" * 32, sk_node=Ed25519PrivateKey.generate())
ctx = {
"roots": one_root(shared_root),
"index": index,
"sk_node": index.sk_node,
"node_user_id": operator,
}
session = WebRTCPeerSession.__new__(WebRTCPeerSession)
session._ctx = ctx
session._group_id = None
session._user_id = user_id
session._pk_user = ""
session.sent = []
session._send = session.sent.append
session._audit = lambda *a, **k: None
return session
def _fake_challenge(issued: list):
return lambda op, subject, payload=None, group_id=None: issued.append(
(op, subject, payload, group_id))
# ── Refused before a challenge is even issued ───────────────────────────────
async def test_non_string_contact_is_refused(tmp_path):
session = _session(tmp_path, "op", operator="op")
session._has_admin_authority = lambda: True
issued = []
session._issue_admin_challenge = _fake_challenge(issued)
session._do_musicbrainz_config({"contact": 12345})
assert not issued
assert [m for m in session.sent if m.get("type") == "error"]
async def test_a_request_with_nobody_to_authorize_it_is_refused(tmp_path):
session = _session(tmp_path, "member-1", operator="the-operator")
session._has_admin_authority = lambda: False
session._do_musicbrainz_config({"contact": "https://example.invalid/contact"})
assert [m for m in session.sent if m.get("type") == "error"]
# ── Who may change it, and what gets signed ─────────────────────────────────
async def test_changing_it_needs_a_signature(tmp_path):
session = _session(tmp_path, "op", operator="op")
session._has_admin_authority = lambda: True
issued = []
session._issue_admin_challenge = _fake_challenge(issued)
session._do_musicbrainz_config({})
assert len(issued) == 1
op, subject, payload, group_id = issued[0]
assert op == OP_MUSICBRAINZ_CONFIG
assert group_id == "", "node-wide, like tmdb_config — not tied to self._group_id"
async def test_the_contact_itself_never_appears_in_the_signed_subject(tmp_path):
"""
Not a secret the way a TMDB token is, but still kept out of the audited
subject line as free text — same "yes/no configured" shape.
"""
session = _session(tmp_path, "op", operator="op")
session._has_admin_authority = lambda: True
issued = []
session._issue_admin_challenge = _fake_challenge(issued)
contact = "operator@example.invalid"
session._do_musicbrainz_config({"contact": contact})
_, subject, payload, _ = issued[0]
assert contact not in subject
assert payload["contact"] == contact, "the real value still has to reach the exec step somehow"
async def test_subject_reflects_whether_a_contact_was_supplied(tmp_path):
session = _session(tmp_path, "op", operator="op")
session._has_admin_authority = lambda: True
issued = []
session._issue_admin_challenge = _fake_challenge(issued)
session._do_musicbrainz_config({"contact": "x"})
_, subject, _, _ = issued[0]
assert subject == "contact_configured=yes"
async def test_subject_says_no_contact_when_none_given(tmp_path):
session = _session(tmp_path, "op", operator="op")
session._has_admin_authority = lambda: True
issued = []
session._issue_admin_challenge = _fake_challenge(issued)
session._do_musicbrainz_config({})
_, subject, _, _ = issued[0]
assert subject == "contact_configured=no"
# ── Where it is stored ──────────────────────────────────────────────────────
async def test_the_setting_lives_on_the_node_and_survives_a_restart(tmp_path):
roster = Roster(db_path=tmp_path / "roster.db")
await roster.open()
try:
assert await roster.musicbrainz_contact() is None, \
"absent must mean 'no contact configured' — no shipped default to fall back to"
await roster.set_musicbrainz_contact("operator@example.invalid", set_by="op")
assert await roster.musicbrainz_contact() == "operator@example.invalid"
finally:
await roster.close()
reopened = Roster(db_path=tmp_path / "roster.db")
await reopened.open()
try:
assert await reopened.musicbrainz_contact() == "operator@example.invalid"
finally:
await reopened.close()
async def test_clearing_the_contact_reverts_to_unconfigured(tmp_path):
roster = Roster(db_path=tmp_path / "roster.db")
await roster.open()
try:
await roster.set_musicbrainz_contact("a-contact", set_by="op")
assert await roster.musicbrainz_contact() == "a-contact"
await roster.set_musicbrainz_contact("", set_by="op")
assert await roster.musicbrainz_contact() is None, \
"an explicit empty string clears the contact"
finally:
await roster.close()
async def test_omitting_the_contact_leaves_it_unchanged(tmp_path):
roster = Roster(db_path=tmp_path / "roster.db")
await roster.open()
try:
await roster.set_musicbrainz_contact("a-contact", set_by="op")
await roster.set_musicbrainz_contact(None, set_by="op")
assert await roster.musicbrainz_contact() == "a-contact"
finally:
await roster.close()
|