Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve auth test coverage #426

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from unittest.mock import MagicMock, patch

import github3
import requests
from auth import auth_to_github, get_github_app_installation_token


Expand Down Expand Up @@ -87,3 +88,39 @@ def test_get_github_app_installation_token(self, mock_post):
)

self.assertEqual(result, dummy_token)

@patch("github3.apps.create_jwt_headers", MagicMock(return_value="gh_token"))
@patch("auth.requests.post")
def test_get_github_app_installation_token_request_failure(self, mock_post):
"""
Test the get_github_app_installation_token function returns None when the request fails.
"""
# Mock the post request to raise a RequestException
mock_post.side_effect = requests.exceptions.RequestException("Request failed")

# Call the function with test data
result = get_github_app_installation_token(
ghe="https://api.github.com",
gh_app_id=12345,
gh_app_private_key_bytes=b"private_key",
gh_app_installation_id=678910,
)

# Assert that the result is None
self.assertIsNone(result)

@patch("github3.login")
def test_auth_to_github_invalid_credentials(self, mock_login):
"""
Test the auth_to_github function raises correct ValueError
when credentials are present but incorrect.
"""
mock_login.return_value = None
with self.assertRaises(ValueError) as context_manager:
auth_to_github("not_a_valid_token", "", "", b"", "", False)

the_exception = context_manager.exception
self.assertEqual(
str(the_exception),
"Unable to authenticate to GitHub",
)