Skip to content

Commit bab8e1d

Browse files
Andrew-Dickinsondanihodovic
authored andcommitted
Formatting fixes
1 parent 0012bec commit bab8e1d

File tree

4 files changed

+27
-35
lines changed

4 files changed

+27
-35
lines changed

django_webhook/http.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ def prepare_request(webhook: Webhook, payload: dict):
1616
now = timezone.now()
1717
timestamp = int(datetime.timestamp(now))
1818

19-
encoder_cls = cast(JSONEncoder, settings.DJANGO_WEBHOOK["PAYLOAD_ENCODER_CLASS"])
19+
encoder_cls = cast(
20+
type[JSONEncoder], settings.DJANGO_WEBHOOK["PAYLOAD_ENCODER_CLASS"]
21+
)
2022
signatures = [
2123
sign_payload(payload, secret, timestamp, encoder_cls)
2224
for secret in webhook.secrets.values_list("token", flat=True)
@@ -36,8 +38,10 @@ def prepare_request(webhook: Webhook, payload: dict):
3638
return r.prepare()
3739

3840

39-
def sign_payload(payload: dict, secret: str, timestamp: int, encoder_cls: JSONEncoder):
40-
combined_payload = f"{timestamp}:{json.dumps(payload, cls=encoder_cls)}" # type: ignore
41+
def sign_payload(
42+
payload: dict, secret: str, timestamp: int, encoder_cls: type[JSONEncoder]
43+
):
44+
combined_payload = f"{timestamp}:{json.dumps(payload, cls=encoder_cls)}"
4145
return hmac.new(
4246
key=secret.encode(), msg=combined_payload.encode(), digestmod=hashlib.sha256
4347
).hexdigest()

tests/model_data.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from datetime import date, datetime
2+
3+
TEST_JOIN_DATE = date(1970, 1, 1)
4+
TEST_LAST_ACTIVE = datetime(2000, 1, 1, 12, 0, 0)
5+
TEST_USER = {
6+
"id": 1,
7+
"name": "Dani",
8+
"email": "dani@doo.com",
9+
"join_date": "1970-01-01",
10+
"last_active": "2000-01-01T12:00:00",
11+
}

tests/test_signals.py

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import hashlib
22
import hmac
33
import json
4-
from datetime import date, datetime, timedelta
4+
from datetime import datetime, timedelta
55

66
import pytest
77
from django.test import override_settings
@@ -14,13 +14,11 @@
1414
WebhookSecretFactory,
1515
WebhookTopicFactory,
1616
)
17+
from tests.model_data import TEST_JOIN_DATE, TEST_LAST_ACTIVE, TEST_USER
1718
from tests.models import Country, User
1819

1920
pytestmark = pytest.mark.django_db
2021

21-
TEST_JOIN_DATE = date(1970, 1, 1)
22-
TEST_LAST_ACTIVE = datetime(2000, 1, 1, 12, 0, 0)
23-
2422

2523
@freeze_time("2012-01-14 03:21:34")
2624
def test_create(responses):
@@ -44,13 +42,7 @@ def test_create(responses):
4442
assert req.headers["Django-Webhook-UUID"] == str(webhook.uuid)
4543
assert json.loads(req.body) == {
4644
"topic": "tests.User/create",
47-
"object": {
48-
"id": 1,
49-
"name": "Dani",
50-
"email": "dani@doo.com",
51-
"join_date": "1970-01-01",
52-
"last_active": "2000-01-01T12:00:00",
53-
},
45+
"object": TEST_USER,
5446
"object_type": "tests.User",
5547
"webhook_uuid": "54c10b6e-42e7-4edc-a047-a53c7ff80c77",
5648
}
@@ -80,15 +72,11 @@ def test_update(responses):
8072
user.save()
8173
assert len(responses.calls) == 1
8274
req = responses.calls[0].request
75+
expected_object = TEST_USER.copy()
76+
expected_object["name"] = "Adin"
8377
assert json.loads(req.body) == {
8478
"topic": "tests.User/update",
85-
"object": {
86-
"id": 1,
87-
"name": "Adin",
88-
"email": "dani@doo.com",
89-
"join_date": "1970-01-01",
90-
"last_active": "2000-01-01T12:00:00",
91-
},
79+
"object": expected_object,
9280
"object_type": "tests.User",
9381
"webhook_uuid": str(webhook.uuid),
9482
}
@@ -111,13 +99,7 @@ def test_delete(responses):
11199
req = responses.calls[0].request
112100
assert json.loads(req.body) == {
113101
"topic": "tests.User/delete",
114-
"object": {
115-
"id": 1,
116-
"name": "Dani",
117-
"email": "dani@doo.com",
118-
"join_date": "1970-01-01",
119-
"last_active": "2000-01-01T12:00:00",
120-
},
102+
"object": TEST_USER,
121103
"object_type": "tests.User",
122104
"webhook_uuid": str(webhook.uuid),
123105
}

tests/test_stores_events.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
WebhookFactory,
1313
WebhookTopicFactory,
1414
)
15+
from tests.model_data import TEST_USER
1516
from tests.models import User
1617

1718
pytestmark = pytest.mark.django_db
@@ -42,13 +43,7 @@ def test_creates_events_when_enabled(responses):
4243
assert event.webhook == webhook
4344
assert event.object == {
4445
"topic": "tests.User/create",
45-
"object": {
46-
"id": 1,
47-
"name": "Dani",
48-
"email": "dani@doo.com",
49-
"join_date": "1970-01-01",
50-
"last_active": "2000-01-01T12:00:00",
51-
},
46+
"object": TEST_USER,
5247
"object_type": "tests.User",
5348
"webhook_uuid": str(webhook.uuid),
5449
}

0 commit comments

Comments
 (0)