|
| 1 | +from unittest import TestCase |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from sewer.lib import SewerError |
| 5 | +from sewer.providers.windns import WinDNS |
| 6 | + |
| 7 | +ACME_CHALLENGE = "_acme-challenge" |
| 8 | + |
| 9 | + |
| 10 | +class TestWinDNS(TestCase): |
| 11 | + def test_init_with_zone_provided(self): |
| 12 | + def _fn(): |
| 13 | + zone = "example.com" |
| 14 | + provider = WinDNS(zone) |
| 15 | + self.assertIsNotNone(provider) |
| 16 | + |
| 17 | + run_test_with_common_mocking(_fn) |
| 18 | + |
| 19 | + def test_init_with_zone_missing(self): |
| 20 | + def _fn(): |
| 21 | + with self.assertRaisesRegex( |
| 22 | + ValueError, "windns requires a string value for the zone argument" |
| 23 | + ): |
| 24 | + WinDNS() |
| 25 | + |
| 26 | + run_test_with_common_mocking(_fn) |
| 27 | + |
| 28 | + def test_init_module_is_installed(self): |
| 29 | + run_test_with_common_mocking(lambda: WinDNS("example.com")) |
| 30 | + |
| 31 | + def test_init_module_is_not_installed(self): |
| 32 | + def _fn(): |
| 33 | + with self.assertRaisesRegex( |
| 34 | + SewerError, "It seems that, the DNSServer module is not installed" |
| 35 | + ): |
| 36 | + WinDNS("example.com") |
| 37 | + |
| 38 | + run_test_with_common_mocking(_fn, False) |
| 39 | + |
| 40 | + def test_validate_domain_contains_zone(self): |
| 41 | + def _fn(): |
| 42 | + mock_challenge = _mock_challenge_1() |
| 43 | + mock_zone = "example.com" |
| 44 | + |
| 45 | + provider = WinDNS(mock_zone) |
| 46 | + provider._validate_domain_contains_zone(mock_challenge) |
| 47 | + |
| 48 | + run_test_with_common_mocking(_fn) |
| 49 | + |
| 50 | + def test_assert_domain_missing_zone(self): |
| 51 | + def _fn(): |
| 52 | + mock_challenge = _mock_challenge_1() |
| 53 | + mock_zone = "missing_zone" |
| 54 | + |
| 55 | + provider = WinDNS(mock_zone) |
| 56 | + |
| 57 | + with self.assertRaisesRegex(SewerError, "Domain must contains zone, domain"): |
| 58 | + provider._validate_domain_contains_zone(mock_challenge) |
| 59 | + |
| 60 | + run_test_with_common_mocking(_fn) |
| 61 | + |
| 62 | + def test_extract_sub_domain(self): |
| 63 | + def _test1(): |
| 64 | + mock_challenge = _mock_challenge_1() |
| 65 | + mock_zone = "example.com" |
| 66 | + |
| 67 | + provider = WinDNS(mock_zone) |
| 68 | + dns_name = provider._extract_sub_domain_from_challenge(mock_challenge) |
| 69 | + |
| 70 | + self.assertEqual(dns_name, ACME_CHALLENGE) |
| 71 | + |
| 72 | + def _test2(): |
| 73 | + mock_challenge = _mock_challenge_2() |
| 74 | + mock_zone = "example.com" |
| 75 | + |
| 76 | + provider = WinDNS(mock_zone) |
| 77 | + |
| 78 | + dns_name = provider._extract_sub_domain_from_challenge(mock_challenge) |
| 79 | + |
| 80 | + self.assertEqual(dns_name, "%s.test" % ACME_CHALLENGE) |
| 81 | + |
| 82 | + run_test_with_common_mocking(_test1) |
| 83 | + |
| 84 | + def test_alias_domain(self): |
| 85 | + def _fn(): |
| 86 | + mock_challenge = _mock_challenge_1() |
| 87 | + mock_alias = "alias.com" |
| 88 | + mock_alias_zone = mock_alias |
| 89 | + |
| 90 | + provider = WinDNS(mock_alias_zone, alias=mock_alias) |
| 91 | + |
| 92 | + dns_name = provider._extract_sub_domain_from_challenge(mock_challenge) |
| 93 | + |
| 94 | + mock_domain = mock_challenge["ident_value"] |
| 95 | + self.assertEqual(dns_name, mock_domain) |
| 96 | + |
| 97 | + run_test_with_common_mocking(_fn) |
| 98 | + |
| 99 | + |
| 100 | +def run_test_with_common_mocking(fn, is_module_installed=True): |
| 101 | + with patch( |
| 102 | + "windowsdnsserver.dns.dnsserver.DnsServerModule.is_dns_server_module_installed" |
| 103 | + ) as mock_module_installed, patch("platform.system") as mock_platform: |
| 104 | + mock_module_installed.return_value = is_module_installed |
| 105 | + mock_platform.return_value = "Windows" |
| 106 | + |
| 107 | + fn() |
| 108 | + |
| 109 | + |
| 110 | +def _mock_challenge_1(): |
| 111 | + return {"ident_value": "example.com"} |
| 112 | + |
| 113 | + |
| 114 | +def _mock_challenge_2(): |
| 115 | + return {"ident_value": "test.example.com"} |
0 commit comments