Skip to content

Commit 4796be6

Browse files
authored
Fix: HexadecimalField accepts non-hex values (jazzband#672)
Fixes jazzband#671
1 parent a208192 commit 4796be6

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

push_notifications/fields.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
UNSIGNED_64BIT_INT_MAX_VALUE = 2 ** 64 - 1
1414

1515

16-
hex_re = re.compile(r"^(([0-9A-f])|(0x[0-9A-f]))+$")
16+
hex_re = re.compile(r"^(0x)?([0-9a-f])+$", re.I)
1717
signed_integer_vendors = [
1818
"postgresql",
1919
"sqlite",

tests/test_fields.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from django.core.exceptions import ValidationError
2+
from django.test import SimpleTestCase
3+
4+
from push_notifications.fields import HexadecimalField
5+
6+
7+
class HexadecimalFieldTestCase(SimpleTestCase):
8+
_INVALID_HEX_VALUES = [
9+
"foobar",
10+
"GLUTEN",
11+
"HeLLo WoRLd",
12+
"international",
13+
"°!#€%&/()[]{}=?",
14+
"0x",
15+
]
16+
17+
_VALID_HEX_VALUES = {
18+
"babe": "babe",
19+
"BEEF": "BEEF",
20+
" \nfeed \t": "feed",
21+
"0x012345789abcdef": "0x012345789abcdef",
22+
"012345789aBcDeF": "012345789aBcDeF",
23+
}
24+
25+
def test_clean_invalid_values(self):
26+
"""Passing invalid values raises ValidationError."""
27+
f = HexadecimalField()
28+
for invalid in self._INVALID_HEX_VALUES:
29+
self.assertRaisesMessage(
30+
ValidationError,
31+
"'Enter a valid hexadecimal number'",
32+
f.clean,
33+
invalid,
34+
)
35+
36+
def test_clean_valid_values(self):
37+
"""Passing valid values returns the expected output."""
38+
f = HexadecimalField()
39+
for valid, expected in self._VALID_HEX_VALUES.items():
40+
self.assertEqual(expected, f.clean(valid))

0 commit comments

Comments
 (0)