Skip to content

Commit

Permalink
Bugfix upc with aiohttp 1.2 (cookies) (#5362)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvizeli authored Jan 16, 2017
1 parent 7a1d4b9 commit f08e264
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
17 changes: 7 additions & 10 deletions homeassistant/components/device_tracker/upc_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ def async_scan_devices(self):
return []

raw = yield from self._async_ws_function(CMD_DEVICES)
xml_root = ET.fromstring(raw)
if raw is None:
_LOGGER.warning("Can't read device from %s", self.host)
return

xml_root = ET.fromstring(raw)
return [mac.text for mac in xml_root.iter('MACAddr')]

@asyncio.coroutine
Expand All @@ -94,7 +97,8 @@ def async_login(self):
"http://{}/common_page/login.html".format(self.host)
)

self.token = self._async_get_token()
yield from response.text()
self.token = response.cookies['sessionToken'].value

# login
data = yield from self._async_ws_function(CMD_LOGIN, {
Expand Down Expand Up @@ -144,7 +148,7 @@ def _async_ws_function(self, function, additional_form=None):

# load data, store token for next request
raw = yield from response.text()
self.token = self._async_get_token()
self.token = response.cookies['sessionToken'].value

return raw

Expand All @@ -155,10 +159,3 @@ def _async_ws_function(self, function, additional_form=None):
finally:
if response is not None:
yield from response.release()

def _async_get_token(self):
"""Extract token from cookies."""
cookie_manager = self.websession.cookie_jar.filter_cookies(
"http://{}".format(self.host))

return cookie_manager.get('sessionToken')
29 changes: 19 additions & 10 deletions tests/test_util/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ def request(self, method, url, *,
content = b''
if params:
url = str(yarl.URL(url).with_query(params))
if cookies:
self._cookies.update(cookies)

self._mocks.append(AiohttpClientMockResponse(
method, url, status, content, exc))
method, url, status, content, cookies, exc))

def get(self, *args, **kwargs):
"""Register a mock get request."""
Expand All @@ -68,10 +66,6 @@ def call_count(self):
"""Number of requests made."""
return len(self.mock_calls)

def filter_cookies(self, host):
"""Return hosts cookies."""
return self._cookies

def clear_requests(self):
"""Reset mock calls."""
self._mocks.clear()
Expand All @@ -97,7 +91,7 @@ def match_request(self, method, url, *, data=None, auth=None, params=None,
class AiohttpClientMockResponse:
"""Mock Aiohttp client response."""

def __init__(self, method, url, status, response, exc=None):
def __init__(self, method, url, status, response, cookies=None, exc=None):
"""Initialize a fake response."""
self.method = method
self._url = url
Expand All @@ -107,6 +101,14 @@ def __init__(self, method, url, status, response, exc=None):
self.response = response
self.exc = exc

self._cookies = {}

if cookies:
for name, data in cookies.items():
cookie = mock.MagicMock()
cookie.value = data
self._cookies[name] = cookie

def match_request(self, method, url, params=None):
"""Test if response answers request."""
if method.lower() != self.method.lower():
Expand Down Expand Up @@ -140,6 +142,11 @@ def match_request(self, method, url, params=None):

return True

@property
def cookies(self):
"""Return dict of cookies."""
return self._cookies

@asyncio.coroutine
def read(self):
"""Return mock response."""
Expand All @@ -160,6 +167,10 @@ def release(self):
"""Mock release."""
pass

def close(self):
"""Mock close."""
pass


@contextmanager
def mock_aiohttp_client():
Expand All @@ -173,6 +184,4 @@ def mock_aiohttp_client():
setattr(instance, method,
functools.partial(mocker.match_request, method))

instance.cookie_jar.filter_cookies = mocker.filter_cookies

yield mocker

0 comments on commit f08e264

Please sign in to comment.