Skip to content

Commit 8451d9f

Browse files
committed
Improve test coverage
1 parent 4d4e80b commit 8451d9f

File tree

5 files changed

+152
-8
lines changed

5 files changed

+152
-8
lines changed

.coveragerc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ source =
88
*/cert_chain_resolver
99

1010
[report]
11-
include = cert_chain_resolver/*
11+
include = cert_chain_resolver/*
12+
exclude_also =
13+
if TYPE_CHECKING:

cert_chain_resolver/models.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,8 @@ def __init__(self, x509_obj):
4444

4545
def __repr__(self):
4646
# type: () -> str
47-
try:
48-
common_name = self.common_name
49-
except MissingCertProperty:
50-
common_name = None
51-
5247
return '<Cert common_name="{0}" subject="{1}" issuer="{2}">'.format(
53-
common_name, self.subject, self.issuer
48+
self.common_name, self.subject, self.issuer
5449
)
5550

5651
def __eq__(self, other):

tests/test_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from cert_chain_resolver import api
2+
import pytest
3+
from cert_chain_resolver.resolver import resolve
4+
from cert_chain_resolver.models import CertificateChain, Cert
5+
from cert_chain_resolver.castore.file_system import FileSystemStore
6+
7+
8+
@pytest.mark.parametrize(
9+
"exported,obj",
10+
[
11+
("resolve", resolve),
12+
("CertificateChain", CertificateChain),
13+
("Cert", Cert),
14+
("FileSystemStore", FileSystemStore),
15+
],
16+
)
17+
def test_api_exports_right_objects(exported, obj):
18+
assert getattr(api, exported) == obj

tests/test_cli.py

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from cert_chain_resolver import __is_py3__
33

4-
from cert_chain_resolver.cli import cli
4+
from cert_chain_resolver.cli import cli, parse_args
55
from cert_chain_resolver.castore.file_system import FileSystemStore
66
from .fixtures import BUNDLE_FIXTURES, certfixture_to_id
77

@@ -11,6 +11,82 @@
1111
unicode = str
1212

1313

14+
@pytest.mark.parametrize(
15+
"cli_args, expected",
16+
[
17+
(
18+
[],
19+
{
20+
"file_name": "-",
21+
"info": False,
22+
"include_root": False,
23+
"ca_bundle_path": None,
24+
},
25+
),
26+
(
27+
["test.crt"],
28+
{
29+
"file_name": "test.crt",
30+
"info": False,
31+
"include_root": False,
32+
"ca_bundle_path": None,
33+
},
34+
),
35+
(
36+
["-i"],
37+
{
38+
"file_name": "-",
39+
"info": True,
40+
"include_root": False,
41+
"ca_bundle_path": None,
42+
},
43+
),
44+
(
45+
["--include-root"],
46+
{
47+
"file_name": "-",
48+
"info": False,
49+
"include_root": True,
50+
"ca_bundle_path": None,
51+
},
52+
),
53+
(
54+
["--ca-bundle-path", "/path/to/ca/bundle"],
55+
{
56+
"file_name": "-",
57+
"info": False,
58+
"include_root": False,
59+
"ca_bundle_path": "/path/to/ca/bundle",
60+
},
61+
),
62+
(
63+
[
64+
"test.crt",
65+
"-i",
66+
"--include-root",
67+
"--ca-bundle-path",
68+
"/path/to/ca/bundle",
69+
],
70+
{
71+
"file_name": "test.crt",
72+
"info": True,
73+
"include_root": True,
74+
"ca_bundle_path": "/path/to/ca/bundle",
75+
},
76+
),
77+
],
78+
)
79+
def test_parse_args(cli_args, expected, monkeypatch):
80+
monkeypatch.setattr("sys.argv", ["script_name"] + cli_args)
81+
82+
args = parse_args()
83+
84+
assert args.file_name == expected["file_name"]
85+
assert args.info == expected["info"]
86+
assert args.include_root == expected["include_root"]
87+
assert args.ca_bundle_path == expected["ca_bundle_path"]
88+
89+
1490
@pytest.mark.parametrize("bundle", BUNDLE_FIXTURES, ids=certfixture_to_id)
1591
def test_cert_returns_completed_chain(capsys, bundle):
1692
cli(file_bytes=bundle[0]["cert_pem"])

tests/test_models.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from unittest.mock import Mock
2+
from cert_chain_resolver.exceptions import MissingCertProperty
13
from .fixtures import BUNDLE_FIXTURES, CERT_FIXTURES, certfixture_to_id
4+
from cryptography import x509
25
from cert_chain_resolver.models import Cert, CertificateChain
6+
from cryptography.x509.oid import ExtensionOID, AuthorityInformationAccessOID, NameOID
37
import pytest
48

59
try:
@@ -88,3 +92,52 @@ def test_certificatechain_constructs_from_pem_in_order(bundle):
8892

8993
chain = CertificateChain.load_from_pem(pem_bundle)
9094
assert list(chain) == [Cert(x["cert_x509"]) for x in bundle]
95+
96+
97+
@pytest.mark.parametrize(
98+
"prop,extension_oid,expected",
99+
[
100+
("ca_issuer_access_location", ExtensionOID.AUTHORITY_INFORMATION_ACCESS, None),
101+
("subject_alternative_names", ExtensionOID.SUBJECT_ALTERNATIVE_NAME, []),
102+
("is_ca", ExtensionOID.BASIC_CONSTRAINTS, False),
103+
],
104+
)
105+
def test_missing_cert_extensions_return_defaults_when_missing(
106+
mocker, prop, extension_oid, expected
107+
):
108+
m = mocker.Mock(spec=x509.Certificate)
109+
mock_extensions = mocker.Mock()
110+
mock_extensions.get_extension_for_oid.side_effect = (
111+
x509.extensions.ExtensionNotFound("Extension not found", extension_oid)
112+
)
113+
m.extensions = mock_extensions
114+
c = Cert(m)
115+
assert getattr(c, prop) == expected
116+
117+
118+
@pytest.mark.parametrize(
119+
"prop,cert_prop, cert_value",
120+
[
121+
("signature_hash_algorithm", "signature_hash_algorithm", None),
122+
("common_name", "subject", Mock(get_attributes_for_oid=Mock(return_value=[]))),
123+
],
124+
)
125+
def test_missing_cert_properties_raise(mocker, prop, cert_prop, cert_value):
126+
m = mocker.Mock(spec=x509.Certificate)
127+
setattr(m, cert_prop, cert_value)
128+
c = Cert(m)
129+
130+
with pytest.raises(MissingCertProperty):
131+
getattr(c, prop)
132+
133+
134+
def test_repr():
135+
class CertOverride(Cert):
136+
subject = "Subject"
137+
issuer = "Issuer"
138+
common_name = "CN"
139+
__init__ = lambda *_: None
140+
141+
c = CertOverride()
142+
143+
assert repr(c) == '<Cert common_name="CN" subject="Subject" issuer="Issuer">'

0 commit comments

Comments
 (0)