Skip to content

Commit

Permalink
Fix the Python 3 incompatibility in crypto utils
Browse files Browse the repository at this point in the history
Write the downloaded file as text since the file is opened in text mode.

fixes #476

Signed-off-by: Jeremy Cline <jeremy@jcline.org>
  • Loading branch information
jeremycline committed Sep 6, 2017
1 parent c0571a8 commit a4f068e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
2 changes: 1 addition & 1 deletion fedmsg/crypto/utils.py
Expand Up @@ -137,7 +137,7 @@ def _load_remote_cert(location, cache, cache_expiry, tries=1, **config):
session.mount('https://', requests.adapters.HTTPAdapter(max_retries=tries))
response = session.get(location, timeout=30)
with open(cache, 'w') as f:
f.write(response.content)
f.write(response.text)
except IOError:
# If we couldn't write to the specified cache location, try a
# similar place but inside our home directory instead.
Expand Down
23 changes: 22 additions & 1 deletion fedmsg/tests/crypto/test_utils.py
@@ -1,10 +1,13 @@
import os
import shutil
import tempfile
import time
import unittest

import mock

from fedmsg.crypto import utils
from fedmsg.tests import base


class FixDatanommerMessageTests(unittest.TestCase):
Expand Down Expand Up @@ -102,9 +105,27 @@ def test_topic_missing_not_nitpicky(self):
self.assertTrue(result)


class LoadRemoteCertTests(unittest.TestCase):
class LoadRemoteCertTests(base.FedmsgTestCase):
"""Tests for :func:`utils._load_remote_cert`."""

def setUp(self):
super(LoadRemoteCertTests, self).setUp()

self.cache_dir = tempfile.mkdtemp()
self.cache_file = os.path.join(self.cache_dir, 'ca.crt')
self.addCleanup(shutil.rmtree, self.cache_dir, True)

def test_remote_cert(self):
"""Assert downloading a certificate to a cache location works."""
with open(os.path.join(base.SSLDIR, 'fedora_ca.crt'), 'r') as fd:
expected_cert = fd.read()
utils._load_remote_cert('https://fedoraproject.org/fedmsg/ca.crt', self.cache_file, 0)

self.assertTrue(os.path.exists(self.cache_file))
with open(self.cache_file, 'r') as fd:
actual_cert = fd.read()
self.assertEqual(expected_cert, actual_cert)

@mock.patch('fedmsg.crypto.utils.os.stat')
def test_valid_cache(self, mock_stat):
"""Assert when the primary cache is valid it's used."""
Expand Down
@@ -0,0 +1,43 @@
interactions:
- request:
body: null
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
User-Agent: [python-requests/2.18.4]
method: GET
uri: https://fedoraproject.org/fedmsg/ca.crt
response:
body:
string: !!binary |
H4sIAAAAAAAAA6WUy5KiShCG9/UUZ290NCioLOvG1QKLq7BTaEFpRe3WAp5+UKOnZ86Z1ZnaVEQG
+Wfy5V/58jIcRA3L/QdTP7R0C8OQ3oMvgFkW6XqMYR+UUFgIlpYNPZebdbHUW1VnUDJwcDYCazMh
nCLEI8gso2T8Q2CeEhBzblBhx1FPFwzWBpQjihHDYc1aSqCHSjdGMA+RbFcbIz5l47LVexg/400I
SJ29Z4bWpQHi+UE7Z4krMT4XZFAfxBdUZIcs8T/zcd6SHrrPRBaiwz1P7kHW0+W9zXtlx2dGdojr
zco9siAVNkxtp8ms6pa7kNdIr4pqSDptanQXveUHXYCvqouD1mU9fNOF1LI9nLAw6oZbuCRbD7Hu
GWM/Y18cAH62+r85gDuIv+EA7iD+hgO4g/idA0bHR5X6XNU7QxMSgpzqcPiTPYeiTGtcphR+FrJN
PusJmE/G4xM+LdBieRXXbWrURTmVlqv9O7nZ7eUsNWFOm6XCZTFauWRaK6NR2W877ey+RyFNgXeS
J+7H2uj223rqONcg3ozGK0W77W0rfSVFPJ1F/Saq0/SsNotTTw6zTJNaxboJiJZBClw+qqp1s/DE
cRooTrEtfCyNosYithOPbNh0EoaCQrj28MBAlCXVGXrwKkjJE4CQf+H6RaXlzk7eikzlKxlWkhMe
5xEkGMn3SfjSHlmmyyzTKf/1PfgtwTHQ6RygZgDZkG9PoIrhOGYt7qH9HGEZwlolzIcCkPIxa5OK
IkiT9n2dFA3zc6E/PeBQoRpP29IyMu3b+hDvC/I9doDbX+b+bR7yR/Pg+ucjGrxzYJgb4Je3bkM/
XaVD0jpR4UN0Ylf5V1WsDl28YczJPkKqGbb5mx9CBu7Kps8oCiGB3Hz90wKBioGgs4+81cWA1kku
t9dNcfBo0W0YGC9YoLl6DpXJfHJ0ikpJ0cUbK03mjXsoa0kQxJzOtriTXnf6PGiVmafttrVpWqTO
bzPg2yOVRNMPv44IuShaVM50vfPn8cqzQyWKZvGucmOZ1eV7gpfKpdtbSS6uqqoYYV9qCDDzYiTa
FQVuqLfS6yls+yN4rEvqkv+u0B/hSo9jXwUAAA==
headers:
Accept-Ranges: [bytes]
AppServer: [proxy08.fedoraproject.org]
AppTime: [D=917]
Connection: [Keep-Alive]
Content-Encoding: [gzip]
Content-Length: ['826']
Date: ['Wed, 06 Sep 2017 14:35:42 GMT']
ETag: ['"55f-52243d9164097-gzip"']
Keep-Alive: ['timeout=15, max=500']
Last-Modified: ['Sat, 17 Oct 2015 02:41:23 GMT']
Server: [Apache/2.4.6 (Red Hat Enterprise Linux)]
Vary: ['Accept-Encoding,User-Agent']
status: {code: 200, message: OK}
version: 1

0 comments on commit a4f068e

Please sign in to comment.