File: test_functional.py

package info (click to toggle)
freedombox 26.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,092 kB
  • sloc: python: 48,542; javascript: 1,730; xml: 481; makefile: 290; sh: 137; php: 32
file content (158 lines) | stat: -rw-r--r-- 5,239 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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',
        'use_ipv6': True,
    },
    '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': '',
        'use_ipv6': False,
    },
    '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',
        'use_ipv6': False,
    },
}


@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,
                                  '/plinth/sys/dynamicdns/domain/add/')
    for key, value in config.items():
        field_id = f'id_domain-{key}'
        if key == 'service_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'/plinth/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'/plinth/sys/dynamicdns/domain/{domain}/delete/'
    functional.click_link_by_href(browser, link)
    functional.submit(browser, form_class='form-delete')