Skip to content

Commit

Permalink
Fix destination field (#187)
Browse files Browse the repository at this point in the history
* Set None if 'destination' is not in the webhook request
  • Loading branch information
okue committed Jun 21, 2019
1 parent 1386ecf commit 99ac7f0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion linebot/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def parse(self, body, signature, as_payload=False):
LOGGER.warn('Unknown event type. type=' + event_type)

if as_payload:
return WebhookPayload(events=events, destination=body_json['destination'])
return WebhookPayload(events=events, destination=body_json.get('destination'))
else:
return events

Expand Down
51 changes: 46 additions & 5 deletions tests/test_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ def test_validate(self):


class TestWebhookParser(unittest.TestCase):
def setUp(self):
parser = WebhookParser('channel_secret')
# mock
parser.signature_validator.validate = lambda a, b: True
self.parser = parser

def test_parse(self):
file_dir = os.path.dirname(__file__)
webhook_sample_json_path = os.path.join(file_dir, 'text', 'webhook.json')
with open(webhook_sample_json_path) as fp:
body = fp.read()

parser = WebhookParser('channel_secret')
# mock
parser.signature_validator.validate = lambda a, b: True

events = parser.parse(body, 'channel_secret')
events = self.parser.parse(body, 'channel_secret')

# MessageEvent, SourceUser, TextMessage
self.assertIsInstance(events[0], MessageEvent)
Expand Down Expand Up @@ -387,6 +389,45 @@ def test_parse(self):
self.assertIsInstance(events[24].things.result.action_results[1], ActionResult)
self.assertEqual(events[24].things.result.action_results[1].type, 'void')

def test_parse_webhook_req_without_destination(self):
body = """
{
"events": [
{
"replyToken": "00000000000000000000000000000000",
"type": "message",
"timestamp": 1561099010135,
"source": {
"type": "user",
"userId": "Udeadbeefdeadbeefdeadbeefdeadbeef"
},
"message": {
"id": "100001",
"type": "text",
"text": "Hello, world"
}
},
{
"replyToken": "ffffffffffffffffffffffffffffffff",
"type": "message",
"timestamp": 1561099010135,
"source": {
"type": "user",
"userId": "Udeadbeefdeadbeefdeadbeefdeadbeef"
},
"message": {
"id": "100002",
"type": "sticker",
"packageId": "1",
"stickerId": "1"
}
}
]
}
"""
payload = self.parser.parse(body=body, signature='channel_secret', as_payload=True)
self.assertEqual(None, payload.destination)


class TestWebhookHandler(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 99ac7f0

Please sign in to comment.