Skip to content

Commit

Permalink
Log a warning message when some message strings are too long. (#5)
Browse files Browse the repository at this point in the history
* Log a warning message when some message strings are too long.

Some title and subtitle fields can be longer than the soft limit given in the
documentation. They will be truncated when displayed on Messenger, and the Send
API does not raise any error.

* Add pytest-catchlog to requirements
  • Loading branch information
Sébastien Fievet authored and rickydunlop committed Mar 21, 2017
1 parent 616a185 commit ef4b5c3
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 19 deletions.
20 changes: 15 additions & 5 deletions fbmessenger/elements.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
from __future__ import absolute_import

import logging

from .error_messages import CHARACTER_LIMIT_MESSAGE

logger = logging.getLogger(__name__)


class Text(object):
def __init__(self, text, quick_replies=None):
self.text = text
Expand Down Expand Up @@ -39,7 +48,8 @@ def __init__(self, button_type, title=None, url=None,
if webview_height_ratio and webview_height_ratio not in self.WEBVIEW_HEIGHT_RATIOS:
raise ValueError('Invalid webview_height_ratio provided.')
if title and len(title) > 20:
raise ValueError('Title cannot be longer 20 characters.')
logger.warning(CHARACTER_LIMIT_MESSAGE.format(field='Title',
maxsize=20))

self.button_type = button_type
self.title = title
Expand Down Expand Up @@ -95,9 +105,8 @@ def title(self):
@title.setter
def title(self, title):
if len(title) > 80:
raise ValueError(
'Title cannot be longer 80 characters'
)
logger.warning(CHARACTER_LIMIT_MESSAGE.format(field='Title',
maxsize=80))
self._title = title

@property
Expand All @@ -107,7 +116,8 @@ def subtitle(self):
@subtitle.setter
def subtitle(self, subtitle):
if subtitle is not None and len(subtitle) > 80:
raise ValueError('Subtitle cannot be longer 80 characters')
logger.warning(CHARACTER_LIMIT_MESSAGE.format(field='Subtitle',
maxsize=80))
self._subtitle = subtitle

def to_dict(self):
Expand Down
2 changes: 2 additions & 0 deletions fbmessenger/error_messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CHARACTER_LIMIT_MESSAGE = ("{field} is longer than {maxsize} characters, "
"it will be truncated when displayed.")
12 changes: 11 additions & 1 deletion fbmessenger/quick_replies.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
from __future__ import absolute_import

import logging

from .error_messages import CHARACTER_LIMIT_MESSAGE

logger = logging.getLogger(__name__)


class QuickReply(object):

CONTENT_TYPES = [
Expand All @@ -12,7 +21,8 @@ def __init__(self, title=None, payload=None, image_url=None, content_type=None):
if content_type not in self.CONTENT_TYPES:
raise ValueError('Invalid content_type provided.')
if title and len(title) > 20:
raise ValueError('Title cannot be longer 20 characters.')
logger.warning(CHARACTER_LIMIT_MESSAGE.format(field='Title',
maxsize=20))
if payload and len(payload) > 1000:
raise ValueError('Payload cannot be longer 1000 characters.')

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pbr==1.9.1
py==1.4.31
pyandoc==0.2.0
pytest==2.8.7
pytest-catchlog==1.2.2
pytest-cov==2.2.1
requests==2.10.0
six==1.10.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'pytest',
'coverage',
'pytest-cov',
'pytest-catchlog',
'responses'
]

Expand Down
30 changes: 20 additions & 10 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import logging

import pytest

from fbmessenger import elements
from fbmessenger import quick_replies
from fbmessenger.error_messages import CHARACTER_LIMIT_MESSAGE


class TestElements:
Expand Down Expand Up @@ -71,12 +74,15 @@ def test_url_button(self):
}
assert expected == res.to_dict()

def test_button_with_title_over_limit(self):
with pytest.raises(ValueError) as err:
res = elements.Button(button_type='web_url', title='This button text is over the limit',
def test_button_with_title_over_limit(self, caplog):
with caplog.at_level(logging.WARNING, logger='fbmessenger.elements'):
res = elements.Button(button_type='web_url',
title='This button text is over the limit',
url='http://facebook.com')
res.to_dict()
assert str(err.value) == 'Title cannot be longer 20 characters.'
assert caplog.record_tuples == [
('fbmessenger.elements', logging.WARNING,
CHARACTER_LIMIT_MESSAGE.format(field='Title', maxsize=20))]

def test_element(self):
btn = elements.Button(button_type='web_url', title='Web button', url='http://facebook.com')
Expand Down Expand Up @@ -120,8 +126,8 @@ def test_button_webview_height_ratio_validation(self):
)
assert str(err.value) == 'Invalid webview_height_ratio provided.'

def test_element_title_validation(self):
with pytest.raises(ValueError) as err:
def test_element_title_validation(self, caplog):
with caplog.at_level(logging.WARNING, logger='fbmessenger.elements'):
btn = elements.Button(button_type='web_url', title='Web button', url='http://facebook.com')
res = elements.Element(
title='The title is too long and should throw an error.'
Expand All @@ -134,10 +140,12 @@ def test_element_title_validation(self):
]
)
res.to_dict()
assert str(err.value) == 'Title cannot be longer 80 characters'
assert caplog.record_tuples == [
('fbmessenger.elements', logging.WARNING,
CHARACTER_LIMIT_MESSAGE.format(field='Title', maxsize=80))]

def test_element_subtitle_validation(self):
with pytest.raises(ValueError) as err:
def test_element_subtitle_validation(self, caplog):
with caplog.at_level(logging.WARNING, logger='fbmessenger.elements'):
btn = elements.Button(button_type='web_url', title='Web button', url='http://facebook.com')
res = elements.Element(
title='Title',
Expand All @@ -150,7 +158,9 @@ def test_element_subtitle_validation(self):
]
)
res.to_dict()
assert str(err.value) == 'Subtitle cannot be longer 80 characters'
assert caplog.record_tuples == [
('fbmessenger.elements', logging.WARNING,
CHARACTER_LIMIT_MESSAGE.format(field='Subtitle', maxsize=80))]

def test_adjustment(self):
res = elements.Adjustment(name='discount', amount=1)
Expand Down
11 changes: 8 additions & 3 deletions tests/test_quick_replies.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging

import pytest

from fbmessenger import quick_replies
from fbmessenger.error_messages import CHARACTER_LIMIT_MESSAGE


class TestQuickReplies:
Expand All @@ -19,11 +22,13 @@ def test_quick_reply(self):
}
assert expected == res.to_dict()

def test_quick_reply_title_too_long(self):
with pytest.raises(ValueError) as err:
def test_quick_reply_title_too_long(self, caplog):
with caplog.at_level(logging.WARNING, logger='fbmessenger.elements'):
quick_replies.QuickReply(title='Title is over the 20 character limit',
payload='QR payload')
assert str(err.value) == 'Title cannot be longer 20 characters.'
assert caplog.record_tuples == [
('fbmessenger.quick_replies', logging.WARNING,
CHARACTER_LIMIT_MESSAGE.format(field='Title', maxsize=20))]

def test_quick_reply_payload_too_long(self):
payload = 'x' * 1001
Expand Down

0 comments on commit ef4b5c3

Please sign in to comment.