Skip to content

Commit 64e98d1

Browse files
committed
add fid arg and token deprecate warning to Message class and add unit tests
1 parent f564d77 commit 64e98d1

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

firebase_admin/_messaging_encoder.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import math
2020
import numbers
2121
import re
22+
import warnings
2223

2324
from firebase_admin import _messaging_utils
2425

@@ -37,20 +38,29 @@ class Message:
3738
webpush: An instance of ``messaging.WebpushConfig`` (optional).
3839
apns: An instance of ``messaging.ApnsConfig`` (optional).
3940
fcm_options: An instance of ``messaging.FCMOptions`` (optional).
40-
token: The registration token of the device to which the message should be sent (optional).
41+
fid: The Firebase installation ID of an FCM registered app instance to which the
42+
message should be sent (optional)
43+
token: Deprecated. Use ``fid`` instead.
4144
topic: Name of the FCM topic to which the message should be sent (optional). Topic name
4245
may contain the ``/topics/`` prefix.
4346
condition: The FCM condition to which the message should be sent (optional).
4447
"""
4548

4649
def __init__(self, data=None, notification=None, android=None, webpush=None, apns=None,
47-
fcm_options=None, token=None, topic=None, condition=None):
50+
fcm_options=None, fid=None, token=None, topic=None, condition=None):
51+
if token is not None:
52+
warnings.warn(
53+
"Deprecated. Use 'fid' instead.",
54+
DeprecationWarning,
55+
stacklevel=2
56+
)
4857
self.data = data
4958
self.notification = notification
5059
self.android = android
5160
self.webpush = webpush
5261
self.apns = apns
5362
self.fcm_options = fcm_options
63+
self.fid = fid
5464
self.token = token
5565
self.topic = topic
5666
self.condition = condition
@@ -695,16 +705,17 @@ def default(self, o): # pylint: disable=method-hidden
695705
'Message.condition', o.condition, non_empty=True),
696706
'data': _Validators.check_string_dict('Message.data', o.data),
697707
'notification': MessageEncoder.encode_notification(o.notification),
708+
'fid': _Validators.check_string('Message.fid', o.fid, non_empty=True),
698709
'token': _Validators.check_string('Message.token', o.token, non_empty=True),
699710
'topic': _Validators.check_string('Message.topic', o.topic, non_empty=True),
700711
'webpush': MessageEncoder.encode_webpush(o.webpush),
701712
'fcm_options': MessageEncoder.encode_fcm_options(o.fcm_options),
702713
}
703714
result['topic'] = MessageEncoder.sanitize_topic_name(result.get('topic'))
704715
result = MessageEncoder.remove_null_values(result)
705-
target_count = sum(t in result for t in ['token', 'topic', 'condition'])
716+
target_count = sum(t in result for t in ['fid', 'token', 'topic', 'condition'])
706717
if target_count != 1:
707-
raise ValueError('Exactly one of token, topic or condition must be specified.')
718+
raise ValueError('Exactly one of fid, token, topic or condition must be specified.')
708719
return result
709720

710721
@classmethod

tests/test_messaging.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,22 @@ class TestMessageStr:
7373
messaging.Message(topic='topic', condition='condition'),
7474
messaging.Message(condition='condition', token='token'),
7575
messaging.Message(topic='topic', token='token', condition='condition'),
76+
messaging.Message(fid='fid', token='token'),
77+
messaging.Message(fid='fid', topic='topic'),
78+
messaging.Message(fid='fid', condition='condition'),
79+
messaging.Message(fid='fid', token='token', topic='topic'),
7680
])
7781
def test_invalid_target_message(self, msg):
7882
with pytest.raises(ValueError) as excinfo:
7983
str(msg)
8084
assert str(
81-
excinfo.value) == 'Exactly one of token, topic or condition must be specified.'
85+
excinfo.value) == 'Exactly one of fid, token, topic or condition must be specified.'
8286

8387
def test_empty_message(self):
8488
assert str(messaging.Message(token='value')) == '{"token": "value"}'
8589
assert str(messaging.Message(topic='value')) == '{"topic": "value"}'
86-
assert str(messaging.Message(condition='value')
87-
) == '{"condition": "value"}'
90+
assert str(messaging.Message(condition='value')) == '{"condition": "value"}'
91+
assert str(messaging.Message(fid='value')) == '{"fid": "value"}'
8892

8993
def test_data_message(self):
9094
assert str(messaging.Message(topic='topic', data={})
@@ -128,18 +132,28 @@ class TestMessageEncoder:
128132
messaging.Message(topic='topic', condition='condition'),
129133
messaging.Message(condition='condition', token='token'),
130134
messaging.Message(topic='topic', token='token', condition='condition'),
135+
messaging.Message(fid='fid', token='token'),
136+
messaging.Message(fid='fid', topic='topic'),
137+
messaging.Message(fid='fid', condition='condition'),
138+
messaging.Message(fid='fid', token='token', topic='topic'),
131139
])
132140
def test_invalid_target_message(self, msg):
133141
with pytest.raises(ValueError) as excinfo:
134142
check_encoding(msg)
135-
assert str(excinfo.value) == 'Exactly one of token, topic or condition must be specified.'
143+
assert str(excinfo.value) == 'Exactly one of fid, token, topic or condition must be specified.'
136144

137145
@pytest.mark.parametrize('target', NON_STRING_ARGS + [''])
138146
def test_invalid_token(self, target):
139147
with pytest.raises(ValueError) as excinfo:
140148
check_encoding(messaging.Message(token=target))
141149
assert str(excinfo.value) == 'Message.token must be a non-empty string.'
142150

151+
@pytest.mark.parametrize('target', NON_STRING_ARGS + [''])
152+
def test_invalid_fid(self, target):
153+
with pytest.raises(ValueError) as excinfo:
154+
check_encoding(messaging.Message(fid=target))
155+
assert str(excinfo.value) == 'Message.fid must be a non-empty string.'
156+
143157
@pytest.mark.parametrize('target', NON_STRING_ARGS + [''])
144158
def test_invalid_topic(self, target):
145159
with pytest.raises(ValueError) as excinfo:
@@ -159,9 +173,14 @@ def test_malformed_topic_name(self, topic):
159173

160174
def test_empty_message(self):
161175
check_encoding(messaging.Message(token='value'), {'token': 'value'})
176+
check_encoding(messaging.Message(fid='value'), {'fid': 'value'})
162177
check_encoding(messaging.Message(topic='value'), {'topic': 'value'})
163178
check_encoding(messaging.Message(condition='value'), {'condition': 'value'})
164179

180+
def test_token_deprecation_warning(self):
181+
with pytest.deprecated_call():
182+
messaging.Message(token='value')
183+
165184
@pytest.mark.parametrize('data', NON_DICT_ARGS)
166185
def test_invalid_data_message(self, data):
167186
with pytest.raises(ValueError):

0 commit comments

Comments
 (0)