Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from thomasvandoren/unicode-support
Browse files Browse the repository at this point in the history
Support unicode characters in push body from github.
  • Loading branch information
thomasvandoren committed Feb 6, 2015
2 parents 28e5add + e0310c2 commit 64742b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
16 changes: 13 additions & 3 deletions emailer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

import envelopes
import envelopes.connstack
from flask import Flask
Expand Down Expand Up @@ -197,8 +199,16 @@ def _get_subject(repo, message):

def _valid_signature(gh_signature, body, secret):
"""Returns True if GitHub signature is valid. False, otherwise."""
if isinstance(gh_signature, unicode):
gh_signature = str(gh_signature)
def to_str(s):
if isinstance(s, unicode):
return str(s)
else:
return s

gh_signature = to_str(gh_signature)
body = to_str(body)
secret = to_str(secret)

expected_hmac = hmac.new(secret, body, sha)
expected_signature = 'sha1=' + expected_hmac.hexdigest()
expected_signature = to_str('sha1=' + expected_hmac.hexdigest())
return hmac.compare_digest(expected_signature, gh_signature)
15 changes: 15 additions & 0 deletions test_emailer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import unicode_literals

import hmac
import json
import mock
Expand Down Expand Up @@ -262,6 +264,19 @@ def test_send_email__no_approved(self, mock_send):
self.check_msg(actual_msg)
self.assertEqual(None, actual_msg.headers.get('Approved'))

@mock.patch('envelopes.connstack.get_current_connection')
def test_send_email__unicode_body(self, mock_send):
"""Verify unicode characters in msg_info are handled."""
msg_info = self.msg_info
msg_info['message'] += '\n\u2026'

self.prep_env()
emailer._send_email(msg_info)

mock_send.return_value.send.assert_called_once_with(mock.ANY)
actual_msg = mock_send.return_value.send.call_args[0][0]
self.check_msg(actual_msg)

def test_get_sender__from_author(self):
"""Verify sent from author when appropriate config var set."""
os.environ['GITHUB_COMMIT_EMAILER_SEND_FROM_AUTHOR'] = 'whatevs'
Expand Down

0 comments on commit 64742b7

Please sign in to comment.