Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwz committed Aug 24, 2016
1 parent 03145a6 commit 0ee2425
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fbmq/fbmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def handle_webhook(self, payload, optin=None, message=None, echo=None, delivery=
# Make sure this is a page subscription
if data.get("object") != "page":
print("Webhook failed, only support page subscription")
return
return False

# Iterate over each entry
# There may be multiple if batched
Expand Down
61 changes: 61 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ class PageTest(unittest.TestCase):
def setUp(self):
self.page = Page('TOKEN')
self.page._send = mock.MagicMock()
self.page._fetch_page_info = mock.MagicMock()

def test_send(self):
self.page.send(12345, "hello world", quick_replies=[{'title': 'Yes', 'payload': 'YES'}])
self.page._send.assert_called_once_with('{"message": {"attachment": null, "metadata": null, '
'"quick_replies": '
'[{"content_type": "text", "payload": "YES", "title": "Yes"}], '
'"text": "hello world"},'
' "notification_type": null, '
'"recipient": {"id": 12345, "phone_number": null}, '
'"sender_action": null}')

def test_typingon(self):
self.page.typing_on(1004)
Expand All @@ -30,6 +41,55 @@ def test_markseen(self):
'"recipient": {"id": 1004, "phone_number": null}, '
'"sender_action": "mark_seen"}')

def test_handle_webhook_errors(self):
payload = """
{
"object":"not_a_page",
"entry":[
{"id":"1691462197845448","time":1472026867114,
"messaging":[
{"sender":{"id":"1134343043305865"},"recipient":{"id":"1691462197845448"},"timestamp":1472026867080,
"message":{"mid":"mid.1472026867074:cfb5e1d4bde07a2a55","seq":812,"text":"hello world"}}
]}
]
}
"""
self.assertFalse(self.page.handle_webhook(payload))

payload = """
{
"object":"page",
"entry":[
{"id":"1691462197845448","time":1472026867114,
"messaging":[
{"sender":{"id":"1134343043305865"},"recipient":{"id":"1691462197845448"},"timestamp":1472026867080,
"unknown":{"mid":"mid.1472026867074:cfb5e1d4bde07a2a55","seq":812,"text":"hello world"}}
]}
]
}
"""

self.page.handle_webhook(payload)

@self.page.callback_quick_reply
@self.page.callback_button
def unknown():
pass

def test_page_info(self):
self.assertEquals(0, self.page._fetch_page_info.call_count)
self.page.page_id
self.assertEquals(1, self.page._fetch_page_info.call_count)
self.page.page_name
self.assertEquals(2, self.page._fetch_page_info.call_count)

self.page._page_id = 1
self.page._page_name = 'name'
print(self.page.page_id, self.page.page_name)

self.assertEquals(2, self.page._fetch_page_info.call_count)


def test_handle_webhook_message(self):
payload = """
{
Expand All @@ -44,6 +104,7 @@ def test_handle_webhook_message(self):
}
"""
counter = mock.MagicMock()
self.page.handle_webhook(payload)

@self.page.handle_message
def handler1(event):
Expand Down
12 changes: 11 additions & 1 deletion tests/test_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ def test_quick_reply(self):

def test_quick_reply_shortcut(self):
q = Payload.Message.convert_shortcut_quick_reply([{'title': 'Yes', 'payload': 'PICK_YES'}])
q = Payload.Message.convert_shortcut_quick_reply(q)
self.assertEquals('[{"content_type": "text", "payload": "PICK_YES", "title": "Yes"}]',
utils.to_json(q))

with self.assertRaises(ValueError) as context:
Payload.Message.convert_shortcut_quick_reply(['hello'])

self.assertEquals(None, Payload.Message.convert_shortcut_quick_reply(None))

def test_receipt(self):
q = Payload.Recipient(id=123456, phone_number='+8210')
self.assertEquals('{"id": 123456, "phone_number": "+8210"}', utils.to_json(q))
Expand All @@ -40,8 +46,12 @@ def test_payload(self):
message=message,
sender_action='typing_off',
notification_type='REGULAR')

self.assertEquals(
'{"message": {"attachment": null, "metadata": "METADATA", "quick_replies": '
'[{"content_type": "text", "payload": "PICK_YES", "title": "Yes"}], "text": "hello"},'
' "notification_type": "REGULAR", "recipient": {"id": 123456, "phone_number": "+8210"},'
' "sender_action": "typing_off"}', utils.to_json(p))
' "sender_action": "typing_off"}', utils.to_json(p))

self.assertTrue(p.__eq__(p))
self.assertTrue(p.__eq__(utils.to_json(p)))
24 changes: 23 additions & 1 deletion tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,41 @@ def test_button_phone(self):
self.assertEquals('{"payload": "+82108011", "title": "title", "type": "phone_number"}', utils.to_json(btn))
print(utils.to_json(btn))

def test_buttons(self):
btns1 = Template.Buttons(text="Title", buttons=[
{'type': 'web_url', 'title': 'title', 'value': 'https://test.com'},
{'type': 'postback', 'title': 'title', 'value': 'TEST_PAYLOAD'},
{'type': 'phone_number', 'title': 'title', 'value': '+82108011'},
])

btns2 = Template.Buttons(text="Title", buttons=[
Template.ButtonWeb(title="title", url="https://test.com"),
Template.ButtonPostBack(title="title", payload="TEST_PAYLOAD"),
Template.ButtonPhoneNumber(title="title", payload="+82108011")
])

self.assertEquals(utils.to_json(btns1), utils.to_json(btns2))

def test_button_shortcut(self):
btns = Template.Buttons.convert_shortcut_buttons([
{'type': 'web_url', 'title': 'title', 'value': 'https://test.com'},
{'type': 'postback', 'title': 'title', 'value': 'TEST_PAYLOAD'},
{'type': 'phone_number', 'title': 'title', 'value': '+82108011'},
Template.ButtonWeb(title="title", url="https://test.com"),
])
self.assertEquals('[{"title": "title", "type": "web_url", "url": "https://test.com"},'
' {"payload": "TEST_PAYLOAD", "title": "title", "type": "postback"},'
' {"payload": "+82108011", "title": "title", "type": "phone_number"}]', utils.to_json(btns))
' {"payload": "+82108011", "title": "title", "type": "phone_number"},'
' {"title": "title", "type": "web_url", "url": "https://test.com"}]', utils.to_json(btns))

with self.assertRaises(ValueError) as context:
Template.Buttons.convert_shortcut_buttons([{'type': 'url', 'title': 'title', 'value': 'https://test.com'}])

with self.assertRaises(ValueError) as context:
Template.Buttons.convert_shortcut_buttons(['hello'])

self.assertEquals(None, Template.Buttons.convert_shortcut_buttons(None))

def test_generic(self):
generic = Template.Generic(
elements=[Template.GenericElement(title='generic', subtitle='subtitle', item_url='https://test.com',
Expand Down

0 comments on commit 0ee2425

Please sign in to comment.