Skip to content

Commit

Permalink
Test all exceptions created by Requester
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoMi committed Mar 21, 2023
1 parent 262c2b5 commit b7c109d
Showing 1 changed file with 75 additions and 9 deletions.
84 changes: 75 additions & 9 deletions tests/Requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,95 @@
# #
################################################################################

import urllib3 # type: ignore
from httpretty import httpretty # type: ignore

import github

from . import Framework


class Requester(Framework.TestCase):
def testShouldCreateBadCredentialsException(self):
exc = self.g._Github__requester.__createException(401, {"header": "value"}, {"message": "Bad credentials"})
exc = self.g._Github__requester.__createException(
401, {"header": "value"}, {"message": "Bad credentials"}
)
self.assertIsInstance(exc, github.BadCredentialsException)
self.assertEqual(exc.status, 401)
self.assertEqual(exc.data, {"message": "Bad credentials"})
self.assertEqual(exc.headers, {"header": "value"})
self.assertEqual(str(exc), '401 {"message": "Bad credentials"}')

def testShouldCreateTwoFactorException(self):
exc = self.g._Github__requester.__createException(401, {"x-github-otp": "required; app"}, {"message": "Must specify two-factor authentication OTP code.", "documentation_url": "https://developer.github.com/v3/auth#working-with-two-factor-authentication"})
exc = self.g._Github__requester.__createException(
401,
{"x-github-otp": "required; app"},
{
"message": "Must specify two-factor authentication OTP code.",
"documentation_url": "https://developer.github.com/v3/auth#working-with-two-factor-authentication",
},
)
self.assertIsInstance(exc, github.TwoFactorException)
self.assertEqual(exc.status, 401)
self.assertEqual(exc.data, {"message": "Must specify two-factor authentication OTP code.", "documentation_url": "https://developer.github.com/v3/auth#working-with-two-factor-authentication"})
self.assertEqual(
exc.data,
{
"message": "Must specify two-factor authentication OTP code.",
"documentation_url": "https://developer.github.com/v3/auth#working-with-two-factor-authentication",
},
)
self.assertEqual(exc.headers, {"x-github-otp": "required; app"})
self.assertEqual(str(exc), '401 {"message": "Must specify two-factor authentication OTP code.", "documentation_url": "https://developer.github.com/v3/auth#working-with-two-factor-authentication"}')
self.assertEqual(
str(exc),
'401 {"message": "Must specify two-factor authentication OTP code.", "documentation_url": "https://developer.github.com/v3/auth#working-with-two-factor-authentication"}',
)

def testShouldCreateBadUserAgentException(self):
exc = self.g._Github__requester.__createException(
403,
{"header": "value"},
{"message": "Missing or invalid User Agent string"},
)
self.assertIsInstance(exc, github.BadUserAgentException)
self.assertEqual(exc.status, 403)
self.assertEqual(exc.data, {"message": "Missing or invalid User Agent string"})
self.assertEqual(exc.headers, {"header": "value"})
self.assertEqual(
str(exc), '403 {"message": "Missing or invalid User Agent string"}'
)

def testShouldCreateRateLimitExceededException(self):
for message in [
"API Rate Limit Exceeded for 92.104.200.119",
"You have triggered an abuse detection mechanism. Please wait a few minutes before you try again.",
"You have exceeded a secondary rate limit. Please wait a few minutes before you try again.",
]:
with self.subTest(message=message):
exc = self.g._Github__requester.__createException(
403, {"header": "value"}, {"message": message}
)
self.assertIsInstance(exc, github.RateLimitExceededException)
self.assertEqual(exc.status, 403)
self.assertEqual(exc.data, {"message": message})
self.assertEqual(exc.headers, {"header": "value"})
self.assertEqual(str(exc), f'403 {{"message": "{message}"}}')

def testShouldCreateUnknownObjectException(self):
exc = self.g._Github__requester.__createException(
404, {"header": "value"}, {"message": "Not Found"}
)
self.assertIsInstance(exc, github.UnknownObjectException)
self.assertEqual(exc.status, 404)
self.assertEqual(exc.data, {"message": "Not Found"})
self.assertEqual(exc.headers, {"header": "value"})
self.assertEqual(str(exc), '404 {"message": "Not Found"}')

def testShouldCreateGithubException(self):
exc = self.g._Github__requester.__createException(
405, {"header": "value"}, {"message": "Something unknown"}
)
self.assertIsInstance(exc, github.GithubException)
self.assertEqual(exc.status, 405)
self.assertEqual(exc.data, {"message": "Something unknown"})
self.assertEqual(exc.headers, {"header": "value"})
self.assertEqual(str(exc), '405 {"message": "Something unknown"}')

def testShouldCreateExceptionWithoutMessage(self):
for status in range(400, 600):
Expand All @@ -52,7 +118,7 @@ def testShouldCreateExceptionWithoutMessage(self):
self.assertEqual(exc.status, status)
self.assertEqual(exc.data, {})
self.assertEqual(exc.headers, {})
self.assertEqual(str(exc), f'{status} {{}}')
self.assertEqual(str(exc), f"{status} {{}}")

def testShouldCreateExceptionWithoutOutput(self):
for status in range(400, 600):
Expand All @@ -62,4 +128,4 @@ def testShouldCreateExceptionWithoutOutput(self):
self.assertEqual(exc.status, status)
self.assertIsNone(exc.data)
self.assertEqual(exc.headers, {})
self.assertEqual(str(exc), f'{status} null')
self.assertEqual(str(exc), f"{status} null")

0 comments on commit b7c109d

Please sign in to comment.