- /2.0/ dispatcher backed by a method registry that declares each method's params and signature/session requirements - api_sig verification per the Last.fm auth spec - XML (default) and Last.fm-style JSON rendering, including error shapes and HTTP statuses per error code - Last.fm request, error and auth-failure metrics with bounded labels Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
import hashlib
|
|
|
|
from scrobbler.lastfm.signature import is_valid, sign
|
|
|
|
|
|
def test_signature_sorts_params_and_appends_secret():
|
|
params = {"token": "tok", "method": "auth.getSession", "api_key": "key"}
|
|
expected = hashlib.md5(b"api_keykeymethodauth.getSessiontokentokSECRET").hexdigest()
|
|
assert sign(params, "SECRET") == expected
|
|
|
|
|
|
def test_format_callback_and_api_sig_are_not_signed():
|
|
base = {"method": "track.scrobble", "api_key": "key"}
|
|
extra = {**base, "format": "json", "callback": "cb", "api_sig": "whatever"}
|
|
assert sign(base, "s") == sign(extra, "s")
|
|
|
|
|
|
def test_signature_uses_utf8():
|
|
params = {"artist": "Sigur Rós", "api_key": "k"}
|
|
expected = hashlib.md5("api_keykartistSigur RósS".encode()).hexdigest()
|
|
assert sign(params, "S") == expected
|
|
|
|
|
|
def test_is_valid_accepts_uppercase_hex_and_rejects_missing():
|
|
params = {"api_key": "k", "method": "m"}
|
|
sig = sign(params, "s")
|
|
assert is_valid({**params, "api_sig": sig.upper()}, "s")
|
|
assert not is_valid(params, "s")
|
|
assert not is_valid({**params, "api_sig": sig}, "other-secret")
|