Permalink
Browse files

Merge pull request #1324 from privacyidea/update_caconnector_test

Improve tests and coverage for `localca.py`
  • Loading branch information...
cornelinux committed Nov 30, 2018
2 parents 4da61fa + f6e76b5 commit bb1ed865c04438c6038c447103833f1280e6dc19
Showing with 35 additions and 9 deletions.
  1. +13 −9 privacyidea/lib/caconnectors/localca.py
  2. +22 −0 tests/test_lib_caconnector.py
@@ -359,8 +359,8 @@ def sign_request(self, csr, options=None):
:param options: Additional options like the validity time or the
template or spkac=1
:type options: dict
:return: Returns the certificate
:rtype: basestring
:return: Returns the certificate object
:rtype: X509
"""
# Sign the certificate for one year
options = options or {}
@@ -504,6 +504,7 @@ def revoke_cert(self, certificate, reason=CRL_REASONS[0]):
cert_obj = certificate
else:
raise CAError("Certificate in unsupported format")
serial = cert_obj.get_serial_number()
serial_hex = int_to_hex(serial)
filename = serial_hex + ".pem"
@@ -568,12 +569,14 @@ def create_crl(self, publish=True, check_validity=False):
return ret
@classmethod
def create_ca(cls, name):
@staticmethod
def create_ca(name):
"""
Create a new CA connector.
The configurations is requested at the command line in questions and
answers. The CA connector definition is also written to the database.
Create parameters for a new CA connector.
The configuration is requested at the command line in questions and
answers.
If the configuration is valid, the CA will be created on the file system
and the configuration for the new LocalCAConnector is returned.
We are asking for the following:
@@ -593,7 +596,9 @@ def create_ca(cls, name):
* We create two templates for users and for servers.
:param name: The name of the CA connector.
:return:
:type name: str
:return: The LocalCAConnector configuration
:rtype: dict
"""
config = CONFIG(name)
@@ -728,4 +733,3 @@ def _init_ca(config):
print("Please check the ownership of the private key")
print("{0!s}/cakey.pem".format(config.directory))
print("!" * 60)
@@ -4,6 +4,10 @@
"""
from .base import MyTestCase
import os
import six
import shutil
from io import StringIO
from mock import patch
from privacyidea.lib.caconnectors.localca import LocalCAConnector, ATTR
from OpenSSL import crypto
from privacyidea.lib.utils import int_to_hex
@@ -277,3 +281,21 @@ def test_05_templates(self):
self.assertTrue(ddiff.days < 760, ddiff.days)
class CreateLocalCATestCase(MyTestCase):
"""
test creating a new CA using the local caconnector
"""
def test_01_create_ca(self):
cwd = os.getcwd()
workdir = os.path.join(cwd, WORKINGDIR + '2')
if os.path.exists(workdir):
shutil.rmtree(workdir)
inputstr = six.text_type(workdir + '\n\n\n\n\n\ny\n')
with patch('sys.stdin', StringIO(inputstr)):
caconfig = LocalCAConnector.create_ca('localCA2')
self.assertEqual(caconfig.get("WorkingDir"), workdir)
cacon = LocalCAConnector('localCA2', caconfig)
self.assertEqual(cacon.name, 'localCA2')
self.assertEqual(cacon.workingdir, workdir)
# check if the generated files exist
self.assertTrue(os.path.exists(os.path.join(workdir, 'cacert.pem')))

0 comments on commit bb1ed86

Please sign in to comment.