def test__handle_synchronize__when_handle_pr_sync_raises_exception__handle_labeled_sync_should_be_called_anyway(self):
hook_data = {'pull_request': {'number': 12, 'head': {'repo': {'full_name': 'repo'}}}}
pr_labels = ['label1', 'label2']
github_controller = GithubController(mock(), mock(), mock(), mock())
# expect
expect(github_controller, times=1).get_pr_labels(repository='repo', pr_number=12).thenReturn(pr_labels)
expect(github_controller, times=1).handle_pr_sync(hook_data, pr_labels).thenRaise(GithubException(404, 'PR not found'))
expect(github_controller, times=1).handle_labeled_sync(hook_data, pr_labels).thenReturn(async_value(None))
# when
with pytest.raises(GithubException):
await github_controller.handle_synchronize(hook_data)
python类GithubException()的实例源码
def test__handle_synchronize__when_handle_pr_and_labeled_sync_raise_exceptions__it_should_be_raised_up(self):
hook_data = {'pull_request': {'number': 12, 'head': {'repo': {'full_name': 'repo'}}}}
pr_labels = ['label1', 'label2']
github_controller = GithubController(mock(), mock(), mock(), mock())
# expect
expect(github_controller, times=1).get_pr_labels(repository='repo', pr_number=12).thenReturn(pr_labels)
expect(github_controller, times=1).handle_pr_sync(hook_data, pr_labels).thenRaise(GithubException(404, 'PR not found'))
expect(github_controller, times=1).handle_labeled_sync(hook_data, pr_labels).thenRaise(jenkins.JenkinsException())
# when
with pytest.raises(jenkins.JenkinsException):
await github_controller.handle_synchronize(hook_data)
def test__are_files_in_repo__should_return_false_on_any_missing_file(self):
github_client: github.Github = mock(spec=github.Github, strict=True)
github_repo: github.Repository.Repository = mock(spec=github.Repository.Repository, strict=True)
github_controller = GithubController(github_client, mock(), mock(), mock())
hook_details = mock({'repository': 'repo', 'sha': '123qwe', 'branch': 'master'}, spec=HookDetails, strict=True)
files = ['app/main.py', '.gitignore']
expect(github_client).get_repo('repo').thenReturn(github_repo)
expect(github_repo).get_file_contents(path='app/main.py', ref='123qwe').thenReturn(None)
expect(github_repo).get_file_contents(path='.gitignore', ref='123qwe').thenRaise(GithubException(404, 'File not found'))
assert not await github_controller.are_files_in_repo(files, hook_details)
def test__when_method_raises_github_exception__should_return_its_status__and_data_as_reason(self):
@handle_exceptions()
async def github_exception_raising_coro():
raise github.GithubException(404, {'message': 'Not found'})
response: aiohttp.web.Response = await github_exception_raising_coro()
assert response.status == 404
assert response.reason == "{'message': 'Not found'}"
def testAuthorizationHeaderWithLogin(self):
# See special case in Framework.fixAuthorizationHeader
g = github.Github("fake_login", "fake_password")
try:
g.get_user().name
except github.GithubException:
pass
def testAuthorizationHeaderWithToken(self):
# See special case in Framework.fixAuthorizationHeader
g = github.Github("ZmFrZV9sb2dpbjpmYWtlX3Bhc3N3b3Jk")
try:
g.get_user().name
except github.GithubException:
pass
def testNonJsonDataReturnedByGithub(self):
# Replay data was forged according to https://github.com/jacquev6/PyGithub/pull/182
raised = False
try:
self.g.get_user("jacquev6")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 503)
self.assertEqual(
exception.data,
{
"data": "<html><body><h1>503 Service Unavailable</h1>No server is available to handle this request.</body></html>",
}
)
self.assertTrue(raised)
def testUnknownObject(self):
raised = False
try:
self.g.get_user().get_repo("Xxx")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 404)
self.assertEqual(exception.data, {"message": "Not Found"})
if atLeastPython26 and atMostPython2:
self.assertEqual(str(exception), "404 {u'message': u'Not Found'}")
else:
self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)
def testBadAuthentication(self):
raised = False
try:
github.Github("BadUser", "BadPassword").get_user().login
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 401)
self.assertEqual(exception.data, {"message": "Bad credentials"})
if atLeastPython26 and atMostPython2:
self.assertEqual(str(exception), "401 {u'message': u'Bad credentials'}")
else:
self.assertEqual(str(exception), "401 {'message': 'Bad credentials'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)
def testExceptionPickling(self):
pickle.loads(pickle.dumps(github.GithubException('foo', 'bar')))
def testGetAuthorizationsFailsWhenAutenticatedThroughOAuth(self):
g = github.Github(self.oauth_token)
raised = False
try:
list(g.get_user().get_authorizations())
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 404)
self.assertTrue(raised)
def testRaiseErrorWithOutBranch(self):
raised = False
try:
self.repo.protect_branch("", True, "everyone", ["test"])
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 404)
self.assertEqual(
exception.data, {
u'documentation_url': u'https://developer.github.com/v3/repos/#get-branch',
u'message': u'Branch not found'
}
)
self.assertTrue(raised)
def testRaiseErrorWithBranchProtectionWithOutContext(self):
raised = False
try:
self.repo.protect_branch("master", True, "everyone")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 422)
self.assertEqual(
exception.data, {
u'documentation_url': u'https://developer.github.com/v3',
u'message': u'Invalid request.\n\n"contexts" wasn\'t supplied.'
}
)
self.assertTrue(raised)
def testMergeWithConflict(self):
raised = False
try:
commit = self.repo.merge("branchForBase", "branchForHead")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 409)
self.assertEqual(exception.data, {"message": "Merge conflict"})
self.assertTrue(raised)
def testBadSubscribePubSubHubbub(self):
raised = False
try:
self.repo.subscribe_to_hub("non-existing-event", "http://requestb.in/1bc1sc61")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 422)
self.assertEqual(exception.data, {"message": "Invalid event: \"non-existing-event\""})
self.assertTrue(raised)
def testAuthorizationHeaderWithLogin(self):
# See special case in Framework.fixAuthorizationHeader
g = github.Github("fake_login", "fake_password")
try:
g.get_user().name
except github.GithubException:
pass
def testAuthorizationHeaderWithToken(self):
# See special case in Framework.fixAuthorizationHeader
g = github.Github("ZmFrZV9sb2dpbjpmYWtlX3Bhc3N3b3Jk")
try:
g.get_user().name
except github.GithubException:
pass
def testNonJsonDataReturnedByGithub(self):
# Replay data was forged according to https://github.com/jacquev6/PyGithub/pull/182
raised = False
try:
self.g.get_user("jacquev6")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 503)
self.assertEqual(
exception.data,
{
"data": "<html><body><h1>503 Service Unavailable</h1>No server is available to handle this request.</body></html>",
}
)
self.assertTrue(raised)
def testUnknownObject(self):
raised = False
try:
self.g.get_user().get_repo("Xxx")
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 404)
self.assertEqual(exception.data, {"message": "Not Found"})
if atLeastPython26 and atMostPython2:
self.assertEqual(str(exception), "404 {u'message': u'Not Found'}")
else:
self.assertEqual(str(exception), "404 {'message': 'Not Found'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)
def testBadAuthentication(self):
raised = False
try:
github.Github("BadUser", "BadPassword").get_user().login
except github.GithubException, exception:
raised = True
self.assertEqual(exception.status, 401)
self.assertEqual(exception.data, {"message": "Bad credentials"})
if atLeastPython26 and atMostPython2:
self.assertEqual(str(exception), "401 {u'message': u'Bad credentials'}")
else:
self.assertEqual(str(exception), "401 {'message': 'Bad credentials'}") # pragma no cover (Covered with Python 3)
self.assertTrue(raised)