Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
fix: Normalize endpoint and web _router_completed calls and tests
Browse files Browse the repository at this point in the history
closes #549
  • Loading branch information
jrconlin committed Aug 2, 2016
1 parent d74c00c commit c05ecbd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
18 changes: 15 additions & 3 deletions autopush/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,11 +584,22 @@ def _route_notification(self, version, result, data, ttl=None):
def _router_completed(self, response, uaid_data, warning=""):
"""Called after router has completed successfully"""
# TODO: Add some custom wake logic here

# Were we told to update the router data?
if response.router_data:
uaid_data["router_data"] = response.router_data
if response.router_data is not None:
if not response.router_data:
# An empty router_data object indicates that the record should
# be deleted. There is no longer valid route information for
# this record.
self.ap_settings.router.drop_user(self.uaid)
return self._router_response(response)
else:
# The router_data needs to be updated to include any changes
# requested by the bridge system.
uaid_data["router_data"] = response.router_data
# set the AWS mandatory data.
uaid_data["connected_at"] = ms_time()
uaid_data["router_type"] = uaid_data.get("router_type",
self.router_key)
d = deferToThread(self.ap_settings.router.register_user,
uaid_data)
response.router_data = None
Expand All @@ -597,6 +608,7 @@ def _router_completed(self, response, uaid_data, warning=""):
warning))
return d
else:
# No changes are requested by the bridge system, proceed as normal
if response.status_code == 200 or response.logged_status == 200:
self.log.info(format="Successful delivery",
**self._client_info)
Expand Down
12 changes: 8 additions & 4 deletions autopush/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,16 @@ def test_put_router_needs_change(self):
)
self.sp_router_mock.route_notification.return_value = RouterResponse(
status_code=500,
router_data={},
router_data=dict(token="new_connect"),
)

def handle_finish(result):
self.assertTrue(result)
self.endpoint.set_status.assert_called_with(500, None)
ok_(not self.router_mock.register_user.called)
ru = self.router_mock.register_user
ok_(ru.called)
eq_('simplepush', ru.call_args[0][0].get('router_type'))

self.finish_deferred.addCallback(handle_finish)

self.endpoint.put(None, dummy_uaid)
Expand All @@ -638,13 +641,14 @@ def test_put_router_needs_update(self):
)
self.sp_router_mock.route_notification.return_value = RouterResponse(
status_code=503,
router_data=dict(token="new_connect"),
router_data=dict(),
)

def handle_finish(result):
self.assertTrue(result)
self.endpoint.set_status.assert_called_with(503, None)
assert(self.router_mock.register_user.called)
self.router_mock.drop_user.assert_called()

self.finish_deferred.addCallback(handle_finish)

self.endpoint.put(None, dummy_uaid)
Expand Down
3 changes: 2 additions & 1 deletion autopush/tests/test_web_webpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def test_router_returns_data_without_detail(self):
def handle_finish(result):
eq_(result, True)
self.wp.set_status.assert_called_with(503)
assert(self.router_mock.register_user.called)
assert(self.router_mock.drop_user.called)

self.finish_deferred.addCallback(handle_finish)

self.wp.post("v1", dummy_token)
Expand Down
14 changes: 12 additions & 2 deletions autopush/web/webpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,22 @@ def post(self, api_ver="v1", token=None):
def _router_completed(self, response, uaid_data, warning=""):
"""Called after router has completed successfully"""
# Were we told to update the router data?
# GCM/APNS bridges can result in data updates
if response.router_data is not None:
if not response.router_data:
del uaid_data["router_data"]
# An empty router_data object indicates that the record should
# be deleted. There is no longer valid route information for
# this record.
self.ap_settings.router.drop_user(self.uaid)
return self._router_response(response)
else:
# The router data needs to be updated to include any changes
# requested by the bridge system
uaid_data["router_data"] = response.router_data
# set the AWS mandatory data
uaid_data["connected_at"] = ms_time()
rt = self.valid_input.get(
'subscription', {}).get('user_data', {}).get('router_type')
uaid_data["router_type"] = uaid_data.get("router_type", rt)
d = deferToThread(self.ap_settings.router.register_user,
uaid_data)
response.router_data = None
Expand All @@ -71,6 +80,7 @@ def _router_completed(self, response, uaid_data, warning=""):
warning))
return d
else:
# No changes are requested by the bridge system, proceed as normal
if response.status_code == 200 or response.logged_status == 200:
self.log.info(format="Successful delivery",
**self._client_info)
Expand Down

0 comments on commit c05ecbd

Please sign in to comment.