From 63ff016c3102e516d1966c964d054ce4ec2b17d1 Mon Sep 17 00:00:00 2001 From: jrconlin Date: Thu, 5 May 2016 13:51:30 -0700 Subject: [PATCH] fix: Check connected month bounds for preflight Add full checks for preflight failure conditions. Closes #461 --- autopush/router/webpush.py | 8 +++-- autopush/tests/test_integration.py | 2 +- autopush/tests/test_router.py | 52 ++++++++++++++++++++++++++++-- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/autopush/router/webpush.py b/autopush/router/webpush.py index c29a9a22..65d81d1b 100644 --- a/autopush/router/webpush.py +++ b/autopush/router/webpush.py @@ -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) diff --git a/autopush/tests/test_integration.py b/autopush/tests/test_integration.py index 1a64db0d..3ec38b63 100644 --- a/autopush/tests/test_integration.py +++ b/autopush/tests/test_integration.py @@ -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) diff --git a/autopush/tests/test_router.py b/autopush/tests/test_router.py index d9a4f137..987c58b1 100644 --- a/autopush/tests/test_router.py +++ b/autopush/tests/test_router.py @@ -757,6 +757,28 @@ 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 @@ -764,7 +786,10 @@ def test_route_with_invalid_channel_id(self): 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 @@ -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