Skip to content

Commit 484d1d9

Browse files
committed
PR Fixes
1 parent 53aa966 commit 484d1d9

File tree

4 files changed

+66
-65
lines changed

4 files changed

+66
-65
lines changed

firebase_admin/_messaging_encoder.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -337,27 +337,27 @@ def encode_android_notification(cls, notification):
337337
result['event_time'] = str(event_time.isoformat()) + 'Z'
338338

339339
priority = result.get('notification_priority')
340-
if priority and priority not in ('min', 'low', 'default', 'high', 'max'):
341-
raise ValueError('AndroidNotification.priority must be "default", "min", "low", "high" '
342-
'or "max".')
343340
if priority:
341+
if priority not in ('min', 'low', 'default', 'high', 'max'):
342+
raise ValueError('AndroidNotification.priority must be "default", "min", "low", '
343+
'"high" or "max".')
344344
result['notification_priority'] = 'PRIORITY_' + priority.upper()
345345

346346
visibility = result.get('visibility')
347-
if visibility and visibility not in ('private', 'public', 'secret'):
348-
raise ValueError(
349-
'AndroidNotification.visibility must be "private", "public" or "secret".')
350347
if visibility:
348+
if visibility not in ('private', 'public', 'secret'):
349+
raise ValueError(
350+
'AndroidNotification.visibility must be "private", "public" or "secret".')
351351
result['visibility'] = visibility.upper()
352352

353353
vibrate_timings_millis = result.get('vibrate_timings')
354354
if vibrate_timings_millis:
355-
vibrate_timings_secs = []
355+
vibrate_timing_strings = []
356356
for msec in vibrate_timings_millis:
357357
formated_string = cls.encode_milliseconds(
358358
'AndroidNotification.vibrate_timings_millis', msec)
359-
vibrate_timings_secs.append(formated_string)
360-
result['vibrate_timings'] = vibrate_timings_secs
359+
vibrate_timing_strings.append(formated_string)
360+
result['vibrate_timings'] = vibrate_timing_strings
361361
return result
362362

363363
@classmethod
@@ -378,24 +378,25 @@ def encode_light_settings(cls, light_settings):
378378
light_settings.light_off_duration_millis),
379379
}
380380
result = cls.remove_null_values(result)
381-
color = result.get('color')
382-
if not color:
383-
raise ValueError('LightSettings.color is required.')
384381
light_on_duration = result.get('light_on_duration')
385382
if not light_on_duration:
386383
raise ValueError(
387384
'LightSettings.light_on_duration_millis is required.')
385+
388386
light_off_duration = result.get('light_off_duration')
389387
if not light_off_duration:
390388
raise ValueError(
391389
'LightSettings.light_off_duration_millis is required.')
392390

391+
color = result.get('color')
392+
if not color:
393+
raise ValueError('LightSettings.color is required.')
393394
if not re.match(r'^#[0-9a-fA-F]{6}$', color) and not re.match(r'^#[0-9a-fA-F]{8}$', color):
394395
raise ValueError(
395396
'LightSettings.color must be in the form #RRGGBB or #RRGGBBAA.')
396397
if len(color) == 7:
397398
color = (color+'FF')
398-
rgba = [int(color[i:i + 2], 16) / 255. for i in (1, 3, 5, 7)]
399+
rgba = [int(color[i:i + 2], 16) / 255.0 for i in (1, 3, 5, 7)]
399400
result['color'] = {'red': rgba[0], 'green': rgba[1],
400401
'blue': rgba[2], 'alpha': rgba[3]}
401402
return result

firebase_admin/_messaging_utils.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ class AndroidNotification(object):
9797
user clicks it in the panel. When set to ``True``, the notification persists even when
9898
the user clicks it (optional).
9999
event_timestamp: For notifications that inform users about events with an absolute time
100-
reference, sets the time that the event in the notification occurred. Notifications
101-
in the panel are sorted by this time (optional).
100+
reference, sets the time that the event in the notification occurred as a
101+
``datetime.datetime`` instance. Notifications in the panel are sorted by this time
102+
(optional).
102103
local_only: Set whether or not this notification is relevant only to the current device.
103104
Some notifications can be bridged to other devices for remote display, such as a Wear OS
104105
watch. This hint can be set to recommend this notification not be bridged (optional).
@@ -110,13 +111,12 @@ class AndroidNotification(object):
110111
has been delivered. Whereas ``AndroidMessagePriority`` is an FCM concept that controls
111112
when the message is delivered (optional). Must be one of ``default``, ``min``, ``low``,
112113
``high``, ``max`` or ``normal``.
113-
vibrate_timings_millis: Set the vibration pattern to use. Pass in an array of seconds
114-
to turn the vibrator on or off (optional). The first value indicates the duration to
115-
wait before turning the vibrator on. The next value indicates the duration to keep the
116-
vibrator on. Subsequent values alternate between duration to turn the vibrator off and
117-
to turn the vibrator on. If ``vibrate_timings_millis`` is set and
118-
``default_vibrate_timings`` is set to ``True``, the default value is used instead of the
119-
user-specified ``vibrate_timings_millis``.
114+
vibrate_timings_millis: Sets the vibration pattern to use. Pass in an array of milliseconds
115+
to turn the vibrator on or off. The first value indicates the duration to wait before
116+
turning the vibrator on. The next value indicates the duration to keep the vibrator on.
117+
Subsequent values alternate between duration to turn the vibrator off and to turn the
118+
vibrator on. If ``vibrate_timings`` is set and ``default_vibrate_timings`` is set to
119+
``True``, the default value is used instead of the user-specified ``vibrate_timings``.
120120
default_vibrate_timings: If set to ``True``, use the Android framework's default vibrate
121121
pattern for the notification (optional). Default values are specified in ``config.xml``
122122
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/config.xml.
@@ -183,14 +183,14 @@ class LightSettings(object):
183183
``messaging.AndroidNotification``.
184184
185185
Args:
186-
color: Set color of the LED in ``#rrggbb`` or ``#rrggbbaa`` format (required).
186+
color: Set color of the LED in ``#rrggbb`` or ``#rrggbbaa`` format.
187187
light_on_duration_millis: Along with ``light_off_duration``, define the blink rate of LED
188-
flashes (required).
188+
flashes.
189189
light_off_duration_millis: Along with ``light_on_duration``, define the blink rate of LED
190-
flashes (required).
190+
flashes.
191191
"""
192-
def __init__(self, color=None, light_on_duration_millis=None,
193-
light_off_duration_millis=None):
192+
def __init__(self, color, light_on_duration_millis,
193+
light_off_duration_millis):
194194
self.color = color
195195
self.light_on_duration_millis = light_on_duration_millis
196196
self.light_off_duration_millis = light_off_duration_millis

integration/test_messaging.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ def test_send():
4242
image='https://images.unsplash.com/'
4343
'photo-1494438639946-1ebd1d20bf85?fit=crop&w=900&q=60',
4444
event_timestamp=datetime.now(),
45-
priority='high', vibrate_timings_millis=[100, 200, 300, 400],
46-
visibility='public', light_settings=messaging.LightSettings(
45+
priority='high',
46+
vibrate_timings_millis=[100, 200, 300, 400],
47+
visibility='public',
48+
light_settings=messaging.LightSettings(
4749
color='#aabbcc',
4850
light_off_duration_millis=200,
4951
light_on_duration_millis=300

tests/test_messaging.py

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
NON_DICT_ARGS = ['', list(), tuple(), True, False, 1, 0, {1: 'foo'}, {'foo': 1}]
3333
NON_OBJECT_ARGS = [list(), tuple(), dict(), 'foo', 0, 1, True, False]
3434
NON_LIST_ARGS = ['', tuple(), dict(), True, False, 1, 0, [1], ['foo', 1]]
35+
NON_UNUM_ARGS = ['1.23s', list(), tuple(), dict(), -1.23]
3536
HTTP_ERROR_CODES = {
3637
400: exceptions.InvalidArgumentError,
3738
403: exceptions.PermissionDeniedError,
@@ -292,7 +293,7 @@ def test_invalid_priority(self, data):
292293
else:
293294
assert str(excinfo.value) == 'AndroidConfig.priority must be a non-empty string.'
294295

295-
@pytest.mark.parametrize('data', ['1.23s', list(), tuple(), dict(), -1.23])
296+
@pytest.mark.parametrize('data', NON_UNUM_ARGS)
296297
def test_invalid_ttl(self, data):
297298
with pytest.raises(ValueError) as excinfo:
298299
check_encoding(messaging.Message(
@@ -479,60 +480,51 @@ def test_invalid_channel_id(self, data):
479480
excinfo = self._check_notification(notification)
480481
assert str(excinfo.value) == 'AndroidNotification.channel_id must be a string.'
481482

482-
@pytest.mark.parametrize('data', NON_STRING_ARGS)
483-
def test_invalid_event_timestamp(self, data):
484-
notification = messaging.AndroidNotification(event_timestamp=data)
483+
@pytest.mark.parametrize('timestamp', [100, '', 'foo', {}, [], list(), dict()])
484+
def test_invalid_event_timestamp(self, timestamp):
485+
notification = messaging.AndroidNotification(event_timestamp=timestamp)
485486
excinfo = self._check_notification(notification)
486487
expected = 'AndroidNotification.event_timestamp must be a datetime.'
487488
assert str(excinfo.value) == expected
488489

489-
@pytest.mark.parametrize('data', NON_STRING_ARGS + ['', 'topic', 'priority', 'foo'])
490-
def test_invalid_priority(self, data):
491-
notification = messaging.AndroidNotification(priority=data)
490+
@pytest.mark.parametrize('priority', NON_STRING_ARGS + ['foo'])
491+
def test_invalid_priority(self, priority):
492+
notification = messaging.AndroidNotification(priority=priority)
492493
excinfo = self._check_notification(notification)
493-
if isinstance(data, six.string_types):
494-
if not data:
494+
if isinstance(priority, six.string_types):
495+
if not priority:
495496
expected = 'AndroidNotification.priority must be a non-empty string.'
496497
else:
497498
expected = ('AndroidNotification.priority must be "default", "min", "low", "high" '
498499
'or "max".')
499-
assert str(excinfo.value) == expected
500500
else:
501501
expected = 'AndroidNotification.priority must be a non-empty string.'
502-
assert str(excinfo.value) == expected
502+
assert str(excinfo.value) == expected
503503

504-
@pytest.mark.parametrize('data', NON_STRING_ARGS + ['', 'topic', 'priority', 'foo'])
505-
def test_invalid_visibility(self, data):
506-
notification = messaging.AndroidNotification(visibility=data)
504+
@pytest.mark.parametrize('visibility', NON_STRING_ARGS + ['', 'topic', 'priority', 'foo'])
505+
def test_invalid_visibility(self, visibility):
506+
notification = messaging.AndroidNotification(visibility=visibility)
507507
excinfo = self._check_notification(notification)
508-
if isinstance(data, six.string_types):
509-
if not data:
508+
if isinstance(visibility, six.string_types):
509+
if not visibility:
510510
expected = 'AndroidNotification.visibility must be a non-empty string.'
511511
else:
512512
expected = ('AndroidNotification.visibility must be "private", "public" or'
513513
' "secret".')
514-
assert str(excinfo.value) == expected
515514
else:
516515
expected = 'AndroidNotification.visibility must be a non-empty string.'
517-
assert str(excinfo.value) == expected
516+
assert str(excinfo.value) == expected
518517

519-
@pytest.mark.parametrize('data', ['', 1, True, 'msec', ['500', 500], [0, 'abc']])
520-
def test_invalid_vibrate_timings_millis(self, data):
521-
notification = messaging.AndroidNotification(vibrate_timings_millis=data)
518+
@pytest.mark.parametrize('vibrate_timings', ['', 1, True, 'msec', ['500', 500], [0, 'abc']])
519+
def test_invalid_vibrate_timings_millis(self, vibrate_timings):
520+
notification = messaging.AndroidNotification(vibrate_timings_millis=vibrate_timings)
522521
excinfo = self._check_notification(notification)
523-
if isinstance(data, list):
522+
if isinstance(vibrate_timings, list):
524523
expected = ('AndroidNotification.vibrate_timings_millis must not contain non-number '
525524
'values.')
526-
assert str(excinfo.value) == expected
527525
else:
528526
expected = 'AndroidNotification.vibrate_timings_millis must be a list of numbers.'
529-
assert str(excinfo.value) == expected
530-
531-
@pytest.mark.parametrize('data', ['', 'foo', list(), tuple(), dict()])
532-
def test_invalid_notification_count(self, data):
533-
notification = messaging.AndroidNotification(notification_count=data)
534-
excinfo = self._check_notification(notification)
535-
assert str(excinfo.value) == 'AndroidNotification.notification_count must be a number.'
527+
assert str(excinfo.value) == expected
536528

537529
def test_negative_vibrate_timings_millis(self):
538530
notification = messaging.AndroidNotification(
@@ -541,6 +533,12 @@ def test_negative_vibrate_timings_millis(self):
541533
expected = 'AndroidNotification.vibrate_timings_millis must not be negative.'
542534
assert str(excinfo.value) == expected
543535

536+
@pytest.mark.parametrize('notification_count', ['', 'foo', list(), tuple(), dict()])
537+
def test_invalid_notification_count(self, notification_count):
538+
notification = messaging.AndroidNotification(notification_count=notification_count)
539+
excinfo = self._check_notification(notification)
540+
assert str(excinfo.value) == 'AndroidNotification.notification_count must be a number.'
541+
544542
def test_android_notification(self):
545543
msg = messaging.Message(
546544
topic='topic',
@@ -628,27 +626,27 @@ def test_invalid_light_settings(self, data):
628626
assert str(excinfo.value) == expected
629627

630628
def test_no_color(self):
631-
light_settings = messaging.LightSettings(light_on_duration_millis=200,
629+
light_settings = messaging.LightSettings(color=None, light_on_duration_millis=200,
632630
light_off_duration_millis=200)
633631
excinfo = self._check_light_settings(light_settings)
634632
expected = 'LightSettings.color is required.'
635633
assert str(excinfo.value) == expected
636634

637635
def test_no_light_on_duration_millis(self):
638-
light_settings = messaging.LightSettings(color='#aabbcc',
636+
light_settings = messaging.LightSettings(color='#aabbcc', light_on_duration_millis=None,
639637
light_off_duration_millis=200)
640638
excinfo = self._check_light_settings(light_settings)
641639
expected = 'LightSettings.light_on_duration_millis is required.'
642640
assert str(excinfo.value) == expected
643641

644642
def test_no_light_off_duration_millis(self):
645-
light_settings = messaging.LightSettings(color='#aabbcc',
646-
light_on_duration_millis=200)
643+
light_settings = messaging.LightSettings(color='#aabbcc', light_on_duration_millis=200,
644+
light_off_duration_millis=None)
647645
excinfo = self._check_light_settings(light_settings)
648646
expected = 'LightSettings.light_off_duration_millis is required.'
649647
assert str(excinfo.value) == expected
650648

651-
@pytest.mark.parametrize('data', ['1.23s', list(), tuple(), dict(), -1.23])
649+
@pytest.mark.parametrize('data', NON_UNUM_ARGS)
652650
def test_invalid_light_off_duration_millis(self, data):
653651
light_settings = messaging.LightSettings(color='#aabbcc',
654652
light_on_duration_millis=200,
@@ -662,7 +660,7 @@ def test_invalid_light_off_duration_millis(self, data):
662660
'duration in milliseconds or '
663661
'an instance of datetime.timedelta.')
664662

665-
@pytest.mark.parametrize('data', ['1.23s', list(), tuple(), dict(), -1.23])
663+
@pytest.mark.parametrize('data', NON_UNUM_ARGS)
666664
def test_invalid_light_on_duration_millis(self, data):
667665
light_settings = messaging.LightSettings(color='#aabbcc',
668666
light_on_duration_millis=data,

0 commit comments

Comments
 (0)