def test_platform_payload_dict(self): payload = PlatformPayload(platform_type=PlatformPayloadType.ANDROID, priority=PlatformPayloadPriority.NORMAL, collapse_key='android_collapse_key') self.assertNotEqual( payload.payload_dict, payload.platform_payload_dict(PlatformPayloadType.APNS))
def test_str(self): notification_payload = NotificationPayload(title='Title Here', body='Body here') data_payload = {'data': 'payload'} platform_payload = PlatformPayload(priority=PlatformPayloadPriority.HIGH, collapse_key='general_collapse_key') apns_payload = PlatformPayload(platform_type=PlatformPayloadType.APNS, priority=PlatformPayloadPriority.NORMAL, collapse_key='ios_collapse_key') notification = MockNotification(notification_payload=notification_payload, data_payload=data_payload, platform_payload=platform_payload, apns_payload=apns_payload) self.assertEqual(str(notification), 'MockNotification(data_payload={\'data\': \'payload\'} notification_payload=NotificationPayload(title="Title Here" body="Body here") platform_payload=PlatformPayload(platform_type=None priority=1 collapse_key="general_collapse_key") apns_payload=PlatformPayload(platform_type=1 priority=0 collapse_key="ios_collapse_key") webhook_payload={\'data\': \'payload\'})')
def test_set_platform_payload_override(self): data = {} platform_payload = PlatformPayload(priority=PlatformPayloadPriority.HIGH, collapse_key='collapse_key') apns_payload = PlatformPayload(platform_type=PlatformPayloadType.APNS, priority=PlatformPayloadPriority.NORMAL, collapse_key='different_collapse_key') notification = MockNotification(platform_payload=platform_payload, apns_payload=apns_payload) FCMRequest._set_platform_payload(data, PlatformPayloadType.APNS, notification.apns_payload, notification.platform_payload) self.assertEqual(data, {'apns': {'apns-collapse-id': 'different_collapse_key', 'apns-priority': '5'}})
def test_platform_payload_webpush(self): payload = PlatformPayload(platform_type=PlatformPayloadType.WEBPUSH, priority=PlatformPayloadPriority.NORMAL, collapse_key='web_collapse_key') self.assertEqual(payload.payload_dict, { 'Urgency': 'normal', 'Topic': 'web_collapse_key' }) self.assertEqual( payload.payload_dict, payload.platform_payload_dict(PlatformPayloadType.WEBPUSH))
def test_platform_payload_apns(self): payload = PlatformPayload(platform_type=PlatformPayloadType.APNS, priority=PlatformPayloadPriority.NORMAL, collapse_key='ios_collapse_key') self.assertEqual(payload.payload_dict, { 'apns-priority': '5', 'apns-collapse-id': 'ios_collapse_key' }) self.assertEqual( payload.payload_dict, payload.platform_payload_dict(PlatformPayloadType.APNS))
def test_platform_payload_android(self): payload = PlatformPayload(platform_type=PlatformPayloadType.ANDROID, priority=PlatformPayloadPriority.NORMAL, collapse_key='android_collapse_key') self.assertEqual(payload.payload_dict, { 'priority': 'NORMAL', 'collapse_key': 'android_collapse_key' }) self.assertEqual( payload.payload_dict, payload.platform_payload_dict(PlatformPayloadType.ANDROID))
def test_json_string_platform_payload_override(self): platform_payload = PlatformPayload( priority=PlatformPayloadPriority.HIGH, collapse_key='collapse_key') apns_payload = PlatformPayload(platform_type=PlatformPayloadType.APNS, priority=PlatformPayloadPriority.NORMAL, collapse_key='different_collapse_key') message = FCMMessage(MockNotification( platform_payload=platform_payload, apns_payload=apns_payload), token='abc') self.assertEqual( message.json_string(), '{"message": {"apns": {"apns-collapse-id": "different_collapse_key", "apns-priority": "5"}, "token": "abc", "data": {"message_type": "verification"}, "android": {"priority": "HIGH", "collapse_key": "collapse_key"}, "webpush": {"Topic": "collapse_key", "Urgency": "high"}}}' )
def test_str_partial(self): payload = PlatformPayload(priority=PlatformPayloadPriority.NORMAL, collapse_key='android_collapse_key') self.assertEqual( str(payload), 'PlatformPayload(platform_type=None priority=0 collapse_key="android_collapse_key")' )
def test_json_string_platform_payload(self): message = FCMMessage(MockNotification(platform_payload=PlatformPayload( priority=PlatformPayloadPriority.HIGH, collapse_key='collapse_key')), token='abc') self.assertEqual( message.json_string(), '{"message": {"apns": {"apns-collapse-id": "collapse_key", "apns-priority": "10"}, "token": "abc", "data": {"message_type": "verification"}, "android": {"priority": "HIGH", "collapse_key": "collapse_key"}, "webpush": {"Topic": "collapse_key", "Urgency": "high"}}}' )
def test_platform_payload_webpush(self): payload = PlatformPayload(platform_type=PlatformPayloadType.WEBPUSH, priority=PlatformPayloadPriority.NORMAL, collapse_key='web_collapse_key') self.assertEqual(payload.payload_dict, {'Urgency': 'normal', 'Topic': 'web_collapse_key'}) self.assertEqual(payload.payload_dict, payload.platform_payload_dict(PlatformPayloadType.WEBPUSH))
def platform_payload(self): return PlatformPayload( collapse_key='{}_{}_update'.format(self.user_id, self.type_name))
def test_platform_payload_android(self): payload = PlatformPayload(platform_type=PlatformPayloadType.ANDROID, priority=PlatformPayloadPriority.NORMAL, collapse_key='android_collapse_key') self.assertEqual(payload.payload_dict, {'priority': 'NORMAL', 'collapse_key': 'android_collapse_key'}) self.assertEqual(payload.payload_dict, payload.platform_payload_dict(PlatformPayloadType.ANDROID))
def test_platform_payload_apns(self): payload = PlatformPayload(platform_type=PlatformPayloadType.APNS, priority=PlatformPayloadPriority.NORMAL, collapse_key='ios_collapse_key') self.assertEqual(payload.payload_dict, {'apns-priority': '5', 'apns-collapse-id': 'ios_collapse_key'}) self.assertEqual(payload.payload_dict, payload.platform_payload_dict(PlatformPayloadType.APNS))
def test_platform_payload_dict_unsupported(self): payload = PlatformPayload() with self.assertRaises(TypeError): payload.platform_payload_dict(200)
def test_platform_payload_dict(self): payload = PlatformPayload(platform_type=PlatformPayloadType.ANDROID, priority=PlatformPayloadPriority.NORMAL, collapse_key='android_collapse_key') self.assertNotEqual(payload.payload_dict, payload.platform_payload_dict(PlatformPayloadType.APNS))
def test_collapse_key_type(self): with self.assertRaises(TypeError): PlatformPayload(collapse_key=True)
def test_platform_type_supported(self): with self.assertRaises(TypeError): PlatformPayload(platform_type=200)
def test_platform_type_empty(self): PlatformPayload()
def test_validate_platform_type(self): PlatformPayload._validate_platform_type(PlatformPayloadType.APNS)
def test_validate_platform_type_unsupported(self): with self.assertRaises(TypeError): PlatformPayload._validate_platform_type(200)
def test_str(self): payload = PlatformPayload(platform_type=PlatformPayloadType.ANDROID, priority=PlatformPayloadPriority.NORMAL, collapse_key='android_collapse_key')
def test_platform_type_type(self): with self.assertRaises(TypeError): PlatformPayload(platform_type='abc')
def platform_payload(self): from tbans.models.notifications.payloads.platform_payload import PlatformPayload return PlatformPayload( collapse_key='{}_{}_update'.format(self.user_id, self.type_name))
def test_platform_payload_empty(self): payload = PlatformPayload(platform_type=PlatformPayloadType.ANDROID) self.assertIsNone(payload.payload_dict)
def test_priority_supported(self): with self.assertRaises(TypeError): PlatformPayload(priority='abc')