Skip to content

Commit 9f31948

Browse files
committed
[Fix] Update tests to match latest changes #1049
Fixed all failing tests that were failing due to inconsistencies with recent updates in notification handling, logging, and retry mechanisms. Fixes #1049
1 parent 09e255b commit 9f31948

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

openwisp_controller/config/tasks_zerotier.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import logging
22
from http import HTTPStatus
3-
from time import sleep
43

54
from celery import shared_task
6-
from django.core.cache import cache
75
from django.core.exceptions import ObjectDoesNotExist
8-
from django.utils.translation import gettext as _
9-
from openwisp_notifications.signals import notify
106
from requests.exceptions import RequestException
117
from swapper import load_model
128

openwisp_controller/config/tests/test_notifications.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class TestNotifications(
3131
app_label = "config"
3232
_ZT_SERVICE_REQUESTS = "openwisp_controller.config.api.zerotier_service.requests"
3333
_ZT_API_TASKS_INFO_LOGGER = "openwisp_controller.config.tasks_zerotier.logger.info"
34-
_ZT_API_TASKS_WARN_LOGGER = "openwisp_controller.config.tasks_zerotier.logger.warn"
34+
_ZT_API_TASKS_WARN_LOGGER = (
35+
"openwisp_controller.config.tasks_zerotier.logger.warning"
36+
)
3537
_ZT_API_TASKS_ERR_LOGGER = "openwisp_controller.config.tasks_zerotier.logger.error"
3638
# As the locmem cache does not support the redis backend cache.keys() method
3739
_ZT_API_TASKS_LOCMEM_CACHE_KEYS = f"{settings.CACHES['default']['BACKEND']}.keys"

openwisp_controller/config/tests/test_vpn.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from django.core.exceptions import ValidationError
1010
from django.db.models.signals import post_save
1111
from django.db.utils import IntegrityError
12-
from django.http.response import HttpResponse, HttpResponseNotFound
1312
from django.test import TestCase, TransactionTestCase
1413
from requests.exceptions import ConnectionError, RequestException, Timeout
1514
from swapper import load_model
@@ -828,6 +827,9 @@ def test_update_vpn_server_configuration(self):
828827
f"Cannot update configuration of {vpn.name} VPN server, "
829828
"webhook endpoint and authentication token are empty."
830829
)
830+
success_response = mock.Mock(spec=requests.Response)
831+
success_response.status_code = 200
832+
success_response.raise_for_status = mock.Mock()
831833

832834
with self.subTest("Webhook endpoint and authentication endpoint is present"):
833835
vpn.webhook_endpoint = "https://example.com"
@@ -838,24 +840,29 @@ def test_update_vpn_server_configuration(self):
838840
with mock.patch(
839841
"openwisp_controller.config.tasks.logger.info"
840842
) as mocked_logger, mock.patch(
841-
"requests.post", return_value=HttpResponse()
843+
"requests.post", return_value=success_response
842844
):
843845
post_save.send(
844846
instance=vpn_client, sender=vpn_client._meta.model, created=False
845847
)
846848
mocked_logger.assert_called_once_with(
847849
f"Triggered update webhook of VPN Server UUID: {vpn.pk}"
848850
)
851+
fail_response = mock.Mock(spec=requests.Response)
852+
fail_response.status_code = 404
853+
fail_response.raise_for_status.side_effect = requests.exceptions.HTTPError(
854+
"Not Found"
855+
)
849856

850-
with mock.patch("logging.Logger.error") as mocked_logger, mock.patch(
851-
"requests.post", return_value=HttpResponseNotFound()
857+
with mock.patch("logging.Logger.warning") as mocked_logger, mock.patch(
858+
"requests.post", return_value=fail_response
852859
):
853860
post_save.send(
854861
instance=vpn_client, sender=vpn_client._meta.model, created=False
855862
)
856863
mocked_logger.assert_called_once_with(
857864
"Failed to update VPN Server configuration. "
858-
f"Response status code: 404, VPN Server UUID: {vpn.pk}"
865+
f"Error: Not Found, VPN Server UUID: {vpn.pk}"
859866
)
860867

861868
def test_vpn_peers_changed(self):
@@ -1467,7 +1474,9 @@ class TestZeroTierTransaction(
14671474
):
14681475
_ZT_SERVICE_REQUESTS = "openwisp_controller.config.api.zerotier_service.requests"
14691476
_ZT_API_TASKS_INFO_LOGGER = "openwisp_controller.config.tasks_zerotier.logger.info"
1470-
_ZT_API_TASKS_WARN_LOGGER = "openwisp_controller.config.tasks_zerotier.logger.warn"
1477+
_ZT_API_TASKS_WARN_LOGGER = (
1478+
"openwisp_controller.config.tasks_zerotier.logger.warning"
1479+
)
14711480
_ZT_API_TASKS_ERR_LOGGER = "openwisp_controller.config.tasks_zerotier.logger.error"
14721481
# As the locmem cache does not support the redis backend cache.keys() method
14731482
_ZT_API_TASKS_LOCMEM_CACHE_KEYS = f"{settings.CACHES['default']['BACKEND']}.keys"

openwisp_controller/config/utils.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
from django.shortcuts import get_object_or_404 as base_get_object_or_404
88
from django.urls import path, re_path
99
from django.utils.translation import gettext_lazy as _
10-
11-
# from time import sleep
1210
from openwisp_notifications.signals import notify
1311
from openwisp_notifications.utils import _get_object_link
1412

0 commit comments

Comments
 (0)