def test_send(self): message = WebhookMessage( MockNotification(webhook_payload={'data': 'value'}), 'https://www.thebluealliance.com/', 'secret') response = message.send() self.assertEqual(response.status_code, 200) self.assertIsNotNone(response.content)
def test_json_string_condition(self): message = FCMMessage(MockNotification(), condition="\'dogs\' in topics") self.assertEqual( message.json_string(), '{"message": {"data": {"message_type": "verification"}, "condition": "\'dogs\' in topics"}}' )
def test_generate_webhook_checksum(self): message = WebhookMessage( MockNotification(webhook_payload={'data': 'value'}), 'https://www.thebluealliance.com/', 'secret') message_json = message.json_string() self.assertEqual(message._generate_webhook_checksum(message_json), 'dbecd85ae53d221c387647085912d2107fa04cd5')
def test_json_string_data_payload(self): message = FCMMessage( MockNotification(data_payload={"verification": "some_code"}), token='abc') self.assertEqual( message.json_string(), '{"message": {"token": "abc", "data": {"message_type": "verification", "verification": "some_code"}}}' )
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_json_string_notification_payload(self): message = FCMMessage( MockNotification(notification_payload=NotificationPayload( title='Title Here', body='Body here')), token='abc') self.assertEqual( message.json_string(), '{"message": {"notification": {"body": "Body here", "title": "Title Here"}, "token": "abc", "data": {"message_type": "verification"}}}' )
def test_webhook_message(self): webhook_payload = {'test': 'something'} notification = MockNotification(webhook_payload=webhook_payload) message = WebhookMessage(notification, 'https://www.thebluealliance.com/', 'secret') self.assertEqual( message.json_string(), '{"message_data": {"test": "something"}, "message_type": "verification"}' )
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_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_set_platform_payload(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) FCMMessage._set_platform_payload(data, PlatformPayloadType.APNS, notification.apns_payload, notification.platform_payload) self.assertEqual(data, { 'apns': { 'apns-collapse-id': 'collapse_key', 'apns-priority': '10' } })
def test_delivery_option_token(self): message = FCMMessage(MockNotification(), condition='ghi') delivery_option = message._delivery_option() self.assertEqual(delivery_option[0], 'condition') self.assertEqual(delivery_option[1], 'ghi')
def test_str(self): message_str = WebhookMessage(MockNotification(), 'https://www.thebluealliance.com/', 'secret') self.assertTrue('WebhookMessage(notification=' in str(message_str))
def test_secret_type(self): with self.assertRaises(TypeError): WebhookMessage(MockNotification(), 'https://www.thebluealliance.com/', 200)
def test_url_type(self): with self.assertRaises(TypeError): WebhookMessage(MockNotification(), 200, 'secret')
def test_url(self): with self.assertRaises(ValueError): WebhookMessage(MockNotification(), None, 'secret')
def test_subclass(self): message = WebhookMessage(MockNotification(), 'https://www.thebluealliance.com/', 'secret') self.assertTrue(isinstance(message, Message))
def test_str_condition(self): message = FCMMessage(MockNotification(), condition='hij') self.assertTrue( 'FCMMessage(condition="hij", notification=' in str(message))
def test_webhook_payload_same_data_payload(self): data_payload = {'data': 'here'} notification = MockNotification(data_payload=data_payload) self.assertEqual(notification.data_payload, data_payload) self.assertEqual(notification.webhook_payload, data_payload)
def test_delivery_option_token(self): message = FCMMessage(MockNotification(), topic='def') delivery_option = message._delivery_option() self.assertEqual(delivery_option[0], 'topic') self.assertEqual(delivery_option[1], 'def')
def test_json_string(self): message = Message(notification=MockNotification()) with self.assertRaises(NotImplementedError): message.send()
def test_send(self): message = FCMMessage(notification=MockNotification(), token='abc') response = message.send() self.assertEqual(response.status_code, 200) self.assertEqual(response.content, 'Some content here')
def test_fcm_url(self): message = FCMMessage(MockNotification(), token='abc') self.assertEqual( message._fcm_url, 'https://fcm.googleapis.com/v1/projects/testbed-test/messages:send' )
def test_webhook_message_no_payload(self): notification = MockNotification() message = WebhookMessage(notification, 'https://www.thebluealliance.com/', 'secret') self.assertEqual(message.json_string(), '{"message_type": "verification"}')
def test_str_token(self): message = FCMMessage(MockNotification(), token='abc') self.assertTrue( 'FCMMessage(token="abc", notification=' in str(message))
def test_subclass(self): message = FCMMessage(MockNotification(), token='abcd') self.assertTrue(isinstance(message, Message))
def test_str_topic(self): message = FCMMessage(MockNotification(), topic='def') self.assertTrue( 'FCMMessage(topic="def", notification=' in str(message))
def test_init_delivery_none(self): with self.assertRaises(TypeError): FCMMessage(notification=MockNotification())
def test_init_delivery_multiple(self): with self.assertRaises(TypeError): FCMMessage(notification=MockNotification(), token='abc', topic='def')
def test_webhook_payload(self): webhook_payload = {'data': 'here'} notification = MockNotification(webhook_payload=webhook_payload) self.assertIsNone(notification.data_payload) self.assertEqual(notification.webhook_payload, webhook_payload)
def test_json_string_topic(self): message = FCMMessage(MockNotification(), topic='abcd') self.assertEqual( message.json_string(), '{"message": {"topic": "abcd", "data": {"message_type": "verification"}}}' )