def test_truncate_alert(self): """ Tests that the 'alert' string field will be truncated when needed. """ overhead = len(json_encode(payload_for_aps({"alert": ""}))) txt = simplestring(10) aps = {"alert": txt} self.assertEquals( txt[:5], truncate(payload_for_aps(aps), overhead + 5)["aps"]["alert"])
def test_truncate_loc_arg(self): """ Tests that the 'alert' 'loc-args' field will be truncated when needed. (Tests with one loc arg) """ overhead = len(json_encode(payload_for_aps({"alert": {"loc-args": [""]}}))) txt = simplestring(10) aps = {"alert": {"loc-args": [txt]}} self.assertEqual( txt[:5], truncate(payload_for_aps(aps), overhead + 5)["aps"]["alert"]["loc-args"][0], )
def test_truncate_string_with_multibyte(self): """ Tests that truncation works as expected on strings containing one multibyte character. """ overhead = len(json_encode(payload_for_aps({"alert": ""}))) txt = u"\U0001F430" + simplestring(30) aps = {"alert": txt} # NB. The number of characters of the string we get is dependent # on the json encoding used. self.assertEquals( txt[:17], truncate(payload_for_aps(aps), overhead + 20)["aps"]["alert"])
def test_truncate_multibyte(self): """ Tests that truncation works as expected on strings containing only multibyte characters. """ overhead = len(json_encode(payload_for_aps({"alert": ""}))) txt = sillystring(30) aps = {"alert": txt} trunc = truncate(payload_for_aps(aps), overhead + 30) # The string is all 4 byte characters so the trunctaed UTF-8 string # should be a multiple of 4 bytes long self.assertEquals(len(trunc["aps"]["alert"].encode()) % 4, 0) # NB. The number of characters of the string we get is dependent # on the json encoding used. self.assertEquals(txt[:7], trunc["aps"]["alert"])
def test_payload_truncation(self): """ Tests that APNS message bodies will be truncated to fit the limits of APNS. """ # Arrange method = self.apns_pushkin_snotif method.return_value = testutils.make_async_magic_mock( NotificationResult("notID", "200")) self.sygnal.pushkins[PUSHKIN_ID].MAX_JSON_BODY_SIZE = 240 # Act self._request(self._make_dummy_notification([DEVICE_EXAMPLE])) # Assert self.assertEqual(1, method.call_count) ((notification_req, ), _kwargs) = method.call_args payload = notification_req.message self.assertLessEqual(len(apnstruncate.json_encode(payload)), 240)
def test_payload_truncation_test_validity(self): """ This tests that L{test_payload_truncation_success} is a valid test by showing that not limiting the truncation size would result in a longer message. """ # Arrange method = self.apns_pushkin_snotif method.return_value = testutils.make_async_magic_mock( NotificationResult("notID", "200")) self.sygnal.pushkins[PUSHKIN_ID].MAX_JSON_BODY_SIZE = 4096 # Act self._request(self._make_dummy_notification([DEVICE_EXAMPLE])) # Assert self.assertEqual(1, method.call_count) ((notification_req, ), _kwargs) = method.call_args payload = notification_req.message self.assertGreater(len(apnstruncate.json_encode(payload)), 200)