1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
"""Test FibaroStateHandler class."""
import time
import pytest
import requests_mock
from pyfibaro.fibaro_client import FibaroClient
from .test_utils import (TEST_BASE_URL, TEST_PASSWORD, TEST_USERNAME,
load_fixture)
refresh_payload = load_fixture("refresh.json")
login_payload = load_fixture("login_success.json")
info_payload = load_fixture("info.json")
class TestRefresh:
"""Use a test class to get async state."""
callback_result = None
def callback_function(self, event) -> None:
"""The callback handler for this test."""
self.callback_result = event
def callback_exception(self, event) -> None:
"""The callback handler for this test."""
self.callback_result = event
raise TypeError()
def test_fibaro_refresh(self) -> None:
"""Test get request"""
with requests_mock.Mocker() as mock:
assert isinstance(mock, requests_mock.Mocker)
mock.register_uri(
"GET", f"{TEST_BASE_URL}refreshStates", json=refresh_payload
)
mock.register_uri("GET", f"{TEST_BASE_URL}loginStatus", json=login_payload)
mock.register_uri("GET", f"{TEST_BASE_URL}settings/info", json=info_payload)
client = FibaroClient(TEST_BASE_URL)
client.set_authentication(TEST_USERNAME, TEST_PASSWORD)
client.connect()
client.register_update_handler(self.callback_function)
time.sleep(0.1)
client.unregister_update_handler()
assert self.callback_result is not None
assert mock.call_count > 2
def test_fibaro_refresh_fail(self) -> None:
"""Test get request"""
with requests_mock.Mocker() as mock:
assert isinstance(mock, requests_mock.Mocker)
mock.register_uri("GET", f"{TEST_BASE_URL}loginStatus", json=login_payload)
mock.register_uri("GET", f"{TEST_BASE_URL}settings/info", json=info_payload)
client = FibaroClient(TEST_BASE_URL)
client.set_authentication(TEST_USERNAME, TEST_PASSWORD)
client.connect()
client.register_update_handler(self.callback_function)
time.sleep(1.1)
client.unregister_update_handler()
assert self.callback_result is None
assert mock.call_count > 2
def test_fibaro_refresh_double_register(self) -> None:
"""Test get request"""
with requests_mock.Mocker() as mock:
assert isinstance(mock, requests_mock.Mocker)
mock.register_uri("GET", f"{TEST_BASE_URL}loginStatus", json=login_payload)
mock.register_uri("GET", f"{TEST_BASE_URL}settings/info", json=info_payload)
client = FibaroClient(TEST_BASE_URL)
client.set_authentication(TEST_USERNAME, TEST_PASSWORD)
client.connect()
client.register_update_handler(self.callback_function)
with pytest.raises(Exception):
client.register_update_handler(self.callback_function)
client.unregister_update_handler()
def test_fibaro_refresh_callback_exception(self) -> None:
"""Test get request"""
with requests_mock.Mocker() as mock:
assert isinstance(mock, requests_mock.Mocker)
mock.register_uri(
"GET", f"{TEST_BASE_URL}refreshStates", json=refresh_payload
)
mock.register_uri("GET", f"{TEST_BASE_URL}loginStatus", json=login_payload)
mock.register_uri("GET", f"{TEST_BASE_URL}settings/info", json=info_payload)
client = FibaroClient(TEST_BASE_URL)
client.set_authentication(TEST_USERNAME, TEST_PASSWORD)
client.connect()
client.register_update_handler(self.callback_exception)
time.sleep(0.1)
client.unregister_update_handler()
assert self.callback_result is not None
assert mock.call_count > 2
|