Skip to content

Commit 8c52544

Browse files
committed
More coverage
1 parent 0532830 commit 8c52544

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

cert_chain_resolver/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ def __repr__(self):
4949
)
5050

5151
def __eq__(self, other):
52-
# type: (object) -> bool
52+
# type: (Cert) -> bool
5353
if not isinstance(other, Cert):
54-
return NotImplemented
54+
raise TypeError
5555
return self.fingerprint == other.fingerprint
5656

5757
@property

tests/test_cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import importlib
2+
import sys
13
from tempfile import NamedTemporaryFile
24
import pytest
35
from cert_chain_resolver import __is_py3__
@@ -222,3 +224,12 @@ def test_main_handles_different_file_input(mocker, file_name, expected_content):
222224
include_root=False,
223225
root_ca_store=mocker.ANY,
224226
)
227+
228+
229+
def test_main_no_args_tty_shows_help_and_exits(mocker):
230+
mocker.patch("sys.stdin.isatty", return_value=True)
231+
mocker.patch("sys.argv", ["script_name"])
232+
233+
with pytest.raises(SystemExit):
234+
main()
235+
assert sys.argv == ["script_name", "-h"]

tests/test_models.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,23 @@
55
from cryptography.x509.oid import ExtensionOID, AuthorityInformationAccessOID, NameOID
66
import pytest
77

8+
9+
try:
10+
from contextlib import nullcontext as does_not_raise
11+
except ImportError:
12+
# Python 2 fallback
13+
class nullcontext(object):
14+
def __init__(self, enter_result=None):
15+
self.enter_result = enter_result
16+
17+
def __enter__(self):
18+
return self.enter_result
19+
20+
def __exit__(self, *excinfo):
21+
pass
22+
23+
does_not_raise = nullcontext
24+
825
try:
926
unicode # type: ignore
1027
except NameError:
@@ -29,6 +46,16 @@ def test_certcontainer_x509_helper_props(cert):
2946
assert fixture["ca_issuer_access_location"] == c.ca_issuer_access_location
3047

3148

49+
def test_cert_constructor_requires_x509():
50+
with pytest.raises(TypeError, match="Argument must be a x509"):
51+
Cert("not a x509 obj")
52+
53+
54+
def test_cert__eq__raises(mocker):
55+
with pytest.raises(TypeError):
56+
Cert(mocker.Mock(spec=x509.Certificate)).__eq__("Not a Cert")
57+
58+
3259
@pytest.mark.parametrize(
3360
"_subject",
3461
["CA - XD 9001", pytest.param("CN=github.com,O=GitHub", marks=[pytest.mark.xfail])],
@@ -137,6 +164,28 @@ def test_missing_cert_properties_raise(mocker, prop, cert_prop, cert_value):
137164
getattr(c, prop)
138165

139166

167+
@pytest.mark.parametrize(
168+
"value,expectation",
169+
[
170+
(b"Common name", does_not_raise()),
171+
(unicode("Common name"), does_not_raise()),
172+
(["unexpected type"], pytest.raises(ValueError)),
173+
],
174+
)
175+
def test_common_name_handles_unicode_or_bytes(mocker, value, expectation):
176+
m = mocker.Mock(
177+
spec=x509.Certificate,
178+
subject=mocker.Mock(
179+
get_attributes_for_oid=mocker.Mock(
180+
return_value=[mocker.Mock(spec=type(value), value=value)]
181+
)
182+
),
183+
)
184+
with expectation:
185+
c = Cert(m)
186+
assert c.common_name == "Common name"
187+
188+
140189
def test_repr():
141190
class CertOverride(Cert):
142191
subject = "Subject"

0 commit comments

Comments
 (0)