Skip to content

Commit

Permalink
more acoverage
Browse files Browse the repository at this point in the history
  • Loading branch information
rkoopmans committed Aug 11, 2024
1 parent 5060dc6 commit 96201c6
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
9 changes: 9 additions & 0 deletions tests/test_castore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
from cert_chain_resolver.castore.base_store import CAStore
from cert_chain_resolver.models import Cert


def test_find_issuer_candidates_needs_impl(mocker):
m = mocker.Mock(spec=Cert)
with pytest.raises(NotImplementedError):
CAStore().find_issuer_candidates(m)
11 changes: 10 additions & 1 deletion tests/test_castore_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
RootCertificateNotFound,
)
from cert_chain_resolver.models import Cert
from cert_chain_resolver.castore.file_system import FileSystemStore
from cert_chain_resolver.castore.file_system import FileSystemStore, eligible_paths
from tests.fixtures import BUNDLE_FIXTURES, certfixture_to_id
import tempfile
import pytest
Expand Down Expand Up @@ -35,3 +35,12 @@ def test_custom_bundle_path_that_does_not_resolve_certs(bundle):
def test_bundle_path_does_not_exist():
with pytest.raises(CertificateChainResolverError):
store = FileSystemStore("/tmp/addd/a/sd/df/g/h/j/x/vz/a/i-dont-exist.pem")


def test_bundle_path_cannot_be_found(monkeypatch):
monkeypatch.setattr(
"cert_chain_resolver.castore.file_system.eligible_paths",
["/tmp/i-do-not-exist"],
)
with pytest.raises(CertificateChainResolverError, match="Can't detect CA bundle"):
FileSystemStore()
43 changes: 42 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from tempfile import NamedTemporaryFile
import pytest
from cert_chain_resolver import __is_py3__

from cert_chain_resolver.cli import cli, parse_args
from cert_chain_resolver.cli import cli, main, parse_args
from cert_chain_resolver.castore.file_system import FileSystemStore
from .fixtures import BUNDLE_FIXTURES, certfixture_to_id

Expand Down Expand Up @@ -173,3 +174,43 @@ def test_display_flag_is_properly_formatted(capsys):
)

assert expected == captured


def test_display_flag_includes_warning_when_root_was_requested_but_not_found(capsys):
bundle = BUNDLE_FIXTURES[0]
cli(file_bytes=bundle[0]["cert_pem"], show_details=True, include_root=True)
captured = unicode(capsys.readouterr().err)
assert captured == "WARNING: Root certificate was requested, but not found!\n"


@pytest.mark.parametrize(
"file_name, expected_content",
[("test.pem", b"test certificate data"), ("-", b"stdin data")],
)
def test_main_handles_different_file_input(mocker, file_name, expected_content):
args = mocker.Mock(
info=True, include_root=False, ca_bundle_path="/test/path", file_name="test.pem"
)
args.file_name = file_name
mocker.patch("cert_chain_resolver.cli.parse_args", return_value=args)

fs_store = mocker.patch("cert_chain_resolver.cli.FileSystemStore")

if file_name == "-":
mocker.patch("sys.stdin.buffer.read", return_value=expected_content)
else:
mocker.patch("builtins.open", mocker.mock_open(read_data=expected_content))

mock_cli = mocker.patch("cert_chain_resolver.cli.cli")

main()

assert fs_store.call_args == mocker.call(
"/test/path",
)
assert mock_cli.call_args == mocker.call(
file_bytes=expected_content,
show_details=True,
include_root=False,
root_ca_store=mocker.ANY,
)

0 comments on commit 96201c6

Please sign in to comment.