Skip to content

Commit 582e6a7

Browse files
[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>
1 parent 97507af commit 582e6a7

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

openwisp_controller/config/tests/test_vpn.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -827,33 +827,35 @@ def test_update_vpn_server_configuration(self):
827827
f"Cannot update configuration of {vpn.name} VPN server, "
828828
"webhook endpoint and authentication token are empty."
829829
)
830-
success_response = mock.Mock(spec=requests.Response)
831-
success_response.status_code = 200
832-
success_response.raise_for_status = mock.Mock()
833830

834831
with self.subTest("Webhook endpoint and authentication endpoint is present"):
835832
vpn.webhook_endpoint = "https://example.com"
836833
vpn.auth_token = "super-secret-token"
837-
vpn.save()
838-
vpn_client.refresh_from_db()
834+
success_response = mock.Mock(spec=requests.Response)
835+
success_response.status_code = 200
836+
success_response.raise_for_status = mock.Mock()
839837

840838
with mock.patch(
841839
"openwisp_controller.config.tasks.logger.info"
842840
) as mocked_logger, mock.patch(
843841
"requests.post", return_value=success_response
844842
):
843+
vpn.save()
844+
vpn_client.refresh_from_db()
845845
post_save.send(
846846
instance=vpn_client, sender=vpn_client._meta.model, created=False
847847
)
848-
mocked_logger.assert_called_once_with(
848+
expected_call = mock.call(
849849
f"Triggered update webhook of VPN Server UUID: {vpn.pk}"
850850
)
851+
mocked_logger.assert_has_calls([expected_call, expected_call])
852+
self.assertEqual(mocked_logger.call_count, 2)
853+
851854
fail_response = mock.Mock(spec=requests.Response)
852855
fail_response.status_code = 404
853856
fail_response.raise_for_status.side_effect = requests.exceptions.HTTPError(
854857
"Not Found"
855858
)
856-
857859
with mock.patch("logging.Logger.warning") as mocked_logger, mock.patch(
858860
"requests.post", return_value=fail_response
859861
):

0 commit comments

Comments
 (0)