def test_route_to_busy_node_saves_looks_up_and_sends_check_201(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, [dummy_chid]) self.db.message_table = Mock(return_value=self.message_mock) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid, current_month=self.db.current_msg_month) 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(result): assert isinstance(result, RouterResponse) assert result.status_code == 201 kwargs = self.message_mock.store_message.call_args[1] t_h = kwargs["notification"].headers assert t_h.get('encryption') == self.headers.get('encryption') assert t_h.get('crypto_key') == self.headers.get('crypto-key') assert t_h.get('encoding') == self.headers.get('content-encoding') assert "Location" in result.headers d.addCallback(verify_deliver) return d
def test_route_and_clear_failure(self): self.agent_mock.request = Mock(side_effect=ConnectionRefusedError) self.message_mock.store_message.return_value = True self.message_mock.all_channels.return_value = (True, [dummy_chid]) self.db.message_table = Mock(return_value=self.message_mock) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid, current_month=self.db.current_msg_month) self.router_mock.get_uaid.return_value = router_data def throw(): raise ClientError({'Error': { 'Code': 'InternalServerError' }}, 'mock_clear_node') self.router_mock.clear_node.side_effect = MockAssist([throw]) self.router.message_id = uuid.uuid4().hex d = self.router.route_notification(self.notif, router_data) def verify_deliver(result): assert isinstance(result, RouterResponse) assert result.status_code == 201 kwargs = self.message_mock.store_message.call_args[1] assert len(self.metrics.increment.mock_calls) == 3 t_h = kwargs["notification"].headers assert t_h.get('encryption') == self.headers.get('encryption') assert t_h.get('crypto_key') == self.headers.get('crypto-key') assert t_h.get('encoding') == self.headers.get('content-encoding') assert "Location" in result.headers d.addCallback(verify_deliver) return d
def test_route_to_busy_node_with_ttl_zero(self): notif = WebPushNotification( uaid=uuid.UUID(dummy_uaid), channel_id=uuid.UUID(dummy_chid), data="data", headers=self.headers, ttl=0, message_id=uuid.uuid4().hex, ) self.notif.cleanup_headers() 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, [dummy_chid]) self.db.message_table = Mock(return_value=self.message_mock) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid, current_month=self.db.current_msg_month) self.router_mock.get_uaid.return_value = router_data self.router.message_id = uuid.uuid4().hex d = self.router.route_notification(notif, router_data) def verify_deliver(fail): exc = fail.value assert isinstance(exc, RouterException) assert exc.status_code == 201 assert len(self.metrics.increment.mock_calls) == 0 assert "Location" in exc.headers d.addBoth(verify_deliver) return d
def test_route_to_busy_node_saves_looks_up_and_sends_check_201(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, [dummy_chid]) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid, current_month=self.settings.current_msg_month) 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(result): ok_(isinstance(result, RouterResponse)) eq_(result.status_code, 201) kwargs = self.message_mock.store_message.call_args[1] t_h = kwargs["notification"].headers eq_(t_h.get('encryption'), self.headers.get('encryption')) eq_(t_h.get('crypto_key'), self.headers.get('crypto-key')) eq_(t_h.get('encoding'), self.headers.get('content-encoding')) self.router.metrics.increment.assert_called_with( "router.broadcast.save_hit") ok_("Location" in result.headers) d.addCallback(verify_deliver) return d
def test_route_busy_node_saves_looks_up_and_send_check_fails_and_db(self): response_mock = Mock() self.agent_mock.request.side_effect = MockAssist( [response_mock, self._raise_connect_error]) response_mock.code = 202 self.storage_mock.save_notification.return_value = True router_data = dict(node_id="http://somewhere", uaid=dummy_uaid) self.router_mock.get_uaid.return_value = router_data self.router_mock.clear_node.side_effect = MockAssist( [self._raise_db_error]) d = self.router.route_notification(self.notif, router_data) def verify_deliver(result): ok_(isinstance(result, RouterResponse)) eq_(result.status_code, 202) ok_(self.router_mock.clear_node.called) d.addBoth(verify_deliver) return d
def test_route_with_no_node_saves_and_lookup_fails(self): self.storage_mock.save_notification.return_value = True self.router_mock.get_uaid.side_effect = MockAssist( [self._raise_db_error]) router_data = dict(uaid=dummy_uaid) d = self.router.route_notification(self.notif, router_data) def verify_deliver(result): ok_(isinstance(result, RouterResponse)) eq_(result.status_code, 202) d.addBoth(verify_deliver) return d
def test_route_with_no_node_saves_and_lookup_fails_with_item_error(self): self.storage_mock.save_notification.return_value = True self.router_mock.get_uaid.side_effect = MockAssist( [self._raise_item_error]) router_data = dict(uaid=dummy_uaid) d = self.router.route_notification(self.notif, router_data) def verify_deliver(fail): exc = fail.value ok_(exc, RouterException) eq_(exc.status_code, 410) d.addBoth(verify_deliver) return d
def test_route_to_busy_node_save_throws_db_error(self): self.agent_mock.request.return_value = response_mock = Mock() response_mock.code = 202 self.storage_mock.save_notification.side_effect = MockAssist( [self._raise_db_error]) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid) d = self.router.route_notification(self.notif, router_data) def verify_deliver(fail): exc = fail.value ok_(exc, RouterException) eq_(exc.status_code, 503) d.addBoth(verify_deliver) return d
def test_route_connection_fail_saved(self): self.agent_mock.request.side_effect = MockAssist( [self._raise_connection_refused_error]) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid) self.router_mock.clear_node.return_value = None self.router_mock.get_uaid.return_value = {} self.storage_mock.save_notification.return_value = True d = self.router.route_notification(self.notif, router_data) def verify_deliver(reply): eq_(len(self.router.log.debug.mock_calls), 1) ok_(reply.status_code, 202) eq_(len(self.router_mock.clear_node.mock_calls), 1) d.addBoth(verify_deliver) return d
def test_route_to_busy_node_saves_looks_up_and_sends_check_200(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.storage_mock.save_notification.return_value = True router_data = dict(node_id="http://somewhere", uaid=dummy_uaid) self.router_mock.get_uaid.return_value = router_data d = self.router.route_notification(self.notif, router_data) def verify_deliver(result): ok_(isinstance(result, RouterResponse)) eq_(result.status_code, 200) d.addBoth(verify_deliver) return d
def test_route_lookup_uaid_not_found(self): def throw(): raise ItemNotFound() self.message_mock.store_message.return_value = True self.db.message_table = Mock(return_value=self.message_mock) self.router_mock.get_uaid.side_effect = MockAssist([throw]) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid, current_month=self.db.current_msg_month) d = self.router.route_notification(self.notif, router_data) def verify_deliver(status): assert status.value.status_code == 410 d.addBoth(verify_deliver) return d
def test_route_lookup_uaid_fails(self): from boto.dynamodb2.exceptions import JSONResponseError def throw(): raise JSONResponseError(500, "Whoops") self.message_mock.store_message.return_value = True self.router_mock.get_uaid.side_effect = MockAssist([throw]) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid, current_month=self.db.current_msg_month) d = self.router.route_notification(self.notif, router_data) def verify_deliver(status): assert status.status_code == 201 d.addBoth(verify_deliver) return d
def test_route_lookup_uaid_fails(self): def throw(): raise ClientError({'Error': { 'Code': 'InternalServerError' }}, 'mock_get_uaid') self.message_mock.store_message.return_value = True self.db.message_table = Mock(return_value=self.message_mock) self.router_mock.get_uaid.side_effect = MockAssist([throw]) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid, current_month=self.db.current_msg_month) d = self.router.route_notification(self.notif, router_data) def verify_deliver(status): assert status.status_code == 201 d.addBoth(verify_deliver) return d
def test_route_to_busy_node_save_throws_db_error(self): from boto.dynamodb2.exceptions import JSONResponseError def throw(): raise JSONResponseError(500, "Whoops") self.agent_mock.request.return_value = response_mock = Mock() response_mock.code = 202 self.message_mock.store_message.side_effect = MockAssist([throw]) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid, current_month=self.db.current_msg_month) d = self.router.route_notification(self.notif, router_data) def verify_deliver(fail): exc = fail.value assert isinstance(exc, RouterException) assert exc.status_code == 503 d.addBoth(verify_deliver) return d
def test_route_to_busy_node_save_throws_db_error(self): def throw(): raise ClientError({'Error': { 'Code': 'InternalServerError' }}, 'mock_store_message') self.agent_mock.request.return_value = response_mock = Mock() response_mock.code = 202 self.message_mock.store_message.side_effect = MockAssist([throw]) self.db.message_table = Mock(return_value=self.message_mock) router_data = dict(node_id="http://somewhere", uaid=dummy_uaid, current_month=self.db.current_msg_month) d = self.router.route_notification(self.notif, router_data) def verify_deliver(fail): exc = fail.value assert isinstance(exc, RouterException) assert exc.status_code == 503 d.addBoth(verify_deliver) return d