Skip to content

Raise ValueError in get_db_prep_value() when value is of invalid length #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion uuidfield/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ def get_db_prep_value(self, value, connection, prepared=False):
value = str(value)
if isinstance(value, str):
if '-' in value:
return value.replace('-', '')
value = value.replace('-', '')
uuid.UUID(value) # raises ValueError with invalid UUID format
return value

def value_to_string(self, obj):
Expand Down
18 changes: 18 additions & 0 deletions uuidfield/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ def test_namespace(self):
self.assertTrue(isinstance(obj.uuid, uuid.UUID))
self.assertEquals(obj.uuid.version, 5)

def test_long_uuid(self):
invalid_uuid = "1" * 33
self.assertRaises(ValueError, AutoUUIDField.objects.get, uuid=invalid_uuid)
q = AutoUUIDField.objects.filter(uuid=invalid_uuid)
self.assertRaises(ValueError, list, q)

def test_short_uuid(self):
invalid_uuid = "1" * 31
self.assertRaises(ValueError, AutoUUIDField.objects.get, uuid=invalid_uuid)
q = AutoUUIDField.objects.filter(uuid=invalid_uuid)
self.assertRaises(ValueError, list, q)

def test_invalid_hex(self):
invalid_uuid = 'z' * 32
self.assertRaises(ValueError, AutoUUIDField.objects.get, uuid=invalid_uuid)
q = AutoUUIDField.objects.filter(uuid=invalid_uuid)
self.assertRaises(ValueError, list, q)

def test_broken_namespace(self):
self.assertRaises(ValueError, BrokenNamespaceUUIDField.objects.create)

Expand Down