Create tests for webhooks #3183

Closed
opened 2025-12-29 18:26:24 +01:00 by adam · 2 comments
Owner

Originally created by @jeremystretch on GitHub (Jan 17, 2020).

Originally assigned to: @jeremystretch on GitHub.

Proposed Changes

Create new unit tests for:

  1. Create/updating/deleting webhooks
  2. Placing webhook events in the queue
  3. Transmitting webhooks

Justification

We have a huge gap in our test coverage around webhooks currently, leading to bugs such as #3951.

Originally created by @jeremystretch on GitHub (Jan 17, 2020). Originally assigned to: @jeremystretch on GitHub. ### Proposed Changes Create new unit tests for: 1. Create/updating/deleting webhooks 2. Placing webhook events in the queue 3. Transmitting webhooks ### Justification We have a huge gap in our test coverage around webhooks currently, leading to bugs such as #3951.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 18:26:24 +01:00
adam closed this issue 2025-12-29 18:26:24 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jan 17, 2020):

I've added tests for the enqueuing process in c6eb40daa8, but I'm not sure how to go about testing the RQ worker process. @lampwins any ideas?

@jeremystretch commented on GitHub (Jan 17, 2020): I've added tests for the enqueuing process in c6eb40daa8dbc3ed7c70c4835089e24ccb8301eb, but I'm not sure how to go about testing the RQ worker process. @lampwins any ideas?
Author
Owner

@hSaria commented on GitHub (Jan 17, 2020):

unittest.mock.patch the requests.send and checking it is called with the expected arguments is an option (I was testing with requests.post, but you get the idea).

>>> from unittest.mock import patch
>>> import requests
>>>
>>> def send():
...     requests.post('http://localhost')
... 
>>> @patch('requests.post')
... def test_send(mock_post):
...     send()
...     mock_post.assert_called_with('http://localhost')
... 
>>> test_send()
>>> 
>>> def send():
...     requests.post('http://not_localhost')
... 
>>> test_send()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1255, in patched
    return func(*args, **keywargs)
  File "<stdin>", line 4, in test_send
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 873, in assert_called_with
    raise AssertionError(_error_message()) from cause
AssertionError: Expected call: post('http://localhost')
Actual call: post('http://not_localhost')
>>> 
@hSaria commented on GitHub (Jan 17, 2020): `unittest.mock.patch` the `requests.send` and checking it is called with the expected arguments is an option (I was testing with `requests.post`, but you get the idea). ```python >>> from unittest.mock import patch >>> import requests >>> >>> def send(): ... requests.post('http://localhost') ... >>> @patch('requests.post') ... def test_send(mock_post): ... send() ... mock_post.assert_called_with('http://localhost') ... >>> test_send() >>> >>> def send(): ... requests.post('http://not_localhost') ... >>> test_send() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1255, in patched return func(*args, **keywargs) File "<stdin>", line 4, in test_send File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 873, in assert_called_with raise AssertionError(_error_message()) from cause AssertionError: Expected call: post('http://localhost') Actual call: post('http://not_localhost') >>> ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3183