This commit is contained in:
Paulus Schoutsen committed 2019-07-31 12:25:30 -07:00
1 parent da05dfe708
commit 4de97abc3a
2676 files changed
+163166 -140084

No files matched your search

+80 -105
View File
@@ -6,8 +6,7 @@ import aiohttp
import homeassistant.components.rest_command as rc
from homeassistant.setup import setup_component
from tests.common import (
get_test_home_assistant, assert_setup_component)
from tests.common import get_test_home_assistant, assert_setup_component
class TestRestCommandSetup:
@@ -17,11 +16,7 @@ class TestRestCommandSetup:
"""Set up things to be run when tests are started."""
self.hass = get_test_home_assistant()
self.config = {
rc.DOMAIN: {'test_get': {
'url': 'http://example.com/'
}}
}
self.config = {rc.DOMAIN: {"test_get": {"url": "http://example.com/"}}}
def teardown_method(self):
"""Stop everything that was started."""
@@ -34,7 +29,7 @@ class TestRestCommandSetup:
def test_setup_component_timeout(self):
"""Test setup component timeout."""
self.config[rc.DOMAIN]['test_get']['timeout'] = 10
self.config[rc.DOMAIN]["test_get"]["timeout"] = 10
with assert_setup_component(1):
setup_component(self.hass, rc.DOMAIN, self.config)
@@ -44,7 +39,7 @@ class TestRestCommandSetup:
with assert_setup_component(1):
setup_component(self.hass, rc.DOMAIN, self.config)
assert self.hass.services.has_service(rc.DOMAIN, 'test_get')
assert self.hass.services.has_service(rc.DOMAIN, "test_get")
class TestRestCommandComponent:
@@ -55,22 +50,10 @@ class TestRestCommandComponent:
self.url = "https://example.com/"
self.config = {
rc.DOMAIN: {
'get_test': {
'url': self.url,
'method': 'get',
},
'post_test': {
'url': self.url,
'method': 'post',
},
'put_test': {
'url': self.url,
'method': 'put',
},
'delete_test': {
'url': self.url,
'method': 'delete',
},
"get_test": {"url": self.url, "method": "get"},
"post_test": {"url": self.url, "method": "post"},
"put_test": {"url": self.url, "method": "put"},
"delete_test": {"url": self.url, "method": "delete"},
}
}
@@ -85,10 +68,10 @@ class TestRestCommandComponent:
with assert_setup_component(4):
setup_component(self.hass, rc.DOMAIN, self.config)
assert self.hass.services.has_service(rc.DOMAIN, 'get_test')
assert self.hass.services.has_service(rc.DOMAIN, 'post_test')
assert self.hass.services.has_service(rc.DOMAIN, 'put_test')
assert self.hass.services.has_service(rc.DOMAIN, 'delete_test')
assert self.hass.services.has_service(rc.DOMAIN, "get_test")
assert self.hass.services.has_service(rc.DOMAIN, "post_test")
assert self.hass.services.has_service(rc.DOMAIN, "put_test")
assert self.hass.services.has_service(rc.DOMAIN, "delete_test")
def test_rest_command_timeout(self, aioclient_mock):
"""Call a rest command with timeout."""
@@ -97,7 +80,7 @@ class TestRestCommandComponent:
aioclient_mock.get(self.url, exc=asyncio.TimeoutError())
self.hass.services.call(rc.DOMAIN, 'get_test', {})
self.hass.services.call(rc.DOMAIN, "get_test", {})
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
@@ -109,7 +92,7 @@ class TestRestCommandComponent:
aioclient_mock.get(self.url, exc=aiohttp.ClientError())
self.hass.services.call(rc.DOMAIN, 'get_test', {})
self.hass.services.call(rc.DOMAIN, "get_test", {})
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
@@ -121,55 +104,50 @@ class TestRestCommandComponent:
aioclient_mock.get(self.url, status=400)
self.hass.services.call(rc.DOMAIN, 'get_test', {})
self.hass.services.call(rc.DOMAIN, "get_test", {})
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
def test_rest_command_auth(self, aioclient_mock):
"""Call a rest command with auth credential."""
data = {
'username': 'test',
'password': '123456',
}
self.config[rc.DOMAIN]['get_test'].update(data)
data = {"username": "test", "password": "123456"}
self.config[rc.DOMAIN]["get_test"].update(data)
with assert_setup_component(4):
setup_component(self.hass, rc.DOMAIN, self.config)
aioclient_mock.get(self.url, content=b'success')
aioclient_mock.get(self.url, content=b"success")
self.hass.services.call(rc.DOMAIN, 'get_test', {})
self.hass.services.call(rc.DOMAIN, "get_test", {})
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
def test_rest_command_form_data(self, aioclient_mock):
"""Call a rest command with post form data."""
data = {
'payload': 'test'
}
self.config[rc.DOMAIN]['post_test'].update(data)
data = {"payload": "test"}
self.config[rc.DOMAIN]["post_test"].update(data)
with assert_setup_component(4):
setup_component(self.hass, rc.DOMAIN, self.config)
aioclient_mock.post(self.url, content=b'success')
aioclient_mock.post(self.url, content=b"success")
self.hass.services.call(rc.DOMAIN, 'post_test', {})
self.hass.services.call(rc.DOMAIN, "post_test", {})
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == b'test'
assert aioclient_mock.mock_calls[0][2] == b"test"
def test_rest_command_get(self, aioclient_mock):
"""Call a rest command with get."""
with assert_setup_component(4):
setup_component(self.hass, rc.DOMAIN, self.config)
aioclient_mock.get(self.url, content=b'success')
aioclient_mock.get(self.url, content=b"success")
self.hass.services.call(rc.DOMAIN, 'get_test', {})
self.hass.services.call(rc.DOMAIN, "get_test", {})
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
@@ -179,95 +157,90 @@ class TestRestCommandComponent:
with assert_setup_component(4):
setup_component(self.hass, rc.DOMAIN, self.config)
aioclient_mock.delete(self.url, content=b'success')
aioclient_mock.delete(self.url, content=b"success")
self.hass.services.call(rc.DOMAIN, 'delete_test', {})
self.hass.services.call(rc.DOMAIN, "delete_test", {})
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
def test_rest_command_post(self, aioclient_mock):
"""Call a rest command with post."""
data = {
'payload': 'data',
}
self.config[rc.DOMAIN]['post_test'].update(data)
data = {"payload": "data"}
self.config[rc.DOMAIN]["post_test"].update(data)
with assert_setup_component(4):
setup_component(self.hass, rc.DOMAIN, self.config)
aioclient_mock.post(self.url, content=b'success')
aioclient_mock.post(self.url, content=b"success")
self.hass.services.call(rc.DOMAIN, 'post_test', {})
self.hass.services.call(rc.DOMAIN, "post_test", {})
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == b'data'
assert aioclient_mock.mock_calls[0][2] == b"data"
def test_rest_command_put(self, aioclient_mock):
"""Call a rest command with put."""
data = {
'payload': 'data',
}
self.config[rc.DOMAIN]['put_test'].update(data)
data = {"payload": "data"}
self.config[rc.DOMAIN]["put_test"].update(data)
with assert_setup_component(4):
setup_component(self.hass, rc.DOMAIN, self.config)
aioclient_mock.put(self.url, content=b'success')
aioclient_mock.put(self.url, content=b"success")
self.hass.services.call(rc.DOMAIN, 'put_test', {})
self.hass.services.call(rc.DOMAIN, "put_test", {})
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[0][2] == b'data'
assert aioclient_mock.mock_calls[0][2] == b"data"
def test_rest_command_headers(self, aioclient_mock):
"""Call a rest command with custom headers and content types."""
header_config_variations = {
rc.DOMAIN: {
'no_headers_test': {},
'content_type_test': {
'content_type': 'text/plain'
},
'headers_test': {
'headers': {
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0'
"no_headers_test": {},
"content_type_test": {"content_type": "text/plain"},
"headers_test": {
"headers": {
"Accept": "application/json",
"User-Agent": "Mozilla/5.0",
}
},
'headers_and_content_type_test': {
'headers': {
'Accept': 'application/json'
},
'content_type': 'text/plain'
"headers_and_content_type_test": {
"headers": {"Accept": "application/json"},
"content_type": "text/plain",
},
'headers_and_content_type_override_test': {
'headers': {
'Accept': 'application/json',
aiohttp.hdrs.CONTENT_TYPE: 'application/pdf'
"headers_and_content_type_override_test": {
"headers": {
"Accept": "application/json",
aiohttp.hdrs.CONTENT_TYPE: "application/pdf",
},
'content_type': 'text/plain'
}
"content_type": "text/plain",
},
}
}
# add common parameters
for variation in header_config_variations[rc.DOMAIN].values():
variation.update({'url': self.url, 'method': 'post',
'payload': 'test data'})
variation.update(
{"url": self.url, "method": "post", "payload": "test data"}
)
with assert_setup_component(5):
setup_component(self.hass, rc.DOMAIN, header_config_variations)
# provide post request data
aioclient_mock.post(self.url, content=b'success')
aioclient_mock.post(self.url, content=b"success")
for test_service in ['no_headers_test',
'content_type_test',
'headers_test',
'headers_and_content_type_test',
'headers_and_content_type_override_test']:
for test_service in [
"no_headers_test",
"content_type_test",
"headers_test",
"headers_and_content_type_test",
"headers_and_content_type_override_test",
]:
self.hass.services.call(rc.DOMAIN, test_service, {})
self.hass.block_till_done()
@@ -278,26 +251,28 @@ class TestRestCommandComponent:
# content_type_test
assert len(aioclient_mock.mock_calls[1][3]) == 1
assert aioclient_mock.mock_calls[1][3].get(
aiohttp.hdrs.CONTENT_TYPE) == 'text/plain'
assert (
aioclient_mock.mock_calls[1][3].get(aiohttp.hdrs.CONTENT_TYPE)
== "text/plain"
)
# headers_test
assert len(aioclient_mock.mock_calls[2][3]) == 2
assert aioclient_mock.mock_calls[2][3].get(
'Accept') == 'application/json'
assert aioclient_mock.mock_calls[2][3].get(
'User-Agent') == 'Mozilla/5.0'
assert aioclient_mock.mock_calls[2][3].get("Accept") == "application/json"
assert aioclient_mock.mock_calls[2][3].get("User-Agent") == "Mozilla/5.0"
# headers_and_content_type_test
assert len(aioclient_mock.mock_calls[3][3]) == 2
assert aioclient_mock.mock_calls[3][3].get(
aiohttp.hdrs.CONTENT_TYPE) == 'text/plain'
assert aioclient_mock.mock_calls[3][3].get(
'Accept') == 'application/json'
assert (
aioclient_mock.mock_calls[3][3].get(aiohttp.hdrs.CONTENT_TYPE)
== "text/plain"
)
assert aioclient_mock.mock_calls[3][3].get("Accept") == "application/json"
# headers_and_content_type_override_test
assert len(aioclient_mock.mock_calls[4][3]) == 2
assert aioclient_mock.mock_calls[4][3].get(
aiohttp.hdrs.CONTENT_TYPE) == 'text/plain'
assert aioclient_mock.mock_calls[4][3].get(
'Accept') == 'application/json'
assert (
aioclient_mock.mock_calls[4][3].get(aiohttp.hdrs.CONTENT_TYPE)
== "text/plain"
)
assert aioclient_mock.mock_calls[4][3].get("Accept") == "application/json"