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
1=== modified file 'src/maasserver/fields.py'
2--- src/maasserver/fields.py 2015-10-07 05:01:23 +0000
3+++ src/maasserver/fields.py 2015-10-20 21:36:17 +0000
4@@ -200,6 +200,14 @@
5 return nodegroup
6
7
8+class StrippedCharField(CharField):
9+ """A CharField that will strip surrounding whitespace before validation."""
10+
11+ def clean(self, value):
12+ value = self.to_python(value).strip()
13+ return super(CharField, self).clean(value)
14+
15+
16 class VerboseRegexField(CharField):
17
18 def __init__(self, regex, message, *args, **kwargs):
19
20=== modified file 'src/maasserver/forms.py'
21--- src/maasserver/forms.py 2015-09-24 16:22:12 +0000
22+++ src/maasserver/forms.py 2015-10-20 21:36:17 +0000
23@@ -105,6 +105,7 @@
24 LargeObjectFile,
25 MACAddressFormField,
26 NodeGroupFormField,
27+ StrippedCharField,
28 )
29 from maasserver.forms_settings import (
30 CONFIG_ITEMS_KEYS,
31@@ -1380,7 +1381,7 @@
32 config_fields = ['boot_images_auto_import']
33
34 boot_images_auto_import = get_config_field('boot_images_auto_import')
35- boot_source_url = forms.CharField(
36+ boot_source_url = forms.URLField(
37 label="Sync URL", required=True,
38 help_text=(
39 "URL to sync boot image from. E.g. "
40@@ -1396,7 +1397,7 @@
41 keyring_data."""
42 boot_source = BootSource.objects.first()
43 if boot_source is None or len(boot_source.keyring_data) == 0:
44- self.fields['boot_source_keyring'] = forms.CharField(
45+ self.fields['boot_source_keyring'] = StrippedCharField(
46 label="Keyring Path", required=True,
47 help_text=(
48 "Path to the keyring to validate the sync URL. E.g. "
49
50=== modified file 'src/maasserver/tests/test_forms_settings.py'
51--- src/maasserver/tests/test_forms_settings.py 2015-07-01 19:02:36 +0000
52+++ src/maasserver/tests/test_forms_settings.py 2015-10-20 21:36:17 +0000
53@@ -16,6 +16,7 @@
54
55
56 from django import forms
57+from maasserver.forms import BootSourceSettingsForm
58 from maasserver.forms_settings import (
59 CONFIG_ITEMS,
60 get_config_doc,
61@@ -78,3 +79,41 @@
62 ips2 = [factory.make_ip_address() for _ in range(3)]
63 input = ' '.join(ips1) + ' ' + ','.join(ips2)
64 self.assertEqual(' '.join(ips1 + ips2), field.clean(input))
65+
66+
67+class TestBootSourceSettingsForm(MAASServerTestCase):
68+
69+ def setUp(self):
70+ super(TestBootSourceSettingsForm, self).setUp()
71+ self.form_data = {
72+ 'boot_source_url': 'http://example.com/good',
73+ 'boot_source_keyring': '/a/path'}
74+
75+ def test_happy_with_good_data(self):
76+ form = BootSourceSettingsForm(data=self.form_data)
77+ self.assertTrue(form.is_valid())
78+ self.assertEqual(
79+ "http://example.com/good",
80+ form.cleaned_data['boot_source_url'])
81+ self.assertEqual("/a/path", form.cleaned_data['boot_source_keyring'])
82+
83+ def test_unhappy_by_default(self):
84+ form = BootSourceSettingsForm()
85+ self.assertFalse(form.is_valid())
86+
87+ def test_reject_leading_spaces_in_boot_source_url(self):
88+ # https://bugs.launchpad.net/maas/+bug/1499062
89+ self.form_data['boot_source_url'] = ' http://example.com/leadingspace'
90+ form = BootSourceSettingsForm(data=self.form_data)
91+ self.assertFalse(form.is_valid())
92+
93+ def test_reject_non_url_in_boot_source_url(self):
94+ self.form_data['boot_source_url'] = 'not_a_URL'
95+ form = BootSourceSettingsForm(data=self.form_data)
96+ self.assertFalse(form.is_valid())
97+
98+ def test_strips_boot_source_keyring(self):
99+ self.form_data['boot_source_keyring'] = ' /a/path '
100+ form = BootSourceSettingsForm(data=self.form_data)
101+ self.assertTrue(form.is_valid())
102+ self.assertEqual("/a/path", form.cleaned_data['boot_source_keyring'])