diff --git a/cert_chain_resolver/models.py b/cert_chain_resolver/models.py index 2f70915..b28d41c 100644 --- a/cert_chain_resolver/models.py +++ b/cert_chain_resolver/models.py @@ -49,9 +49,9 @@ def __repr__(self): ) def __eq__(self, other): - # type: (object) -> bool + # type: (Cert) -> bool if not isinstance(other, Cert): - return NotImplemented + raise TypeError return self.fingerprint == other.fingerprint @property diff --git a/tests/test_cli.py b/tests/test_cli.py index 12c1a87..e40ee74 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,3 +1,5 @@ +import importlib +import sys from tempfile import NamedTemporaryFile import pytest from cert_chain_resolver import __is_py3__ @@ -222,3 +224,12 @@ def test_main_handles_different_file_input(mocker, file_name, expected_content): include_root=False, root_ca_store=mocker.ANY, ) + + +def test_main_no_args_tty_shows_help_and_exits(mocker): + mocker.patch("sys.stdin.isatty", return_value=True) + mocker.patch("sys.argv", ["script_name"]) + + with pytest.raises(SystemExit): + main() + assert sys.argv == ["script_name", "-h"] diff --git a/tests/test_models.py b/tests/test_models.py index 7fd38c0..abf4aca 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,4 +1,5 @@ from cert_chain_resolver.exceptions import MissingCertProperty +from contextlib import nullcontext as does_not_raise from .fixtures import BUNDLE_FIXTURES, CERT_FIXTURES, certfixture_to_id from cryptography import x509 from cert_chain_resolver.models import Cert, CertificateChain @@ -29,6 +30,16 @@ def test_certcontainer_x509_helper_props(cert): assert fixture["ca_issuer_access_location"] == c.ca_issuer_access_location +def test_cert_constructor_requires_x509(): + with pytest.raises(TypeError, match="Argument must be a x509"): + Cert("not a x509 obj") + + +def test_cert__eq__raises(mocker): + with pytest.raises(TypeError): + Cert(mocker.Mock(spec=x509.Certificate)).__eq__("Not a Cert") + + @pytest.mark.parametrize( "_subject", ["CA - XD 9001", pytest.param("CN=github.com,O=GitHub", marks=[pytest.mark.xfail])], @@ -137,6 +148,28 @@ def test_missing_cert_properties_raise(mocker, prop, cert_prop, cert_value): getattr(c, prop) +@pytest.mark.parametrize( + "value,expectation", + [ + (b"Common name", does_not_raise()), + (unicode("Common name"), does_not_raise()), + (["unexpected type"], pytest.raises(ValueError)), + ], +) +def test_common_name_handles_unicode_or_bytes(mocker, value, expectation): + m = mocker.Mock( + spec=x509.Certificate, + subject=mocker.Mock( + get_attributes_for_oid=mocker.Mock( + return_value=[mocker.Mock(spec=type(value), value=value)] + ) + ), + ) + with expectation: + c = Cert(m) + assert c.common_name == "Common name" + + def test_repr(): class CertOverride(Cert): subject = "Subject"