Fix PEP257 issues (#3962)
This commit is contained in:
1 parent
c32afcd961
commit
3aa1b6a3f8
5 files changed
+93
-110
No files matched your search
@@ -12,18 +12,16 @@ import homeassistant.components.http as http
|
||||
|
||||
from tests.common import get_test_instance_port, get_test_home_assistant
|
||||
|
||||
API_PASSWORD = "test1234"
|
||||
API_PASSWORD = 'test1234'
|
||||
SERVER_PORT = get_test_instance_port()
|
||||
HTTP_BASE = "127.0.0.1:{}".format(SERVER_PORT)
|
||||
HTTP_BASE_URL = "http://{}".format(HTTP_BASE)
|
||||
HTTP_BASE = '127.0.0.1:{}'.format(SERVER_PORT)
|
||||
HTTP_BASE_URL = 'http://{}'.format(HTTP_BASE)
|
||||
HA_HEADERS = {
|
||||
const.HTTP_HEADER_HA_AUTH: API_PASSWORD,
|
||||
const.HTTP_HEADER_CONTENT_TYPE: const.CONTENT_TYPE_JSON,
|
||||
}
|
||||
# dont' add 127.0.0.1/::1 as trusted, as it may interfere with other test cases
|
||||
TRUSTED_NETWORKS = ["192.0.2.0/24",
|
||||
"2001:DB8:ABCD::/48",
|
||||
'100.64.0.1',
|
||||
# Don't add 127.0.0.1/::1 as trusted, as it may interfere with other test cases
|
||||
TRUSTED_NETWORKS = ['192.0.2.0/24', '2001:DB8:ABCD::/48', '100.64.0.1',
|
||||
'FD01:DB8::1']
|
||||
|
||||
CORS_ORIGINS = [HTTP_BASE_URL, HTTP_BASE]
|
||||
@@ -31,12 +29,13 @@ CORS_ORIGINS = [HTTP_BASE_URL, HTTP_BASE]
|
||||
hass = None
|
||||
|
||||
|
||||
def _url(path=""):
|
||||
def _url(path=''):
|
||||
"""Helper method to generate URLs."""
|
||||
return HTTP_BASE_URL + path
|
||||
|
||||
|
||||
def setUpModule(): # pylint: disable=invalid-name
|
||||
# pylint: disable=invalid-name
|
||||
def setUpModule():
|
||||
"""Initialize a Home Assistant server."""
|
||||
global hass
|
||||
|
||||
@@ -46,10 +45,14 @@ def setUpModule(): # pylint: disable=invalid-name
|
||||
hass.states.set('test.test', 'a_state')
|
||||
|
||||
bootstrap.setup_component(
|
||||
hass, http.DOMAIN,
|
||||
{http.DOMAIN: {http.CONF_API_PASSWORD: API_PASSWORD,
|
||||
http.CONF_SERVER_PORT: SERVER_PORT,
|
||||
http.CONF_CORS_ORIGINS: CORS_ORIGINS}})
|
||||
hass, http.DOMAIN, {
|
||||
http.DOMAIN: {
|
||||
http.CONF_API_PASSWORD: API_PASSWORD,
|
||||
http.CONF_SERVER_PORT: SERVER_PORT,
|
||||
http.CONF_CORS_ORIGINS: CORS_ORIGINS,
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
bootstrap.setup_component(hass, 'api')
|
||||
|
||||
@@ -85,16 +88,13 @@ class TestHttp:
|
||||
|
||||
def test_access_denied_with_untrusted_ip(self, caplog):
|
||||
"""Test access with an untrusted ip address."""
|
||||
|
||||
for remote_addr in ['198.51.100.1',
|
||||
'2001:DB8:FA1::1',
|
||||
'127.0.0.1',
|
||||
for remote_addr in ['198.51.100.1', '2001:DB8:FA1::1', '127.0.0.1',
|
||||
'::1']:
|
||||
with patch('homeassistant.components.http.'
|
||||
'HomeAssistantWSGI.get_real_ip',
|
||||
return_value=remote_addr):
|
||||
req = requests.get(_url(const.URL_API),
|
||||
params={'api_password': ''})
|
||||
req = requests.get(
|
||||
_url(const.URL_API), params={'api_password': ''})
|
||||
|
||||
assert req.status_code == 401, \
|
||||
"{} shouldn't be trusted".format(remote_addr)
|
||||
@@ -102,8 +102,8 @@ class TestHttp:
|
||||
def test_access_with_password_in_header(self, caplog):
|
||||
"""Test access with password in URL."""
|
||||
# Hide logging from requests package that we use to test logging
|
||||
caplog.set_level(logging.WARNING,
|
||||
logger='requests.packages.urllib3.connectionpool')
|
||||
caplog.set_level(
|
||||
logging.WARNING, logger='requests.packages.urllib3.connectionpool')
|
||||
|
||||
req = requests.get(
|
||||
_url(const.URL_API),
|
||||
@@ -118,19 +118,19 @@ class TestHttp:
|
||||
|
||||
def test_access_denied_with_wrong_password_in_url(self):
|
||||
"""Test access with wrong password."""
|
||||
req = requests.get(_url(const.URL_API),
|
||||
params={'api_password': 'wrongpassword'})
|
||||
req = requests.get(
|
||||
_url(const.URL_API), params={'api_password': 'wrongpassword'})
|
||||
|
||||
assert req.status_code == 401
|
||||
|
||||
def test_access_with_password_in_url(self, caplog):
|
||||
"""Test access with password in URL."""
|
||||
# Hide logging from requests package that we use to test logging
|
||||
caplog.set_level(logging.WARNING,
|
||||
logger='requests.packages.urllib3.connectionpool')
|
||||
caplog.set_level(
|
||||
logging.WARNING, logger='requests.packages.urllib3.connectionpool')
|
||||
|
||||
req = requests.get(_url(const.URL_API),
|
||||
params={'api_password': API_PASSWORD})
|
||||
req = requests.get(
|
||||
_url(const.URL_API), params={'api_password': API_PASSWORD})
|
||||
|
||||
assert req.status_code == 200
|
||||
|
||||
@@ -141,18 +141,16 @@ class TestHttp:
|
||||
|
||||
def test_access_with_trusted_ip(self, caplog):
|
||||
"""Test access with trusted addresses."""
|
||||
for remote_addr in ['100.64.0.1',
|
||||
'192.0.2.100',
|
||||
'FD01:DB8::1',
|
||||
for remote_addr in ['100.64.0.1', '192.0.2.100', 'FD01:DB8::1',
|
||||
'2001:DB8:ABCD::1']:
|
||||
with patch('homeassistant.components.http.'
|
||||
'HomeAssistantWSGI.get_real_ip',
|
||||
return_value=remote_addr):
|
||||
req = requests.get(_url(const.URL_API),
|
||||
params={'api_password': ''})
|
||||
req = requests.get(
|
||||
_url(const.URL_API), params={'api_password': ''})
|
||||
|
||||
assert req.status_code == 200, \
|
||||
"{} should be trusted".format(remote_addr)
|
||||
'{} should be trusted'.format(remote_addr)
|
||||
|
||||
def test_cors_allowed_with_password_in_url(self):
|
||||
"""Test cross origin resource sharing with password in url."""
|
||||
@@ -162,7 +160,7 @@ class TestHttp:
|
||||
|
||||
allow_origin = const.HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN
|
||||
allow_headers = const.HTTP_HEADER_ACCESS_CONTROL_ALLOW_HEADERS
|
||||
all_allow_headers = ", ".join(const.ALLOWED_CORS_HEADERS)
|
||||
all_allow_headers = ', '.join(const.ALLOWED_CORS_HEADERS)
|
||||
|
||||
assert req.status_code == 200
|
||||
assert req.headers.get(allow_origin) == HTTP_BASE_URL
|
||||
@@ -174,12 +172,11 @@ class TestHttp:
|
||||
const.HTTP_HEADER_HA_AUTH: API_PASSWORD,
|
||||
const.HTTP_HEADER_ORIGIN: HTTP_BASE_URL
|
||||
}
|
||||
req = requests.get(_url(const.URL_API),
|
||||
headers=headers)
|
||||
req = requests.get(_url(const.URL_API), headers=headers)
|
||||
|
||||
allow_origin = const.HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN
|
||||
allow_headers = const.HTTP_HEADER_ACCESS_CONTROL_ALLOW_HEADERS
|
||||
all_allow_headers = ", ".join(const.ALLOWED_CORS_HEADERS)
|
||||
all_allow_headers = ', '.join(const.ALLOWED_CORS_HEADERS)
|
||||
|
||||
assert req.status_code == 200
|
||||
assert req.headers.get(allow_origin) == HTTP_BASE_URL
|
||||
@@ -190,8 +187,7 @@ class TestHttp:
|
||||
headers = {
|
||||
const.HTTP_HEADER_HA_AUTH: API_PASSWORD
|
||||
}
|
||||
req = requests.get(_url(const.URL_API),
|
||||
headers=headers)
|
||||
req = requests.get(_url(const.URL_API), headers=headers)
|
||||
|
||||
allow_origin = const.HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN
|
||||
allow_headers = const.HTTP_HEADER_ACCESS_CONTROL_ALLOW_HEADERS
|
||||
@@ -207,12 +203,11 @@ class TestHttp:
|
||||
'Access-Control-Request-Method': 'GET',
|
||||
'Access-Control-Request-Headers': 'x-ha-access'
|
||||
}
|
||||
req = requests.options(_url(const.URL_API),
|
||||
headers=headers)
|
||||
req = requests.options(_url(const.URL_API), headers=headers)
|
||||
|
||||
allow_origin = const.HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN
|
||||
allow_headers = const.HTTP_HEADER_ACCESS_CONTROL_ALLOW_HEADERS
|
||||
all_allow_headers = ", ".join(const.ALLOWED_CORS_HEADERS)
|
||||
all_allow_headers = ', '.join(const.ALLOWED_CORS_HEADERS)
|
||||
|
||||
assert req.status_code == 200
|
||||
assert req.headers.get(allow_origin) == HTTP_BASE_URL
|
||||
|
||||
Reference in new issue
Block a user