fleet-platform/tests/test_tracksolid_client.py

48 lines
1.6 KiB
Python
Raw Normal View History

"""Unit tests for Tracksolid signing — no network."""
import hashlib
from app.tracksolid.client import sign_params
def test_sign_params_docs_example() -> None:
secret = "c0aa0226fddc4365a3c67fef45427f8a"
params = {
"app_key": "8FB345B8693CCD00CE073CAB5F094009339A22A4105B6558",
"expires_in": "7200",
"format": "json",
"method": "jimi.oauth.token.get",
"sign_method": "md5",
"timestamp": "2025-05-19 10:23:00",
"user_id": "JMTEST123",
"user_pwd_md5": "21218cca77804d2ba1922c33e0151105",
"v": "1.0",
}
body = "".join(f"{k}{v}" for k, v in sorted(params.items()))
expected_raw = f"{secret}{body}{secret}"
expected_sig = hashlib.md5(expected_raw.encode()).hexdigest().upper()
sig = sign_params(params, secret)
assert sig == expected_sig
assert len(sig) == 32
assert sig == sig.upper()
def test_sign_params_alphabetical_invariance() -> None:
secret = "secret"
params = {"b": "2", "a": "1", "c": "3"}
sig1 = sign_params(params, secret)
sig2 = sign_params({"c": "3", "a": "1", "b": "2"}, secret)
assert sig1 == sig2
def test_sign_params_handles_space_in_value() -> None:
"""Values like timestamp ('2025-05-19 10:23:00') include spaces; they
should be signed verbatim, not url-encoded."""
secret = "s"
params = {"timestamp": "2025-05-19 10:23:00", "method": "x"}
body = "method" + "x" + "timestamp" + "2025-05-19 10:23:00"
expected = hashlib.md5(f"{secret}{body}{secret}".encode()).hexdigest().upper()
assert sign_params(params, secret) == expected