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

Skip to content

Commit bae687a

Browse files
committed
add tests for rate limit
1 parent b5fd8bb commit bae687a

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

auth0/v3/test/authentication/test_base.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import requests
88
import unittest
99
from ...authentication.base import AuthenticationBase
10-
from ...exceptions import Auth0Error
10+
from ...exceptions import Auth0Error, RateLimitError
1111

1212

1313
class TestBase(unittest.TestCase):
@@ -27,7 +27,7 @@ def test_telemetry_enabled_by_default(self):
2727
sys.version_info.micro)
2828

2929
client_info = {
30-
'name': 'auth0-python',
30+
'name': 'auth0-python',
3131
'version': auth0_version,
3232
'env': {
3333
'python': python_version
@@ -53,10 +53,10 @@ def test_post(self, mock_post):
5353
data = ab.post('the-url', data={'a': 'b'}, headers={'c': 'd'})
5454

5555
mock_post.assert_called_with(url='the-url', json={'a': 'b'},
56-
headers={'c': 'd', 'Content-Type': 'application/json'}, timeout=(10, 2))
56+
headers={'c': 'd', 'Content-Type': 'application/json'}, timeout=(10, 2))
5757

5858
self.assertEqual(data, {'x': 'y'})
59-
59+
6060
@mock.patch('requests.post')
6161
def test_post_with_defaults(self, mock_post):
6262
ab = AuthenticationBase('auth0.com', telemetry=False)
@@ -68,7 +68,7 @@ def test_post_with_defaults(self, mock_post):
6868
data = ab.post('the-url')
6969

7070
mock_post.assert_called_with(url='the-url', json=None,
71-
headers={'Content-Type': 'application/json'}, timeout=5.0)
71+
headers={'Content-Type': 'application/json'}, timeout=5.0)
7272

7373
self.assertEqual(data, {'x': 'y'})
7474

@@ -109,6 +109,29 @@ def test_post_error(self, mock_post):
109109
self.assertEqual(context.exception.error_code, 'e0')
110110
self.assertEqual(context.exception.message, 'desc')
111111

112+
@mock.patch('requests.post')
113+
def test_post_rate_limit_error(self, mock_post):
114+
ab = AuthenticationBase('auth0.com', telemetry=False)
115+
116+
mock_post.return_value.text = '{"statusCode": 429,' \
117+
' "error": "e0",' \
118+
' "error_description": "desc"}'
119+
mock_post.return_value.status_code = 429
120+
mock_post.return_value.headers = {
121+
'x-ratelimit-limit': '3',
122+
'x-ratelimit-remaining': '6',
123+
'x-ratelimit-reset': '9',
124+
}
125+
126+
with self.assertRaises(Auth0Error) as context:
127+
ab.post('the-url', data={'a': 'b'}, headers={'c': 'd'})
128+
129+
self.assertEqual(context.exception.status_code, 429)
130+
self.assertEqual(context.exception.error_code, 'e0')
131+
self.assertEqual(context.exception.message, 'desc')
132+
self.assertIsInstance(context.exception, RateLimitError)
133+
self.assertEqual(context.exception.reset_at, 9)
134+
112135
@mock.patch('requests.post')
113136
def test_post_error_with_code_property(self, mock_post):
114137
ab = AuthenticationBase('auth0.com', telemetry=False)
@@ -184,7 +207,7 @@ def test_get(self, mock_get):
184207
data = ab.get('the-url', params={'a': 'b'}, headers={'c': 'd'})
185208

186209
mock_get.assert_called_with(url='the-url', params={'a': 'b'},
187-
headers={'c': 'd', 'Content-Type': 'application/json'}, timeout=(10, 2))
210+
headers={'c': 'd', 'Content-Type': 'application/json'}, timeout=(10, 2))
188211

189212
self.assertEqual(data, {'x': 'y'})
190213

@@ -199,7 +222,7 @@ def test_get_with_defaults(self, mock_get):
199222
data = ab.get('the-url')
200223

201224
mock_get.assert_called_with(url='the-url', params=None,
202-
headers={'Content-Type': 'application/json'}, timeout=5.0)
225+
headers={'Content-Type': 'application/json'}, timeout=5.0)
203226

204227
self.assertEqual(data, {'x': 'y'})
205228

auth0/v3/test/management/test_rest.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import requests
88

99
from ...management.rest import RestClient
10-
from ...exceptions import Auth0Error
10+
from ...exceptions import Auth0Error, RateLimitError
1111

1212

1313
class TestRest(unittest.TestCase):
@@ -149,6 +149,29 @@ def test_get_errors(self, mock_get):
149149
self.assertEqual(context.exception.error_code, 'code')
150150
self.assertEqual(context.exception.message, 'message')
151151

152+
@mock.patch('requests.get')
153+
def test_get_rate_limit_error(self, mock_get):
154+
rc = RestClient(jwt='a-token', telemetry=False)
155+
156+
mock_get.return_value.text = '{"statusCode": 429,' \
157+
' "errorCode": "code",' \
158+
' "message": "message"}'
159+
mock_get.return_value.status_code = 429
160+
mock_get.return_value.headers = {
161+
'x-ratelimit-limit': '3',
162+
'x-ratelimit-remaining': '6',
163+
'x-ratelimit-reset': '9',
164+
}
165+
166+
with self.assertRaises(Auth0Error) as context:
167+
rc.get('the/url')
168+
169+
self.assertEqual(context.exception.status_code, 429)
170+
self.assertEqual(context.exception.error_code, 'code')
171+
self.assertEqual(context.exception.message, 'message')
172+
self.assertIsInstance(context.exception, RateLimitError)
173+
self.assertEqual(context.exception.reset_at, 9)
174+
152175
@mock.patch('requests.post')
153176
def test_post(self, mock_post):
154177
rc = RestClient(jwt='a-token', telemetry=False)

0 commit comments

Comments
 (0)