78 lines
2.8 KiB
Python
78 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
|
|
from ddns.route53_update import update_ipv4, update_ipv6
|
|
|
|
|
|
class TestUpdateIpv4:
|
|
def test_update_ipv4_success(self) -> None:
|
|
mock_client = MagicMock()
|
|
mock_client.change_resource_record_sets.return_value = {}
|
|
|
|
with patch("ddns.route53_update._get_route53_client", return_value=mock_client):
|
|
update_ipv4("ZONE123", "test.example.com", "1.2.3.4")
|
|
|
|
mock_client.change_resource_record_sets.assert_called_once_with(
|
|
HostedZoneId="ZONE123",
|
|
ChangeBatch={
|
|
"Comment": "Update Public Addresses",
|
|
"Changes": [
|
|
{
|
|
"Action": "UPSERT",
|
|
"ResourceRecordSet": {
|
|
"Name": "test.example.com",
|
|
"Type": "A",
|
|
"TTL": 300,
|
|
"ResourceRecords": [{"Value": "1.2.3.4"}],
|
|
},
|
|
}
|
|
],
|
|
},
|
|
)
|
|
|
|
def test_update_ipv4_raises_on_error(self) -> None:
|
|
mock_client = MagicMock()
|
|
mock_client.change_resource_record_sets.side_effect = Exception("API error")
|
|
|
|
with patch("ddns.route53_update._get_route53_client", return_value=mock_client):
|
|
with pytest.raises(Exception, match="API error"):
|
|
update_ipv4("ZONE123", "test.example.com", "1.2.3.4")
|
|
|
|
|
|
class TestUpdateIpv6:
|
|
def test_update_ipv6_success(self) -> None:
|
|
mock_client = MagicMock()
|
|
mock_client.change_resource_record_sets.return_value = {}
|
|
|
|
with patch("ddns.route53_update._get_route53_client", return_value=mock_client):
|
|
update_ipv6("ZONE123", "test.example.com", "2001:db8::1")
|
|
|
|
mock_client.change_resource_record_sets.assert_called_once_with(
|
|
HostedZoneId="ZONE123",
|
|
ChangeBatch={
|
|
"Comment": "Update Public Addresses",
|
|
"Changes": [
|
|
{
|
|
"Action": "UPSERT",
|
|
"ResourceRecordSet": {
|
|
"Name": "test.example.com",
|
|
"Type": "AAAA",
|
|
"TTL": 300,
|
|
"ResourceRecords": [{"Value": "2001:db8::1"}],
|
|
},
|
|
}
|
|
],
|
|
},
|
|
)
|
|
|
|
def test_update_ipv6_raises_on_error(self) -> None:
|
|
mock_client = MagicMock()
|
|
mock_client.change_resource_record_sets.side_effect = Exception("API error")
|
|
|
|
with patch("ddns.route53_update._get_route53_client", return_value=mock_client):
|
|
with pytest.raises(Exception, match="API error"):
|
|
update_ipv6("ZONE123", "test.example.com", "2001:db8::1")
|