class TestNaverTalkAPI(unittest.TestCase):
    def setUp(self):
        self.tested = NaverTalkApi('test_naver_talk_access_token')

    @responses.activate
    def test_send_composite(self):
        responses.add(responses.POST,
                      NaverTalkApi.DEFAULT_API_ENDPOINT,
                      json={
                          "success": True,
                          "resultCode": "00"
                      },
                      status=200)

        counter = mock.MagicMock()

        def test_callback(res, payload):
            self.assertEqual(res.result_code, "00")
            self.assertEqual(res.success, True)
            self.assertEqual(
                payload.as_json_dict(), {
                    'event': 'send',
                    'user': '******',
                    'compositeContent': {
                        'compositeList': [{
                            'title': 'test_title',
                            'description': 'test_descript',
                            'image': {
                                'imageUrl': 'test_image'
                            },
                            'elementList': {
                                'type':
                                'LIST',
                                'data': [{
                                    'title': 'test_ed_title',
                                    'description': 'test_ed_descript',
                                    'subDescription': 'test_ed_subdescript',
                                    'image': {
                                        'imageUrl': 'test_ed_image'
                                    },
                                    'button': {
                                        'type': 'TEXT',
                                        'data': {
                                            'title': 'test'
                                        }
                                    }
                                }]
                            },
                            'buttonList': None
                        }]
                    },
                    'options': {
                        'notification': False
                    }
                })
            counter()

        self.tested.send(
            'test_user_id',
            message=CompositeContent(composite_list=[
                Composite(title='test_title',
                          description='test_descript',
                          image='test_image',
                          element_list=ElementList([
                              ElementData(
                                  title='test_ed_title',
                                  description='test_ed_descript',
                                  sub_description='test_ed_subdescript',
                                  image='test_ed_image',
                                  button=ButtonText('test'))
                          ]))
            ]),
            callback=test_callback)
        self.assertEqual(counter.call_count, 1)

    @responses.activate
    def test_send_composite_with_quick_reply(self):
        responses.add(responses.POST,
                      NaverTalkApi.DEFAULT_API_ENDPOINT,
                      json={
                          "success": True,
                          "resultCode": "00"
                      },
                      status=200)

        counter = mock.MagicMock()

        def test_callback(res, payload):
            self.assertEqual(res.result_code, "00")
            self.assertEqual(res.success, True)
            self.assertEqual(
                payload.as_json_dict(), {
                    'event': 'send',
                    'user': '******',
                    'compositeContent': {
                        'compositeList': [{
                            'title': 'test_title',
                            'description': None,
                            'elementList': None,
                            'buttonList': None
                        }],
                        'quickReply': {
                            'buttonList': [{
                                'data': {
                                    'code': 'PAYLOAD',
                                    'title': 'text'
                                },
                                'type': 'TEXT'
                            }, {
                                'data': {
                                    'mobileUrl': None,
                                    'title': 'text',
                                    'url': 'PAYLOAD'
                                },
                                'type': 'LINK'
                            }]
                        }
                    },
                    'options': {
                        'notification': False
                    }
                })
            counter()

        self.tested.send('test_user_id',
                         message=CompositeContent(
                             composite_list=[Composite(title='test_title')]),
                         quick_reply=QuickReply([{
                             'type': 'TEXT',
                             'title': 'text',
                             'value': 'PAYLOAD'
                         }, {
                             'type': 'LINK',
                             'title': 'text',
                             'value': 'PAYLOAD'
                         }]),
                         callback=test_callback)
        self.assertEqual(counter.call_count, 1)

        self.tested.send('test_user_id',
                         message=CompositeContent(
                             composite_list=[Composite(title='test_title')],
                             quick_reply=[
                                 ButtonText('text', 'PAYLOAD'),
                                 ButtonLink('text', 'PAYLOAD')
                             ]),
                         callback=test_callback)
        self.assertEqual(counter.call_count, 2)

    @responses.activate
    def test_composite_with_calendar(self):
        responses.add(responses.POST,
                      NaverTalkApi.DEFAULT_API_ENDPOINT,
                      json={
                          "success": True,
                          "resultCode": "00"
                      },
                      status=200)

        counter = mock.MagicMock()

        def test_callback(res, payload):
            target = {
                "event": "send",
                "user": "******",
                "compositeContent": {
                    "compositeList": [{
                        "title":
                        "톡톡 레스토랑",
                        "description":
                        "파스타가 맛있는집",
                        'elementList':
                        None,
                        "buttonList": [{
                            "type": "CALENDAR",
                            "data": {
                                "title": "방문 날짜 선택하기",
                                "code": "code_for_your_bot",
                                "options": {
                                    "calendar": {
                                        "placeholder": "방문 날짜를 선택해주세요.",
                                        "start": "20180301",
                                        "end": "20180430",
                                        "disables":
                                        "1,20180309,20180315-20180316"
                                    }
                                }
                            }
                        }]
                    }]
                },
                'options': {
                    'notification': False
                }
            }
            self.assertEqual(target, payload.as_json_dict())
            counter()

        self.tested.send(
            "test_user_id",
            message=CompositeContent(composite_list=[
                Composite(title="톡톡 레스토랑",
                          description="파스타가 맛있는집",
                          button_list=[
                              ButtonCalendar(
                                  title="방문 날짜 선택하기",
                                  code="code_for_your_bot",
                                  placeholder="방문 날짜를 선택해주세요.",
                                  start="20180301",
                                  end="20180430",
                                  disables="1,20180309,20180315-20180316")
                          ])
            ]),
            callback=test_callback)
        self.assertEqual(counter.call_count, 1)
class TestNaverTalkApi(unittest.TestCase):
    def setUp(self):
        self.tested = NaverTalkApi('test_naver_talk_access_token')

    @responses.activate
    def test_connection_error_handle(self):
        responses.add(
            responses.POST,
            NaverTalkApi.DEFAULT_API_ENDPOINT,
            json={
                "message": "Internal Server Error"
            },
            status=500
        )

        try:
            self.tested.send('test_id', 'test message')
        except NaverTalkApiConnectionError as e:
            self.assertEqual(e.status_code, 500)
            self.assertEqual(e.message, '{"message": "Internal Server Error"}')


    @responses.activate
    def test_error_with_detail_handle(self):
        responses.add(
            responses.POST,
            NaverTalkApi.DEFAULT_API_ENDPOINT,
            json={
                'success': False,
                'resultCode': "02",
                'resultMessage': "request json 문자열 파싱 에러"
            },
            status=200
        )

        try:
            self.tested.send('test_id', 'test message')
        except NaverTalkApiError as e:
            self.assertEqual(e.status_code, 200)
            self.assertEqual(e.result_code, "02")
            self.assertEqual(e.message, 'request json 문자열 파싱 에러')

    @responses.activate
    def test_error_handle_get_user_profile(self):
        responses.add(
            responses.POST,
            NaverTalkApi.DEFAULT_API_ENDPOINT,
            json={
                'success': False,
                'resultCode': "02",
                'resultMessage': "request json 문자열 파싱 에러"
            },
            status=200
        )

        try:
            self.tested.request_profile('test_id', 'nickname')
        except NaverTalkApiError as e:
            self.assertEqual(e.status_code, 200)
            self.assertEqual(e.result_code, "02")
            self.assertEqual(e.message, 'request json 문자열 파싱 에러')
            self.assertEqual("%s" % e, "<NaverTalkApiError [request json 문자열 파싱 에러]>")

    @responses.activate
    def test_error_handle_upload_image_url(self):
        responses.add(
            responses.POST,
            NaverTalkApi.DEFAULT_API_ENDPOINT,
            json={
                'success': False,
                'resultCode': "IMG-99",
                'resultMessage': "이미지 업로드 중 에러"
            },
            status=200
        )

        try:
            self.tested.upload_image('https://example.com/test.jpg')
        except NaverTalkApiError as e:
            self.assertEqual(e.status_code, 200)
            self.assertEqual(e.result_code, "IMG-99")
            self.assertEqual(e.message, "이미지 업로드 중 에러")
            self.assertEqual("%s" % e, "<NaverTalkApiError [이미지 업로드 중 에러]>")

    def test_callback_error(self):
        with self.assertRaises(ValueError):
            @self.tested.callback('Hello')
            def callback_test(event):
                pass

    def test_naver_pay(self):
        req = {
            "event": "pay_complete",
            "user": "******",
            "options": {
                "paymentResult": {
                    "code" : "Success",
                    "paymentId" : "20170811D3adfaasLL",
                    "merchantPayKey" : "bot-custom-pay-key-1234",
                    "merchantUserKey" : "al-2eGuGr5WQOnco1_V-FQ",
	            }
            }
        }
        @self.tested.handle_pay_complete
        def pay_complete_fail_error(event):
            raise NaverTalkPaymentError('재고 없음')

        try:
            self.tested.webhook_handler(json.dumps(req))
        except NaverTalkPaymentError as e:
            self.assertEqual(e.message, "재고 없음")
            self.assertEqual("%s" % e, "<NaverTalkPaymentError [재고 없음]>")
Beispiel #3
0
class TestNaverTalkAPI(unittest.TestCase):
    def setUp(self):
        self.tested = NaverTalkApi('test_naver_talk_access_token')

    @responses.activate
    def test_send_image(self):
        responses.add(responses.POST,
                      NaverTalkApi.DEFAULT_API_ENDPOINT,
                      json={
                          "success": True,
                          "resultCode": "00"
                      },
                      status=200)

        counter = mock.MagicMock()

        def test_callback(res, payload):
            self.assertEqual(res.result_code, "00")
            self.assertEqual(res.success, True)
            self.assertEqual(
                payload, {
                    'event': 'send',
                    'user': '******',
                    'imageContent': {
                        'imageUrl': 'test.jpg',
                    },
                    'options': {
                        'notification': False
                    }
                })
            counter()

        self.tested.send('test_user_id',
                         ImageContent('test.jpg'),
                         callback=test_callback)
        self.assertEqual(counter.call_count, 1)

        counter2 = mock.MagicMock()

        def test_image_id_callback(res, payload):
            self.assertEqual(res.result_code, "00")
            self.assertEqual(res.success, True)
            self.assertEqual(
                payload, {
                    'event': 'send',
                    'user': '******',
                    'imageContent': {
                        'imageId': '1234test',
                    },
                    'options': {
                        'notification': False
                    }
                })
            counter2()

        self.tested.send('test_user_id',
                         ImageContent(image_id='1234test'),
                         callback=test_image_id_callback)

        self.assertEqual(counter2.call_count, 1)

    @responses.activate
    def test_send_image_with_quick_reply(self):
        responses.add(responses.POST,
                      NaverTalkApi.DEFAULT_API_ENDPOINT,
                      json={
                          "success": True,
                          "resultCode": "00"
                      },
                      status=200)

        counter = mock.MagicMock()

        def test_callback(res, payload):
            self.assertEqual(res.result_code, "00")
            self.assertEqual(res.success, True)
            self.assertEqual(
                payload, {
                    'event': 'send',
                    'user': '******',
                    'imageContent': {
                        'imageUrl': 'test.jpg',
                        'quickReply': {
                            'buttonList': [{
                                'data': {
                                    'code': 'PAYLOAD',
                                    'title': 'text'
                                },
                                'type': 'TEXT'
                            }, {
                                'data': {
                                    'mobileUrl': None,
                                    'title': 'text',
                                    'url': 'PAYLOAD'
                                },
                                'type': 'LINK'
                            }]
                        }
                    },
                    'options': {
                        'notification': False
                    }
                })
            counter()

        self.tested.send('test_user_id',
                         ImageContent('test.jpg'),
                         quick_reply=QuickReply([{
                             'type': 'TEXT',
                             'title': 'text',
                             'value': 'PAYLOAD'
                         }, {
                             'type': 'LINK',
                             'title': 'text',
                             'value': 'PAYLOAD'
                         }]),
                         callback=test_callback)
        self.assertEqual(counter.call_count, 1)

        self.tested.send('test_user_id',
                         ImageContent('test.jpg',
                                      quick_reply=QuickReply([
                                          ButtonText('text', 'PAYLOAD'),
                                          ButtonLink('text', 'PAYLOAD')
                                      ])),
                         callback=test_callback)
        self.assertEqual(counter.call_count, 2)
Beispiel #4
0
class TestNaverTalkAPI(unittest.TestCase):
    def setUp(self):
        self.tested = NaverTalkApi('test_naver_talk_access_token')

    @responses.activate
    def test_send_text(self):
        responses.add(
            responses.POST,
            NaverTalkApi.DEFAULT_API_ENDPOINT,
            json={
                "success": True,
                "resultCode": "00"
            },
            status=200
        )

        counter = mock.MagicMock()
        def test_callback(res, payload):
            self.assertEqual(res.result_code, "00")
            self.assertEqual(res.success, True)
            self.assertEqual(
                payload,
                {
                    'event': 'send',
                    'user': '******',
                    'textContent': {
                        'text': 'test_str_message',
                        'code': None,
                        'inputType': None
                    },
                    'options': {
                        'notification': False
                    }
                }
            )
            counter()

        self.tested.send(
            'test_user_id',
            'test_str_message',
            callback=test_callback
        )
        self.assertEqual(counter.call_count, 1)

        self.tested.send(
            user_id='test_user_id',
            message=TextContent('test_str_message'),
            callback=test_callback
        )
        self.assertEqual(counter.call_count, 2)



    @responses.activate
    def test_send_with_quick_reply(self):
        responses.add(
            responses.POST,
            NaverTalkApi.DEFAULT_API_ENDPOINT,
            json={
                "success": True,
                "resultCode": "00"
            },
            status=200
        )

        counter = mock.MagicMock()

        def test_callback(res, payload):
            self.assertEqual(res.result_code, "00")
            self.assertEqual(res.success, True)
            self.assertEqual(
                payload,
                {
                    "event": "send",
                    "user": "******",
                    "options": {
                        "notification": False
                    },
                    "textContent": {
                        "code": None,
                        "inputType": None,
                        "quickReply": {
                            "buttonList": [{
                                              "data": {
                                                  "code": "PAYLOAD",
                                                  "title": "text"},
                                              "type": "TEXT"},
                                          {
                                              "data": {
                                                  "mobileUrl": None,
                                                  "title": "text",
                                                  "url": "PAYLOAD"},
                                              "type": "LINK"}]},
                                      "text": "test_str_message"}
                }
            )

            counter()

        self.tested.send(
            'test_user_id',
            'test_str_message',
            quick_reply=QuickReply(
                [
                    {'type': 'TEXT', 'title': 'text', 'value': 'PAYLOAD'},
                    {'type': 'LINK', 'title': 'text', 'value': 'PAYLOAD'}
                ]
            ),
            callback=test_callback
        )
        self.assertEqual(counter.call_count, 1)

        self.tested.send(
            'test_user_id',
            'test_str_message',
            quick_reply=[
                ButtonText('text', 'PAYLOAD'),
                ButtonLink('text', 'PAYLOAD')
            ],
            callback=test_callback
        )
        self.assertEqual(counter.call_count, 2)