Exemplo n.º 1
0
    def test_payload(self):
        recipient = Payload.Recipient(id=123456, phone_number='+8210')
        message = Payload.Message(text="hello", metadata="METADATA",
                                  quick_replies=[{'title': 'Yes', 'payload': 'PICK_YES'}])

        with self.assertRaises(ValueError):
            Payload.Payload(recipient=recipient,
                            message=message,
                            sender_action='NEW_ACTION')

        with self.assertRaises(ValueError):
            Payload.Payload(recipient=recipient,
                            message=message,
                            sender_action='typing_off',
                            notification_type='NEW_NOTIFICATION_TYPE')

        p = Payload.Payload(recipient=recipient,
                            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))

        self.assertTrue(p.__eq__(p))
        self.assertTrue(p.__eq__(utils.to_json(p)))
Exemplo n.º 2
0
    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'
                                     },
                                     {
                                         'type': 'element_share'
                                     },
                                 ])

        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"),
                Template.ButtonShare()
            ])

        self.assertEquals(utils.to_json(btns1), utils.to_json(btns2))
Exemplo n.º 3
0
    def test_message(self):
        with self.assertRaises(Exception):
            m = Payload.Message(text="hello", attachment=Attachment.Image('img'))
        with self.assertRaises(ValueError):
            m = Payload.Message(text="hello", quick_replies=Payload.QuickReply(title='Yes', payload='PICK_YES'))

        m = Payload.Message(text="hello", metadata="METADATA", quick_replies=[{'title': 'Yes', 'payload': 'PICK_YES'}])
        self.assertEquals('{"attachment": null, "metadata": "METADATA", '
                          '"quick_replies": [{"content_type": "text", "payload": "PICK_YES", "title": "Yes"}], '
                          '"text": "hello"}', utils.to_json(m))

        m = Payload.Message(text="hello", metadata="METADATA", quick_replies=[{'title': 'Yes', 'payload': 'PICK_YES'}])
        self.assertEquals('{"attachment": null, "metadata": "METADATA", '
                          '"quick_replies": [{"content_type": "text", "payload": "PICK_YES", "title": "Yes"}], '
                          '"text": "hello"}', utils.to_json(m))
Exemplo n.º 4
0
 def test_account_link(self):
     link = Template.AccountLink(text="title",
                                 account_link_url="http://test.com",
                                 account_unlink_button=True)
     self.assertEquals(
         '{"payload": {"buttons": [{"type": "account_link", "url": "http://test.com"}, '
         '{"type": "account_unlink"}], "template_type": "button", "text": "title"}, '
         '"type": "template"}', utils.to_json(link))
Exemplo n.º 5
0
    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))
Exemplo n.º 6
0
 def test_generic(self):
     generic = Template.Generic(elements=[
         Template.GenericElement(title='generic',
                                 subtitle='subtitle',
                                 item_url='https://test.com',
                                 image_url='https://test.com/img',
                                 buttons=[{
                                     'type': 'web_url',
                                     'title': 'title',
                                     'value': 'https://test.com'
                                 }])
     ])
     self.assertEquals(
         '{"payload": {"elements": [{"buttons": [{"title": "title", "type": "web_url", "url": "https://test.com"}],'
         ' "image_url": "https://test.com/img", "item_url": "https://test.com", "subtitle": "subtitle",'
         ' "title": "generic"}], "image_aspect_ratio": "horizontal", "template_type": "generic"}, "type": "template"}',
         utils.to_json(generic))
Exemplo n.º 7
0
    def test_receipt_template(self):
        receipt_id = "order1357"
        element = Template.ReceiptElement(
            title="Oculus Rift",
            subtitle="Includes: headset, sensor, remote",
            quantity=1,
            price=599.00,
            currency="USD",
            image_url="/assets/riftsq.png")

        address = Template.ReceiptAddress(street_1="1 Hacker Way",
                                          street_2="",
                                          city="Menlo Park",
                                          postal_code="94025",
                                          state="CA",
                                          country="US")

        summary = Template.ReceiptSummary(subtotal=698.99,
                                          shipping_cost=20.00,
                                          total_tax=57.67,
                                          total_cost=626.66)

        adjustment = Template.ReceiptAdjustment(name="New Customer Discount",
                                                amount=-50)

        receipt = Template.Receipt(recipient_name='Peter Chang',
                                   order_number=receipt_id,
                                   currency='USD',
                                   payment_method='Visa 1234',
                                   timestamp="1428444852",
                                   elements=[element],
                                   address=address,
                                   summary=summary,
                                   adjustments=[adjustment])

        self.assertEquals(
            '{"payload": {"address": {"city": "Menlo Park", "country": "US", '
            '"postal_code": "94025", "state": "CA", "street_1": "1 Hacker Way", "street_2": ""}, '
            '"adjustments": [{"amount": -50, "name": "New Customer Discount"}], "currency": "USD", '
            '"elements": [{"currency": "USD", "image_url": "/assets/riftsq.png", "price": 599.0, '
            '"quantity": 1, "subtitle": "Includes: headset, sensor, remote", "title": "Oculus Rift"}], '
            '"order_number": "order1357", "payment_method": "Visa 1234", "recipient_name": '
            '"Peter Chang", "summary": {"shipping_cost": 20.0, "subtotal": 698.99, "total_cost": 626.66, '
            '"total_tax": 57.67}, "template_type": "receipt", "timestamp": "1428444852"}, '
            '"type": "template"}', utils.to_json(receipt))
Exemplo n.º 8
0
    def test_list(self):
        listt = Template.List(elements=[
            Template.GenericElement(title='generic1',
                                    subtitle='subtitle1',
                                    item_url='https://test1.com',
                                    image_url='https://test.com1/img',
                                    buttons=[{
                                        'type': 'web_url',
                                        'title': 'title',
                                        'value': 'https://test1.com'
                                    }]),
            Template.GenericElement(title='generic2',
                                    subtitle='subtitle2',
                                    item_url='https://test2.com',
                                    image_url='https://test.com2/img',
                                    buttons=[{
                                        'type': 'web_url',
                                        'title': 'title',
                                        'value': 'https://test2.com'
                                    }])
        ],
                              top_element_style='large',
                              buttons=[{
                                  "title": "View More",
                                  "type": "postback",
                                  "payload": "payload"
                              }, {
                                  "title": "View Less",
                                  "type": "postback",
                                  "payload": "payload"
                              }])

        self.assertEquals(
            '{"payload": {"buttons": [{"payload": "payload", "title": "View More", "type": "postback"}, '
            '{"payload": "payload", "title": "View Less", "type": "postback"}], '
            '"elements": [{"buttons": [{"title": "title", "type": "web_url", "url": "https://test1.com"}], '
            '"image_url": "https://test.com1/img", "item_url": "https://test1.com", "subtitle": "subtitle1", "title": "generic1"}, '
            '{"buttons": [{"title": "title", "type": "web_url", "url": "https://test2.com"}], '
            '"image_url": "https://test.com2/img", "item_url": "https://test2.com", "subtitle": "subtitle2", "title": "generic2"}], '
            '"template_type": "list", '
            '"top_element_style": "large"}, '
            '"type": "template"}', utils.to_json(listt))
Exemplo n.º 9
0
    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"},'
            ' {"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))
Exemplo n.º 10
0
 def test_button_web(self):
     btn = Template.ButtonWeb(title="title", url="https://test.com")
     self.assertEquals(
         '{"title": "title", "type": "web_url", "url": "https://test.com"}',
         utils.to_json(btn))
     print(utils.to_json(btn))
Exemplo n.º 11
0
 def test_image(self):
     image = Attachment.Image('https://test.com/resource')
     self.assertEquals(
         '{"payload": {"url": "https://test.com/resource"}, "type": "image"}',
         utils.to_json(image))
Exemplo n.º 12
0
 def test_button_share(self):
     btn = Template.ButtonShare()
     self.assertEquals('{"type": "element_share"}', utils.to_json(btn))
     print(utils.to_json(btn))
Exemplo n.º 13
0
 def test_receipt(self):
     q = Payload.Recipient(id=123456)
     self.assertEquals('{"id": 123456}', utils.to_json(q))
Exemplo n.º 14
0
 def test_receipt_with_phone(self):
     q = Payload.Recipient(id=123456, phone_number='+8210')
     self.assertEquals('{"id": 123456, "phone_number": "+8210"}', utils.to_json(q))
Exemplo n.º 15
0
 def test_button_phone(self):
     btn = Template.ButtonPhoneNumber(title="title", payload="+82108011")
     self.assertEquals(
         '{"payload": "+82108011", "title": "title", "type": "phone_number"}',
         utils.to_json(btn))
     print(utils.to_json(btn))
Exemplo n.º 16
0
 def test_quick_reply(self):
     q = Payload.QuickReply(title='Yes', payload='PICK_YES')
     self.assertEquals('{"content_type": "text", "payload": "PICK_YES", "title": "Yes"}',
                       utils.to_json(q))
Exemplo n.º 17
0
 def test_button_postback(self):
     btn = Template.ButtonPostBack(title="title", payload="TEST_PAYLOAD")
     self.assertEquals(
         '{"payload": "TEST_PAYLOAD", "title": "title", "type": "postback"}',
         utils.to_json(btn))
     print(utils.to_json(btn))
Exemplo n.º 18
0
 def test_audio(self):
     audio = Attachment.Audio('https://test.com/resource')
     self.assertEquals(
         '{"payload": {"url": "https://test.com/resource"}, "type": "audio"}',
         utils.to_json(audio))
Exemplo n.º 19
0
 def test_video(self):
     video = Attachment.Video('https://test.com/resource')
     self.assertEquals(
         '{"payload": {"url": "https://test.com/resource"}, "type": "video"}',
         utils.to_json(video))
Exemplo n.º 20
0
 def test_file(self):
     file = Attachment.File('https://test.com/resource')
     self.assertEquals(
         '{"payload": {"url": "https://test.com/resource"}, "type": "file"}',
         utils.to_json(file))