clickpesa-python-sdk 0.2.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/PKG-INFO +5 -3
  2. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/README.md +4 -2
  3. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/pyproject.toml +1 -1
  4. clickpesa_python_sdk-0.2.1/src/clickpesa/_version.py +1 -0
  5. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/security.py +34 -10
  6. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/webhooks.py +4 -3
  7. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa_python_sdk.egg-info/PKG-INFO +5 -3
  8. clickpesa_python_sdk-0.2.1/tests/test_webhook.py +176 -0
  9. clickpesa_python_sdk-0.2.0/src/clickpesa/_version.py +0 -1
  10. clickpesa_python_sdk-0.2.0/tests/test_webhook.py +0 -93
  11. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/LICENSE +0 -0
  12. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/setup.cfg +0 -0
  13. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/__init__.py +0 -0
  14. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/async_client.py +0 -0
  15. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/client.py +0 -0
  16. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/exceptions.py +0 -0
  17. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/py.typed +0 -0
  18. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/services/__init__.py +0 -0
  19. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/services/account.py +0 -0
  20. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/services/billpay.py +0 -0
  21. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/services/exchange.py +0 -0
  22. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/services/links.py +0 -0
  23. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/services/payments.py +0 -0
  24. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa/services/payouts.py +0 -0
  25. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa_python_sdk.egg-info/SOURCES.txt +0 -0
  26. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa_python_sdk.egg-info/dependency_links.txt +0 -0
  27. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa_python_sdk.egg-info/requires.txt +0 -0
  28. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/src/clickpesa_python_sdk.egg-info/top_level.txt +0 -0
  29. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/tests/test_async_client.py +0 -0
  30. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/tests/test_billpay.py +0 -0
  31. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/tests/test_client.py +0 -0
  32. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/tests/test_links.py +0 -0
  33. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/tests/test_payments.py +0 -0
  34. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/tests/test_payouts.py +0 -0
  35. {clickpesa_python_sdk-0.2.0 → clickpesa_python_sdk-0.2.1}/tests/test_payouts_async.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: clickpesa-python-sdk
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Production-grade Python SDK for the ClickPesa API — sync & async, collections, payouts, BillPay and more
5
5
  Author-email: Jackson Linus <jacksonlinus95@gmail.com>
6
6
  License-Expression: MIT
@@ -466,14 +466,16 @@ asyncio.run(run_payments())
466
466
  from clickpesa import WebhookValidator
467
467
 
468
468
  # In your webhook endpoint (Flask / FastAPI / Django etc.)
469
+ # ClickPesa sends the checksum inside the body (data.checksum), not in a header.
469
470
  def webhook_handler(request):
470
471
  is_valid = WebhookValidator.verify(
471
472
  payload=request.json,
472
- signature=request.headers["X-ClickPesa-Signature"],
473
473
  checksum_key="YOUR_CHECKSUM_KEY",
474
474
  )
475
475
  if not is_valid:
476
- return {"error": "Invalid signature"}, 401
476
+ # Don't trust the payload. Either answer 401, or (safer for payments)
477
+ # confirm the status with the API before acting on it.
478
+ return {"error": "Invalid checksum"}, 401
477
479
 
478
480
  # Process event ...
479
481
  ```
@@ -431,14 +431,16 @@ asyncio.run(run_payments())
431
431
  from clickpesa import WebhookValidator
432
432
 
433
433
  # In your webhook endpoint (Flask / FastAPI / Django etc.)
434
+ # ClickPesa sends the checksum inside the body (data.checksum), not in a header.
434
435
  def webhook_handler(request):
435
436
  is_valid = WebhookValidator.verify(
436
437
  payload=request.json,
437
- signature=request.headers["X-ClickPesa-Signature"],
438
438
  checksum_key="YOUR_CHECKSUM_KEY",
439
439
  )
440
440
  if not is_valid:
441
- return {"error": "Invalid signature"}, 401
441
+ # Don't trust the payload. Either answer 401, or (safer for payments)
442
+ # confirm the status with the API before acting on it.
443
+ return {"error": "Invalid checksum"}, 401
442
444
 
443
445
  # Process event ...
444
446
  ```
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "clickpesa-python-sdk"
7
- version = "0.2.0"
7
+ version = "0.2.1"
8
8
  authors = [
9
9
  { name = "Jackson Linus", email = "jacksonlinus95@gmail.com" },
10
10
  ]
@@ -0,0 +1 @@
1
+ __version__ = "0.2.1"
@@ -56,26 +56,50 @@ class SecurityManager:
56
56
  ).hexdigest()
57
57
 
58
58
  @staticmethod
59
- def verify_webhook(checksum_key: str, payload: dict, signature: str) -> bool:
59
+ def webhook_checksum(checksum_key: str, payload: dict) -> str:
60
60
  """
61
- Verify an incoming ClickPesa webhook signature.
61
+ The checksum ClickPesa puts in a webhook body.
62
62
 
63
- Uses ``hmac.compare_digest`` for constant-time comparison to prevent
64
- timing-based side-channel attacks.
63
+ ClickPesa webhooks look like ``{"event": "...", "data": {..., "checksum":
64
+ "...", "checksumMethod": "canonical"}}``. The checksum is
65
+ HMAC-SHA256(checksum_key, canonical compact JSON of ``data`` WITHOUT the
66
+ ``checksum`` and ``checksumMethod`` fields). The ``event`` envelope is
67
+ not signed.
68
+
69
+ Returns ``""`` when there is no key or no ``data`` object.
70
+ """
71
+ data = payload.get("data") if isinstance(payload, dict) else None
72
+ if not checksum_key or not isinstance(data, dict):
73
+ return ""
74
+ signed = {k: v for k, v in data.items() if k not in ("checksum", "checksumMethod")}
75
+ return SecurityManager.create_checksum(checksum_key, signed)
76
+
77
+ @staticmethod
78
+ def verify_webhook(checksum_key: str, payload: dict, signature: str | None = None) -> bool:
79
+ """
80
+ Verify an incoming ClickPesa webhook.
81
+
82
+ ClickPesa sends no signature header: the checksum is ``data.checksum``
83
+ in the body. ``signature`` is optional and, when given, is compared
84
+ instead of ``data.checksum`` (kept for callers that pass it).
85
+
86
+ Uses ``hmac.compare_digest`` for constant-time comparison.
65
87
 
66
88
  Args:
67
89
  checksum_key: Your application's checksum secret key.
68
90
  payload: The parsed webhook body dict.
69
- signature: The ``X-ClickPesa-Signature`` header value.
91
+ signature: Optional; defaults to ``payload["data"]["checksum"]``.
70
92
 
71
93
  Returns:
72
- ``True`` if the signature is valid, ``False`` otherwise.
94
+ ``True`` if the checksum is valid, ``False`` otherwise.
73
95
  """
74
- if not signature:
96
+ if signature is None:
97
+ data = payload.get("data") if isinstance(payload, dict) else None
98
+ signature = data.get("checksum") if isinstance(data, dict) else None
99
+ if not signature or not isinstance(signature, str):
75
100
  return False
76
-
77
- computed = SecurityManager.create_checksum(checksum_key, payload)
78
- return hmac.compare_digest(computed, signature)
101
+ computed = SecurityManager.webhook_checksum(checksum_key, payload)
102
+ return bool(computed) and hmac.compare_digest(computed, signature)
79
103
 
80
104
 
81
105
  __all__ = ["SecurityManager"]
@@ -5,9 +5,9 @@ Usage::
5
5
 
6
6
  from clickpesa import WebhookValidator
7
7
 
8
+ # The checksum travels inside the body (data.checksum) - no header.
8
9
  is_valid = WebhookValidator.verify(
9
10
  payload=request.json(),
10
- signature=request.headers["X-ClickPesa-Signature"],
11
11
  checksum_key="your-checksum-secret",
12
12
  )
13
13
  """
@@ -21,7 +21,7 @@ class WebhookValidator:
21
21
  """Static helper for validating ClickPesa webhook payloads."""
22
22
 
23
23
  @staticmethod
24
- def verify(payload: dict, signature: str, checksum_key: str) -> bool:
24
+ def verify(payload: dict, signature: str | None = None, checksum_key: str = "") -> bool:
25
25
  """
26
26
  Verify that an incoming webhook was genuinely sent by ClickPesa.
27
27
 
@@ -30,7 +30,8 @@ class WebhookValidator:
30
30
 
31
31
  Args:
32
32
  payload: Parsed JSON body of the webhook request.
33
- signature: Value of the ``X-ClickPesa-Signature`` header.
33
+ signature: Optional. ClickPesa sends the checksum in the body
34
+ (``data.checksum``); that is used when this is None.
34
35
  checksum_key: Your application's checksum secret key.
35
36
 
36
37
  Returns:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: clickpesa-python-sdk
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: Production-grade Python SDK for the ClickPesa API — sync & async, collections, payouts, BillPay and more
5
5
  Author-email: Jackson Linus <jacksonlinus95@gmail.com>
6
6
  License-Expression: MIT
@@ -466,14 +466,16 @@ asyncio.run(run_payments())
466
466
  from clickpesa import WebhookValidator
467
467
 
468
468
  # In your webhook endpoint (Flask / FastAPI / Django etc.)
469
+ # ClickPesa sends the checksum inside the body (data.checksum), not in a header.
469
470
  def webhook_handler(request):
470
471
  is_valid = WebhookValidator.verify(
471
472
  payload=request.json,
472
- signature=request.headers["X-ClickPesa-Signature"],
473
473
  checksum_key="YOUR_CHECKSUM_KEY",
474
474
  )
475
475
  if not is_valid:
476
- return {"error": "Invalid signature"}, 401
476
+ # Don't trust the payload. Either answer 401, or (safer for payments)
477
+ # confirm the status with the API before acting on it.
478
+ return {"error": "Invalid checksum"}, 401
477
479
 
478
480
  # Process event ...
479
481
  ```
@@ -0,0 +1,176 @@
1
+ """
2
+ Tests for WebhookValidator and the underlying SecurityManager checksum logic.
3
+ """
4
+
5
+ import pytest
6
+
7
+ from clickpesa import WebhookValidator
8
+ from clickpesa.security import SecurityManager
9
+
10
+ CHECKSUM_KEY = "test_webhook_secret"
11
+ PAYLOAD = {"amount": "5000", "orderReference": "ORD001", "status": "SUCCESS"}
12
+
13
+
14
+ def _sig(payload=PAYLOAD, key=CHECKSUM_KEY):
15
+ return SecurityManager.create_checksum(key, payload)
16
+
17
+
18
+ # ---------------------------------------------------------------------------
19
+ # WebhookValidator.verify — real ClickPesa webhook shape
20
+ #
21
+ # ClickPesa sends {"event": ..., "data": {..., "checksum", "checksumMethod":
22
+ # "canonical"}}: the checksum is HMAC-SHA256(key, compact JSON of `data`,
23
+ # keys sorted at every level, without checksum/checksumMethod). The expected
24
+ # value is computed here with plain hmac/json, NOT with the SDK, so these
25
+ # tests can't just agree with the code under test. (Checked against 2,645
26
+ # production webhooks, 2026-11-22.)
27
+ # ---------------------------------------------------------------------------
28
+
29
+ import copy
30
+ import hashlib
31
+ import hmac
32
+ import json
33
+
34
+
35
+ def _sorted(obj):
36
+ if isinstance(obj, dict):
37
+ return {k: _sorted(obj[k]) for k in sorted(obj)}
38
+ if isinstance(obj, list):
39
+ return [_sorted(v) for v in obj]
40
+ return obj
41
+
42
+
43
+ def _clickpesa_checksum(data, key=CHECKSUM_KEY):
44
+ body = {k: v for k, v in data.items() if k not in ("checksum", "checksumMethod")}
45
+ msg = json.dumps(_sorted(body), separators=(",", ":"))
46
+ return hmac.new(key.encode(), msg.encode(), hashlib.sha256).hexdigest()
47
+
48
+
49
+ def _webhook(event="PAYMENT RECEIVED", key=CHECKSUM_KEY, **overrides):
50
+ data = {
51
+ "id": "LCPCA6H146T6TJ",
52
+ "status": "SUCCESS",
53
+ "paymentReference": "PAY205007094",
54
+ "paymentPhoneNumber": "255700000000",
55
+ "orderReference": "QXVT1NBMJ2KY",
56
+ "collectedAmount": "500",
57
+ "collectedCurrency": "TZS",
58
+ "message": "Payment received",
59
+ "channel": "M-PESA",
60
+ "updatedAt": "2026-11-22T10:00:01.000Z",
61
+ "createdAt": "2026-11-22T09:59:40.000Z",
62
+ "customer": {"customerName": "JOHN DOE", "customerPhoneNumber": "255700000000"},
63
+ "clientId": "IDtestclient",
64
+ **overrides,
65
+ }
66
+ data["checksum"] = _clickpesa_checksum(data, key)
67
+ data["checksumMethod"] = "canonical"
68
+ return {"event": event, "data": data}
69
+
70
+
71
+ def test_verify_real_payment_webhook_from_body():
72
+ """The checksum is read from data.checksum — no header needed."""
73
+ assert WebhookValidator.verify(_webhook(), checksum_key=CHECKSUM_KEY) is True
74
+
75
+
76
+ def test_verify_real_payout_webhook():
77
+ payout = _webhook(event="PAYOUT INITIATED", amount="928", currency="TZS", fee="72",
78
+ beneficiary={"accountNumber": "255700000000", "accountName": "JOHN DOE"})
79
+ assert WebhookValidator.verify(payout, checksum_key=CHECKSUM_KEY) is True
80
+
81
+
82
+ def test_verify_positional_call_still_works():
83
+ """verify(payload, signature, key) callers: passing data.checksum explicitly."""
84
+ w = _webhook()
85
+ assert WebhookValidator.verify(w, w["data"]["checksum"], CHECKSUM_KEY) is True
86
+
87
+
88
+ def test_verify_key_order_does_not_matter():
89
+ w = _webhook()
90
+ w["data"] = dict(reversed(list(w["data"].items())))
91
+ assert WebhookValidator.verify(w, checksum_key=CHECKSUM_KEY) is True
92
+
93
+
94
+ def test_event_envelope_is_not_signed():
95
+ """ClickPesa signs only `data`; the event name is outside the checksum."""
96
+ w = _webhook()
97
+ w["event"] = "PAYMENT FAILED"
98
+ assert WebhookValidator.verify(w, checksum_key=CHECKSUM_KEY) is True
99
+
100
+
101
+ @pytest.mark.parametrize("field,value", [
102
+ ("status", "FAILED"),
103
+ ("collectedAmount", "50000"),
104
+ ("orderReference", "SOMEONEELSE1"),
105
+ ])
106
+ def test_verify_tampered_field_fails(field, value):
107
+ w = _webhook()
108
+ w["data"][field] = value
109
+ assert WebhookValidator.verify(w, checksum_key=CHECKSUM_KEY) is False
110
+
111
+
112
+ def test_verify_tampered_nested_field_fails():
113
+ w = _webhook()
114
+ w["data"]["customer"]["customerPhoneNumber"] = "255711111111"
115
+ assert WebhookValidator.verify(w, checksum_key=CHECKSUM_KEY) is False
116
+
117
+
118
+ def test_verify_wrong_key_fails():
119
+ assert WebhookValidator.verify(_webhook(key="another_key"), checksum_key=CHECKSUM_KEY) is False
120
+
121
+
122
+ def test_verify_missing_checksum_fails():
123
+ w = _webhook()
124
+ del w["data"]["checksum"]
125
+ assert WebhookValidator.verify(w, checksum_key=CHECKSUM_KEY) is False
126
+
127
+
128
+ def test_verify_made_up_checksum_fails():
129
+ w = _webhook()
130
+ w["data"]["checksum"] = "a" * 64
131
+ assert WebhookValidator.verify(w, checksum_key=CHECKSUM_KEY) is False
132
+
133
+
134
+ def test_verify_no_key_fails():
135
+ assert WebhookValidator.verify(_webhook(), checksum_key="") is False
136
+
137
+
138
+ @pytest.mark.parametrize("payload", [{}, {"event": "X"}, {"data": "not-a-dict"}, []])
139
+ def test_verify_malformed_payload_fails(payload):
140
+ assert WebhookValidator.verify(payload, checksum_key=CHECKSUM_KEY) is False
141
+
142
+
143
+ def test_old_full_body_signature_no_longer_accepted():
144
+ """<= 0.2.0 hashed the whole body (incl. checksum) — never what ClickPesa sends."""
145
+ w = _webhook()
146
+ full = SecurityManager.create_checksum(CHECKSUM_KEY, w)
147
+ assert WebhookValidator.verify(w, full, CHECKSUM_KEY) is False
148
+
149
+
150
+ def test_webhook_checksum_matches_independent_computation():
151
+ w = _webhook()
152
+ assert SecurityManager.webhook_checksum(CHECKSUM_KEY, w) == _clickpesa_checksum(copy.deepcopy(w["data"]))
153
+
154
+
155
+ # ---------------------------------------------------------------------------
156
+ # SecurityManager.create_checksum
157
+ # ---------------------------------------------------------------------------
158
+
159
+ def test_checksum_is_64_char_hex():
160
+ """HMAC-SHA256 hex digest is always 64 characters."""
161
+ sig = SecurityManager.create_checksum(CHECKSUM_KEY, PAYLOAD)
162
+ assert len(sig) == 64
163
+ assert all(c in "0123456789abcdef" for c in sig)
164
+
165
+
166
+ def test_checksum_empty_key_returns_empty_string():
167
+ """No checksum_key → empty string (checksum injection disabled)."""
168
+ assert SecurityManager.create_checksum("", PAYLOAD) == ""
169
+ assert SecurityManager.create_checksum(None, PAYLOAD) == ""
170
+
171
+
172
+ def test_checksum_sorted_key_order():
173
+ """Key order in the payload dict does not affect the checksum."""
174
+ a = SecurityManager.create_checksum(CHECKSUM_KEY, {"z": "1", "a": "2"})
175
+ b = SecurityManager.create_checksum(CHECKSUM_KEY, {"a": "2", "z": "1"})
176
+ assert a == b
@@ -1 +0,0 @@
1
- __version__ = "0.2.0"
@@ -1,93 +0,0 @@
1
- """
2
- Tests for WebhookValidator and the underlying SecurityManager checksum logic.
3
- """
4
-
5
- import pytest
6
-
7
- from clickpesa import WebhookValidator
8
- from clickpesa.security import SecurityManager
9
-
10
- CHECKSUM_KEY = "test_webhook_secret"
11
- PAYLOAD = {"amount": "5000", "orderReference": "ORD001", "status": "SUCCESS"}
12
-
13
-
14
- def _sig(payload=PAYLOAD, key=CHECKSUM_KEY):
15
- return SecurityManager.create_checksum(key, payload)
16
-
17
-
18
- # ---------------------------------------------------------------------------
19
- # WebhookValidator.verify
20
- # ---------------------------------------------------------------------------
21
-
22
- def test_verify_valid_signature():
23
- """A signature computed from the same payload and key verifies correctly."""
24
- sig = _sig()
25
- assert WebhookValidator.verify(PAYLOAD, sig, CHECKSUM_KEY) is True
26
-
27
-
28
- def test_verify_wrong_signature():
29
- """A signature that does not match the payload returns False."""
30
- assert WebhookValidator.verify(PAYLOAD, "deadbeef" * 8, CHECKSUM_KEY) is False
31
-
32
-
33
- def test_verify_tampered_payload():
34
- """Changing a field after signing invalidates the signature."""
35
- sig = _sig()
36
- tampered = {**PAYLOAD, "amount": "99999"}
37
- assert WebhookValidator.verify(tampered, sig, CHECKSUM_KEY) is False
38
-
39
-
40
- def test_verify_wrong_key():
41
- """A signature produced with a different key does not verify."""
42
- sig = _sig(key="other_secret")
43
- assert WebhookValidator.verify(PAYLOAD, sig, CHECKSUM_KEY) is False
44
-
45
-
46
- def test_verify_empty_signature_returns_false():
47
- """An empty signature string is rejected immediately."""
48
- assert WebhookValidator.verify(PAYLOAD, "", CHECKSUM_KEY) is False
49
-
50
-
51
- def test_verify_real_webhook_structure():
52
- """Signature covers the full payload including nested data — mirrors ClickPesa's
53
- actual webhook shape: {"event": "...", "data": {...nested...}}."""
54
- webhook = {
55
- "event": "PAYMENT RECEIVED",
56
- "data": {
57
- "id": "ORD123LCP456",
58
- "status": "SUCCESS",
59
- "orderReference": "ORD123",
60
- "collectedAmount": "10000",
61
- "collectedCurrency": "TZS",
62
- "customer": {
63
- "customerName": "John Doe",
64
- "customerPhoneNumber": "255700000000",
65
- },
66
- },
67
- }
68
- sig = SecurityManager.create_checksum(CHECKSUM_KEY, webhook)
69
- assert WebhookValidator.verify(webhook, sig, CHECKSUM_KEY) is True
70
-
71
-
72
- # ---------------------------------------------------------------------------
73
- # SecurityManager.create_checksum
74
- # ---------------------------------------------------------------------------
75
-
76
- def test_checksum_is_64_char_hex():
77
- """HMAC-SHA256 hex digest is always 64 characters."""
78
- sig = SecurityManager.create_checksum(CHECKSUM_KEY, PAYLOAD)
79
- assert len(sig) == 64
80
- assert all(c in "0123456789abcdef" for c in sig)
81
-
82
-
83
- def test_checksum_empty_key_returns_empty_string():
84
- """No checksum_key → empty string (checksum injection disabled)."""
85
- assert SecurityManager.create_checksum("", PAYLOAD) == ""
86
- assert SecurityManager.create_checksum(None, PAYLOAD) == ""
87
-
88
-
89
- def test_checksum_sorted_key_order():
90
- """Key order in the payload dict does not affect the checksum."""
91
- a = SecurityManager.create_checksum(CHECKSUM_KEY, {"z": "1", "a": "2"})
92
- b = SecurityManager.create_checksum(CHECKSUM_KEY, {"a": "2", "z": "1"})
93
- assert a == b