Skip to content

Commit

Permalink
better test case for unwritable
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasertl committed Nov 3, 2018
1 parent eafa395 commit ed03a4a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ca/django_ca/management/commands/import_ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def handle(self, name, key, pem, **options):
try:
write_private_file(ca.private_key_path, pem)
except PermissionError:
perm_denied = '%s: Permission denied: Could not open file for writing.' % ca.private_key_path
perm_denied = '%s: Permission denied: Could not open file for writing' % ca.private_key_path
raise CommandError(perm_denied)

# Only save CA to database if we loaded all data and copied private key
Expand Down
17 changes: 8 additions & 9 deletions ca/django_ca/tests/tests_command_import_ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey

from django.conf import settings
from django.core.management.base import CommandError

from .. import ca_settings
from ..models import CertificateAuthority
Expand Down Expand Up @@ -116,24 +115,24 @@ def test_intermediate(self):

@override_tmpcadir()
def test_permission_denied(self):
# Import the same CA twice, 2nd time should throw an error, because the file already exists
name = 'testname'
pem_path = os.path.join(settings.FIXTURES_DIR, 'root.pem')
key_path = os.path.join(settings.FIXTURES_DIR, 'root.key')
out, err = self.cmd('import_ca', name, key_path, pem_path)

ca = CertificateAuthority.objects.get(name=name)
os.chmod(settings.CA_DIR, 0o000)

error = '%s: Permission denied: Could not open file for writing.' % ca.private_key_path
error = r'^%s/%s.key: Permission denied: Could not open file for writing$' % (
settings.CA_DIR, certs['root']['serial']
)
with self.assertCommandError(error):
out, err = self.cmd('import_ca', name, key_path, pem_path)
self.cmd('import_ca', name, key_path, pem_path)
os.chmod(settings.CA_DIR, 0o644)

@override_tmpcadir(CA_MIN_KEY_SIZE=1024)
def test_bogus_pub(self):
name = 'testname'
pem_path = os.path.join(settings.FIXTURES_DIR, __file__)
key_path = os.path.join(settings.FIXTURES_DIR, 'root-key.der')
with self.assertRaisesRegex(CommandError, r'^Unable to load public key\.$'):
with self.assertCommandError(r'^Unable to load public key\.$'):
self.cmd('import_ca', name, key_path, pem_path)
self.assertEqual(CertificateAuthority.objects.count(), 0)

Expand All @@ -142,6 +141,6 @@ def test_bogus_priv(self):
name = 'testname'
pem_path = os.path.join(settings.FIXTURES_DIR, 'root-pub.der')
key_path = os.path.join(settings.FIXTURES_DIR, __file__)
with self.assertRaisesRegex(CommandError, r'^Unable to load private key\.$'):
with self.assertCommandError(r'^Unable to load private key\.$'):
self.cmd('import_ca', name, key_path, pem_path)
self.assertEqual(CertificateAuthority.objects.count(), 0)

0 comments on commit ed03a4a

Please sign in to comment.