Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
- Fix JSON parsing and error handling
- (Hopefully) fix doc generation
- Change redirect
- Got rid of default fake token value
  • Loading branch information
posita committed Feb 24, 2018
1 parent 7126ff6 commit 6e51e9e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
4 changes: 1 addition & 3 deletions emojiwatch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 9 additions & 1 deletion emojiwatch/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@

import django.conf.urls as d_c_urls

from .version import (
__release__,
__version__,
)

from .views import (
CsrfExemptRedirectView,
event_hook_handler,
Expand All @@ -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,
)),
)
12 changes: 9 additions & 3 deletions emojiwatch/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/django_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@

DEBUG = True

EMOJIWATCH = {
'slack_verification_token': 'FoRtEsTiNgOnLyNoTrEaLlYaVeRiFiCaTiOnToKeN',
}

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
Expand Down

0 comments on commit 6e51e9e

Please sign in to comment.