Skip to content

Commit

Permalink
100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
robertdfrench committed Jun 16, 2024
1 parent f535169 commit 62693cf
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/__pycache__/*
.venv/*
.coverage
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
venv=. .venv/bin/activate &&

test: .venv/ready
$(venv) pytest
test: lint typecheck check

.venv/ready: requirements.txt .venv/update
check: .venv/ready
$(venv) pytest --cov=. --cov-fail-under=100

lint: .venv/ready
$(venv) flake8 wmap tests/test_wmap.py

typecheck: .venv/ready
$(venv) mypy wmap

.venv/ready: dev-requirements.txt .venv/update
$(venv) pip install -r $<
touch $@

Expand Down
13 changes: 13 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
coverage==7.5.3
flake8==7.1.0
iniconfig==2.0.0
mccabe==0.7.0
mypy==1.10.0
mypy-extensions==1.0.0
packaging==24.1
pluggy==1.5.0
pycodestyle==2.12.0
pyflakes==3.2.0
pytest==8.2.2
pytest-cov==5.0.0
typing_extensions==4.12.2
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

62 changes: 38 additions & 24 deletions tests/test_wmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,83 @@
import tempfile
from . import wmap

def test_parse_algorithm():
key = wmap.AuthorizedKey.parse("ssh-rsa abc123 blah blah blah")
assert(key.algorithm == wmap.Algorithm.RSA)

def test_parse_material():
key = wmap.AuthorizedKey.parse("ssh-rsa abc123 blah blah blah")
assert(key.material == "abc123")

def test_parse_comment():
key = wmap.AuthorizedKey.parse("ssh-rsa abc123 blah blah blah")
assert(key.comment == "blah blah blah")

def test_into_allowed_signer():
key = wmap.AuthorizedKey.parse("ssh-rsa abc123 blah blah blah")
profile = wmap.Profile("example")
signer = key.into_allowed_signer(profile)
assert(signer == "example namespaces=\"[email protected]\" ssh-rsa abc123")

def test_algorithm_rsa():
rsa = wmap.Algorithm.parse("ssh-rsa")
assert(rsa == wmap.Algorithm.RSA)
assert rsa == wmap.Algorithm.RSA


def test_algorithm_ed25519():
ed25519 = wmap.Algorithm.parse("ssh-ed25519")
assert(ed25519 == wmap.Algorithm.ED25519)
assert ed25519 == wmap.Algorithm.ED25519


def test_algorithm_bogus():
with pytest.raises(Exception):
wmap.Algorithm.parse("ssh-junk")


def test_authorized_key_parse_algorithm():
key = wmap.AuthorizedKey.parse("ssh-rsa abc123 blah blah blah")
assert key.algorithm == wmap.Algorithm.RSA


def test_authorized_key_parse_material():
key = wmap.AuthorizedKey.parse("ssh-rsa abc123 blah blah blah")
assert key.material == "abc123"


def test_authorized_key_parse_comment():
key = wmap.AuthorizedKey.parse("ssh-rsa abc123 blah blah blah")
assert key.comment == "blah blah blah"


def test_authorized_key_into_allowed_signer():
key = wmap.AuthorizedKey.parse("ssh-rsa abc123 blah blah blah")
profile = wmap.Profile("example")
signer = key.into_allowed_signer(profile)
assert signer == "example namespaces=\"[email protected]\" ssh-rsa abc123"


def test_profile_key_url():
profile_url = "https://github.com/robertdfrench"
authorized_keys_url = "https://github.com/robertdfrench.keys"
profile = wmap.Profile(profile_url)
assert(profile.authorized_keys_url() == authorized_keys_url)
assert profile.authorized_keys_url() == authorized_keys_url


def test_profile_fetch_authorized_keys_text():
profile = wmap.Profile("https://github.com/robertdfrench")
authorized_keys = profile.authorized_keys()
assert(len(authorized_keys) > 0)
assert len(authorized_keys) > 0


def test_profile_allowed_signers():
profile = wmap.Profile("https://github.com/robertdfrench")
for signer in profile.allowed_signers():
assert(signer.startswith("https://github.com/robertdfrench"))
assert signer.startswith("https://github.com/robertdfrench")


@pytest.fixture
def ssh_private_key_path():
# Create a temporary directory to store the key pair
with tempfile.TemporaryDirectory() as temp_dir:
private_key_path = os.path.join(temp_dir, "id_ed25519")
public_key_path = private_key_path + ".pub"

# Generate the ed25519 SSH key pair using ssh-keygen
subprocess.run([
'ssh-keygen', '-t', 'ed25519', '-f', private_key_path, '-N', ''
], check=True)

# Yield the private key path for use in tests
yield private_key_path

# Clean up the key files
os.remove(private_key_path)
os.remove(public_key_path)


def test_private_key_signing(ssh_private_key_path):
profile = wmap.Profile("https://github.com/robertdfrench")
private_key = wmap.PrivateKey(profile, ssh_private_key_path)
Expand All @@ -79,13 +91,15 @@ def test_private_key_signing(ssh_private_key_path):
signature_file = Path(f.name + ".sig")
assert signature_file.exists()


def test_signature_load():
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(b"Hello World!")
sig = wmap.Signature.load(f.name)
assert sig.content == "SGVsbG8gV29ybGQh"
os.remove(f.name)


def test_signature_dump():
with tempfile.NamedTemporaryFile(delete=False) as f:
f.write(b"Hello World!")
Expand Down
4 changes: 2 additions & 2 deletions wmap
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@ class Signature:
f.write(decoded)


if __name__ == "__main__":
print("Hello")
if __name__ == "__main__": # pragma: no cover
print("Hello")

0 comments on commit 62693cf

Please sign in to comment.