-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_webhooks.py
More file actions
50 lines (38 loc) · 1.23 KB
/
test_webhooks.py
File metadata and controls
50 lines (38 loc) · 1.23 KB
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
import json
from flagsmith.webhooks import generate_signature, verify_signature
def test_generate_signature() -> None:
# Given
request_body = json.dumps({"data": {"foo": 123}})
shared_secret = "shh"
# When
signature = generate_signature(request_body, shared_secret)
# Then
assert isinstance(signature, str)
assert len(signature) == 64 # SHA-256 hex digest is 64 characters
def test_verify_signature_valid() -> None:
# Given
request_body = json.dumps({"data": {"foo": 123}})
shared_secret = "shh"
# When
signature = generate_signature(request_body, shared_secret)
# Then
assert verify_signature(
request_body=request_body,
received_signature=signature,
shared_secret=shared_secret,
)
# Test with bytes instead of str
assert verify_signature(
request_body=request_body.encode(),
received_signature=signature,
shared_secret=shared_secret,
)
def test_verify_signature_invalid() -> None:
# Given
request_body = json.dumps({"event": "flag_updated", "data": {"id": 123}})
# Then
assert not verify_signature(
request_body=request_body,
received_signature="bad",
shared_secret="?",
)