Skip to content

fix(fcm): String representation in Message class #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions firebase_admin/_messaging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def __init__(self, data=None, notification=None, android=None, webpush=None, apn
self.topic = topic
self.condition = condition

def __str__(self):
return json.dumps(self, cls=MessageEncoder, sort_keys=True)


class MulticastMessage(object):
"""A message that can be sent to multiple tokens via Firebase Cloud Messaging.
Expand Down
28 changes: 28 additions & 0 deletions tests/test_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,34 @@ def check_exception(exception, message, status):
assert exception.http_response.status_code == status


class TestMessageStr(object):

@pytest.mark.parametrize('msg', [
messaging.Message(),
messaging.Message(topic='topic', token='token'),
messaging.Message(topic='topic', condition='condition'),
messaging.Message(condition='condition', token='token'),
messaging.Message(topic='topic', token='token', condition='condition'),
])
def test_invalid_target_message(self, msg):
with pytest.raises(ValueError) as excinfo:
str(msg)
assert str(
excinfo.value) == 'Exactly one of token, topic or condition must be specified.'

def test_empty_message(self):
assert str(messaging.Message(token='value')) == '{"token": "value"}'
assert str(messaging.Message(topic='value')) == '{"topic": "value"}'
assert str(messaging.Message(condition='value')
) == '{"condition": "value"}'

def test_data_message(self):
assert str(messaging.Message(topic='topic', data={})
) == '{"topic": "topic"}'
assert str(messaging.Message(topic='topic', data={
'k1': 'v1', 'k2': 'v2'})) == '{"data": {"k1": "v1", "k2": "v2"}, "topic": "topic"}'


class TestMulticastMessage(object):

@pytest.mark.parametrize('tokens', NON_LIST_ARGS)
Expand Down