Jeroen T. Vermeulen has proposed merging lp:~jtv/maas/bug-962101 into lp:maas.

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)
Related bugs:
  Bug #962101 in MAAS: "MAC address validation should strip preceding and 
trailing spaces"
  https://bugs.launchpad.net/maas/+bug/962101

For more details, see:
https://code.launchpad.net/~jtv/maas/bug-962101/+merge/99257

Turns out all this took was to make the validator regex for MAC form fields 
accept whitespace.  It gets stripped automatically.

And since we store MAC addresses using postgres's native MAC type, this won't 
upset the uniqueness check either.  So an address “ aa-bb-cc-dd-ee-ff ” (note 
whitespace) will be accepted by the validator — but rejected by the form if 
there already is a node with a MAC address “aa-bb-cc-dd-ee-ff” in the database.
-- 
https://code.launchpad.net/~jtv/maas/bug-962101/+merge/99257
Your team Launchpad code reviewers is requested to review the proposed merge of 
lp:~jtv/maas/bug-962101 into lp:maas.
=== modified file 'src/maasserver/fields.py'
--- src/maasserver/fields.py	2012-03-19 17:05:35 +0000
+++ src/maasserver/fields.py	2012-03-26 06:03:17 +0000
@@ -31,7 +31,7 @@
 from south.modelsinspector import add_introspection_rules
 
 
-mac_re = re.compile(r'^([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$')
+mac_re = re.compile(r'^\s*([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}\s*$')
 
 
 mac_error_msg = "Enter a valid MAC address (e.g. AA:BB:CC:DD:EE:FF)."
@@ -46,9 +46,10 @@
 # See http://south.aeracode.org/docs/customfields.html#extending-introspection
 # for details.
 add_introspection_rules(
-    [],
-    ["^maasserver\.fields\.MACAddressField",
-     "^maasserver\.fields\.JSONObjectField"])
+    [], [
+        "^maasserver\.fields\.MACAddressField",
+        "^maasserver\.fields\.JSONObjectField",
+    ])
 
 
 class MACAddressFormField(RegexField):

=== modified file 'src/maasserver/tests/test_fields.py'
--- src/maasserver/tests/test_fields.py	2012-03-13 05:34:38 +0000
+++ src/maasserver/tests/test_fields.py	2012-03-26 06:03:17 +0000
@@ -25,7 +25,7 @@
 class TestMACAddressField(TestCase):
 
     def test_mac_address_is_stored_normalized_and_loaded(self):
-        stored_mac = factory.make_mac_address('AA-bb-CC-dd-EE-Ff')
+        stored_mac = factory.make_mac_address(' AA-bb-CC-dd-EE-Ff ')
         stored_mac.save()
         loaded_mac = MACAddress.objects.get(id=stored_mac.id)
         self.assertEqual('aa:bb:cc:dd:ee:ff', loaded_mac.mac_address)
@@ -45,6 +45,11 @@
         # No error.
         pass
 
+    def test_accepts_leading_and_trailing_whitespace(self):
+        validate_mac(' AA:BB:CC:DD:EE:FF ')
+        # No error.
+        pass
+
     def test_rejects_short_mac(self):
         self.assertRaises(ValidationError, validate_mac, '00:11:22:33:44')
 

_______________________________________________
Mailing list: https://launchpad.net/~launchpad-reviewers
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~launchpad-reviewers
More help   : https://help.launchpad.net/ListHelp

Reply via email to