Sunil Mohan Adapa 62deda0ec7
dynamicdns: Allow updating both IPv4 and IPv6 records together
- Change the IPv6 checkbox into an option to select from IPv4 only, IPv6 only,
or IPv4 and IPv6. Update functional tests.

- Add type hints everywhere.

- Separate out the generic URL update method to its own module. Add unit tests.

- Migrate old configuration to new configuration on first read.

- Store multiple IP addresses in the status. Support showing old status format
of single IP address as well. Show multiple IP addresses in the status table.

- Handle errors during update using exceptions for clearer code.

- For generic URL based updates, capture return code, stdout and stdin in error
message.

Tests:

- Adding a new GnuDIP domain leads to configuration setting ip_type = ipv4

- When adding a new dynamic domain, when generic option is selected IP Address
  Type is shown. When GnuDIP is selected, the field is hidden.

- IP Address Type field is as expected. Options and description are as expected.

- When a new domain is added for generic domain, the value of ip_type is set as
  expected. Changing the value to other values works. Configuration is updated
  as expected. Option values and description are as expected.

- The column for IP Address is now 'IP Addresses'. When multiple IP addresses
  are present in the status, they are shown in multiple lines in the tables as
  <pre>. When no IP addresses are present, a '-' is shown in the column.

- When generic domain update fails, subprocess failure code, stderr and stdout
  are shown in the error message.

- Unit tests work.

- Functional tests work.

- After 5 minute period, attempts are made to update the domain

- For GnuDIP and generic method:

  - IP address lookup works for IPv4 and IPv6.

  - IP address updates works for IPv4 (tested) and IPv6 (untested).

  - In 'both' configuration, when IPv6 update fails, IPv4 still succeeds.

  - Updates are skipped if the record is up-to-date.

  - Successful updates are performed otherwise.

  - Wrong password leads to proper error being shown.

- When the old status is stored, it is updated during read and proper status is
  shown.

- When configuration has old use_ipv6 key, then it is migrated when newer
  version of the service is started. True value is converted as 'ipv6', False
  value is converted as 'ipv4'. 'null' key is properly removed.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
2026-08-15 08:49:43 -04:00

159 lines
5.1 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Functional, browser based tests for dynamicdns app.
"""
import pytest
from plinth.tests import functional
pytestmark = [
pytest.mark.system, pytest.mark.essential, pytest.mark.domain,
pytest.mark.dynamicdns
]
_configs = {
'gnudip1': {
'service_type': 'gnudip',
'server': 'localhost',
'domain': 'freedombox.example.com',
'username': 'tester',
'password': 'testingtesting',
},
'gnudip2': {
'service_type': 'gnudip',
'server': '127.0.0.1',
'domain': 'freedombox2.example.com',
'username': 'tester2',
'password': 'testingtesting2',
},
'noip.com': {
'service_type': 'noip.com',
'update_url': 'https://localhost/update3/',
'disable_ssl_cert_check': True,
'use_http_basic_auth': True,
'domain': 'freedombox3.example.com',
'username': 'tester3',
'password': 'testingtesting3',
'ip_type': 'ipv6',
},
'freedns.afraid.org': {
'service_type': 'freedns.afraid.org',
'update_url': 'https://localhost/update5/',
'disable_ssl_cert_check': False,
'use_http_basic_auth': False,
'domain': 'freedombox5.example.com',
'username': '',
'password': '',
'ip_type': 'ipv4',
},
'other': {
'service_type': 'other',
'update_url': 'https://localhost/update6/',
'disable_ssl_cert_check': False,
'use_http_basic_auth': False,
'domain': 'freedombox6.example.com',
'username': 'tester6',
'password': 'testingtesting6',
'ip_type': 'both',
},
}
@pytest.fixture(scope='module', autouse=True)
def fixture_background(session_browser):
"""Login."""
functional.login(session_browser)
class TestDynamicDNSApp(functional.BaseAppTests):
app_name = 'dynamicdns'
has_service = False
has_web = False
can_uninstall = False
check_diagnostics = False
@staticmethod
def test_capitalized_domain_name(session_browser):
"""Test handling of capitalized domain name."""
config = dict(_configs['gnudip1'], domain='FreedomBox.example.com')
_configure(session_browser, config)
_assert_has_config(session_browser,
{'domain': 'freedombox.example.com'})
@staticmethod
@pytest.mark.parametrize('config_name', list(_configs.keys()))
def test_various_form_values(session_browser, config_name):
"""Test feeding various values and check that they are saved."""
_configure(session_browser, _configs[config_name])
_assert_has_config(session_browser, _configs[config_name])
@staticmethod
@pytest.mark.backups
def test_backup_restore(session_browser):
"""Test backup and restore of configuration."""
_configure(session_browser, _configs['gnudip1'])
functional.backup_create(session_browser, 'dynamicdns',
'test_dynamicdns')
_configure(session_browser, _configs['gnudip2'])
functional.backup_restore(session_browser, 'dynamicdns',
'test_dynamicdns')
_assert_has_config(session_browser, _configs['gnudip1'])
def _configure(browser, config):
functional.nav_to_module(browser, 'dynamicdns')
current_domains = _get_domains(browser)
for domain in current_domains:
if domain.endswith('.example.com'):
_delete_domain(browser, domain)
functional.nav_to_module(browser, 'dynamicdns')
functional.click_link_by_href(browser,
'/freedombox/sys/dynamicdns/domain/add/')
for key, value in config.items():
field_id = f'id_domain-{key}'
if key in ('service_type', 'ip_type'):
browser.find_by_id(field_id).select(value)
elif isinstance(value, bool):
if value:
browser.find_by_id(field_id).check()
else:
browser.find_by_id(field_id).uncheck()
else:
browser.find_by_id(field_id).fill(value)
functional.submit(browser, form_class='form-domain')
def _assert_has_config(browser, config):
functional.nav_to_module(browser, 'dynamicdns')
link = f'/freedombox/sys/dynamicdns/domain/{config["domain"]}/edit/'
functional.click_link_by_href(browser, link)
for key, value in config.items():
if key == 'password':
continue
field_id = f'id_domain-{key}'
if isinstance(value, bool):
assert browser.find_by_id(field_id).checked == value
else:
assert value == browser.find_by_id(field_id).value
def _get_domains(browser):
"""Return the list of configured domains."""
functional.nav_to_module(browser, 'dynamicdns')
elements = browser.find_by_css('.domains-status .domain-name a')
return [element.text.strip() for element in elements]
def _delete_domain(browser, domain):
"""Delete a given domain."""
functional.nav_to_module(browser, 'dynamicdns')
link = f'/freedombox/sys/dynamicdns/domain/{domain}/delete/'
functional.click_link_by_href(browser, link)
functional.submit(browser, form_class='form-delete')