From 6e51e9e2a268f9f88fefd855f1a33cf971da16f5 Mon Sep 17 00:00:00 2001 From: Matt Bogosian Date: Fri, 23 Feb 2018 11:55:14 -0800 Subject: [PATCH] Minor fixes - Fix JSON parsing and error handling - (Hopefully) fix doc generation - Change redirect - Got rid of default fake token value --- emojiwatch/__init__.py | 4 +--- emojiwatch/urls.py | 10 +++++++++- emojiwatch/views.py | 12 +++++++++--- tests/django_settings.py | 4 ++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/emojiwatch/__init__.py b/emojiwatch/__init__.py index 972d519..9de3569 100644 --- a/emojiwatch/__init__.py +++ b/emojiwatch/__init__.py @@ -35,11 +35,9 @@ __all__ = () LOGGER = _logging.getLogger(__name__) - SETTINGS = getattr(d_conf.settings, 'EMOJIWATCH', {}) try: SLACK_VERIFICATION_TOKEN = SETTINGS['slack_verification_token'] except (KeyError, TypeError): - SLACK_VERIFICATION_TOKEN = 'xoxa-NOT-A-REAL-TOKEN' - LOGGER.warning("EMOJIWATCH['slack_verification_token'] setting is missing (using '%s')", SLACK_VERIFICATION_TOKEN) + SLACK_VERIFICATION_TOKEN = None diff --git a/emojiwatch/urls.py b/emojiwatch/urls.py index 802a351..041cc27 100644 --- a/emojiwatch/urls.py +++ b/emojiwatch/urls.py @@ -24,6 +24,11 @@ import django.conf.urls as d_c_urls +from .version import ( + __release__, + __version__, +) + from .views import ( CsrfExemptRedirectView, event_hook_handler, @@ -33,12 +38,15 @@ __all__ = () +_RTD_RELEASE = __release__ if __version__ != (0, 0, 0) else 'latest' +_RTD_URL = 'https://django-emojiwatch.readthedocs.io/en/{}/'.format(_RTD_RELEASE) + app_name = 'emojiwatch' urlpatterns = ( d_c_urls.url(r'event_hook$', event_hook_handler, name='event_hook'), d_c_urls.url(r'', CsrfExemptRedirectView.as_view( permanent=True, - url='https://github.com/posita/django-emojiwatch', + url=_RTD_URL, )), ) diff --git a/emojiwatch/views.py b/emojiwatch/views.py index 6526108..9e4f5e5 100644 --- a/emojiwatch/views.py +++ b/emojiwatch/views.py @@ -112,6 +112,11 @@ def dispatch(self, request, *args, **kw): def event_hook_handler( request, # type: d_http.HttpRequest ): # type: (...) -> d_http.HttpResponse + if not SLACK_VERIFICATION_TOKEN: + LOGGER.critical("EMOJIWATCH['slack_verification_token'] setting is missing") + + return d_http.HttpResponseServerError() + slack_retry_num = request.META.get('HTTP_X_SLACK_RETRY_NUM', 0) slack_retry_reason = request.META.get('HTTP_X_SLACK_RETRY_REASON', None) @@ -120,12 +125,13 @@ def event_hook_handler( content_type = request.META.get('HTTP_CONTENT_TYPE', 'application/json') - if content_type != 'application/json': + if content_type != 'application/json' \ + or request.encoding not in {None, 'utf-8', 'UTF-8', 'csUTF8'}: return d_http.HttpResponse(status_code=415) try: - payload_data = json.loads(request.body.decode('utf-8'), encoding=request.encoding) # type: typing.Dict - except JSONDecodeError: + payload_data = json.loads(request.body.decode('utf-8')) # type: typing.Dict + except (JSONDecodeError, UnicodeDecodeError): LOGGER.info('unable to parse JSON from request body') truncate_len = 1024 half_truncate_len = truncate_len >> 1 diff --git a/tests/django_settings.py b/tests/django_settings.py index 3f267b9..ba6625b 100644 --- a/tests/django_settings.py +++ b/tests/django_settings.py @@ -44,6 +44,10 @@ DEBUG = True +EMOJIWATCH = { + 'slack_verification_token': 'FoRtEsTiNgOnLyNoTrEaLlYaVeRiFiCaTiOnToKeN', +} + INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.auth',