Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Make token auth default in tests
  • Loading branch information
EnricoMi committed Feb 24, 2025
commit 009ee364428954ff9de1a506d2501aaa3400274c
6 changes: 3 additions & 3 deletions tests/Authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
import jwt

import github
from github.Auth import Auth
from github.Auth import Auth, Login

from . import Framework
from .GithubIntegration import APP_ID, PRIVATE_KEY, PUBLIC_KEY
Expand All @@ -64,7 +64,7 @@ def testNoAuthentication(self):

def testBasicAuthentication(self):
with self.assertWarns(DeprecationWarning) as warning:
g = github.Github(self.login.login, self.login.password)
g = github.Github("login", "password")
self.assertEqual(g.get_user("jacquev6").name, "Vincent Jacques")
self.assertWarning(
warning,
Expand Down Expand Up @@ -107,7 +107,7 @@ def testAppAuthentication(self):

def testLoginAuthentication(self):
# test data copied from testBasicAuthentication to test parity
g = github.Github(auth=self.login)
g = github.Github(auth=Login("login", "password"))
self.assertEqual(g.get_user("jacquev6").name, "Vincent Jacques")

def testTokenAuthentication(self):
Expand Down
10 changes: 5 additions & 5 deletions tests/Enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
# Replay data for this test case is forged, because I don't have access to a real Github Enterprise install
class Enterprise(Framework.BasicTestCase):
def testHttps(self):
g = github.Github(auth=self.login, base_url="https://my.enterprise.com")
g = github.Github(auth=self.oauth_token, base_url="https://my.enterprise.com")
self.assertListKeyEqual(
g.get_user().get_repos(),
lambda r: r.name,
Expand All @@ -65,7 +65,7 @@ def testHttps(self):
)

def testHttp(self):
g = github.Github(auth=self.login, base_url="http://my.enterprise.com")
g = github.Github(auth=self.oauth_token, base_url="http://my.enterprise.com")
self.assertListKeyEqual(
g.get_user().get_repos(),
lambda r: r.name,
Expand All @@ -91,11 +91,11 @@ def testHttp(self):

def testUnknownUrlScheme(self):
with self.assertRaises(AssertionError) as raisedexp:
github.Github(auth=self.login, base_url="foobar://my.enterprise.com")
github.Github(auth=self.oauth_token, base_url="foobar://my.enterprise.com")
self.assertEqual(raisedexp.exception.args[0], "Unknown URL scheme")

def testLongUrl(self):
g = github.Github(auth=self.login, base_url="http://my.enterprise.com/path/to/github")
g = github.Github(auth=self.oauth_token, base_url="http://my.enterprise.com/path/to/github")
repos = g.get_user().get_repos()
self.assertListKeyEqual(
repos,
Expand All @@ -122,7 +122,7 @@ def testLongUrl(self):
self.assertEqual(repos[0].owner.name, "Vincent Jacques")

def testSpecificPort(self):
g = github.Github(auth=self.login, base_url="http://my.enterprise.com:8080")
g = github.Github(auth=self.oauth_token, base_url="http://my.enterprise.com:8080")
self.assertListKeyEqual(
g.get_user().get_repos(),
lambda r: r.name,
Expand Down
2 changes: 1 addition & 1 deletion tests/Exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def testUnknownObject(self):
def testBadUserAgent(self):
self.assertRaises(
github.BadUserAgentException,
lambda: github.Github(auth=self.login, user_agent="").get_user().name,
lambda: github.Github(auth=self.oauth_token, user_agent="").get_user().name,
)

def testRateLimitExceeded(self):
Expand Down
55 changes: 16 additions & 39 deletions tests/Framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,7 @@ def __init__(self, *args, **kwds):

class BasicTestCase(unittest.TestCase):
recordMode = False
tokenAuthMode = False
jwtAuthMode = False
authMode = "token"
per_page = Consts.DEFAULT_PER_PAGE
retry = None
pool_size = None
Expand All @@ -365,11 +364,6 @@ def setUp(self):
)
import GithubCredentials # type: ignore

self.login = (
github.Auth.Login(GithubCredentials.login, GithubCredentials.password)
if GithubCredentials.login and GithubCredentials.password
else None
)
self.oauth_token = (
github.Auth.Token(GithubCredentials.oauth_token) if GithubCredentials.oauth_token else None
)
Expand All @@ -385,7 +379,6 @@ def setUp(self):
ReplayingHttpConnection,
ReplayingHttpsConnection,
)
self.login = github.Auth.Login("login", "password")
self.oauth_token = github.Auth.Token("oauth_token")
self.jwt = github.Auth.AppAuthToken("jwt")
self.app_auth = github.Auth.AppAuth(123456, APP_PRIVATE_KEY)
Expand Down Expand Up @@ -478,45 +471,29 @@ def setUp(self):
self.g = self.get_github(self.retry, self.pool_size)

def get_github(self, retry, pool_size):
if self.tokenAuthMode:
return github.Github(
auth=self.oauth_token,
per_page=self.per_page,
retry=retry,
pool_size=pool_size,
seconds_between_requests=self.seconds_between_requests,
seconds_between_writes=self.seconds_between_writes,
)
elif self.jwtAuthMode:
return github.Github(
auth=self.jwt,
per_page=self.per_page,
retry=retry,
pool_size=pool_size,
seconds_between_requests=self.seconds_between_requests,
seconds_between_writes=self.seconds_between_writes,
)
if self.authMode == "token":
auth = self.oauth_token
elif self.authMode == "jwt":
auth = self.jwt
else:
return github.Github(
auth=self.login,
per_page=self.per_page,
retry=retry,
pool_size=pool_size,
seconds_between_requests=self.seconds_between_requests,
seconds_between_writes=self.seconds_between_writes,
)
raise ValueError(f"Unsupported test auth mode: {self.authMode}")

return github.Github(
auth=auth,
per_page=self.per_page,
retry=retry,
pool_size=pool_size,
seconds_between_requests=self.seconds_between_requests,
seconds_between_writes=self.seconds_between_writes,
)


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


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


def activateJWTAuthMode(): # pragma no cover (Function useful only when recording new tests, not used during automated tests)
BasicTestCase.jwtAuthMode = True
BasicTestCase.authMode = "jwt"


def enableRetry(retry):
Expand Down
4 changes: 2 additions & 2 deletions tests/GithubIntegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

import github
from github import Consts
from github.Auth import AppInstallationAuth
from github.Auth import AppInstallationAuth, Login

from . import Framework

Expand Down Expand Up @@ -95,7 +95,7 @@ def testDeprecatedAppAuth(self):

def testRequiredAppAuth(self):
# GithubIntegration requires AppAuth authentication.
for auth in [self.oauth_token, self.jwt, self.login]:
for auth in [self.oauth_token, self.jwt, Login("login", "password")]:
with self.assertRaises(AssertionError) as r:
github.GithubIntegration(auth=auth)
self.assertEqual(
Expand Down
3 changes: 2 additions & 1 deletion tests/Issue134.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
################################################################################

import github
from github.Auth import Login

from . import Framework

Expand All @@ -48,7 +49,7 @@ def testGetAuthorizationsFailsWhenAutenticatedThroughOAuth(self):
self.assertEqual(raisedexp.exception.status, 404)

def testGetAuthorizationsSucceedsWhenAutenticatedThroughLoginPassword(self):
g = github.Github(auth=self.login)
g = github.Github(auth=Login("login", "password"))
self.assertListKeyEqual(
g.get_user().get_authorizations(),
lambda a: a.note,
Expand Down
4 changes: 2 additions & 2 deletions tests/Issue80.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

class Issue80(Framework.BasicTestCase): # https://github.com/jacquev6/PyGithub/issues/80
def testIgnoreHttpsFromGithubEnterprise(self):
g = github.Github(auth=self.login, base_url="http://my.enterprise.com/some/prefix") # http here
g = github.Github(auth=self.oauth_token, base_url="http://my.enterprise.com/some/prefix") # http here
org = g.get_organization("BeaverSoftware")
self.assertEqual(org.url, "https://my.enterprise.com/some/prefix/orgs/BeaverSoftware") # https returned
self.assertListKeyEqual(
Expand All @@ -47,7 +47,7 @@ def testIgnoreHttpsFromGithubEnterprise(self):

def testIgnoreHttpsFromGithubEnterpriseWithPort(self):
g = github.Github(
auth=self.login,
auth=self.oauth_token,
base_url="http://my.enterprise.com:1234/some/prefix",
) # http here
org = g.get_organization("BeaverSoftware")
Expand Down
3 changes: 2 additions & 1 deletion tests/Logging_.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
################################################################################

import github
from github.Auth import Login

from . import Framework
from .Authentication import CustomAuth
Expand Down Expand Up @@ -96,7 +97,7 @@ def assertLogging(self, verb, url, requestHeaders, responseHeaders, output):
self.assertEqual(self.logger.output, output)

def testLoggingWithBasicAuthentication(self):
self.assertEqual(github.Github(auth=self.login).get_user().name, "Vincent Jacques")
self.assertEqual(github.Github(auth=Login("login", "password")).get_user().name, "Vincent Jacques")
url = "https://api.github.com/user"
requestHeaders = {
"Authorization": "Basic (login and password removed)",
Expand Down
3 changes: 1 addition & 2 deletions tests/NamedUser.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,7 @@ class OrganizationInvitation(Framework.TestCase):
def setUp(self):
super().setUp()
# TODO: create an instance of type OrganizationInvitation and assign to self.attr, then run:
# pytest tests/OrganizationInvitation.py -k testAttributes --record --auth_with_token
# sed -i -e "s/token private_token_removed/Basic login_and_password_removed/" tests/ReplayData/OrganizationInvitation.setUp.txt
# pytest tests/OrganizationInvitation.py -k testAttributes --record
# ./scripts/update-assertions.sh tests/OrganizationInvitation.py testAttributes
self.org = self.g.get_organization("TestOrganization2072")
self.invitations = list(self.org.invitations())
Expand Down
2 changes: 1 addition & 1 deletion tests/PoolSize.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def testReturnsRepoAfterSettingPoolSize(self):

def testReturnsRepoAfterSettingPoolSizeHttp(self):
g = github.Github(
auth=self.login,
auth=self.oauth_token,
base_url="http://my.enterprise.com",
pool_size=20,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/ReplayData/Artifact.setUp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GET
api.github.com
None
/repos/transmission-web-control/transmission-web-control
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
{'Authorization': 'token private_token_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('Server', 'GitHub.com'), ('Date', 'Thu, 01 Jun 2023 09:00:25 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'private, max-age=60, s-maxage=60'), ('Vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"60d4519044dda2b5c9593f4aa02b569a78ec686f39aa039dd00455707d723d9f"'), ('Last-Modified', 'Thu, 01 Jun 2023 07:39:48 GMT'), ('X-OAuth-Scopes', 'delete:packages, gist, read:discussion, read:org, repo, workflow, write:packages'), ('X-Accepted-OAuth-Scopes', 'repo'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('x-github-api-version-selected', '2022-11-28'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4680'), ('X-RateLimit-Reset', '1685613199'), ('X-RateLimit-Used', '320'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, 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', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'EB88:1556:436041:46EAA0:64785E28')]
Expand Down
8 changes: 4 additions & 4 deletions tests/ReplayData/Artifact.testDelete.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ GET
api.github.com
None
/repos/lexa/PyGithub
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
{'Authorization': 'token private_token_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('Server', 'GitHub.com'), ('Date', 'Thu, 13 Oct 2022 11:21:25 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'private, max-age=60, s-maxage=60'), ('Vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"fa17a5276e7acf7935e265c35562e198168eba7e364cec522ed3c943db34b9ab"'), ('Last-Modified', 'Tue, 27 Sep 2022 16:35:24 GMT'), ('X-OAuth-Scopes', 'admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages'), ('X-Accepted-OAuth-Scopes', 'repo'), ('github-authentication-token-expiration', '2022-12-12 11:42:35 UTC'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4986'), ('X-RateLimit-Reset', '1665662666'), ('X-RateLimit-Used', '14'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, 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', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'D144:88EA:13115A0:1360F01:6347F4B5')]
Expand All @@ -14,7 +14,7 @@ GET
api.github.com
None
/repos/lexa/PyGithub/actions/artifacts/396724439
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
{'Authorization': 'token private_token_removed', 'User-Agent': 'PyGithub/Python'}
None
200
[('Server', 'GitHub.com'), ('Date', 'Thu, 13 Oct 2022 11:21:25 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('Cache-Control', 'private, max-age=60, s-maxage=60'), ('Vary', 'Accept, Authorization, Cookie, X-GitHub-OTP, Accept-Encoding, Accept, X-Requested-With'), ('ETag', 'W/"eb922edbfb3be37adfad91d445b790d560458056f413c992245fab18f82ff90f"'), ('X-OAuth-Scopes', 'admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages'), ('X-Accepted-OAuth-Scopes', ''), ('github-authentication-token-expiration', '2022-12-12 11:42:35 UTC'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4985'), ('X-RateLimit-Reset', '1665662666'), ('X-RateLimit-Used', '15'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, 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', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'D14E:9294:41910F2:427C643:6347F4B5')]
Expand All @@ -25,7 +25,7 @@ DELETE
api.github.com
None
/repos/lexa/PyGithub/actions/artifacts/396724439
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
{'Authorization': 'token private_token_removed', 'User-Agent': 'PyGithub/Python'}
None
204
[('Server', 'GitHub.com'), ('Date', 'Thu, 13 Oct 2022 11:21:26 GMT'), ('X-OAuth-Scopes', 'admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages'), ('X-Accepted-OAuth-Scopes', ''), ('github-authentication-token-expiration', '2022-12-12 11:42:35 UTC'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4984'), ('X-RateLimit-Reset', '1665662666'), ('X-RateLimit-Used', '16'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, 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', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Vary', 'Accept-Encoding, Accept, X-Requested-With'), ('X-GitHub-Request-Id', 'D15A:2275:3BBD835:3CA61EF:6347F4B5')]
Expand All @@ -36,7 +36,7 @@ GET
api.github.com
None
/repos/lexa/PyGithub/actions/artifacts/396724439
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
{'Authorization': 'token private_token_removed', 'User-Agent': 'PyGithub/Python'}
None
404
[('Server', 'GitHub.com'), ('Date', 'Thu, 13 Oct 2022 11:21:26 GMT'), ('Content-Type', 'application/json; charset=utf-8'), ('Transfer-Encoding', 'chunked'), ('X-OAuth-Scopes', 'admin:enterprise, admin:gpg_key, admin:org, admin:org_hook, admin:public_key, admin:repo_hook, admin:ssh_signing_key, delete:packages, delete_repo, gist, notifications, project, repo, user, workflow, write:discussion, write:packages'), ('X-Accepted-OAuth-Scopes', ''), ('github-authentication-token-expiration', '2022-12-12 11:42:35 UTC'), ('X-GitHub-Media-Type', 'github.v3; format=json'), ('X-RateLimit-Limit', '5000'), ('X-RateLimit-Remaining', '4983'), ('X-RateLimit-Reset', '1665662666'), ('X-RateLimit-Used', '17'), ('X-RateLimit-Resource', 'core'), ('Access-Control-Expose-Headers', 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, 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', '0'), ('Referrer-Policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('Content-Security-Policy', "default-src 'none'"), ('Vary', 'Accept-Encoding, Accept, X-Requested-With'), ('Content-Encoding', 'gzip'), ('X-GitHub-Request-Id', 'D168:1AFB:3CAF4A2:3D97D08:6347F4B6')]
Expand Down
Loading