From 148daa72d08b246d0e6b62599d6c801b664aff2b 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 | 3 +++ autopush/tests/test_router.py | 50 ++++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/autopush/router/webpush.py b/autopush/router/webpush.py index c29a9a22..26401574 100644 --- a/autopush/router/webpush.py +++ b/autopush/router/webpush.py @@ -70,6 +70,9 @@ def preflight_check(self, uaid, channel_id): log_exception=False, errno=106) month_table = record["current_month"] + if month_table not in self.ap_settings.message_tables: + raise RouterException("No such subscription", status_code=404, + log_exception=False, errno=106) exists, chans = yield deferToThread( self.ap_settings.message_tables[month_table].all_channels, uaid=uaid) diff --git a/autopush/tests/test_router.py b/autopush/tests/test_router.py index d9a4f137..42e97a17 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, 404) + 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,33 @@ 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 + + d = self.router.route_notification(self.notif, router_data) + + def verify_deliver(fail): + exc = fail.value + ok_(exc, RouterException) + eq_(exc.status_code, 404) + 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