Skip to content

Commit f72a3a4

Browse files
committed
- Updated strings with f-strings on test suite.
- Removed future imports.
1 parent 928ff7b commit f72a3a4

8 files changed

Lines changed: 127 additions & 140 deletions

File tree

tests/test_env.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@
1818

1919
from six import text_type
2020

21-
FAKE = environ.get("FAKE")
22-
RANDOM_DOMAIN = environ.get("RANDOM_DOMAIN")
23-
TPP_URL = environ.get("TPP_URL")
24-
TPP_USER = environ.get("TPP_USER")
25-
TPP_PASSWORD = environ.get("TPP_PASSWORD")
26-
TPP_ZONE = environ.get("TPP_ZONE")
27-
TPP_ZONE_ECDSA = environ.get("TPP_ZONE_ECDSA")
28-
TPP_TOKEN_URL = environ.get("TPP_TOKEN_URL")
29-
TPP_ACCESS_TOKEN = environ.get("TPP_ACCESS_TOKEN")
30-
CLOUD_URL = environ.get("CLOUD_URL")
31-
CLOUD_APIKEY = environ.get("CLOUD_APIKEY")
32-
CLOUD_ZONE = environ.get("CLOUD_ZONE")
21+
FAKE = environ.get('FAKE')
22+
RANDOM_DOMAIN = environ.get('RANDOM_DOMAIN')
23+
TPP_URL = environ.get('TPP_URL')
24+
TPP_USER = environ.get('TPP_USER')
25+
TPP_PASSWORD = environ.get('TPP_PASSWORD')
26+
TPP_ZONE = environ.get('TPP_ZONE')
27+
TPP_ZONE_ECDSA = environ.get('TPP_ZONE_ECDSA')
28+
TPP_TOKEN_URL = environ.get('TPP_TOKEN_URL')
29+
TPP_ACCESS_TOKEN = environ.get('TPP_ACCESS_TOKEN')
30+
CLOUD_URL = environ.get('CLOUD_URL')
31+
CLOUD_APIKEY = environ.get('CLOUD_APIKEY')
32+
CLOUD_ZONE = environ.get('CLOUD_ZONE')
3333

34-
TPP_PM_ROOT = environ.get("TPP_PM_ROOT")
35-
TPP_CA_NAME = environ.get("TPP_CA_NAME")
36-
CLOUD_ENTRUST_CA_NAME = environ.get("CLOUD_ENTRUST_CA_NAME")
37-
CLOUD_DIGICERT_CA_NAME = environ.get("CLOUD_DIGICERT_CA_NAME")
34+
TPP_PM_ROOT = environ.get('TPP_PM_ROOT')
35+
TPP_CA_NAME = environ.get('TPP_CA_NAME')
36+
CLOUD_ENTRUST_CA_NAME = environ.get('CLOUD_ENTRUST_CA_NAME')
37+
CLOUD_DIGICERT_CA_NAME = environ.get('CLOUD_DIGICERT_CA_NAME')
3838

39-
TPP_SSH_CADN = environ.get("TPP_SSH_CADN")
39+
TPP_SSH_CADN = environ.get('TPP_SSH_CADN')
4040

4141
if RANDOM_DOMAIN and not isinstance(RANDOM_DOMAIN, text_type):
4242
RANDOM_DOMAIN = RANDOM_DOMAIN.decode()

tests/test_local_methods.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
from cryptography.hazmat.backends import default_backend
2222

2323
from assets import POLICY_CLOUD1, POLICY_TPP1, EXAMPLE_CSR, EXAMPLE_CHAIN
24-
from vcert import CloudConnection, KeyType, TPPConnection, CertificateRequest, ZoneConfig, CertField, FakeConnection, \
25-
logger
24+
from vcert import (CloudConnection, KeyType, TPPConnection, CertificateRequest, ZoneConfig, CertField, FakeConnection,
25+
logger)
2626
from vcert.pem import parse_pem, Certificate
2727

2828
pkcs12_enc_cert = """-----BEGIN CERTIFICATE-----
@@ -171,9 +171,8 @@ def test_parse_cloud_zone1(self):
171171
p = conn._parse_policy_response_to_object(json.loads(POLICY_CLOUD1))
172172
self.assertEqual(p.id, "3da4ba30-c370-11e9-9e69-99559a9ae32a")
173173
self.assertEqual(p.SubjectCNRegexes[-1], ".*.test")
174-
self.assertTrue(
175-
p.SubjectCRegexes == p.SubjectLRegexes == p.SubjectORegexes == p.SubjectOURegexes == p.SubjectSTRegexes == [
176-
".*"])
174+
self.assertTrue(p.SubjectCRegexes == p.SubjectLRegexes == p.SubjectORegexes == p.SubjectOURegexes
175+
== p.SubjectSTRegexes == [".*"])
177176
self.assertEqual(p.key_types[0].key_type, KeyType.RSA)
178177
self.assertEqual(p.key_types[0].option, 2048)
179178
self.assertEqual(p.key_types[1].key_type, KeyType.RSA)
@@ -341,9 +340,9 @@ def test_parse_pem_chain(self):
341340
def test_pkcs12_enc_pk(self):
342341
certificate = Certificate(cert=pkcs12_enc_cert, chain=chain, key=pkcs12_enc_pk)
343342
output = certificate.as_pkcs12(passphrase=pkcs12_enc_pk_pass)
344-
log.info("PKCS12 created successfully:\n%s" % output)
343+
log.info(f"PKCS12 created successfully:\n{output}")
345344

346345
def test_pkcs12_plain_pk(self):
347346
cert = Certificate(cert=pkcs12_plain_cert, chain=chain, key=pkcs12_plain_pk)
348347
output = cert.as_pkcs12()
349-
log.info("PKCS12 created successfully:\n%s" % output)
348+
log.info(f"PKCS12 created successfully:\n{output}")

tests/test_pm.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,22 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
1817
import os
1918
import unittest
2019
from pprint import pformat
2120

22-
from test_env import TPP_TOKEN_URL, CLOUD_APIKEY, CLOUD_URL, TPP_PM_ROOT, CLOUD_ENTRUST_CA_NAME, \
23-
CLOUD_DIGICERT_CA_NAME, TPP_CA_NAME, TPP_USER, TPP_PASSWORD
21+
from test_env import (TPP_TOKEN_URL, CLOUD_APIKEY, CLOUD_URL, TPP_PM_ROOT, CLOUD_ENTRUST_CA_NAME,CLOUD_DIGICERT_CA_NAME,
22+
TPP_CA_NAME, TPP_USER, TPP_PASSWORD)
2423
from test_utils import timestamp
2524
from vcert import TPPTokenConnection, CloudConnection, Authentication, SCOPE_PM, logger
2625
from vcert.parser import json_parser, yaml_parser
2726
from vcert.parser.utils import parse_policy_spec
28-
from vcert.policy import Policy, Subject, KeyPair, SubjectAltNames, Defaults, DefaultSubject, DefaultKeyPair, \
29-
PolicySpecification
27+
from vcert.policy import (Policy, Subject, KeyPair, SubjectAltNames, Defaults, DefaultSubject, DefaultKeyPair,
28+
PolicySpecification)
3029
from vcert.policy.pm_cloud import CA_TYPE_DIGICERT, CA_TYPE_ENTRUST
3130

32-
POLICY_SPEC_JSON = '/resources/policy_specification.json'
33-
POLICY_SPEC_YAML = '/resources/policy_specification.yaml'
31+
POLICY_SPEC_JSON = 'resources/policy_specification.json'
32+
POLICY_SPEC_YAML = 'resources/policy_specification.yaml'
3433
CA_TYPE_TPP = 'TPP'
3534

3635
log = logger.get_child("test-pm")
@@ -68,7 +67,7 @@ def test_yaml_serialization(self):
6867

6968
class TestTPPPolicyManagement(unittest.TestCase):
7069
def __init__(self, *args, **kwargs):
71-
self.tpp_conn = TPPTokenConnection(url=TPP_TOKEN_URL, http_request_kwargs={"verify": "/tmp/chain.pem"})
70+
self.tpp_conn = TPPTokenConnection(url=TPP_TOKEN_URL, http_request_kwargs={'verify': "/tmp/chain.pem"})
7271
auth = Authentication(user=TPP_USER, password=TPP_PASSWORD, scope=SCOPE_PM)
7372
self.tpp_conn.get_access_token(auth)
7473
self.json_file = _resolve_resources_path(POLICY_SPEC_JSON)
@@ -102,7 +101,7 @@ def test_create_policy_no_defaults(self):
102101
self._create_policy_tpp(policy=policy)
103102

104103
def _create_policy_tpp(self, policy_spec=None, policy=None, defaults=None):
105-
zone = '%s\\%s' % (TPP_PM_ROOT, _get_tpp_policy_name())
104+
zone = f"{TPP_PM_ROOT}\\{_get_tpp_policy_name()}"
106105
create_policy(self.tpp_conn, zone, policy_spec, policy, defaults)
107106

108107

@@ -166,7 +165,7 @@ def create_policy(connector, zone, policy_spec=None, policy=None, defaults=None)
166165
connector.set_policy(zone, policy_spec)
167166
resp = connector.get_policy(zone)
168167
data = parse_policy_spec(resp)
169-
log.debug('Created Policy at %s' % zone)
168+
log.debug(f"Created Policy at {zone}")
170169
log.debug(pformat(data))
171170
return resp
172171

@@ -228,29 +227,29 @@ def _get_defaults_obj():
228227

229228

230229
def _get_app_name():
231-
name = 'vcert-python-app-%s'
230+
name = 'vcert-python-app-{}'
232231
return name
233232

234233

235234
def _get_cit_name():
236-
cit_name = 'vcert-python-cit-%s'
235+
cit_name = 'vcert-python-cit-{}'
237236
return cit_name
238237

239238

240239
def _get_zone():
241240
time = timestamp()
242-
zone = (_get_app_name() + '\\' + _get_cit_name()) % (time, time)
241+
zone = f"{_get_app_name().format(time)}\\{_get_cit_name().format(time)}"
243242
return zone
244243

245244

246245
def _get_tpp_policy_name():
247246
time = timestamp()
248-
return _get_app_name() % time
247+
return f"{_get_app_name().format(time)}"
249248

250249

251250
def _resolve_resources_path(path):
252251
resources_dir = os.path.dirname(__file__)
253-
log.debug('Testing root folder: [%s]' % resources_dir)
254-
resolved_path = ('.%s' % path) if resources_dir.endswith('tests') else ('./tests%s' % path)
255-
log.debug('resolved path: [%s]' % resolved_path)
252+
log.debug(f"Testing root folder: [{resources_dir}]")
253+
resolved_path = f"./{path}" if resources_dir.endswith('tests') else f"./tests/{path}"
254+
log.debug(f"resolved path: [{resolved_path}]")
256255
return resolved_path

tests/test_ssh.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,26 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
import logging
1817
import platform
1918
import re
2019
import unittest
2120

2221
from assets import SSH_CERT_DATA, SSH_PRIVATE_KEY, SSH_PUBLIC_KEY
2322
from test_env import TPP_TOKEN_URL, TPP_USER, TPP_PASSWORD, TPP_SSH_CADN
2423
from test_utils import timestamp
25-
from vcert import CommonConnection, SSHCertRequest, TPPTokenConnection, Authentication, \
26-
SCOPE_SSH, write_ssh_files, logger, venafi_connection, VenafiPlatform
24+
from vcert import (CommonConnection, SSHCertRequest, TPPTokenConnection, Authentication,
25+
SCOPE_SSH, write_ssh_files, logger, venafi_connection, VenafiPlatform)
2726
from vcert.ssh_utils import SSHRetrieveResponse, SSHKeyPair, SSHCATemplateRequest
2827

2928
log = logger.get_child("test-ssh")
3029

31-
SERVICE_GENERATED_NO_KEY_ERROR = "%s key data is %s empty for Certificate %s" # type: str
32-
SSH_CERT_DATA_ERROR = "Certificate data is empty for Certificate %s" # type: str
30+
SERVICE_GENERATED_NO_KEY_ERROR = "{} key data is {} empty for Certificate {}" # type: str
31+
SSH_CERT_DATA_ERROR = "Certificate data is empty for Certificate {}" # type: str
3332

3433

3534
class TestTPPSSHCertificate(unittest.TestCase):
3635
def __init__(self, *args, **kwargs):
37-
self.tpp_conn = TPPTokenConnection(url=TPP_TOKEN_URL, http_request_kwargs={"verify": "/tmp/chain.pem"})
36+
self.tpp_conn = TPPTokenConnection(url=TPP_TOKEN_URL, http_request_kwargs={'verify': "/tmp/chain.pem"})
3837
auth = Authentication(user=TPP_USER, password=TPP_PASSWORD, scope=SCOPE_SSH)
3938
self.tpp_conn.get_access_token(auth)
4039
super(TestTPPSSHCertificate, self).__init__(*args, **kwargs)
@@ -49,64 +48,65 @@ def test_enroll_local_generated_keypair(self):
4948
request.set_public_key_data(keypair.public_key())
5049
response = _enroll_ssh_cert(self.tpp_conn, request)
5150
self.assertTrue(response.private_key_data is None,
52-
SERVICE_GENERATED_NO_KEY_ERROR % ("Private", "not", request.key_id))
53-
self.assertTrue(response.public_key_data, SERVICE_GENERATED_NO_KEY_ERROR % ("Public", "", request.key_id))
51+
SERVICE_GENERATED_NO_KEY_ERROR.format("Private", "not", request.key_id))
52+
self.assertTrue(response.public_key_data, SERVICE_GENERATED_NO_KEY_ERROR.format("Public", "", request.key_id))
5453
self.assertTrue(response.public_key_data == request.get_public_key_data(),
55-
"Public key on response does not match request.\nExpected: %s\nGot: %s"
56-
% (request.get_public_key_data(), response.public_key_data))
57-
self.assertTrue(response.certificate_data, SSH_CERT_DATA_ERROR % request.key_id)
54+
f"Public key on response does not match request."
55+
f"\nExpected: {request.get_public_key_data()}"
56+
f"\nGot: {response.public_key_data}")
57+
self.assertTrue(response.certificate_data, SSH_CERT_DATA_ERROR.format(request.key_id))
5858

5959
def test_enroll_service_generated_keypair(self):
6060
request = SSHCertRequest(cadn=TPP_SSH_CADN, key_id=_random_key_id())
6161
request.validity_period = "4h"
6262
request.source_addresses = ["test.com"]
6363
response = _enroll_ssh_cert(self.tpp_conn, request)
64-
self.assertTrue(response.private_key_data, SERVICE_GENERATED_NO_KEY_ERROR % ("Private", "", request.key_id))
65-
self.assertTrue(response.public_key_data, SERVICE_GENERATED_NO_KEY_ERROR % ("Public", "", request.key_id))
66-
self.assertTrue(response.certificate_data, SSH_CERT_DATA_ERROR % request.key_id)
64+
self.assertTrue(response.private_key_data, SERVICE_GENERATED_NO_KEY_ERROR.format("Private", "", request.key_id))
65+
self.assertTrue(response.public_key_data, SERVICE_GENERATED_NO_KEY_ERROR.format("Public", "", request.key_id))
66+
self.assertTrue(response.certificate_data, SSH_CERT_DATA_ERROR.format(request.key_id))
6767

6868
def test_retrieve_ca_public_key(self):
6969
tpp_connector = venafi_connection(platform=VenafiPlatform.TPP, url=TPP_TOKEN_URL,
70-
http_request_kwargs={"verify": "/tmp/chain.pem"})
70+
http_request_kwargs={'verify': "/tmp/chain.pem"})
7171
request = SSHCATemplateRequest(ca_template=TPP_SSH_CADN)
7272
ssh_config = tpp_connector.retrieve_ssh_config(ca_request=request)
73-
self.assertIsNotNone(ssh_config.ca_public_key, "%s Public Key data is empty" % TPP_SSH_CADN)
74-
self.assertIsNone(ssh_config.ca_principals, "%s default principals is not empty" % TPP_SSH_CADN)
75-
log.debug("%s Public Key data:\n%s" % (TPP_SSH_CADN, ssh_config.ca_public_key))
73+
self.assertIsNotNone(ssh_config.ca_public_key, f"{TPP_SSH_CADN} Public Key data is empty")
74+
self.assertIsNone(ssh_config.ca_principals, f"{TPP_SSH_CADN} default principals is not empty")
75+
log.debug(f"{TPP_SSH_CADN} Public Key data:\n{ssh_config.ca_public_key}")
7676

7777
def test_retrieve_ca_public_key_and_principals(self):
7878
request = SSHCATemplateRequest(ca_template=TPP_SSH_CADN)
7979
ssh_config = self.tpp_conn.retrieve_ssh_config(ca_request=request)
80-
self.assertIsNotNone(ssh_config.ca_public_key, "%s Public Key data is empty" % TPP_SSH_CADN)
81-
self.assertIsNotNone(ssh_config.ca_principals, "%s default principals is empty" % TPP_SSH_CADN)
82-
log.debug("%s Public Key data: %s" % (TPP_SSH_CADN, ssh_config.ca_public_key))
83-
log.debug("%s default principals: %s" % (TPP_SSH_CADN, ssh_config.ca_principals))
80+
self.assertIsNotNone(ssh_config.ca_public_key, f"{TPP_SSH_CADN} Public Key data is empty")
81+
self.assertIsNotNone(ssh_config.ca_principals, f"{TPP_SSH_CADN} default principals is empty")
82+
log.debug(f"{TPP_SSH_CADN} Public Key data: {ssh_config.ca_public_key}")
83+
log.debug(f"{TPP_SSH_CADN} default principals: {ssh_config.ca_principals}")
8484

8585

8686
class TestSSHUtils(unittest.TestCase):
8787
def test_write_ssh_files(self):
8888
key_id = _random_key_id()
8989
normalized_name = re.sub(r"[^A-Za-z0-9]+", "_", key_id)
90-
full_path = "./" + normalized_name
90+
full_path = f"./{normalized_name}"
9191
write_ssh_files("./", key_id, SSH_CERT_DATA, SSH_PRIVATE_KEY, SSH_PUBLIC_KEY)
9292

93-
err_msg = "%s serialization does not match expected value"
93+
err_msg = "{} serialization does not match expected value"
9494

95-
with open(full_path + "-cert.pub", "r") as cert_file:
95+
with open(f"{full_path}-cert.pub", "r") as cert_file:
9696
s_cert = cert_file.read()
97-
self.assertTrue(SSH_CERT_DATA == s_cert, err_msg % "SSH Certificate")
97+
self.assertTrue(SSH_CERT_DATA == s_cert, err_msg.format("SSH Certificate"))
9898

9999
with open(full_path, "r") as priv_key_file:
100100
s_priv_key = priv_key_file.read()
101101
expected_priv_key = SSH_PRIVATE_KEY
102102
if platform.system() != "Windows":
103103
expected_priv_key = expected_priv_key.replace("\r\n", "\n")
104104

105-
self.assertTrue(expected_priv_key == s_priv_key, err_msg % "SSH Private Key")
105+
self.assertTrue(expected_priv_key == s_priv_key, err_msg.format("SSH Private Key"))
106106

107-
with open(full_path + ".pub", "r") as pub_key_file:
107+
with open(f"{full_path}.pub", "r") as pub_key_file:
108108
s_pub_key = pub_key_file.read()
109-
self.assertTrue(SSH_PUBLIC_KEY == s_pub_key, err_msg % "SSH Public Key")
109+
self.assertTrue(SSH_PUBLIC_KEY == s_pub_key, err_msg.format("SSH Public Key"))
110110

111111

112112
def _enroll_ssh_cert(connector, request):
@@ -123,4 +123,4 @@ def _enroll_ssh_cert(connector, request):
123123

124124

125125
def _random_key_id():
126-
return "vcert-python-ssh-%s" % timestamp()
126+
return f"vcert-python-ssh-{timestamp()}"
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
18-
from __future__ import absolute_import, division, generators, unicode_literals, print_function, nested_scopes, \
19-
with_statement
20-
2117
import binascii
2218
import logging
2319
import time
@@ -41,22 +37,22 @@ class TestFakeMethods(unittest.TestCase):
4137
def test_fake_enroll(self):
4238
conn = FakeConnection()
4339
zone = "Default"
44-
cn = random_word(10) + ".venafi.example.com"
40+
cn = f"{random_word(10)}.venafi.example.com"
4541
enroll(conn, zone, cn)
4642

4743

4844
class TestTPPMethods(unittest.TestCase):
4945
def __init__(self, *args, **kwargs):
5046
self.tpp_zone = TPP_ZONE
5147
self.tpp_zone_ecdsa = TPP_ZONE_ECDSA
52-
self.tpp_conn = TPPConnection(TPP_USER, TPP_PASSWORD, TPP_URL, http_request_kwargs={"verify": "/tmp/chain.pem"})
48+
self.tpp_conn = TPPConnection(TPP_USER, TPP_PASSWORD, TPP_URL, http_request_kwargs={'verify': "/tmp/chain.pem"})
5349
super(TestTPPMethods, self).__init__(*args, **kwargs)
5450

5551
def test_tpp_enroll(self):
56-
cn = random_word(10) + ".venafi.example.com"
52+
cn = f"{random_word(10)}.venafi.example.com"
5753
_, pkey, cert, _, cert_guid = enroll(self.tpp_conn, self.tpp_zone, cn)
5854
cert_config = self.tpp_conn._get_certificate_details(cert_guid)
59-
self.assertEqual(cert_config["Origin"], "Venafi VCert-Python")
55+
self.assertEqual(cert_config['Origin'], "Venafi VCert-Python")
6056

6157
def test_tpp_enroll_with_custom_fields(self):
6258
cn = random_word(10) + ".venafi.example.com"

0 commit comments

Comments
 (0)