django-webhook-subscriber 0.4.0__py3-none-any.whl → 2.0.0__py3-none-any.whl
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.
- django_webhook_subscriber/__init__.py +7 -1
- django_webhook_subscriber/admin.py +831 -182
- django_webhook_subscriber/apps.py +3 -20
- django_webhook_subscriber/conf.py +11 -24
- django_webhook_subscriber/delivery.py +414 -159
- django_webhook_subscriber/http.py +51 -0
- django_webhook_subscriber/management/commands/webhook.py +169 -0
- django_webhook_subscriber/management/commands/webhook_cache.py +173 -0
- django_webhook_subscriber/management/commands/webhook_logs.py +226 -0
- django_webhook_subscriber/management/commands/webhook_performance_test.py +469 -0
- django_webhook_subscriber/management/commands/webhook_send.py +96 -0
- django_webhook_subscriber/management/commands/webhook_status.py +139 -0
- django_webhook_subscriber/managers.py +36 -14
- django_webhook_subscriber/migrations/0002_remove_webhookregistry_content_type_and_more.py +192 -0
- django_webhook_subscriber/models.py +291 -114
- django_webhook_subscriber/serializers.py +16 -50
- django_webhook_subscriber/tasks.py +434 -56
- django_webhook_subscriber/tests/factories.py +40 -0
- django_webhook_subscriber/tests/settings.py +27 -8
- django_webhook_subscriber/tests/test_delivery.py +453 -190
- django_webhook_subscriber/tests/test_http.py +32 -0
- django_webhook_subscriber/tests/test_managers.py +26 -37
- django_webhook_subscriber/tests/test_models.py +341 -251
- django_webhook_subscriber/tests/test_serializers.py +22 -56
- django_webhook_subscriber/tests/test_tasks.py +477 -189
- django_webhook_subscriber/tests/test_utils.py +98 -94
- django_webhook_subscriber/utils.py +87 -69
- django_webhook_subscriber/validators.py +53 -0
- django_webhook_subscriber-2.0.0.dist-info/METADATA +774 -0
- django_webhook_subscriber-2.0.0.dist-info/RECORD +38 -0
- django_webhook_subscriber/management/commands/check_webhook_tasks.py +0 -113
- django_webhook_subscriber/management/commands/clean_webhook_logs.py +0 -65
- django_webhook_subscriber/management/commands/test_webhook.py +0 -96
- django_webhook_subscriber/signals.py +0 -152
- django_webhook_subscriber/testing.py +0 -14
- django_webhook_subscriber/tests/test_signals.py +0 -268
- django_webhook_subscriber-0.4.0.dist-info/METADATA +0 -448
- django_webhook_subscriber-0.4.0.dist-info/RECORD +0 -33
- {django_webhook_subscriber-0.4.0.dist-info → django_webhook_subscriber-2.0.0.dist-info}/WHEEL +0 -0
- {django_webhook_subscriber-0.4.0.dist-info → django_webhook_subscriber-2.0.0.dist-info}/licenses/LICENSE +0 -0
- {django_webhook_subscriber-0.4.0.dist-info → django_webhook_subscriber-2.0.0.dist-info}/top_level.txt +0 -0
|
@@ -1,448 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: django-webhook-subscriber
|
|
3
|
-
Version: 0.4.0
|
|
4
|
-
Summary: A Django package designed to handle webhook creation, management, and delivery.
|
|
5
|
-
Author-email: 42 Portugal <root@42porto.com>
|
|
6
|
-
Classifier: Environment :: Web Environment
|
|
7
|
-
Classifier: Framework :: Django
|
|
8
|
-
Classifier: Framework :: Django :: 5.0
|
|
9
|
-
Classifier: Intended Audience :: Developers
|
|
10
|
-
Classifier: License :: OSI Approved :: BSD License
|
|
11
|
-
Classifier: Operating System :: OS Independent
|
|
12
|
-
Classifier: Programming Language :: Python
|
|
13
|
-
Classifier: Programming Language :: Python :: 3
|
|
14
|
-
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
-
Classifier: Topic :: Internet :: WWW/HTTP
|
|
20
|
-
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
|
|
21
|
-
Requires-Python: >=3.10
|
|
22
|
-
Description-Content-Type: text/markdown
|
|
23
|
-
License-File: LICENSE
|
|
24
|
-
Requires-Dist: Django<5.3,>=5.0
|
|
25
|
-
Requires-Dist: djangorestframework<3.17,>=3.15.0
|
|
26
|
-
Requires-Dist: requests<2.33,>=2.32.3
|
|
27
|
-
Requires-Dist: celery<5.6,>=5.0.0
|
|
28
|
-
Provides-Extra: celery
|
|
29
|
-
Requires-Dist: celery<5.6,>=5.5.2; extra == "celery"
|
|
30
|
-
Requires-Dist: redis<5.3,>=5.2.1; extra == "celery"
|
|
31
|
-
Provides-Extra: test
|
|
32
|
-
Requires-Dist: pytest<8.4,>=8.3.4; extra == "test"
|
|
33
|
-
Requires-Dist: pytest-django<5.0,>=4.9.0; extra == "test"
|
|
34
|
-
Requires-Dist: pytest-cov<6.1,>=6.0.0; extra == "test"
|
|
35
|
-
Dynamic: license-file
|
|
36
|
-
|
|
37
|
-
# Django Webhook Subscriber
|
|
38
|
-
|
|
39
|
-
A Django application for easily implementing and managing webhooks with Django REST Framework.
|
|
40
|
-
|
|
41
|
-
## Overview
|
|
42
|
-
|
|
43
|
-
Django Webhook Subscriber provides a simple yet powerful way to send webhook notifications when your models change. It integrates seamlessly with Django's signals system and Django REST Framework's serializers to automatically deliver webhook payloads when models are created, updated, or deleted. The package supports both synchronous and asynchronous webhook delivery using Celery.
|
|
44
|
-
|
|
45
|
-
## Features
|
|
46
|
-
|
|
47
|
-
- Automatic webhook triggering on model changes (create, update, delete)
|
|
48
|
-
- Easy configuration through Django settings
|
|
49
|
-
- Flexible serialization using Django REST Framework serializers
|
|
50
|
-
- Simple registration API for programmatic webhook configuration
|
|
51
|
-
- Log storage and automatic cleanup for webhook deliveries
|
|
52
|
-
- Asynchronous webhook delivery with Celery
|
|
53
|
-
- Configurable retry policies for failed webhooks
|
|
54
|
-
|
|
55
|
-
## Installation
|
|
56
|
-
|
|
57
|
-
```bash
|
|
58
|
-
pip install django-webhook-subscriber
|
|
59
|
-
```
|
|
60
|
-
|
|
61
|
-
Add 'django_webhook_subscriber' to your INSTALLED_APPS:
|
|
62
|
-
|
|
63
|
-
```python
|
|
64
|
-
INSTALLED_APPS = [
|
|
65
|
-
# ... your other apps
|
|
66
|
-
|
|
67
|
-
# IMPORTANT: django_webhook_subscriber must be listed LAST
|
|
68
|
-
'django_webhook_subscriber',
|
|
69
|
-
]
|
|
70
|
-
```
|
|
71
|
-
|
|
72
|
-
Run migrations to create the necessary tables:
|
|
73
|
-
|
|
74
|
-
```bash
|
|
75
|
-
python manage.py migrate django_webhook_subscriber
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
### Important: App Registration Order
|
|
79
|
-
|
|
80
|
-
**django_webhook_subscriber must be the last app in your INSTALLED_APPS list.**
|
|
81
|
-
|
|
82
|
-
This is crucial because:
|
|
83
|
-
|
|
84
|
-
1. Django registers signal handlers in the order that apps are initialized
|
|
85
|
-
2. Signal handlers execute in the order they were registered
|
|
86
|
-
3. Webhook handlers should run after all other signal handlers have completed
|
|
87
|
-
|
|
88
|
-
If django_webhook_subscriber is not listed last, other apps' signal handlers might run after your webhook has already been sent, resulting in outdated or inconsistent data in your webhooks.
|
|
89
|
-
|
|
90
|
-
Example of correct installation:
|
|
91
|
-
|
|
92
|
-
```python
|
|
93
|
-
INSTALLED_APPS = [
|
|
94
|
-
'django.contrib.admin',
|
|
95
|
-
'django.contrib.auth',
|
|
96
|
-
# ... other Django and third-party apps
|
|
97
|
-
|
|
98
|
-
# Your application apps
|
|
99
|
-
'users',
|
|
100
|
-
'products',
|
|
101
|
-
'orders',
|
|
102
|
-
|
|
103
|
-
# django_webhook_subscriber must be last
|
|
104
|
-
'django_webhook_subscriber',
|
|
105
|
-
]
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
## Celery Configuration
|
|
109
|
-
|
|
110
|
-
Django Webhook Subscriber uses Celery for asynchronous webhook delivery. To enable this functionality, you need to configure Celery in your Django project.
|
|
111
|
-
|
|
112
|
-
### Setting up Celery
|
|
113
|
-
|
|
114
|
-
1. Install the required dependencies:
|
|
115
|
-
```bash
|
|
116
|
-
pip install django-webhook-subscriber
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
2. Create a `celery.py` file in your project directory:
|
|
120
|
-
```python
|
|
121
|
-
# myproject/celery.py
|
|
122
|
-
import os
|
|
123
|
-
from celery import Celery
|
|
124
|
-
|
|
125
|
-
# Set the default Django settings module
|
|
126
|
-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
|
|
127
|
-
|
|
128
|
-
app = Celery('myproject')
|
|
129
|
-
|
|
130
|
-
# Use Django settings for Celery
|
|
131
|
-
app.config_from_object('django.conf:settings', namespace='CELERY')
|
|
132
|
-
|
|
133
|
-
# Discover tasks from all installed apps
|
|
134
|
-
app.autodiscover_tasks()
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
3. Configure Celery in your `settings.py`:
|
|
138
|
-
```python
|
|
139
|
-
# Celery settings
|
|
140
|
-
CELERY_BROKER_URL = 'redis://localhost:6379/0'
|
|
141
|
-
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
|
|
142
|
-
CELERY_ACCEPT_CONTENT = ['json']
|
|
143
|
-
CELERY_TASK_SERIALIZER = 'json'
|
|
144
|
-
CELERY_RESULT_SERIALIZER = 'json'
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
4. Import the Celery app in your project's `__init__.py`:
|
|
148
|
-
```python
|
|
149
|
-
# myproject/__init__.py
|
|
150
|
-
from .celery import app as celery_app
|
|
151
|
-
|
|
152
|
-
__all__ = ['celery_app']
|
|
153
|
-
```
|
|
154
|
-
|
|
155
|
-
5. Start a Celery worker:
|
|
156
|
-
```bash
|
|
157
|
-
celery -A myproject worker -l INFO
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
## Configuration
|
|
161
|
-
|
|
162
|
-
Configure your webhooks in your Django settings:
|
|
163
|
-
|
|
164
|
-
```python
|
|
165
|
-
REST_WEBHOOKS = {
|
|
166
|
-
# Models that can trigger webhooks
|
|
167
|
-
'WEBHOOK_MODELS': {
|
|
168
|
-
'app.Model': {
|
|
169
|
-
'serializer': 'path.to.ModelSerializer',
|
|
170
|
-
'events': ['CREATE', 'UPDATE', 'DELETE'], # Optional, defaults to all
|
|
171
|
-
},
|
|
172
|
-
'another_app.AnotherModel': {
|
|
173
|
-
'serializer': 'another_path.to.AnotherSerializer',
|
|
174
|
-
'events': ['CREATE'], # Only trigger on creation
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
|
|
178
|
-
# Log retention settings (optional)
|
|
179
|
-
'LOG_RETENTION_DAYS': 30, # Number of days to keep delivery logs (default: 30)
|
|
180
|
-
'AUTO_CLEANUP': True, # Automatically clean old logs when creating new ones (default: True)
|
|
181
|
-
|
|
182
|
-
# Delivery settings (optional)
|
|
183
|
-
'DEFAULT_USE_ASYNC': True, # Default setting for async delivery (default: False)
|
|
184
|
-
'DEFAULT_MAX_RETRIES': 3, # Default max retry attempts (default: 3)
|
|
185
|
-
'DEFAULT_RETRY_DELAY': 60, # Default seconds between retries (default: 60)
|
|
186
|
-
'REQUEST_TIMEOUT': 30, # Default timeout for webhook requests in seconds (default: 30)
|
|
187
|
-
}
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
### Async Delivery Configuration
|
|
191
|
-
|
|
192
|
-
The async settings control how webhooks are delivered:
|
|
193
|
-
|
|
194
|
-
- `DEFAULT_USE_ASYNC`: When set to `True`, webhooks will be delivered asynchronously using Celery tasks. This is useful for preventing slowdowns in your application when delivering webhooks.
|
|
195
|
-
- `DEFAULT_MAX_RETRIES`: Maximum number of retry attempts for failed webhook deliveries.
|
|
196
|
-
- `DEFAULT_RETRY_DELAY`: Time (in seconds) to wait between retry attempts.
|
|
197
|
-
|
|
198
|
-
These settings can be overridden on a per-webhook basis through the WebhookRegistry model in the admin interface.
|
|
199
|
-
|
|
200
|
-
## Usage
|
|
201
|
-
|
|
202
|
-
### Automatic Registration
|
|
203
|
-
|
|
204
|
-
Webhooks are automatically registered from your settings when Django starts up.
|
|
205
|
-
|
|
206
|
-
### Manual Registration
|
|
207
|
-
|
|
208
|
-
You can also register webhooks programmatically:
|
|
209
|
-
|
|
210
|
-
```python
|
|
211
|
-
from django_webhook_subscriber.utils import register_webhook_signals
|
|
212
|
-
from myapp.models import MyModel
|
|
213
|
-
from myapp.serializers import MyModelSerializer
|
|
214
|
-
|
|
215
|
-
# Register webhooks for a model
|
|
216
|
-
register_webhook_signals(
|
|
217
|
-
model_class=MyModel,
|
|
218
|
-
serializer=MyModelSerializer, # Optional
|
|
219
|
-
events=['CREATE', 'UPDATE'] # Optional
|
|
220
|
-
)
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
### Custom Serialization
|
|
224
|
-
|
|
225
|
-
By default, all model fields are included in the webhook payload. You can customize this by providing your own serializer:
|
|
226
|
-
|
|
227
|
-
```python
|
|
228
|
-
from rest_framework import serializers
|
|
229
|
-
from myapp.models import MyModel
|
|
230
|
-
|
|
231
|
-
class MyModelWebhookSerializer(serializers.ModelSerializer):
|
|
232
|
-
class Meta:
|
|
233
|
-
model = MyModel
|
|
234
|
-
fields = ['id', 'name', 'created_at'] # Only include these fields
|
|
235
|
-
|
|
236
|
-
# Add custom fields or methods as needed
|
|
237
|
-
extra_info = serializers.SerializerMethodField()
|
|
238
|
-
|
|
239
|
-
def get_extra_info(self, obj):
|
|
240
|
-
return f"This is {obj.name}"
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
### Webhook Payload Format
|
|
244
|
-
|
|
245
|
-
The webhook payload follows this format:
|
|
246
|
-
|
|
247
|
-
```json
|
|
248
|
-
{
|
|
249
|
-
"pk": 123,
|
|
250
|
-
"model": "app_label.model_name",
|
|
251
|
-
"signal_type": "created|updated|deleted",
|
|
252
|
-
"fields": {
|
|
253
|
-
"id": 123,
|
|
254
|
-
"field1": "value1",
|
|
255
|
-
"field2": "value2"
|
|
256
|
-
// All serialized fields here
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
### Managing Webhooks in Admin
|
|
262
|
-
|
|
263
|
-
Django Webhook Subscriber includes a Django admin interface for managing webhooks:
|
|
264
|
-
|
|
265
|
-
- Create webhooks for registered models
|
|
266
|
-
- View delivery logs and status
|
|
267
|
-
- Configure endpoints, authentication, and event types
|
|
268
|
-
- Enable/disable webhooks
|
|
269
|
-
- Configure async delivery settings per webhook
|
|
270
|
-
|
|
271
|
-
## Delivery Logs
|
|
272
|
-
|
|
273
|
-
The package automatically logs all webhook delivery attempts, including:
|
|
274
|
-
|
|
275
|
-
- Payload sent
|
|
276
|
-
- Response status and body
|
|
277
|
-
- Error messages (if any)
|
|
278
|
-
- Timestamp
|
|
279
|
-
|
|
280
|
-
Logs are automatically cleaned up based on your retention settings (`LOG_RETENTION_DAYS`).
|
|
281
|
-
### Log Cleanup
|
|
282
|
-
|
|
283
|
-
You can clean up old webhook delivery logs using the following methods:
|
|
284
|
-
|
|
285
|
-
#### Using the Management Command
|
|
286
|
-
|
|
287
|
-
Run the following command to clean up old logs across all webhooks:
|
|
288
|
-
|
|
289
|
-
```bash
|
|
290
|
-
python manage.py clean_webhook_logs
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
#### Using Python Code
|
|
294
|
-
|
|
295
|
-
You can also clean up logs programmatically:
|
|
296
|
-
|
|
297
|
-
```python
|
|
298
|
-
from django_webhook_subscriber.models import WebhookDeliveryLog, WebhookRegistry
|
|
299
|
-
|
|
300
|
-
# Clean up all old logs across all webhooks
|
|
301
|
-
WebhookDeliveryLog.objects.cleanup_old_logs()
|
|
302
|
-
|
|
303
|
-
# Clean up logs for a specific webhook
|
|
304
|
-
webhook = WebhookRegistry.objects.get(name="My Webhook")
|
|
305
|
-
WebhookDeliveryLog.objects.cleanup_old_logs(webhook=webhook)
|
|
306
|
-
```
|
|
307
|
-
|
|
308
|
-
These methods respect the `LOG_RETENTION_DAYS` setting, ensuring only logs older than the specified retention period are removed.
|
|
309
|
-
|
|
310
|
-
## Asynchronous Webhook Delivery
|
|
311
|
-
|
|
312
|
-
When a webhook is configured to use asynchronous delivery, the following happens:
|
|
313
|
-
|
|
314
|
-
1. When a model event occurs (create, update, delete), the webhook payload is prepared as usual.
|
|
315
|
-
2. Instead of immediately delivering the webhook, a Celery task is created and queued for processing.
|
|
316
|
-
3. The Celery worker processes the task, delivering the webhook to the specified endpoint.
|
|
317
|
-
4. If the delivery fails, it will be retried according to the retry settings.
|
|
318
|
-
|
|
319
|
-
### Configuring Per-Webhook Async Settings
|
|
320
|
-
|
|
321
|
-
In the Django admin, each webhook can have its own specific async settings:
|
|
322
|
-
|
|
323
|
-
- **Use Async**: Set to Yes/No/Use Default to control whether this specific webhook uses async delivery
|
|
324
|
-
- **Max Retries**: Maximum number of retry attempts for this webhook (overrides the default)
|
|
325
|
-
- **Retry Delay**: Time (in seconds) to wait between retry attempts for this webhook (overrides the default)
|
|
326
|
-
|
|
327
|
-
## Signal Handling
|
|
328
|
-
|
|
329
|
-
Django Webhook Subscriber registers signal handlers that respond to model changes. Understanding how these signals work is important for correct operation:
|
|
330
|
-
|
|
331
|
-
### Signal Order and Multiple Handlers
|
|
332
|
-
|
|
333
|
-
When multiple signal handlers are registered for the same signal, Django executes them in registration order. Since webhooks should typically be triggered after all model changes are complete, the webhook handlers need to run last.
|
|
334
|
-
|
|
335
|
-
This is automatically handled by placing `django_webhook_subscriber` last in your `INSTALLED_APPS`, which ensures webhook handlers are registered after all other signal handlers.
|
|
336
|
-
|
|
337
|
-
If you're experiencing issues with webhook data accuracy, verify that:
|
|
338
|
-
|
|
339
|
-
1. `django_webhook_subscriber` is the last app in your `INSTALLED_APPS` list
|
|
340
|
-
2. You're not manually registering additional signal handlers after Django initializes
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
## Testing with Django Webhook Subscriber
|
|
344
|
-
|
|
345
|
-
When running tests for applications that use Django Webhook Subscriber, you typically want to prevent actual webhook deliveries from occurring. Django Webhook Subscriber provides several approaches to disable webhooks during testing:
|
|
346
|
-
|
|
347
|
-
### Method 1: Disable webhooks globally via settings
|
|
348
|
-
The simplest approach is to add the DISABLE_WEBHOOKS setting to your test settings file:
|
|
349
|
-
|
|
350
|
-
```py
|
|
351
|
-
# tests/settings.py
|
|
352
|
-
from your_project.settings import *
|
|
353
|
-
|
|
354
|
-
# Disable all webhooks for testing
|
|
355
|
-
DISABLE_WEBHOOKS = True
|
|
356
|
-
```
|
|
357
|
-
|
|
358
|
-
This will prevent any webhooks from being registered when the Django app is initialized and will also block any webhook processing that might occur during tests.
|
|
359
|
-
|
|
360
|
-
### Method 2: Programmatically unregister webhooks
|
|
361
|
-
|
|
362
|
-
You can use the unregister_webhook_signals() function to disable webhooks for specific test classes or methods:
|
|
363
|
-
|
|
364
|
-
```py
|
|
365
|
-
from django.test import TestCase
|
|
366
|
-
from django_webhook_subscriber.utils import unregister_webhook_signals
|
|
367
|
-
|
|
368
|
-
class MyModelTests(TestCase):
|
|
369
|
-
def setUp(self):
|
|
370
|
-
# Disable all webhooks before running the test
|
|
371
|
-
unregister_webhook_signals()
|
|
372
|
-
|
|
373
|
-
# Continue with the rest of your setup
|
|
374
|
-
# ...
|
|
375
|
-
|
|
376
|
-
def test_create_model(self):
|
|
377
|
-
# Your model operations won't trigger any webhooks
|
|
378
|
-
model = MyModel.objects.create(name="Test")
|
|
379
|
-
```
|
|
380
|
-
|
|
381
|
-
You can also disable webhooks for a specific model if needed:
|
|
382
|
-
|
|
383
|
-
```py
|
|
384
|
-
# Only disable webhooks for this specific model
|
|
385
|
-
unregister_webhook_signals(model_class=MyModel)
|
|
386
|
-
```
|
|
387
|
-
|
|
388
|
-
### Method 3: Use the testing utilities
|
|
389
|
-
|
|
390
|
-
Django Webhook Subscriber provides testing utilities to make it easier to disable webhooks in tests:
|
|
391
|
-
|
|
392
|
-
```py
|
|
393
|
-
from django.test import TestCase
|
|
394
|
-
from django_webhook_subscriber.testing import disabled_webhooks
|
|
395
|
-
|
|
396
|
-
class MyTests(TestCase):
|
|
397
|
-
def test_something(self):
|
|
398
|
-
# Webhooks are active here
|
|
399
|
-
|
|
400
|
-
with disabled_webhooks:
|
|
401
|
-
# No webhooks will be triggered in this block
|
|
402
|
-
instance = MyModel.objects.create()
|
|
403
|
-
instance.save()
|
|
404
|
-
instance.delete()
|
|
405
|
-
|
|
406
|
-
# Webhooks are active again here
|
|
407
|
-
```
|
|
408
|
-
|
|
409
|
-
### Method 4: Mock the webhook delivery
|
|
410
|
-
|
|
411
|
-
For more advanced testing where you want to verify that webhooks would have been triggered without actually sending them, you can use mocking:
|
|
412
|
-
|
|
413
|
-
```py
|
|
414
|
-
from django.test import TestCase
|
|
415
|
-
from unittest.mock import patch
|
|
416
|
-
|
|
417
|
-
class WebhookTests(TestCase):
|
|
418
|
-
@patch('django_webhook_subscriber.delivery.process_and_deliver_webhook')
|
|
419
|
-
def test_webhook_delivery(self, mock_deliver):
|
|
420
|
-
# Create your model
|
|
421
|
-
instance = MyModel.objects.create(name="Test")
|
|
422
|
-
|
|
423
|
-
# Verify webhook would have been triggered with the right event type
|
|
424
|
-
mock_deliver.assert_called_once()
|
|
425
|
-
args, kwargs = mock_deliver.call_args
|
|
426
|
-
self.assertEqual(kwargs.get('event_signal'), 'created')
|
|
427
|
-
```
|
|
428
|
-
|
|
429
|
-
### Method 5: Empty webhook configuration
|
|
430
|
-
|
|
431
|
-
You can also override the REST_WEBHOOKS setting in your test settings with an empty configuration:
|
|
432
|
-
|
|
433
|
-
```py
|
|
434
|
-
# tests/settings.py
|
|
435
|
-
REST_WEBHOOKS = {
|
|
436
|
-
'WEBHOOK_MODELS': {}, # Empty dict = no webhooks registered
|
|
437
|
-
}
|
|
438
|
-
```
|
|
439
|
-
|
|
440
|
-
### Best Practices for Testing with Webhooks
|
|
441
|
-
|
|
442
|
-
1. Use a separate settings file for tests: This allows you to disable webhooks globally for all tests.
|
|
443
|
-
2. Set up a test environment in your CI/CD pipeline: Make sure your CI/CD pipeline uses settings with webhooks disabled.
|
|
444
|
-
3. Test webhook delivery separately: Use dedicated tests with mocks to verify webhook delivery logic.
|
|
445
|
-
4. Test actual webhook deliveries in integration tests: Use a local webhook receiver service for integration tests where you want to verify the complete webhook flow.
|
|
446
|
-
5. Consider using fixtures: Set up fixtures that include webhook configurations for testing specific webhook scenarios.
|
|
447
|
-
|
|
448
|
-
By implementing these testing strategies, you can effectively test your application's core functionality without triggering unwanted webhook deliveries during testing.
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
django_webhook_subscriber/__init__.py,sha256=w5qVkZpiPBR8_o98EujX286aL1VTZfmLL2n7Q8d-dog,73
|
|
2
|
-
django_webhook_subscriber/admin.py,sha256=_k_WadZ3nm0xMehgMQXUAAFZu4HEtslAqQ-CiJ0M4ps,8023
|
|
3
|
-
django_webhook_subscriber/apps.py,sha256=OMFlYI15pBtb-jq0K_OA5Kzci_p2PjdOIAk6sNL2Hlo,839
|
|
4
|
-
django_webhook_subscriber/conf.py,sha256=FGEqfjP5yI3Jy6AtwpVLc_7Uc9fXVTndMXihB1um73c,2265
|
|
5
|
-
django_webhook_subscriber/delivery.py,sha256=fYQnGgmyUQh4QgAJcEeQCLS2xU0ejqSRyCq1eO8LI6Y,6367
|
|
6
|
-
django_webhook_subscriber/managers.py,sha256=wublRifNiyjeaRpSN3LJPHBvFiwpz_6kDflgZ1nbeRE,1720
|
|
7
|
-
django_webhook_subscriber/models.py,sha256=_Qu17Y6C0-IMory_8GgRKECf4VkQjkbWzcA5KL3gORs,5294
|
|
8
|
-
django_webhook_subscriber/serializers.py,sha256=8VrK1fy-SICy56WMhDdZ4K3Rf1H71yAy9bXkmJ-uj7I,2567
|
|
9
|
-
django_webhook_subscriber/signals.py,sha256=qEcYprVsWnr9g8Q1AEnecM6tgchRmpXyComXC57eJLc,4797
|
|
10
|
-
django_webhook_subscriber/tasks.py,sha256=urF0fboqKBHnxZJjCFTIX-Np7B7_iJbWg5YH6AoXeFU,2272
|
|
11
|
-
django_webhook_subscriber/testing.py,sha256=XDXe31miiYVof8fEhbmbLB6QZINQP3l5Xz3JXDBTLzI,473
|
|
12
|
-
django_webhook_subscriber/utils.py,sha256=tGKFLXuW2HKaWxvq4laA0vzTVt17c_wFi8LGMk7bh6k,2683
|
|
13
|
-
django_webhook_subscriber/management/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
14
|
-
django_webhook_subscriber/management/commands/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
15
|
-
django_webhook_subscriber/management/commands/check_webhook_tasks.py,sha256=A4ycJpMVgRJXS8WcB05W39JNK2-5XQcqqkCXG81SFDY,3946
|
|
16
|
-
django_webhook_subscriber/management/commands/clean_webhook_logs.py,sha256=wko8md6FcjHci2kbjw-BuTurcvR-Zk9Kntf81pkUtqI,2016
|
|
17
|
-
django_webhook_subscriber/management/commands/test_webhook.py,sha256=SPT1kYqsXFsjkBOwHihNXrXrNJMm8yJnjFouX1EKYU4,3345
|
|
18
|
-
django_webhook_subscriber/migrations/0001_initial.py,sha256=HZ4CF8U0aoQNMIswHv039HR32eLIEB0rttvcjrAma-Y,4160
|
|
19
|
-
django_webhook_subscriber/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
20
|
-
django_webhook_subscriber/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
21
|
-
django_webhook_subscriber/tests/settings.py,sha256=WrpUbSAMc5rmLWrJybXnL-1GBJWRUlSLQ_TGJT-iC6s,455
|
|
22
|
-
django_webhook_subscriber/tests/test_delivery.py,sha256=lpVVnQjZm_kxEpsbbeAOugPO7RWhXuaAHCLe1Y2KqfY,7446
|
|
23
|
-
django_webhook_subscriber/tests/test_managers.py,sha256=AUmzhcMLWPjcwr7_X7OQLqQzeaqIU2Oqhuasdk4WTlw,3364
|
|
24
|
-
django_webhook_subscriber/tests/test_models.py,sha256=wXpu50zjJPlnFkoXN8g3pYR5XexOsurBj6ANYZ8DL4I,11430
|
|
25
|
-
django_webhook_subscriber/tests/test_serializers.py,sha256=5jspWrGhzXZRYryFr_5JJkqgz0BqU2Zm6EdwuQti0Ds,2913
|
|
26
|
-
django_webhook_subscriber/tests/test_signals.py,sha256=IrU6soy5YNz6Of9Wr0k3pBo209rPRTlDwrR0UOy10Cc,10160
|
|
27
|
-
django_webhook_subscriber/tests/test_tasks.py,sha256=rkzBjmTgcL26h0_NotuRbb-Neu-_MpBBc1sakAeCJ0o,7870
|
|
28
|
-
django_webhook_subscriber/tests/test_utils.py,sha256=qTSgF13v591XoOMOCue_xCUZUw1ExqHoMKCj35EDyAU,3774
|
|
29
|
-
django_webhook_subscriber-0.4.0.dist-info/licenses/LICENSE,sha256=Cqe_H97kNjzh70ZIee47TV5t0GQyath_iH2i0Yp47SU,1064
|
|
30
|
-
django_webhook_subscriber-0.4.0.dist-info/METADATA,sha256=ba2M-Pt-AuosPBVYhQ2ZELnmf4Cf7RFE9fL0cw4TeZU,15264
|
|
31
|
-
django_webhook_subscriber-0.4.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
32
|
-
django_webhook_subscriber-0.4.0.dist-info/top_level.txt,sha256=t2_XjlYcTMLR9OfBGdsW0xucs3OeRWrJ7ypETMXqrvw,26
|
|
33
|
-
django_webhook_subscriber-0.4.0.dist-info/RECORD,,
|
{django_webhook_subscriber-0.4.0.dist-info → django_webhook_subscriber-2.0.0.dist-info}/WHEEL
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|