Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Commit

Permalink
fix highfive not working on py3
Browse files Browse the repository at this point in the history
  • Loading branch information
pietroalbini committed Jul 31, 2019
1 parent 8607694 commit 6de8241
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion highfive/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def new_pr():

# Check the signature only if the secret is configured
if 'payload' in flask.request.form and webhook_secret is not None:
expected = hmac.new(str(webhook_secret), digestmod=hashlib.sha1)
expected = hmac.new(webhook_secret.encode("utf-8"), digestmod=hashlib.sha1)
expected.update(raw_data)
expected = expected.hexdigest()
if not hmac.compare_digest('sha1='+expected, signature):
Expand Down
6 changes: 3 additions & 3 deletions highfive/newpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def modifies_submodule(self, diff):
return submodule_re.match(diff)

def api_req(self, method, url, data=None, media_type=None):
data = None if not data else json.dumps(data)
data = None if not data else json.dumps(data).encode("utf-8")
headers = {} if not data else {'Content-Type': 'application/json'}
req = urllib.request.Request(url, data, headers)
req.get_method = lambda: method
Expand All @@ -102,7 +102,7 @@ def api_req(self, method, url, data=None, media_type=None):
if header.get('Content-Encoding') == 'gzip':
buf = StringIO(f.read())
f = gzip.GzipFile(fileobj=buf)
body = f.read()
body = f.read().decode("utf-8")
return { "header": header, "body": body }

def set_assignee(self, assignee, owner, repo, issue, user, author, to_mention):
Expand Down Expand Up @@ -154,7 +154,7 @@ def get_irc_nick(self, gh_name):
or if the user has no `irc` field associated with their username
"""
try:
data = urllib.urlopen(rustaceans_api_url.format(username=gh_name))
data = urllib.request.urlopen(rustaceans_api_url.format(username=gh_name))
if data.getcode() == 200:
rustacean_data = json.loads(data.read())
if rustacean_data:
Expand Down
14 changes: 7 additions & 7 deletions highfive/tests/test_newpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def setup_get_irc_nick_mocks(self, mock_urllib, status_code, data=None):
mock_data = mock.Mock()
mock_data.getcode.return_value = status_code
mock_data.read.return_value = data
mock_urllib.urlopen.return_value = mock_data
mock_urllib.request.urlopen.return_value = mock_data
return mock_data

@mock.patch('highfive.newpr.urllib')
Expand All @@ -359,7 +359,7 @@ def test_get_irc_nick_non_200(self, mock_urllib):
self.setup_get_irc_nick_mocks(mock_urllib, 503)
assert handler.get_irc_nick('foo') is None

mock_urllib.urlopen.assert_called_with(
mock_urllib.request.urlopen.assert_called_with(
'http://www.ncameron.org/rustaceans/user?username=foo'
)

Expand All @@ -369,7 +369,7 @@ def test_get_irc_nick_no_data(self, mock_urllib):
mock_data = self.setup_get_irc_nick_mocks(mock_urllib, 200, '[]')
assert handler.get_irc_nick('foo') is None

mock_urllib.urlopen.assert_called_with(
mock_urllib.request.urlopen.assert_called_with(
'http://www.ncameron.org/rustaceans/user?username=foo'
)
mock_data.getcode.assert_called()
Expand All @@ -384,7 +384,7 @@ def test_get_irc_nick_has_data(self, mock_urllib):
)
assert handler.get_irc_nick('nrc') == 'nrc'

mock_urllib.urlopen.assert_called_with(
mock_urllib.request.urlopen.assert_called_with(
'http://www.ncameron.org/rustaceans/user?username=nrc'
)
mock_data.getcode.assert_called()
Expand All @@ -405,10 +405,10 @@ def make_defaults(cls, patcherize):
cls.res = cls.mocks['urlopen'].return_value
cls.res.info.return_value = {'Content-Encoding': 'gzip'}

cls.body = cls.res.read.return_value = 'body1'
cls.body = cls.res.read.return_value = b'body1'

cls.gzipped_body = cls.mocks['GzipFile'].return_value.read
cls.gzipped_body.return_value = 'body2'
cls.gzipped_body.return_value = b'body2'

cls.handler = HighfiveHandlerMock(Payload({})).handler

Expand All @@ -417,7 +417,7 @@ def make_defaults(cls, patcherize):

def verify_mock_calls(self, header_calls, gzipped):
self.mocks['Request'].assert_called_with(
self.url, json.dumps(self.data) if self.data else self.data,
self.url, json.dumps(self.data).encode("utf-8") if self.data else self.data,
{'Content-Type': 'application/json'} if self.data else {}
)
assert self.req.get_method() == 'METHOD'
Expand Down
6 changes: 6 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[pytest]
markers =
unit
hermetic
config
integration

0 comments on commit 6de8241

Please sign in to comment.