Skip to content

Commit

Permalink
hook bad request error returned
Browse files Browse the repository at this point in the history
  • Loading branch information
jlmadurga committed Apr 13, 2016
1 parent edfed10 commit 93f92ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion microbot/test/testcases.py
Expand Up @@ -96,7 +96,8 @@ def _test_message(self, action, update=None, number=1, no_handler=False):
self.assertEqual(number, Update.objects.count())
self.assertUpdate(Update.objects.get(update_id=update.update_id), update)

def _test_hook(self, action, data, no_hook=False, num_recipients=1, recipients=[], auth=None, status_to_check=None):
def _test_hook(self, action, data, no_hook=False, num_recipients=1, recipients=[], auth=None, status_to_check=None,
error_to_check=None):
with mock.patch("telegram.bot.Bot.sendMessage", callable=mock.MagicMock()) as mock_send:
hook_url = reverse('microbot:hook', kwargs={'key': action['in']})
if auth:
Expand All @@ -108,6 +109,8 @@ def _test_hook(self, action, data, no_hook=False, num_recipients=1, recipients=[
else:
if status_to_check:
self.assertEqual(response.status_code, status_to_check)
if error_to_check:
self.assertIn(error_to_check, response.data)
else:
# Check response 200 OK
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand Down
8 changes: 6 additions & 2 deletions microbot/views/hooks/microbot_hook.py
Expand Up @@ -10,6 +10,7 @@
from rest_framework.authentication import TokenAuthentication
from rest_framework import exceptions
from django.utils.translation import ugettext_lazy as _
from rest_framework.exceptions import ParseError

logger = logging.getLogger(__name__)

Expand All @@ -27,8 +28,11 @@ def post(self, request, key):
if hook.bot.owner != request.user:
raise exceptions.AuthenticationFailed()
try:
logger.debug("Hook %s attending request %s" % (hook, request.data))
handle_hook.delay(hook.id, request.data)
parsed_data = request.data
logger.debug("Hook %s attending request %s" % (hook, parsed_data))
handle_hook.delay(hook.id, parsed_data)
except ParseError as e:
return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
except:
exc_info = sys.exc_info()
traceback.print_exception(*exc_info)
Expand Down
7 changes: 6 additions & 1 deletion tests/functional/test_hook.py
Expand Up @@ -70,4 +70,9 @@ def test_hook_multiple_recipients(self):

def test_not_auth(self):
self._test_hook(self.hook_name, '{"name": "juan"}', num_recipients=1, recipients=[self.recipient.chat_id],
auth=self._gen_token("notoken"), status_to_check=status.HTTP_401_UNAUTHORIZED)
auth=self._gen_token("notoken"), status_to_check=status.HTTP_401_UNAUTHORIZED)

def test_error(self):
self._test_hook(self.hook_name, '{"name": "juan",}', num_recipients=1, recipients=[self.recipient.chat_id],
auth=self._gen_token(self.hook.bot.owner.auth_token), status_to_check=status.HTTP_400_BAD_REQUEST,
error_to_check="JSON parse error")

0 comments on commit 93f92ad

Please sign in to comment.