7
7
import requests
8
8
import unittest
9
9
from ...authentication .base import AuthenticationBase
10
- from ...exceptions import Auth0Error
10
+ from ...exceptions import Auth0Error , RateLimitError
11
11
12
12
13
13
class TestBase (unittest .TestCase ):
@@ -27,7 +27,7 @@ def test_telemetry_enabled_by_default(self):
27
27
sys .version_info .micro )
28
28
29
29
client_info = {
30
- 'name' : 'auth0-python' ,
30
+ 'name' : 'auth0-python' ,
31
31
'version' : auth0_version ,
32
32
'env' : {
33
33
'python' : python_version
@@ -53,10 +53,10 @@ def test_post(self, mock_post):
53
53
data = ab .post ('the-url' , data = {'a' : 'b' }, headers = {'c' : 'd' })
54
54
55
55
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 ))
57
57
58
58
self .assertEqual (data , {'x' : 'y' })
59
-
59
+
60
60
@mock .patch ('requests.post' )
61
61
def test_post_with_defaults (self , mock_post ):
62
62
ab = AuthenticationBase ('auth0.com' , telemetry = False )
@@ -68,7 +68,7 @@ def test_post_with_defaults(self, mock_post):
68
68
data = ab .post ('the-url' )
69
69
70
70
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 )
72
72
73
73
self .assertEqual (data , {'x' : 'y' })
74
74
@@ -109,6 +109,29 @@ def test_post_error(self, mock_post):
109
109
self .assertEqual (context .exception .error_code , 'e0' )
110
110
self .assertEqual (context .exception .message , 'desc' )
111
111
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
+
112
135
@mock .patch ('requests.post' )
113
136
def test_post_error_with_code_property (self , mock_post ):
114
137
ab = AuthenticationBase ('auth0.com' , telemetry = False )
@@ -184,7 +207,7 @@ def test_get(self, mock_get):
184
207
data = ab .get ('the-url' , params = {'a' : 'b' }, headers = {'c' : 'd' })
185
208
186
209
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 ))
188
211
189
212
self .assertEqual (data , {'x' : 'y' })
190
213
@@ -199,7 +222,7 @@ def test_get_with_defaults(self, mock_get):
199
222
data = ab .get ('the-url' )
200
223
201
224
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 )
203
226
204
227
self .assertEqual (data , {'x' : 'y' })
205
228
0 commit comments