Unify tests that use upload documents. Add explicit mode when opening a file.
Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
@@ -87,7 +87,7 @@ TEST_SIGNED_FILE = os.path.join(
|
||||
settings.BASE_DIR, 'apps', 'django_gpg', 'tests', 'contrib',
|
||||
'test_files', 'test_file.txt.gpg'
|
||||
)
|
||||
TEST_SIGNED_FILE_CONTENT = 'test_file.txt\n'
|
||||
TEST_SIGNED_FILE_CONTENT = b'test_file.txt\n'
|
||||
|
||||
TEST_RECEIVE_KEY = '''-----BEGIN PGP PUBLIC KEY BLOCK-----
|
||||
Version: SKS 1.1.5
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import StringIO
|
||||
import io
|
||||
|
||||
import gnupg
|
||||
import mock
|
||||
|
||||
from django.utils.encoding import force_bytes
|
||||
|
||||
from common.tests import BaseTestCase
|
||||
from common.utils import TemporaryFile
|
||||
|
||||
@@ -75,7 +77,7 @@ class KeyTestCase(BaseTestCase):
|
||||
|
||||
def test_cleartext_file_verification(self):
|
||||
cleartext_file = TemporaryFile()
|
||||
cleartext_file.write('test')
|
||||
cleartext_file.write(b'test')
|
||||
cleartext_file.seek(0)
|
||||
|
||||
with self.assertRaises(VerificationError):
|
||||
@@ -84,7 +86,7 @@ class KeyTestCase(BaseTestCase):
|
||||
cleartext_file.close()
|
||||
|
||||
def test_embedded_verification_no_key(self):
|
||||
with open(TEST_SIGNED_FILE) as signed_file:
|
||||
with open(TEST_SIGNED_FILE, 'rb') as signed_file:
|
||||
result = Key.objects.verify_file(signed_file)
|
||||
|
||||
self.assertTrue(result.key_id in TEST_KEY_FINGERPRINT)
|
||||
@@ -92,7 +94,7 @@ class KeyTestCase(BaseTestCase):
|
||||
def test_embedded_verification_with_key(self):
|
||||
Key.objects.create(key_data=TEST_KEY_DATA)
|
||||
|
||||
with open(TEST_SIGNED_FILE) as signed_file:
|
||||
with open(TEST_SIGNED_FILE, 'rb') as signed_file:
|
||||
result = Key.objects.verify_file(signed_file)
|
||||
|
||||
self.assertEqual(result.fingerprint, TEST_KEY_FINGERPRINT)
|
||||
@@ -100,7 +102,7 @@ class KeyTestCase(BaseTestCase):
|
||||
def test_embedded_verification_with_correct_fingerprint(self):
|
||||
Key.objects.create(key_data=TEST_KEY_DATA)
|
||||
|
||||
with open(TEST_SIGNED_FILE) as signed_file:
|
||||
with open(TEST_SIGNED_FILE, 'rb') as signed_file:
|
||||
result = Key.objects.verify_file(
|
||||
signed_file, key_fingerprint=TEST_KEY_FINGERPRINT
|
||||
)
|
||||
@@ -111,21 +113,21 @@ class KeyTestCase(BaseTestCase):
|
||||
def test_embedded_verification_with_incorrect_fingerprint(self):
|
||||
Key.objects.create(key_data=TEST_KEY_DATA)
|
||||
|
||||
with open(TEST_SIGNED_FILE) as signed_file:
|
||||
with open(TEST_SIGNED_FILE, 'rb') as signed_file:
|
||||
with self.assertRaises(KeyDoesNotExist):
|
||||
Key.objects.verify_file(signed_file, key_fingerprint='999')
|
||||
|
||||
def test_signed_file_decryption(self):
|
||||
Key.objects.create(key_data=TEST_KEY_DATA)
|
||||
|
||||
with open(TEST_SIGNED_FILE) as signed_file:
|
||||
with open(TEST_SIGNED_FILE, 'rb') as signed_file:
|
||||
result = Key.objects.decrypt_file(file_object=signed_file)
|
||||
|
||||
self.assertEqual(result.read(), TEST_SIGNED_FILE_CONTENT)
|
||||
|
||||
def test_cleartext_file_decryption(self):
|
||||
cleartext_file = TemporaryFile()
|
||||
cleartext_file.write('test')
|
||||
cleartext_file.write(b'test')
|
||||
cleartext_file.seek(0)
|
||||
|
||||
with self.assertRaises(DecryptionError):
|
||||
@@ -134,8 +136,8 @@ class KeyTestCase(BaseTestCase):
|
||||
cleartext_file.close()
|
||||
|
||||
def test_detached_verification_no_key(self):
|
||||
with open(TEST_DETACHED_SIGNATURE) as signature_file:
|
||||
with open(TEST_FILE) as test_file:
|
||||
with open(TEST_DETACHED_SIGNATURE, 'rb') as signature_file:
|
||||
with open(TEST_FILE, 'rb') as test_file:
|
||||
result = Key.objects.verify_file(
|
||||
file_object=test_file, signature_file=signature_file
|
||||
)
|
||||
@@ -145,8 +147,8 @@ class KeyTestCase(BaseTestCase):
|
||||
def test_detached_verification_with_key(self):
|
||||
Key.objects.create(key_data=TEST_KEY_DATA)
|
||||
|
||||
with open(TEST_DETACHED_SIGNATURE) as signature_file:
|
||||
with open(TEST_FILE) as test_file:
|
||||
with open(TEST_DETACHED_SIGNATURE, 'rb') as signature_file:
|
||||
with open(TEST_FILE, 'rb') as test_file:
|
||||
result = Key.objects.verify_file(
|
||||
file_object=test_file, signature_file=signature_file
|
||||
)
|
||||
@@ -158,7 +160,7 @@ class KeyTestCase(BaseTestCase):
|
||||
key = Key.objects.create(key_data=TEST_KEY_DATA)
|
||||
|
||||
with self.assertRaises(NeedPassphrase):
|
||||
with open(TEST_FILE) as test_file:
|
||||
with open(TEST_FILE, 'rb') as test_file:
|
||||
key.sign_file(
|
||||
file_object=test_file, detached=True,
|
||||
)
|
||||
@@ -167,7 +169,7 @@ class KeyTestCase(BaseTestCase):
|
||||
key = Key.objects.create(key_data=TEST_KEY_DATA)
|
||||
|
||||
with self.assertRaises(PassphraseError):
|
||||
with open(TEST_FILE) as test_file:
|
||||
with open(TEST_FILE, 'rb') as test_file:
|
||||
key.sign_file(
|
||||
file_object=test_file, detached=True,
|
||||
passphrase='bad passphrase'
|
||||
@@ -176,17 +178,17 @@ class KeyTestCase(BaseTestCase):
|
||||
def test_detached_signing_with_passphrase(self):
|
||||
key = Key.objects.create(key_data=TEST_KEY_DATA)
|
||||
|
||||
with open(TEST_FILE) as test_file:
|
||||
with open(TEST_FILE, 'rb') as test_file:
|
||||
detached_signature = key.sign_file(
|
||||
file_object=test_file, detached=True,
|
||||
passphrase=TEST_KEY_PASSPHRASE
|
||||
)
|
||||
|
||||
signature_file = StringIO.StringIO()
|
||||
signature_file.write(detached_signature)
|
||||
signature_file = io.BytesIO()
|
||||
signature_file.write(force_bytes(detached_signature))
|
||||
signature_file.seek(0)
|
||||
|
||||
with open(TEST_FILE) as test_file:
|
||||
with open(TEST_FILE, 'rb') as test_file:
|
||||
result = Key.objects.verify_file(
|
||||
file_object=test_file, signature_file=signature_file
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user