Skip to content

Commit

Permalink
* add appAuthMode BasicTestCase field
Browse files Browse the repository at this point in the history
* move `assertWarning` and `assertWarnings` into `BasicTestCase` so we can re-use it across tests.
* assert deprecation warning and transparently call GithubIntegration.get_app() when no slug is provided
* check that deprecation warning happens in tech case
* assert that auth is an AppAuth in GithubIntegration
  • Loading branch information
chantra committed Jun 9, 2023
1 parent db61637 commit 525cc3d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 18 deletions.
5 changes: 4 additions & 1 deletion github/GithubIntegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ def __init__(
jwt_algorithm=jwt_algorithm,
)

assert auth is not None
assert isinstance(
auth, AppAuth
), f"GithubIntegration requires github.Auth.AppAuth authentication, not {type(auth)}"

self.auth = auth

self.__requester = Requester(
Expand Down
9 changes: 6 additions & 3 deletions github/MainClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,9 +808,12 @@ def get_app(self, slug=github.GithubObject.NotSet):
if slug is github.GithubObject.NotSet:
# with no slug given, calling /app returns the authenticated app,
# including the actual /apps/{slug}
assert self.__requester._Requester__auth.token_type == 'Bearer', "Only AppAuth authentication support get_app without a slug."
headers, data = self.__requester.requestJsonAndCheck("GET", "/app")
return GithubApp.GithubApp(self.__requester, headers, data, completed=True)
warnings.warn(
"Argument slug is mandatory, calling this method without the slug argument is deprecated, please use "
"github.GithubIntegration(auth=github.Auth.AppAuth(...)).get_app() instead",
category=DeprecationWarning,
)
return GithubIntegration(auth=self.__requester._Requester__auth).get_app()
else:
# with a slug given, we can lazily load the GithubApp
return GithubApp.GithubApp(
Expand Down
11 changes: 0 additions & 11 deletions tests/Authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################
import warnings
from unittest import mock

import jwt
Expand All @@ -41,16 +40,6 @@ def testNoAuthentication(self):
g = github.Github()
self.assertEqual(g.get_user("jacquev6").name, "Vincent Jacques")

def assertWarning(self, warning, expected):
self.assertWarnings(warning, expected)

def assertWarnings(self, warning, *expecteds):
self.assertEqual(len(warning.warnings), len(expecteds))
for message, expected in zip(warning.warnings, expecteds):
self.assertIsInstance(message, warnings.WarningMessage)
self.assertIsInstance(message.message, DeprecationWarning)
self.assertEqual(message.message.args, (expected,))

def testBasicAuthentication(self):
with self.assertWarns(DeprecationWarning) as warning:
g = github.Github(self.login.login, self.login.password)
Expand Down
20 changes: 20 additions & 0 deletions tests/Framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import os
import traceback
import unittest
import warnings

import httpretty # type: ignore
from requests.structures import CaseInsensitiveDict
Expand Down Expand Up @@ -262,6 +263,7 @@ class BasicTestCase(unittest.TestCase):
recordMode = False
tokenAuthMode = False
jwtAuthMode = False
appAuthMode = False
retry = None
pool_size = None
replayDataFolder = os.path.join(os.path.dirname(__file__), "ReplayData")
Expand Down Expand Up @@ -328,6 +330,16 @@ def tearDown(self):
self.__closeReplayFileIfNeeded()
github.Requester.Requester.resetConnectionClasses()

def assertWarning(self, warning, expected):
self.assertWarnings(warning, expected)

def assertWarnings(self, warning, *expecteds):
self.assertEqual(len(warning.warnings), len(expecteds))
for message, expected in zip(warning.warnings, expecteds):
self.assertIsInstance(message, warnings.WarningMessage)
self.assertIsInstance(message.message, DeprecationWarning)
self.assertEqual(message.message.args, (expected,))

def __openFile(self, mode):
for (_, _, functionName, _) in traceback.extract_stack():
if (
Expand Down Expand Up @@ -392,6 +404,10 @@ def setUp(self):
self.g = github.Github(
auth=self.jwt, retry=self.retry, pool_size=self.pool_size
)
elif self.appAuthMode:
self.g = github.Github(
auth=self.app_auth, retry=self.retry, pool_size=self.pool_size
)
else:
self.g = github.Github(
auth=self.login, retry=self.retry, pool_size=self.pool_size
Expand All @@ -410,6 +426,10 @@ def activateJWTAuthMode(): # pragma no cover (Function useful only when recordi
BasicTestCase.jwtAuthMode = True


def activateAppAuthMode(): # pragma no cover (Function useful only when recording new tests, not used during automated tests)
BasicTestCase.appAuthMode = True


def enableRetry(retry):
BasicTestCase.retry = retry

Expand Down
11 changes: 9 additions & 2 deletions tests/GithubApp.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,19 @@ def testGetPublicApp(self):

class GithubAppAuth(Framework.TestCase):
def setUp(self):
self.jwtAuthMode = True
self.appAuthMode = True
super().setUp()

def testGetAuthenticatedApp(self):
with self.assertWarns(DeprecationWarning) as warning:
app = self.g.get_app()

self.assertWarning(
warning,
"Argument slug is mandatory, calling this method without the slug argument is deprecated, please use "
"github.GithubIntegration(auth=github.Auth.AppAuth(...)).get_app() instead",
)

app = self.g.get_app()
self.assertEqual(app.created_at, datetime(2020, 8, 1, 17, 23, 46))
self.assertEqual(app.description, "Sample App to test PyGithub")
self.assertListEqual(
Expand Down
2 changes: 1 addition & 1 deletion tests/ReplayData/GithubAppAuth.testGetAuthenticatedApp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GET
api.github.com
None
/app
{'Authorization': 'Bearer jwt_removed', 'User-Agent': 'PyGithub/Python'}
{'Authorization': 'Bearer jwt_removed', 'User-Agent': 'PyGithub/Python', 'Accept': 'application/vnd.github.machine-man-preview+json'}
None
200
[('Date', 'Sun, 02 Aug 2020 04:57:48 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Server', 'GitHub.com'), ('Status', '200 OK'), ('Cache-Control', 'public, max-age=60, s-maxage=60'), ('Vary', 'Accept, Accept-Encoding, Accept, X-Requested-With, Accept-Encoding'), ('ETag', 'W/"76244215f77fc6f3d9262dea400b2567"'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, Deprecation, Sunset'), ('Access-Control-Allow-Origin', '*'), ('Strict-Transport-Security', 'max-age=31536000; includeSubdomains; preload'), ('X-Frame-Options', 'deny'), ('X-Content-Type-Options', 'nosniff'), ('X-XSS-Protection', '1; mode=block'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'C28A:25FE:11739F:15A3E5:5F2647CC')]
Expand Down

0 comments on commit 525cc3d

Please sign in to comment.