Validate UUID for tankerkoenig (#32805)

* Validate UUIDs against custom validator

Instead of just validating against strings, use a custom validator,
so that the format can be checked.

* Add tests for custom UUID4 validator
This commit is contained in:
guillempages authored and GitHub committed 2020-03-21 09:43:12 -05:00
1 parent ebc4804e04
commit 49c2a4a4e3
4 files changed
+54 -2

No files matched your search

+1
View File
@@ -0,0 +1 @@
"""Tests for the tankerkoenig integration."""
@@ -0,0 +1,32 @@
"""The tests for the custom tankerkoenig validators."""
import unittest
import uuid
import pytest
import voluptuous as vol
from homeassistant.components.tankerkoenig import uuid4_string
class TestUUID4StringValidator(unittest.TestCase):
"""Test the UUID4 string custom validator."""
def test_uuid4_string(caplog):
"""Test string uuid validation."""
schema = vol.Schema(uuid4_string)
for value in ["Not a hex string", "0", 0]:
with pytest.raises(vol.Invalid):
schema(value)
with pytest.raises(vol.Invalid):
# the third block should start with 4
schema("a03d31b2-2eee-2acc-bb90-eec40be6ed23")
with pytest.raises(vol.Invalid):
# the fourth block should start with 8-a
schema("a03d31b2-2eee-4acc-1b90-eec40be6ed23")
_str = str(uuid.uuid4())
assert schema(_str) == _str
assert schema(_str.upper()) == _str