Skip to content

Commit

Permalink
chore: add tests for functions
Browse files Browse the repository at this point in the history
  • Loading branch information
philipdelorenzo committed Oct 20, 2024
1 parent a4f440a commit 114989c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 18 deletions.
3 changes: 1 addition & 2 deletions src/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def check_gh_token(gh_token: str) -> None:
if not gh_token.startswith("ghp_"):
raise GHTokenError(message="GH_TOKEN environment variable is not a valid GitHub token.")

pass

def prerequisites(args: argparse.ArgumentParser.parse_args) -> bool:
"""This function checks to ensure that all prerequisites are met before running the script.
Expand All @@ -41,7 +40,7 @@ def prerequisites(args: argparse.ArgumentParser.parse_args) -> bool:

def is_org(github_actor: str) -> bool:
"""This function will return True if the Github actor is an organization."""
if requests.get(f"https://github.com/org/{github_actor}").status_code == 200:
if requests.get(f"https://api.github.com/orgs/{github_actor}").status_code == 200:
return True

return False
Expand Down
7 changes: 1 addition & 6 deletions src/gh.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ def get_repository_dispatch(args: argparse.ArgumentParser.parse_args) -> str:
Returns:
str: The repository dispatch URL for the repository passed in as an argument.
"""
if args.is_org:
url = f"https://github.com/org/{args.target_repo}"
else:
url = f"https://github.com/{args.target_repo}"

return url
return f"https://github.com/{args.target_repo}"


def get_workflow_data(repo: str) -> list[dict]:
Expand Down
10 changes: 5 additions & 5 deletions src/gh_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ def __init__(
token: str,
api_data: str
) -> None:
self.token = token
self.api_data = api_data # The API data to use for the Github API repos/{username}/{repo}/actions/runs
self.token: str = token
self.api_data: str = api_data # The API data to use for the Github API repos/{username}/{repo}/actions/runs

self.repo_actions_data = self.actions_runs
ic(f"Repo Actions Data(Type) --> {type(self.repo_actions_data)}")
self.repo_actions_url = self.actions_runs()
ic(f"Repo Actions Data(Type) --> {type(self.repo_actions_url)}")

def actions_runs(self) -> str:
return f"https://api.github.com/repos/{self.api_data}/actions/runs"
return f"https://api.github.com/{self.api_data}/actions/runs"

# using an access token
def auth(gh_token: str) -> Auth.Token:
Expand Down
12 changes: 10 additions & 2 deletions src/test_checks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
from helpers import Args # helpers are or testing only
from checks import repo_owner_verification, prerequisites
from checks import repo_owner_verification, prerequisites, is_org, check_gh_token
from src.exceptions import GHTokenError, RepoError

class TestGithubOwnerMixmatch(unittest.TestCase):
Expand All @@ -26,5 +26,13 @@ def test_prerequisites(self):
with self.assertRaises(RepoError):
prerequisites(args=self.args)

def test_is_org(self):
self.github_actor = "philipdelorenzo"
assert is_org(github_actor=self.github_actor) == False, "The Github actor is NOT an organization."

self.github_actor = "google"
assert is_org(github_actor=self.github_actor) == True, "The Github actor is an organization."


def test_check_gh_token(self):
self.gh_token = "ghp_123"
assert check_gh_token(gh_token=self.gh_token) == None, "The GH_TOKEN environment variable is set."
13 changes: 13 additions & 0 deletions src/test_gh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import unittest
import requests

from helpers import Args # helpers are or testing only
from gh import get_repository_dispatch


class TestRepoDispatch(unittest.TestCase):
def test_repo_dispatch(self):
self.args = Args()
self.args.is_org = False
assert get_repository_dispatch(args=self.args) == "https://github.com/philipdelorenzo/test-sync-action-status", "The repository dispatch URL is incorrect."

37 changes: 37 additions & 0 deletions src/test_gh_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest
import requests

from icecream import ic # We will use icecream to print out the job information
from helpers import Args # helpers are or testing only
from gh_api import GithubAPI

class TestAPI(unittest.TestCase):
args = Args()

### Let's set debugging on if the flag is past
try:
args.debug
except AttributeError:
args.debug = False

if args.debug:
ic.enable()
else:
ic.disable()

def test_action_runs(self):
self.args.is_org = False
self.api = GithubAPI(
token="ghp_1234567890",
api_data="repos/philipdelorenzo/test-sync-action-status"
)
assert isinstance(self.api.repo_actions_url, str), f"This is not a string -- {self.api.repo_actions_url}"
assert self.api.repo_actions_url == "https://api.github.com/repos/philipdelorenzo/test-sync-action-status/actions/runs", f"The action runs URL -- {self.api.repo_actions_data} is incorrect."

self.args.is_org = True
self.api = GithubAPI(
token="ghp_1234567890",
api_data="orgs/philipdelorenzo/test-sync-action-status"
)
assert isinstance(self.api.repo_actions_url, str), f"This is not a string -- {self.api.repo_actions_url}"
assert self.api.repo_actions_url == "https://api.github.com/orgs/philipdelorenzo/test-sync-action-status/actions/runs", f"The action runs URL -- {self.api.repo_actions_data} is incorrect."
4 changes: 2 additions & 2 deletions sync-action-status.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@

if is_org(github_actor=gh_actor_org):
args.is_org = True
_api_data = f"/orgs/{args.target_repo}"
_api_data = f"orgs/{args.target_repo}"
ic(f"GitHub Actor: {gh_actor_org} is an organization.") # debug
ic(f"Args: {args}") # debug
else:
args.is_org = False
_api_data = f"/repos/{args.target_repo}"
_api_data = f"repos/{args.target_repo}"
ic(f"GitHub Actor: {gh_actor_org} is not an organization.") # debug
ic(f"Args: {args}") # debug

Expand Down
2 changes: 1 addition & 1 deletion test/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ APP="${DIR}"/../src

run_unit_tests ()
{
python -m unittest discover ${APP}
"${PY}"/bin/python -m unittest discover ${APP}
}

##### ----------------------------- #####
Expand Down

0 comments on commit 114989c

Please sign in to comment.