API updated to be RESTful
This commit is contained in:
1 parent
102690a770
commit
f9d712d175
7 files changed
+495
-437
No files matched your search
+46
-77
@@ -13,13 +13,13 @@ import requests
|
||||
|
||||
import homeassistant as ha
|
||||
import homeassistant.remote as remote
|
||||
import homeassistant.httpinterface as httpinterface
|
||||
import homeassistant.httpinterface as hah
|
||||
|
||||
|
||||
|
||||
API_PASSWORD = "test1234"
|
||||
|
||||
HTTP_BASE_URL = "http://127.0.0.1:{}".format(httpinterface.SERVER_PORT)
|
||||
HTTP_BASE_URL = "http://127.0.0.1:{}".format(hah.SERVER_PORT)
|
||||
|
||||
# pylint: disable=too-many-public-methods
|
||||
class TestHTTPInterface(unittest.TestCase):
|
||||
@@ -27,13 +27,16 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
|
||||
HTTP_init = False
|
||||
|
||||
def _url(self, path=""):
|
||||
""" Helper method to generate urls. """
|
||||
return HTTP_BASE_URL + path
|
||||
|
||||
def setUp(self): # pylint: disable=invalid-name
|
||||
""" Initialize the HTTP interface if not started yet. """
|
||||
if not TestHTTPInterface.HTTP_init:
|
||||
TestHTTPInterface.HTTP_init = True
|
||||
|
||||
httpinterface.HTTPInterface(self.eventbus, self.statemachine,
|
||||
API_PASSWORD)
|
||||
hah.HTTPInterface(self.eventbus, self.statemachine, API_PASSWORD)
|
||||
|
||||
self.statemachine.set_state("test", "INIT_STATE")
|
||||
self.sm_with_remote_eb.set_state("test", "INIT_STATE")
|
||||
@@ -55,17 +58,21 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
def test_debug_interface(self):
|
||||
""" Test if we can login by comparing not logged in screen to
|
||||
logged in screen. """
|
||||
self.assertNotEqual(requests.get(HTTP_BASE_URL).text,
|
||||
requests.get("{}/?api_password={}".format(
|
||||
HTTP_BASE_URL, API_PASSWORD)).text)
|
||||
|
||||
with_pw = requests.get(
|
||||
self._url("/?api_password={}".format(API_PASSWORD)))
|
||||
|
||||
without_pw = requests.get(self._url())
|
||||
|
||||
self.assertNotEqual(without_pw.text, with_pw.text)
|
||||
|
||||
|
||||
def test_debug_state_change(self):
|
||||
""" Test if the debug interface allows us to change a state. """
|
||||
requests.post("{}/state/change".format(HTTP_BASE_URL),
|
||||
data={"category":"test",
|
||||
"new_state":"debug_state_change",
|
||||
"api_password":API_PASSWORD})
|
||||
requests.post(
|
||||
self._url(hah.URL_STATES_CATEGORY.format("test")),
|
||||
data={"new_state":"debug_state_change",
|
||||
"api_password":API_PASSWORD})
|
||||
|
||||
self.assertEqual(self.statemachine.get_state("test")['state'],
|
||||
"debug_state_change")
|
||||
@@ -74,19 +81,21 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
def test_api_password(self):
|
||||
""" Test if we get access denied if we omit or provide
|
||||
a wrong api password. """
|
||||
req = requests.post("{}/api/state/change".format(HTTP_BASE_URL))
|
||||
req = requests.post(
|
||||
self._url(hah.URL_API_STATES_CATEGORY.format("test")))
|
||||
|
||||
self.assertEqual(req.status_code, 401)
|
||||
|
||||
req = requests.post("{}/api/state/change".format(HTTP_BASE_URL,
|
||||
data={"api_password":"not the password"}))
|
||||
req = requests.post(
|
||||
self._url(hah.URL_API_STATES_CATEGORY.format("test")),
|
||||
data={"api_password":"not the password"})
|
||||
|
||||
self.assertEqual(req.status_code, 401)
|
||||
|
||||
|
||||
def test_api_list_state_categories(self):
|
||||
""" Test if the debug interface allows us to list state categories. """
|
||||
req = requests.post("{}/api/state/categories".format(HTTP_BASE_URL),
|
||||
req = requests.get(self._url(hah.URL_API_STATES),
|
||||
data={"api_password":API_PASSWORD})
|
||||
|
||||
data = req.json()
|
||||
@@ -96,16 +105,15 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
|
||||
|
||||
def test_api_get_state(self):
|
||||
""" Test if the debug interface allows us to list state categories. """
|
||||
req = requests.post("{}/api/state/get".format(HTTP_BASE_URL),
|
||||
data={"api_password":API_PASSWORD,
|
||||
"category": "test"})
|
||||
""" Test if the debug interface allows us to get a state. """
|
||||
req = requests.get(
|
||||
self._url(hah.URL_API_STATES_CATEGORY.format("test")),
|
||||
data={"api_password":API_PASSWORD})
|
||||
|
||||
data = req.json()
|
||||
|
||||
state = self.statemachine.get_state("test")
|
||||
|
||||
|
||||
self.assertEqual(data['category'], "test")
|
||||
self.assertEqual(data['state'], state['state'])
|
||||
self.assertEqual(data['last_changed'], state['last_changed'])
|
||||
@@ -117,9 +125,8 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
|
||||
self.statemachine.set_state("test", "not_to_be_set_state")
|
||||
|
||||
requests.post("{}/api/state/change".format(HTTP_BASE_URL),
|
||||
data={"category":"test",
|
||||
"new_state":"debug_state_change2",
|
||||
requests.post(self._url(hah.URL_API_STATES_CATEGORY.format("test")),
|
||||
data={"new_state":"debug_state_change2",
|
||||
"api_password":API_PASSWORD})
|
||||
|
||||
self.assertEqual(self.statemachine.get_state("test")['state'],
|
||||
@@ -156,22 +163,6 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
self.assertEqual(state['attributes']['test'], 1)
|
||||
|
||||
|
||||
def test_api_multiple_state_change(self):
|
||||
""" Test if we can change multiple states in 1 request. """
|
||||
|
||||
self.statemachine.set_state("test", "not_to_be_set_state")
|
||||
self.statemachine.set_state("test2", "not_to_be_set_state")
|
||||
|
||||
requests.post("{}/api/state/change".format(HTTP_BASE_URL),
|
||||
data={"category": ["test", "test2"],
|
||||
"new_state": ["test_state_1", "test_state_2"],
|
||||
"api_password":API_PASSWORD})
|
||||
|
||||
self.assertEqual(self.statemachine.get_state("test")['state'],
|
||||
"test_state_1")
|
||||
self.assertEqual(self.statemachine.get_state("test2")['state'],
|
||||
"test_state_2")
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_api_state_change_of_non_existing_category(self):
|
||||
""" Test if the API allows us to change a state of
|
||||
@@ -179,15 +170,16 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
|
||||
new_state = "debug_state_change"
|
||||
|
||||
req = requests.post("{}/api/state/change".format(HTTP_BASE_URL),
|
||||
data={"category":"test_category_that_does_not_exist",
|
||||
"new_state":new_state,
|
||||
"api_password":API_PASSWORD})
|
||||
req = requests.post(
|
||||
self._url(hah.URL_API_STATES_CATEGORY.format(
|
||||
"test_category_that_does_not_exist")),
|
||||
data={"new_state": new_state,
|
||||
"api_password": API_PASSWORD})
|
||||
|
||||
cur_state = (self.statemachine.
|
||||
get_state("test_category_that_does_not_exist")['state'])
|
||||
get_state("test_category_that_does_not_exist")['state'])
|
||||
|
||||
self.assertEqual(req.status_code, 200)
|
||||
self.assertEqual(req.status_code, 201)
|
||||
self.assertEqual(cur_state, new_state)
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
@@ -201,10 +193,9 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
|
||||
self.eventbus.listen_once("test_event_no_data", listener)
|
||||
|
||||
requests.post("{}/api/event/fire".format(HTTP_BASE_URL),
|
||||
data={"event_name":"test_event_no_data",
|
||||
"event_data":"",
|
||||
"api_password":API_PASSWORD})
|
||||
requests.post(
|
||||
self._url(hah.URL_EVENTS_EVENT.format("test_event_no_data")),
|
||||
data={"api_password":API_PASSWORD})
|
||||
|
||||
# Allow the event to take place
|
||||
time.sleep(1)
|
||||
@@ -224,9 +215,9 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
|
||||
self.eventbus.listen_once("test_event_with_data", listener)
|
||||
|
||||
requests.post("{}/api/event/fire".format(HTTP_BASE_URL),
|
||||
data={"event_name":"test_event_with_data",
|
||||
"event_data":'{"test": 1}',
|
||||
requests.post(
|
||||
self._url(hah.URL_EVENTS_EVENT.format("test_event_with_data")),
|
||||
data={"event_data":'{"test": 1}',
|
||||
"api_password":API_PASSWORD})
|
||||
|
||||
# Allow the event to take place
|
||||
@@ -235,28 +226,6 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
self.assertEqual(len(test_value), 1)
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_api_fire_event_with_no_params(self):
|
||||
""" Test how the API respsonds when we specify no event attributes. """
|
||||
test_value = []
|
||||
|
||||
def listener(event):
|
||||
""" Helper method that will verify that our event got called and
|
||||
that test if our data came through. """
|
||||
if "test" in event.data:
|
||||
test_value.append(1)
|
||||
|
||||
self.eventbus.listen_once("test_event_with_data", listener)
|
||||
|
||||
requests.post("{}/api/event/fire".format(HTTP_BASE_URL),
|
||||
data={"api_password":API_PASSWORD})
|
||||
|
||||
# Allow the event to take place
|
||||
time.sleep(1)
|
||||
|
||||
self.assertEqual(len(test_value), 0)
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_api_fire_event_with_invalid_json(self):
|
||||
""" Test if the API allows us to fire an event. """
|
||||
@@ -268,9 +237,9 @@ class TestHTTPInterface(unittest.TestCase):
|
||||
|
||||
self.eventbus.listen_once("test_event_with_bad_data", listener)
|
||||
|
||||
req = requests.post("{}/api/event/fire".format(HTTP_BASE_URL),
|
||||
data={"event_name":"test_event_with_bad_data",
|
||||
"event_data":'not json',
|
||||
req = requests.post(
|
||||
self._url(hah.URL_API_EVENTS_EVENT.format("test_event")),
|
||||
data={"event_data":'not json',
|
||||
"api_password":API_PASSWORD})
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user