Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fupduck committed Jun 19, 2024
1 parent 2dc7164 commit 35c278b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
6 changes: 2 additions & 4 deletions certbot_dns_hetzner/dns_hetzner.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ def _get_zone(domain):
return '.'.join([zone_name.domain, zone_name.suffix])

def _perform(self, domain, validation_name, validation):
with self._get_hetzner_client(domain) as client:
client.create_record("TXT", validation_name, validation)
self._get_hetzner_client(domain).create_record("TXT", self._fqdn_format(validation_name), validation)

def _cleanup(self, domain, validation_name, validation):
with self._get_hetzner_client(domain) as client:
client.delete_record(None, "TXT", validation_name, validation)
self._get_hetzner_client(domain).delete_record(None, "TXT", self._fqdn_format(validation_name), validation)

def _get_hetzner_client(self, domain):
config = ConfigResolver().with_env().with_dict({
Expand Down
27 changes: 13 additions & 14 deletions certbot_dns_hetzner/dns_hetzner_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from certbot_dns_hetzner.fakes import FAKE_API_TOKEN, FAKE_RECORD



patch_display_util = test_util.patch_display_util


Expand Down Expand Up @@ -50,46 +49,46 @@ def setUp(self):

@patch_display_util()
def test_perform(self, unused_mock_get_utility):
self.mock_client.add_txt_record.return_value = FAKE_RECORD
self.mock_client.create_record.return_value = FAKE_RECORD
self.auth.perform([self.achall])
self.mock_client.add_txt_record.assert_called_with(
DOMAIN, "_acme-challenge." + DOMAIN + ".", mock.ANY
self.mock_client.create_record.assert_called_with(
"TXT", "_acme-challenge." + DOMAIN + ".", mock.ANY
)

def test_perform_but_raises_plugin_error(self):
self.mock_client.add_txt_record.side_effect = mock.MagicMock(
self.mock_client.create_record.side_effect = mock.MagicMock(
side_effect=PluginError()
)
self.assertRaises(PluginError, self.auth.perform, [self.achall])
self.mock_client.add_txt_record.assert_called_with(
DOMAIN, "_acme-challenge." + DOMAIN + ".", mock.ANY
self.mock_client.create_record.assert_called_with(
"TXT", "_acme-challenge." + DOMAIN + ".", mock.ANY
)

@patch_display_util()
def test_cleanup(self, unused_mock_get_utility):
self.mock_client.add_txt_record.return_value = FAKE_RECORD
self.mock_client.create_record.return_value = FAKE_RECORD
# _attempt_cleanup | pylint: disable=protected-access
self.auth.perform([self.achall])
self.auth._attempt_cleanup = True
self.auth.cleanup([self.achall])

self.mock_client.del_txt_record.assert_called_with(
DOMAIN, "_acme-challenge." + DOMAIN + ".", mock.ANY
self.mock_client.delete_record.assert_called_with(
None, "TXT", "_acme-challenge." + DOMAIN + ".", mock.ANY
)

@patch_display_util()
def test_cleanup_but_raises_plugin_error(self, unused_mock_get_utility):
self.mock_client.add_txt_record.return_value = FAKE_RECORD
self.mock_client.del_txt_record.side_effect = mock.MagicMock(
self.mock_client.create_record.return_value = FAKE_RECORD
self.mock_client.delete_record.side_effect = mock.MagicMock(
side_effect=PluginError()
)
# _attempt_cleanup | pylint: disable=protected-access
self.auth.perform([self.achall])
self.auth._attempt_cleanup = True

self.assertRaises(PluginError, self.auth.cleanup, [self.achall])
self.mock_client.del_txt_record.assert_called_with(
DOMAIN, "_acme-challenge." + DOMAIN + ".", mock.ANY
self.mock_client.delete_record.assert_called_with(
None, "TXT", "_acme-challenge." + DOMAIN + ".", mock.ANY
)


Expand Down

0 comments on commit 35c278b

Please sign in to comment.