Skip to content

Commit a9aecb6

Browse files
committed
More coverage
1 parent 0532830 commit a9aecb6

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

cert_chain_resolver/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __repr__(self):
5151
def __eq__(self, other):
5252
# type: (object) -> 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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
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+
from contextlib import contextmanager
13+
14+
@contextmanager
15+
def does_not_raise():
16+
yield
17+
18+
819
try:
920
unicode # type: ignore
1021
except NameError:
@@ -29,6 +40,16 @@ def test_certcontainer_x509_helper_props(cert):
2940
assert fixture["ca_issuer_access_location"] == c.ca_issuer_access_location
3041

3142

43+
def test_cert_constructor_requires_x509():
44+
with pytest.raises(TypeError, match="Argument must be a x509"):
45+
Cert("not a x509 obj")
46+
47+
48+
def test_cert__eq__raises(mocker):
49+
with pytest.raises(TypeError):
50+
Cert(mocker.Mock(spec=x509.Certificate)).__eq__("Not a Cert")
51+
52+
3253
@pytest.mark.parametrize(
3354
"_subject",
3455
["CA - XD 9001", pytest.param("CN=github.com,O=GitHub", marks=[pytest.mark.xfail])],
@@ -137,6 +158,28 @@ def test_missing_cert_properties_raise(mocker, prop, cert_prop, cert_value):
137158
getattr(c, prop)
138159

139160

161+
@pytest.mark.parametrize(
162+
"value,expectation",
163+
[
164+
(b"Common name", does_not_raise()),
165+
(unicode("Common name"), does_not_raise()),
166+
(["unexpected type"], pytest.raises(ValueError)),
167+
],
168+
)
169+
def test_common_name_handles_unicode_or_bytes(mocker, value, expectation):
170+
m = mocker.Mock(
171+
spec=x509.Certificate,
172+
subject=mocker.Mock(
173+
get_attributes_for_oid=mocker.Mock(
174+
return_value=[mocker.Mock(spec=type(value), value=value)]
175+
)
176+
),
177+
)
178+
with expectation:
179+
c = Cert(m)
180+
assert c.common_name == "Common name"
181+
182+
140183
def test_repr():
141184
class CertOverride(Cert):
142185
subject = "Subject"

tests/test_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import pytest
2+
3+
from cert_chain_resolver.exceptions import ImproperlyFormattedCert
24
from .fixtures import TEST_CERTS_IN_VARIOUS_FORMATS
35
from cert_chain_resolver.utils import load_bytes_to_x509
46
from cryptography.x509 import Certificate
@@ -10,3 +12,8 @@ def test_load_bytes_to_x509(file_type, source_file):
1012
content = f.read()
1113
res = load_bytes_to_x509(content)
1214
assert isinstance(res, Certificate)
15+
16+
17+
def test_load_other_text_raises():
18+
with pytest.raises(ImproperlyFormattedCert):
19+
load_bytes_to_x509(b"just text")

0 commit comments

Comments
 (0)