Update config form for hostnames and domainnames

- Make minimum length 1 for the fields.

- Write tests.

- Update description to reflect the new conditions.
This commit is contained in:
Sunil Mohan Adapa 2015-11-15 10:59:27 +05:30
parent 45fff7057b
commit c985fdae56
2 changed files with 88 additions and 12 deletions

View File

@ -59,28 +59,39 @@ class TrimmedCharField(forms.CharField):
class ConfigurationForm(forms.Form):
"""Main system configuration form"""
# We're more conservative than RFC 952 and RFC 1123
# See:
# https://tools.ietf.org/html/rfc952
# https://tools.ietf.org/html/rfc1035#section-2.3.1
# https://tools.ietf.org/html/rfc1123#section-2
# https://tools.ietf.org/html/rfc2181#section-11
hostname = TrimmedCharField(
label=ugettext_lazy('Hostname'),
help_text=\
ugettext_lazy('Your hostname is the local name by which other machines '
'on your LAN can reach you. It must be alphanumeric, '
'start with an alphabet and must not be greater than 63 '
'characters in length.'),
ugettext_lazy('Hostname is the local name by which other machines on '
'the local network reach your machine. It must start '
'and end with an alphabet or a digit and have as '
'interior characters only alphabets, digits and '
'hyphens. Total length must be 63 characters or less.'),
validators=[
validators.RegexValidator(r'^[a-zA-Z][-a-zA-Z0-9]{,61}[a-zA-Z0-9]$',
ugettext_lazy('Invalid hostname'))])
validators.RegexValidator(
r'^[a-zA-Z0-9]([-a-zA-Z0-9]{,61}[a-zA-Z0-9])?$',
ugettext_lazy('Invalid hostname'))])
domainname = TrimmedCharField(
label=ugettext_lazy('Domain Name'),
help_text=\
ugettext_lazy('Your domain name is the global name by which other '
'machines on the Internet can reach you. It must consist '
'of alphanumeric words separated by dots.'),
ugettext_lazy('Domain name is the global name by which other machines '
'on the Internet can reach you. It must consist of '
'labels separated by dots. Each label must start and '
'end with an alphabet or a digit and have as interior '
'characters only alphabets, digits and hyphens. Length '
'of each label must be 63 characters or less. Total '
'length of domain name must be 253 characters or less.'),
required=False,
validators=[
validators.RegexValidator(r'^[a-zA-Z][-a-zA-Z0-9.]*[a-zA-Z0-9]$',
ugettext_lazy('Invalid domain name'))])
validators.RegexValidator(
r'^[a-zA-Z0-9]([-a-zA-Z0-9.]*[a-zA-Z0-9])?$',
ugettext_lazy('Invalid domain name'))])
def init():

View File

@ -0,0 +1,65 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Tests for config module.
"""
import unittest
from ..config import ConfigurationForm
class TestConfig(unittest.TestCase):
"""Test cases for testing the config module."""
def test_hostname_field(self):
"""Test that hostname field accepts only valid hostnames."""
valid_hostnames = [
'a', '0a', 'a0', 'AAA', '00', '0-0', 'example-hostname', 'example',
'012345678901234567890123456789012345678901234567890123456789012']
invalid_hostnames = [
'', '-', '-a', 'a-', '.a', 'a.', 'a.a', '?', 'a?a',
'0123456789012345678901234567890123456789012345678901234567890123']
for hostname in valid_hostnames:
form = ConfigurationForm({'hostname': hostname,
'domainname': 'example.com'})
self.assertTrue(form.is_valid())
for hostname in invalid_hostnames:
form = ConfigurationForm({'hostname': hostname,
'domainname': 'example.com'})
self.assertFalse(form.is_valid())
def test_domainname_field(self):
"""Test that domainname field accepts only valid domainnames."""
valid_domainnames = [
'', 'a', '0a', 'a0', 'AAA', '00', '0-0', 'example-hostname',
'example', 'example.org', 'a.b.c.d', 'a-0.b-0.c-0',
'012345678901234567890123456789012345678901234567890123456789012']
invalid_domainnames = [
'-', '-a', 'a-', '.a', 'a.', '?', 'a?a']
for domainname in valid_domainnames:
form = ConfigurationForm({'hostname': 'example',
'domainname': domainname})
self.assertTrue(form.is_valid())
for domainname in invalid_domainnames:
form = ConfigurationForm({'hostname': 'example',
'domainname': domainname})
self.assertFalse(form.is_valid())