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

Commit

Permalink
fix: Check connected month bounds for preflight
Browse files Browse the repository at this point in the history
Add full checks for preflight failure conditions.

Closes #461
  • Loading branch information
jrconlin committed May 5, 2016
1 parent d2c36fc commit 63ff016
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
8 changes: 6 additions & 2 deletions autopush/router/webpush.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,21 @@ def preflight_check(self, uaid, channel_id):
record = yield deferToThread(self.ap_settings.router.get_uaid, uaid)

if 'current_month' not in record:
raise RouterException("No such subscription", status_code=404,
raise RouterException("No such subscription", status_code=410,
log_exception=False, errno=106)

month_table = record["current_month"]
if month_table not in self.ap_settings.message_tables:
yield deferToThread(self.ap_settings.router.drop_user, uaid)
raise RouterException("No such subscription", status_code=410,
log_exception=False, errno=106)
exists, chans = yield deferToThread(
self.ap_settings.message_tables[month_table].all_channels,
uaid=uaid)

if (not exists or channel_id.lower() not
in map(lambda x: normalize_id(x), chans)):
raise RouterException("No such subscription", status_code=404,
raise RouterException("No such subscription", status_code=410,
log_exception=False, errno=106)
returnValue(month_table)

Expand Down
2 changes: 1 addition & 1 deletion autopush/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ def test_no_delivery_to_unregistered(self):
yield client.ack(result["channelID"], result["version"])

yield client.unregister(chan)
result = yield client.send_notification(data=data, status=404)
result = yield client.send_notification(data=data, status=410)
eq_(result, None)
yield self.shut_down(client)

Expand Down
52 changes: 50 additions & 2 deletions autopush/tests/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,14 +757,39 @@ def verify_deliver(fail):
d.addBoth(verify_deliver)
return d

def test_route_with_missing_month(self):
self.agent_mock.request.return_value = response_mock = Mock()
response_mock.addCallback.return_value = response_mock
type(response_mock).code = PropertyMock(
side_effect=MockAssist([202, 200]))
self.message_mock.store_message.return_value = True
self.message_mock.all_channels.return_value = (True, [])
router_data = dict(node_id="http://somewhere",
uaid=dummy_uaid)
self.router_mock.get_uaid.return_value = router_data
self.router.message_id = uuid.uuid4().hex

d = self.router.route_notification(self.notif, router_data)

def verify_deliver(fail):
exc = fail.value
ok_(exc, RouterException)
eq_(exc.status_code, 410)
self.flushLoggedErrors()
d.addBoth(verify_deliver)
return d

def test_route_with_invalid_channel_id(self):
self.agent_mock.request.return_value = response_mock = Mock()
response_mock.addCallback.return_value = response_mock
type(response_mock).code = PropertyMock(
side_effect=MockAssist([202, 200]))
self.message_mock.store_message.return_value = True
self.message_mock.all_channels.return_value = (True, [])
router_data = dict(node_id="http://somewhere", uaid=dummy_uaid)
month = self.router.ap_settings.current_msg_month
router_data = dict(node_id="http://somewhere",
current_month=month,
uaid=dummy_uaid)
self.router_mock.get_uaid.return_value = router_data
self.router.message_id = uuid.uuid4().hex

Expand All @@ -773,7 +798,30 @@ def test_route_with_invalid_channel_id(self):
def verify_deliver(fail):
exc = fail.value
ok_(exc, RouterException)
eq_(exc.status_code, 404)
eq_(exc.status_code, 410)
self.flushLoggedErrors()
d.addBoth(verify_deliver)
return d

def test_route_with_invalid_month(self):
self.agent_mock.request.return_value = response_mock = Mock()
response_mock.addCallback.return_value = response_mock
type(response_mock).code = PropertyMock(
side_effect=MockAssist([202, 200]))
self.message_mock.store_message.return_value = True
self.message_mock.all_channels.return_value = (True, [])
router_data = dict(node_id="http://somewhere",
current_month="invalid_month",
uaid=dummy_uaid)
self.router_mock.get_uaid.return_value = router_data
self.router.message_id = uuid.uuid4().hex

d = self.router.route_notification(self.notif, router_data)

def verify_deliver(fail):
exc = fail.value
ok_(exc, RouterException)
eq_(exc.status_code, 410)
self.flushLoggedErrors()
d.addBoth(verify_deliver)
return d
Expand Down

0 comments on commit 63ff016

Please sign in to comment.