2026-02-06 00:46:16 +01:00
|
|
|
import pytest
|
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
|
from infra_cli.cloudflare import CloudflareManager
|
|
|
|
|
|
|
|
|
|
class MockConfig:
|
|
|
|
|
def get(self, key, default=None):
|
|
|
|
|
if key == 'cloudflare.token': return 'test-token'
|
2026-02-06 01:05:42 +01:00
|
|
|
if key == 'cloudflare.ddns_state_file': return '/tmp/test_ddns.json'
|
2026-02-06 00:46:16 +01:00
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
@patch('infra_cli.cloudflare.requests')
|
|
|
|
|
def test_cloudflare_zone_id_retrieval(mock_requests):
|
|
|
|
|
mock_res = MagicMock()
|
|
|
|
|
mock_res.json.return_value = {
|
|
|
|
|
"success": True,
|
|
|
|
|
"result": [{"id": "zone-123"}]
|
|
|
|
|
}
|
|
|
|
|
mock_requests.get.return_value = mock_res
|
|
|
|
|
|
|
|
|
|
mgr = CloudflareManager(MockConfig())
|
|
|
|
|
zone_id = mgr.get_zone_id("example.com")
|
|
|
|
|
|
|
|
|
|
assert zone_id == "zone-123"
|
|
|
|
|
|
|
|
|
|
@patch('infra_cli.cloudflare.requests')
|
|
|
|
|
def test_cloudflare_update_logic(mock_requests):
|
2026-02-06 01:05:42 +01:00
|
|
|
# Setup state file cleanup
|
|
|
|
|
state_file = '/tmp/test_ddns.json'
|
|
|
|
|
if os.path.exists(state_file): os.remove(state_file)
|
|
|
|
|
|
2026-02-06 00:46:16 +01:00
|
|
|
mgr = CloudflareManager(MockConfig())
|
|
|
|
|
mgr.add_domain("example.com")
|
|
|
|
|
|
2026-02-06 01:05:42 +01:00
|
|
|
def mocked_get(url, **kwargs):
|
|
|
|
|
res = MagicMock()
|
|
|
|
|
if "checkip.amazonaws.com" in url:
|
|
|
|
|
res.text = "1.2.3.4"
|
|
|
|
|
elif "zones?name=" in url:
|
|
|
|
|
res.json.return_value = {"success": True, "result": [{"id": "zone-123"}]}
|
|
|
|
|
elif "dns_records" in url:
|
|
|
|
|
res.json.return_value = {
|
|
|
|
|
"result": [{"id": "rec-456", "content": "9.9.9.9", "proxied": True}]
|
|
|
|
|
}
|
|
|
|
|
return res
|
|
|
|
|
|
|
|
|
|
mock_requests.get.side_effect = mocked_get
|
|
|
|
|
|
|
|
|
|
# Mock PUT
|
|
|
|
|
mock_put_res = MagicMock()
|
|
|
|
|
mock_put_res.json.return_value = {"success": True}
|
|
|
|
|
mock_requests.put.return_value = mock_put_res
|
2026-02-06 00:46:16 +01:00
|
|
|
|
2026-02-06 01:05:42 +01:00
|
|
|
res = mgr.update_ddns()
|
|
|
|
|
assert "Updated to 1.2.3.4" in res
|
|
|
|
|
|
|
|
|
|
if os.path.exists(state_file): os.remove(state_file)
|
|
|
|
|
|
|
|
|
|
import os
|