File: test_authenticator_cleanup.py

package info (click to toggle)
python-certbot-dns-hetzner-cloud 1.0.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 168 kB
  • sloc: python: 317; makefile: 8
file content (52 lines) | stat: -rw-r--r-- 1,807 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import pytest
from certbot_dns_hetzner_cloud.authenticator import HetznerCloudDNSAuthenticator


class DummyHelper:
    """Mock helper that records delete_txt_record calls."""
    def __init__(self):
        self.delete_calls = []

    def delete_txt_record(self, *, zone: str, name: str, value: str = None):
        self.delete_calls.append((zone, name, value))


@pytest.fixture
def authenticator(monkeypatch):
    """Fixture that sets up an Authenticator with a mocked Hetzner helper."""
    auth = HetznerCloudDNSAuthenticator(config=None, name="dns-hetzner-cloud")

    # Replace the real helper with our dummy mock
    dummy = DummyHelper()
    auth.hetzner_dns_helper = dummy

    return auth, dummy


def test_cleanup_removes_correct_txt_record(authenticator):
    """
    _cleanup() should call delete_txt_record() with the correct zone and record name
    derived from the validation name.
    """
    auth, dummy = authenticator

    # Example DNS-01 challenge details
    domain = "example.com"
    validation_name = "_acme-challenge.sub.example.com."
    validation_value = "abcdef123456"  # not used by cleanup, but Certbot provides it

    # Act: perform the cleanup
    auth._cleanup(domain, validation_name, validation_value)

    # Assert: exactly one delete call was made
    assert len(dummy.delete_calls) == 1, "expected exactly one TXT record deletion call"

    # Extract the call arguments
    zone, name, value = dummy.delete_calls[0]

    # Zone should be the registered domain
    assert zone == "example.com", f"unexpected zone: {zone}"
    # Record name should be relative within the zone
    assert name == "_acme-challenge.sub", f"unexpected record name: {name}"
    # Value should match the validation token
    assert value == validation_value, f"unexpected value: {value}"