mirror of
https://github.com/home-assistant/core.git
synced 2025-07-08 22:07:10 +00:00
Prevent invalid context from crashing (#21231)
* Prevent invalid context from crashing * Lint
This commit is contained in:
parent
27d598fff8
commit
cf3a8b60ff
@ -744,7 +744,10 @@ class State:
|
|||||||
|
|
||||||
context = json_dict.get('context')
|
context = json_dict.get('context')
|
||||||
if context:
|
if context:
|
||||||
context = Context(**context)
|
context = Context(
|
||||||
|
id=context.get('id'),
|
||||||
|
user_id=context.get('user_id'),
|
||||||
|
)
|
||||||
|
|
||||||
return cls(json_dict['entity_id'], json_dict['state'],
|
return cls(json_dict['entity_id'], json_dict['state'],
|
||||||
json_dict.get('attributes'), last_changed, last_updated,
|
json_dict.get('attributes'), last_changed, last_updated,
|
||||||
|
@ -460,61 +460,76 @@ class TestEventBus(unittest.TestCase):
|
|||||||
assert len(coroutine_calls) == 1
|
assert len(coroutine_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
class TestState(unittest.TestCase):
|
def test_state_init():
|
||||||
"""Test State methods."""
|
"""Test state.init."""
|
||||||
|
with pytest.raises(InvalidEntityFormatError):
|
||||||
|
ha.State('invalid_entity_format', 'test_state')
|
||||||
|
|
||||||
def test_init(self):
|
with pytest.raises(InvalidStateError):
|
||||||
"""Test state.init."""
|
ha.State('domain.long_state', 't' * 256)
|
||||||
with pytest.raises(InvalidEntityFormatError):
|
|
||||||
ha.State('invalid_entity_format', 'test_state')
|
|
||||||
|
|
||||||
with pytest.raises(InvalidStateError):
|
|
||||||
ha.State('domain.long_state', 't' * 256)
|
|
||||||
|
|
||||||
def test_domain(self):
|
def test_state_domain():
|
||||||
"""Test domain."""
|
"""Test domain."""
|
||||||
state = ha.State('some_domain.hello', 'world')
|
state = ha.State('some_domain.hello', 'world')
|
||||||
assert 'some_domain' == state.domain
|
assert 'some_domain' == state.domain
|
||||||
|
|
||||||
def test_object_id(self):
|
|
||||||
"""Test object ID."""
|
|
||||||
state = ha.State('domain.hello', 'world')
|
|
||||||
assert 'hello' == state.object_id
|
|
||||||
|
|
||||||
def test_name_if_no_friendly_name_attr(self):
|
def test_state_object_id():
|
||||||
"""Test if there is no friendly name."""
|
"""Test object ID."""
|
||||||
state = ha.State('domain.hello_world', 'world')
|
state = ha.State('domain.hello', 'world')
|
||||||
assert 'hello world' == state.name
|
assert 'hello' == state.object_id
|
||||||
|
|
||||||
def test_name_if_friendly_name_attr(self):
|
|
||||||
"""Test if there is a friendly name."""
|
|
||||||
name = 'Some Unique Name'
|
|
||||||
state = ha.State('domain.hello_world', 'world',
|
|
||||||
{ATTR_FRIENDLY_NAME: name})
|
|
||||||
assert name == state.name
|
|
||||||
|
|
||||||
def test_dict_conversion(self):
|
def test_state_name_if_no_friendly_name_attr():
|
||||||
"""Test conversion of dict."""
|
"""Test if there is no friendly name."""
|
||||||
state = ha.State('domain.hello', 'world', {'some': 'attr'})
|
state = ha.State('domain.hello_world', 'world')
|
||||||
assert state == ha.State.from_dict(state.as_dict())
|
assert 'hello world' == state.name
|
||||||
|
|
||||||
def test_dict_conversion_with_wrong_data(self):
|
|
||||||
"""Test conversion with wrong data."""
|
|
||||||
assert ha.State.from_dict(None) is None
|
|
||||||
assert ha.State.from_dict({'state': 'yes'}) is None
|
|
||||||
assert ha.State.from_dict({'entity_id': 'yes'}) is None
|
|
||||||
|
|
||||||
def test_repr(self):
|
def test_state_name_if_friendly_name_attr():
|
||||||
"""Test state.repr."""
|
"""Test if there is a friendly name."""
|
||||||
assert "<state happy.happy=on @ 1984-12-08T12:00:00+00:00>" == \
|
name = 'Some Unique Name'
|
||||||
str(ha.State(
|
state = ha.State('domain.hello_world', 'world',
|
||||||
"happy.happy", "on",
|
{ATTR_FRIENDLY_NAME: name})
|
||||||
last_changed=datetime(1984, 12, 8, 12, 0, 0)))
|
assert name == state.name
|
||||||
|
|
||||||
assert "<state happy.happy=on; brightness=144 @ " \
|
|
||||||
"1984-12-08T12:00:00+00:00>" == \
|
def test_state_dict_conversion():
|
||||||
str(ha.State("happy.happy", "on", {"brightness": 144},
|
"""Test conversion of dict."""
|
||||||
datetime(1984, 12, 8, 12, 0, 0)))
|
state = ha.State('domain.hello', 'world', {'some': 'attr'})
|
||||||
|
assert state == ha.State.from_dict(state.as_dict())
|
||||||
|
|
||||||
|
|
||||||
|
def test_state_dict_conversion_with_wrong_data():
|
||||||
|
"""Test conversion with wrong data."""
|
||||||
|
assert ha.State.from_dict(None) is None
|
||||||
|
assert ha.State.from_dict({'state': 'yes'}) is None
|
||||||
|
assert ha.State.from_dict({'entity_id': 'yes'}) is None
|
||||||
|
# Make sure invalid context data doesn't crash
|
||||||
|
wrong_context = ha.State.from_dict({
|
||||||
|
'entity_id': 'light.kitchen',
|
||||||
|
'state': 'on',
|
||||||
|
'context': {
|
||||||
|
'id': '123',
|
||||||
|
'non-existing': 'crash'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
assert wrong_context is not None
|
||||||
|
assert wrong_context.context.id == '123'
|
||||||
|
|
||||||
|
|
||||||
|
def test_state_repr():
|
||||||
|
"""Test state.repr."""
|
||||||
|
assert "<state happy.happy=on @ 1984-12-08T12:00:00+00:00>" == \
|
||||||
|
str(ha.State(
|
||||||
|
"happy.happy", "on",
|
||||||
|
last_changed=datetime(1984, 12, 8, 12, 0, 0)))
|
||||||
|
|
||||||
|
assert "<state happy.happy=on; brightness=144 @ " \
|
||||||
|
"1984-12-08T12:00:00+00:00>" == \
|
||||||
|
str(ha.State("happy.happy", "on", {"brightness": 144},
|
||||||
|
datetime(1984, 12, 8, 12, 0, 0)))
|
||||||
|
|
||||||
|
|
||||||
class TestStateMachine(unittest.TestCase):
|
class TestStateMachine(unittest.TestCase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user