Skip to content

Commit

Permalink
verify webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmadurga committed Apr 22, 2016
1 parent 0c6b373 commit 0788fb5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions microbot/views/hooks/messenger_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def get(self, request, hook_id):
except MessengerBot.DoesNotExist:
logger.warning("Hook id %s not associated to a bot" % hook_id)
return Response(status=status.HTTP_404_NOT_FOUND)
if request.query_params.get('hub.verify_token') == bot.token:
return Response(request.query_params.get('hub.challenge').replace('"',''))
if request.query_params.get('hub.verify_token') == str(bot.id):
return Response(int(request.query_params.get('hub.challenge')))
return Response('Error, wrong validation token')

def create_message(self, webhook_message, bot):
Expand Down
12 changes: 9 additions & 3 deletions tests/functional/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ def test_bot_disabled(self):
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(0, mock_send.call_count)

def test_bot_verify(self):
response = self.client.get(self.messenger_webhook_url)
self.assertEqual(status.HTTP_200_OK, response.status_code)
def test_bot_verify_ok(self):
response = self.client.get(self.messenger_webhook_url, {'hub.mode': 'subscribe', 'hub.challenge': 12345, 'hub.verify_token': self.bot.messenger_bot.id})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(response.data, 12345)

def test_bot_verify_not_ok(self):
response = self.client.get(self.messenger_webhook_url, {'hub.mode': 'subscribe', 'hub.challenge': 12345, 'hub.verify_token': 'other_id'})
self.assertEqual(status.HTTP_200_OK, response.status_code)
self.assertIn('Error', response.data)

0 comments on commit 0788fb5

Please sign in to comment.