|
| 1 | +import importlib |
| 2 | +import importlib.util |
| 3 | +import os |
| 4 | +import sys |
| 5 | +from types import ModuleType |
| 6 | +from unittest.mock import MagicMock |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | + |
| 11 | +def load_manage_docs_dns_with_env(env: dict): |
| 12 | + """ |
| 13 | + Dynamically load scripts/manage_docs_dns.py while: |
| 14 | + - injecting a fake 'ovh' module into sys.modules (to avoid real dependency) |
| 15 | + - controlling environment variables at import time (module reads them on import) |
| 16 | + Returns the loaded module. |
| 17 | + """ |
| 18 | + # Backup environment and sys.modules entries we'll override |
| 19 | + old_env = os.environ.copy() |
| 20 | + try: |
| 21 | + # Apply provided environment |
| 22 | + os.environ.clear() |
| 23 | + os.environ.update(env) |
| 24 | + |
| 25 | + # Inject a fake 'ovh' module with required surface |
| 26 | + fake_ovh = ModuleType("ovh") |
| 27 | + |
| 28 | + class _FakeAPIError(Exception): |
| 29 | + pass |
| 30 | + |
| 31 | + exceptions_mod = ModuleType("ovh.exceptions") |
| 32 | + exceptions_mod.APIError = _FakeAPIError |
| 33 | + fake_ovh.exceptions = exceptions_mod |
| 34 | + sys.modules["ovh"] = fake_ovh |
| 35 | + sys.modules["ovh.exceptions"] = exceptions_mod |
| 36 | + |
| 37 | + # Also provide a placeholder Client type so get_client() can construct it if needed |
| 38 | + class _FakeClient: |
| 39 | + def __init__(self, *args, **kwargs): |
| 40 | + pass |
| 41 | + |
| 42 | + def get(self, *args, **kwargs): |
| 43 | + raise NotImplementedError |
| 44 | + |
| 45 | + def post(self, *args, **kwargs): |
| 46 | + raise NotImplementedError |
| 47 | + |
| 48 | + def put(self, *args, **kwargs): |
| 49 | + raise NotImplementedError |
| 50 | + |
| 51 | + fake_ovh.Client = _FakeClient |
| 52 | + |
| 53 | + # Load the target module by file path |
| 54 | + module_path = os.path.abspath( |
| 55 | + os.path.join(os.path.dirname(__file__), "..", "..", "manage_docs_dns.py") |
| 56 | + ) |
| 57 | + spec = importlib.util.spec_from_file_location("manage_docs_dns", module_path) |
| 58 | + module = importlib.util.module_from_spec(spec) |
| 59 | + assert spec and spec.loader |
| 60 | + spec.loader.exec_module(module) |
| 61 | + return module |
| 62 | + finally: |
| 63 | + # Restore environment to avoid leaking between tests |
| 64 | + os.environ.clear() |
| 65 | + os.environ.update(old_env) |
| 66 | + # Remove our fake ovh modules if present |
| 67 | + for name in ["ovh", "ovh.exceptions"]: |
| 68 | + if name in sys.modules: |
| 69 | + del sys.modules[name] |
| 70 | + |
| 71 | + |
| 72 | +def test_main_exits_when_missing_credentials(monkeypatch): |
| 73 | + mod = load_manage_docs_dns_with_env({}) |
| 74 | + |
| 75 | + # Intercept sys.exit to assert exit code |
| 76 | + exit_called = {"code": None} |
| 77 | + |
| 78 | + def fake_exit(code=0): |
| 79 | + exit_called["code"] = code |
| 80 | + raise SystemExit(code) |
| 81 | + |
| 82 | + monkeypatch.setattr(mod.sys, "exit", fake_exit) |
| 83 | + |
| 84 | + with pytest.raises(SystemExit) as ei: |
| 85 | + mod.main() |
| 86 | + |
| 87 | + assert ei.value.code == 1 |
| 88 | + assert exit_called["code"] == 1 |
| 89 | + |
| 90 | + |
| 91 | +def test_main_calls_update_for_cname_and_txt_when_credentials_present(monkeypatch): |
| 92 | + env = { |
| 93 | + "OVH_APP_KEY": "k", |
| 94 | + "OVH_APP_SECRET": "s", |
| 95 | + "OVH_CONSUMER_KEY": "c", |
| 96 | + } |
| 97 | + mod = load_manage_docs_dns_with_env(env) |
| 98 | + |
| 99 | + # Mock client and functions |
| 100 | + fake_client = MagicMock() |
| 101 | + monkeypatch.setattr(mod, "get_client", lambda: fake_client) |
| 102 | + uocr = MagicMock() |
| 103 | + monkeypatch.setattr(mod, "update_or_create_record", uocr) |
| 104 | + |
| 105 | + mod.main() |
| 106 | + |
| 107 | + # Should have been called twice: once for CNAME, once for TXT |
| 108 | + assert uocr.call_count == 2 |
| 109 | + cname_call = uocr.call_args_list[0] |
| 110 | + txt_call = uocr.call_args_list[1] |
| 111 | + |
| 112 | + assert cname_call.args == (fake_client, mod.DOMAIN, mod.CNAME_SUBDOMAIN, "CNAME", mod.CNAME_TARGET) |
| 113 | + assert txt_call.args == (fake_client, mod.DOMAIN, mod.TXT_SUBDOMAIN, "TXT", mod.TXT_TARGET) |
| 114 | + |
| 115 | + |
| 116 | +def test_update_or_create_record_valid_no_action(monkeypatch): |
| 117 | + env = { |
| 118 | + "OVH_APP_KEY": "k", |
| 119 | + "OVH_APP_SECRET": "s", |
| 120 | + "OVH_CONSUMER_KEY": "c", |
| 121 | + } |
| 122 | + mod = load_manage_docs_dns_with_env(env) |
| 123 | + |
| 124 | + fake_client = MagicMock() |
| 125 | + |
| 126 | + # Simulate find_record returns one record id and details match expected target |
| 127 | + monkeypatch.setattr(mod, "find_record", lambda client, domain, subdomain, rtype: [123]) |
| 128 | + monkeypatch.setattr(mod, "get_record_details", lambda client, domain, record_id: {"target": "expected"}) |
| 129 | + |
| 130 | + mod.update_or_create_record(fake_client, "example.com", "sub", "CNAME", "expected") |
| 131 | + |
| 132 | + # No update or create should be performed |
| 133 | + fake_client.put.assert_not_called() |
| 134 | + fake_client.post.assert_not_called() |
| 135 | + |
| 136 | + |
| 137 | +def test_update_or_create_record_updates_existing_record(monkeypatch): |
| 138 | + env = { |
| 139 | + "OVH_APP_KEY": "k", |
| 140 | + "OVH_APP_SECRET": "s", |
| 141 | + "OVH_CONSUMER_KEY": "c", |
| 142 | + } |
| 143 | + mod = load_manage_docs_dns_with_env(env) |
| 144 | + |
| 145 | + fake_client = MagicMock() |
| 146 | + |
| 147 | + monkeypatch.setattr(mod, "find_record", lambda client, domain, subdomain, rtype: [456]) |
| 148 | + monkeypatch.setattr(mod, "get_record_details", lambda client, domain, record_id: {"target": "old-value"}) |
| 149 | + |
| 150 | + mod.update_or_create_record(fake_client, "example.com", "sub", "TXT", "new-value") |
| 151 | + |
| 152 | + fake_client.put.assert_called_once() |
| 153 | + assert fake_client.put.call_args[0][0] == "/domain/zone/example.com/record/456" |
| 154 | + assert fake_client.put.call_args.kwargs == {"target": "new-value"} |
| 155 | + |
| 156 | + fake_client.post.assert_called_once_with("/domain/zone/example.com/refresh") |
| 157 | + |
| 158 | + |
| 159 | +def test_update_or_create_record_creates_new_record(monkeypatch): |
| 160 | + env = { |
| 161 | + "OVH_APP_KEY": "k", |
| 162 | + "OVH_APP_SECRET": "s", |
| 163 | + "OVH_CONSUMER_KEY": "c", |
| 164 | + } |
| 165 | + mod = load_manage_docs_dns_with_env(env) |
| 166 | + |
| 167 | + fake_client = MagicMock() |
| 168 | + |
| 169 | + # No existing records |
| 170 | + monkeypatch.setattr(mod, "find_record", lambda client, domain, subdomain, rtype: []) |
| 171 | + |
| 172 | + mod.update_or_create_record(fake_client, "example.com", "sub", "TXT", "txt-value") |
| 173 | + |
| 174 | + # First post: create record with proper parameters; Second post: refresh zone |
| 175 | + assert fake_client.post.call_count == 2 |
| 176 | + |
| 177 | + # Validate first post call arguments |
| 178 | + first_call = fake_client.post.call_args_list[0] |
| 179 | + assert first_call.args[0] == "/domain/zone/example.com/record" |
| 180 | + assert first_call.kwargs == { |
| 181 | + "fieldType": "TXT", |
| 182 | + "subDomain": "sub", |
| 183 | + "target": "txt-value", |
| 184 | + "ttl": 3600, |
| 185 | + } |
| 186 | + |
| 187 | + # Validate refresh call |
| 188 | + second_call = fake_client.post.call_args_list[1] |
| 189 | + assert second_call.args == ("/domain/zone/example.com/refresh",) |
0 commit comments