Skip to content

Commit 5aaf05e

Browse files
committed
[chores:tests] Fixed mocking in test_update_vpn_server_configuration
The first call to vpn.save() was generting a real HTTP request, which failed in the CI of ansible-openwisp2. Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> (cherry picked from commit 582e6a7) # Conflicts: # openwisp_controller/config/tests/test_vpn.py
1 parent 4a44a94 commit 5aaf05e

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

openwisp_controller/config/tests/test_vpn.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -830,23 +830,33 @@ def test_update_vpn_server_configuration(self):
830830
with self.subTest("Webhook endpoint and authentication endpoint is present"):
831831
vpn.webhook_endpoint = "https://example.com"
832832
vpn.auth_token = "super-secret-token"
833-
vpn.save()
834-
vpn_client.refresh_from_db()
833+
success_response = mock.Mock(spec=requests.Response)
834+
success_response.status_code = 200
835+
success_response.raise_for_status = mock.Mock()
835836

836837
with mock.patch(
837838
"openwisp_controller.config.tasks.logger.info"
838839
) as mocked_logger, mock.patch(
839840
"requests.post", return_value=HttpResponse()
840841
):
842+
vpn.save()
843+
vpn_client.refresh_from_db()
841844
post_save.send(
842845
instance=vpn_client, sender=vpn_client._meta.model, created=False
843846
)
844-
mocked_logger.assert_called_once_with(
847+
expected_call = mock.call(
845848
f"Triggered update webhook of VPN Server UUID: {vpn.pk}"
846849
)
850+
mocked_logger.assert_has_calls([expected_call, expected_call])
851+
self.assertEqual(mocked_logger.call_count, 2)
847852

848-
with mock.patch("logging.Logger.error") as mocked_logger, mock.patch(
849-
"requests.post", return_value=HttpResponseNotFound()
853+
fail_response = mock.Mock(spec=requests.Response)
854+
fail_response.status_code = 404
855+
fail_response.raise_for_status.side_effect = requests.exceptions.HTTPError(
856+
"Not Found"
857+
)
858+
with mock.patch("logging.Logger.warning") as mocked_logger, mock.patch(
859+
"requests.post", return_value=fail_response
850860
):
851861
post_save.send(
852862
instance=vpn_client, sender=vpn_client._meta.model, created=False

0 commit comments

Comments
 (0)