Merge lp:~trapnine/maas/fix-1499062 into lp:~maas-committers/maas/trunk

Proposed by Jeffrey C Jones
Status: Merged
Approved by: Jeffrey C Jones
Approved revision: no longer in the source branch.
Merged at revision: 4389
Proposed branch: lp:~trapnine/maas/fix-1499062
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 102 lines (+50/-2)
3 files modified
src/maasserver/fields.py (+8/-0)
src/maasserver/forms.py (+3/-2)
src/maasserver/tests/test_forms_settings.py (+39/-0)
To merge this branch: bzr merge lp:~trapnine/maas/fix-1499062
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+274980@code.launchpad.net

Commit message

Fix lp:1499062. Use URLField for boot_source_url, strip whitespace on boot_source_keyring in BootSourceSettingsForm.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Code itself looks good. The unit testing needs to be improved. Overall you have good coverage just the format and how the tests are being done need to be improved. Comments inline about the testing.

review: Needs Fixing
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Almost there. Just need to split out the tests.

review: Needs Fixing
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Looks good. Thanks for the fixes.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/maasserver/fields.py'
--- src/maasserver/fields.py 2015-10-07 05:01:23 +0000
+++ src/maasserver/fields.py 2015-10-20 21:36:17 +0000
@@ -200,6 +200,14 @@
200 return nodegroup200 return nodegroup
201201
202202
203class StrippedCharField(CharField):
204 """A CharField that will strip surrounding whitespace before validation."""
205
206 def clean(self, value):
207 value = self.to_python(value).strip()
208 return super(CharField, self).clean(value)
209
210
203class VerboseRegexField(CharField):211class VerboseRegexField(CharField):
204212
205 def __init__(self, regex, message, *args, **kwargs):213 def __init__(self, regex, message, *args, **kwargs):
206214
=== modified file 'src/maasserver/forms.py'
--- src/maasserver/forms.py 2015-09-24 16:22:12 +0000
+++ src/maasserver/forms.py 2015-10-20 21:36:17 +0000
@@ -105,6 +105,7 @@
105 LargeObjectFile,105 LargeObjectFile,
106 MACAddressFormField,106 MACAddressFormField,
107 NodeGroupFormField,107 NodeGroupFormField,
108 StrippedCharField,
108)109)
109from maasserver.forms_settings import (110from maasserver.forms_settings import (
110 CONFIG_ITEMS_KEYS,111 CONFIG_ITEMS_KEYS,
@@ -1380,7 +1381,7 @@
1380 config_fields = ['boot_images_auto_import']1381 config_fields = ['boot_images_auto_import']
13811382
1382 boot_images_auto_import = get_config_field('boot_images_auto_import')1383 boot_images_auto_import = get_config_field('boot_images_auto_import')
1383 boot_source_url = forms.CharField(1384 boot_source_url = forms.URLField(
1384 label="Sync URL", required=True,1385 label="Sync URL", required=True,
1385 help_text=(1386 help_text=(
1386 "URL to sync boot image from. E.g. "1387 "URL to sync boot image from. E.g. "
@@ -1396,7 +1397,7 @@
1396 keyring_data."""1397 keyring_data."""
1397 boot_source = BootSource.objects.first()1398 boot_source = BootSource.objects.first()
1398 if boot_source is None or len(boot_source.keyring_data) == 0:1399 if boot_source is None or len(boot_source.keyring_data) == 0:
1399 self.fields['boot_source_keyring'] = forms.CharField(1400 self.fields['boot_source_keyring'] = StrippedCharField(
1400 label="Keyring Path", required=True,1401 label="Keyring Path", required=True,
1401 help_text=(1402 help_text=(
1402 "Path to the keyring to validate the sync URL. E.g. "1403 "Path to the keyring to validate the sync URL. E.g. "
14031404
=== modified file 'src/maasserver/tests/test_forms_settings.py'
--- src/maasserver/tests/test_forms_settings.py 2015-07-01 19:02:36 +0000
+++ src/maasserver/tests/test_forms_settings.py 2015-10-20 21:36:17 +0000
@@ -16,6 +16,7 @@
1616
1717
18from django import forms18from django import forms
19from maasserver.forms import BootSourceSettingsForm
19from maasserver.forms_settings import (20from maasserver.forms_settings import (
20 CONFIG_ITEMS,21 CONFIG_ITEMS,
21 get_config_doc,22 get_config_doc,
@@ -78,3 +79,41 @@
78 ips2 = [factory.make_ip_address() for _ in range(3)]79 ips2 = [factory.make_ip_address() for _ in range(3)]
79 input = ' '.join(ips1) + ' ' + ','.join(ips2)80 input = ' '.join(ips1) + ' ' + ','.join(ips2)
80 self.assertEqual(' '.join(ips1 + ips2), field.clean(input))81 self.assertEqual(' '.join(ips1 + ips2), field.clean(input))
82
83
84class TestBootSourceSettingsForm(MAASServerTestCase):
85
86 def setUp(self):
87 super(TestBootSourceSettingsForm, self).setUp()
88 self.form_data = {
89 'boot_source_url': 'http://example.com/good',
90 'boot_source_keyring': '/a/path'}
91
92 def test_happy_with_good_data(self):
93 form = BootSourceSettingsForm(data=self.form_data)
94 self.assertTrue(form.is_valid())
95 self.assertEqual(
96 "http://example.com/good",
97 form.cleaned_data['boot_source_url'])
98 self.assertEqual("/a/path", form.cleaned_data['boot_source_keyring'])
99
100 def test_unhappy_by_default(self):
101 form = BootSourceSettingsForm()
102 self.assertFalse(form.is_valid())
103
104 def test_reject_leading_spaces_in_boot_source_url(self):
105 # https://bugs.launchpad.net/maas/+bug/1499062
106 self.form_data['boot_source_url'] = ' http://example.com/leadingspace'
107 form = BootSourceSettingsForm(data=self.form_data)
108 self.assertFalse(form.is_valid())
109
110 def test_reject_non_url_in_boot_source_url(self):
111 self.form_data['boot_source_url'] = 'not_a_URL'
112 form = BootSourceSettingsForm(data=self.form_data)
113 self.assertFalse(form.is_valid())
114
115 def test_strips_boot_source_keyring(self):
116 self.form_data['boot_source_keyring'] = ' /a/path '
117 form = BootSourceSettingsForm(data=self.form_data)
118 self.assertTrue(form.is_valid())
119 self.assertEqual("/a/path", form.cleaned_data['boot_source_keyring'])