Changeset - 1b16b8ce67df
[Not reviewed]
0 6 0
Branko Majic (branko) - 6 years ago 2018-05-02 22:47:55
branko@majic.rs
Noticket: Compatibility fixes for Python 3.4:

- Fixed invocation of pexepect.spawnu to convert the passed-in
arguments explicitly into a list. Necessary since Python 3.4 can't
use the *args construct outside of assignment.
- Updated the certificate_to_pem function to return str instead of
bytes. Necessary since Python 3.4 does not support things like b"%s"
% mybytes.
- Fixed test for existence of help CLI command. Previous code was
referencing a wrong/non-existent function help (the actual name has
to be help_ in order not to shadow the built-in function).
- Updated unit test invocations that use the read_certificate
function.
- Updated tests for the read_certificate function.
6 files changed with 11 insertions and 10 deletions:
0 comments (0 inline, 0 general)
functional_tests/base.py
Show inline comments
 
@@ -89,7 +89,7 @@ def run_interactive_command(prompt_answers, command, *args):
 
    # stdout/stderr.
 
    output_stream = io.StringIO()
 
    send_stream = io.StringIO()
 
    process = pexpect.spawnu(command, [*args], timeout=2)
 
    process = pexpect.spawnu(command, list(args), timeout=2)
 
    process.logfile_read = output_stream
 
    process.logfile_send = send_stream
 

	
gimmecert/storage.py
Show inline comments
 
@@ -103,11 +103,11 @@ def write_certificate_chain(certificate_chain, path):
 
    :type path: str
 
    """
 

	
 
    chain_pem = b"\n".join(
 
    chain_pem = "\n".join(
 
        [gimmecert.utils.certificate_to_pem(certificate) for certificate in certificate_chain]
 
    )
 

	
 
    with open(path, 'wb') as certificate_chain_file:
 
    with open(path, 'w') as certificate_chain_file:
 
        certificate_chain_file.write(chain_pem)
 

	
 

	
gimmecert/utils.py
Show inline comments
 
@@ -38,12 +38,12 @@ def certificate_to_pem(certificate):
 
    :type certificate: cryptography.x509.Certificate
 

	
 
    :returns: Certificate in OpenSSL-style PEM format.
 
    :rtype: bytes
 
    :rtype: str
 
    """
 

	
 
    certificate_pem = certificate.public_bytes(encoding=cryptography.hazmat.primitives.serialization.Encoding.PEM)
 

	
 
    return certificate_pem
 
    return certificate_pem.decode()
 

	
 

	
 
def dn_to_str(dn):
tests/test_cli.py
Show inline comments
 
@@ -211,7 +211,7 @@ def test_setup_subcommand_parser_registered(setup_subcommand_parser):
 
# command from CLI. See test documentation for more details.
 
VALID_CLI_INVOCATIONS = [
 
    # help, no options
 
    ("gimmecert.cli.help", ["gimmecert", "help"]),
 
    ("gimmecert.cli.help_", ["gimmecert", "help"]),
 

	
 
    # init, no options
 
    ("gimmecert.cli.init", ["gimmecert", "init"]),
tests/test_storage.py
Show inline comments
 
@@ -84,8 +84,8 @@ def test_write_certificate_chain(tmpdir):
 
    level1_pem, level2_pem, level3_pem = [gimmecert.utils.certificate_to_pem(certificate) for certificate in certificate_chain]
 

	
 
    gimmecert.storage.write_certificate_chain(certificate_chain, output_file.strpath)
 
    content = output_file.read(mode='rb')
 
    expected_content = b"%s\n%s\n%s" % (level1_pem, level2_pem, level3_pem)
 
    content = output_file.read(mode='r')
 
    expected_content = "%s\n%s\n%s" % (level1_pem, level2_pem, level3_pem)
 

	
 
    assert content == expected_content
 

	
tests/test_utils.py
Show inline comments
 
@@ -39,8 +39,9 @@ def test_certificate_to_pem_returns_valid_pem():
 

	
 
    certificate_pem = gimmecert.utils.certificate_to_pem(certificate)
 

	
 
    assert isinstance(certificate_pem, bytes)
 
    certificate_from_pem = cryptography.x509.load_pem_x509_certificate(certificate_pem, cryptography.hazmat.backends.default_backend())  # Should not throw
 
    assert isinstance(certificate_pem, str)
 
    certificate_from_pem = cryptography.x509.load_pem_x509_certificate(bytes(certificate_pem, encoding='UTF-8'),
 
                                                                       cryptography.hazmat.backends.default_backend())  # Should not throw
 
    assert certificate_from_pem.subject == certificate.subject
 
    assert certificate_from_pem.issuer == certificate.issuer
 

	
0 comments (0 inline, 0 general)