Skip to content

Commit

Permalink
More coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rkoopmans committed Aug 11, 2024
1 parent 0532830 commit aeda40b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cert_chain_resolver/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib
import sys
from tempfile import NamedTemporaryFile
import pytest
from cert_chain_resolver import __is_py3__
Expand Down Expand Up @@ -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"]
33 changes: 33 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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])],
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit aeda40b

Please sign in to comment.