Skip to content

Commit

Permalink
Add additional testing for terraform_to_secrets.github
Browse files Browse the repository at this point in the history
Ensure we are testing every function that does not need to reach out to
the GitHub API.
  • Loading branch information
mcdonnnj committed Jan 20, 2023
1 parent ca32fde commit a4fed22
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ jobs:
- name: Install testing dependencies
run: |
python -m pip install --upgrade pip setuptools wheel
python -m pip install --upgrade pytest pytest-cov
python -m pip install --upgrade pytest pytest-cov pytest-subprocess
- name: Install the built wheel (there should only be one)
run: python -m pip install *.whl
- name: Run tests
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def get_version(version_file):
"coveralls != 1.11.0",
"pre-commit",
"pytest-cov",
"pytest-subprocess",
"pytest",
]
},
Expand Down
73 changes: 71 additions & 2 deletions tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Tests for the terraform_to_secrets.github module."""
# Standard Python Libraries
from base64 import b64decode
import logging

# Third-Party Libraries
from nacl import encoding, public
Expand All @@ -13,6 +14,7 @@
test_plaintext = "Hello, World!"

repository_name = "cisagov/terraform-to-secrets"
git_subprocess_call = ["git", "remote", "get-url", "origin"]

test_single_user_secrets = {
"test-user": (" AKIAIOSFODNN7TEST", "wJalrXUtnFEMI/K7MDENG/bPxRfiCYTESTKEY")
Expand Down Expand Up @@ -45,12 +47,79 @@ def test_encrypt(encryption_keys):
assert plaintext_value == test_plaintext


def test_get_repo_name():
"""Verify that the current repository name is returned."""
def test_get_repo_name_from_local_clone():
"""Verify that the correct repository name is retrieved from the local git configuration."""
repo_name = terraform_to_secrets.github.get_repo_name()
assert repo_name == repository_name, "expected repository name not found"


@pytest.mark.parametrize(
"url",
[
f"git@github.com:{repository_name}",
f"git@github.com:{repository_name}.git",
f"https://github.com/{repository_name}",
f"https://github.com/{repository_name}.git",
],
)
def test_get_repo_name_good_urls(fp, url):
"""Verify that the correct repository name is extracted from good git remote URLs."""
fp.register(git_subprocess_call, stdout=url + "\n")
repo_name = terraform_to_secrets.github.get_repo_name()
assert repo_name == repository_name, "expected repository name not found"


@pytest.mark.parametrize(
"url",
[
f"git@githubbcom:{repository_name}",
f"git@githubbcom:{repository_name}.git",
f"https://githubbcom/{repository_name}",
f"https://githubbcom/{repository_name}.git",
],
)
def test_get_repo_name_bad_urls(caplog, fp, url):
"""Verify that the function fails when an invalid git remote URL is found."""
expected_log_messages = [
"Could not determine GitHub repository name.",
"Use the --repo option to specify it manually.",
]
fp.register(git_subprocess_call, stdout=url + "\n")
caplog.set_level(logging.CRITICAL)
repo_name = None

with pytest.raises(Exception) as exc_info:
repo_name = terraform_to_secrets.github.get_repo_name()

assert repo_name is None, "unxpected return result"
assert (
str(exc_info.value) == "Could not determine GitHub repository name."
), "unexpected exception encountered"
assert expected_log_messages == [
rec.message for rec in caplog.records
], "missing expected logging output"


def test_get_repo_name_non_zero_return_code(caplog, fp):
"""Verify that the function fails if the subprocess call has a non-zero return code."""
test_string = "Testing non-zero return code."
expected_log_messages = [
"Could not determine GitHub repository name.",
]
fp.register(git_subprocess_call, returncode=1, stderr=test_string)
caplog.set_level(logging.CRITICAL)
repo_name = None

with pytest.raises(Exception) as exc_info:
repo_name = terraform_to_secrets.github.get_repo_name()

assert repo_name is None, "unexpected return result"
assert test_string == str(exc_info.value), "unexpected exception encountered"
assert expected_log_messages == [
rec.message for rec in caplog.records
], "missing expected logging output"


def test_create_user_secrets_single_user():
"""Verify that the secrets for a single user are created."""
created_secrets = terraform_to_secrets.github.create_user_secrets(
Expand Down

0 comments on commit a4fed22

Please sign in to comment.