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

Skip to content

Commit 3e670b4

Browse files
committed
update tests and add missing ones
1 parent ff72cce commit 3e670b4

File tree

9 files changed

+87
-69
lines changed

9 files changed

+87
-69
lines changed

auth0/v3/test/authentication/test_base.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,25 @@ def test_post(self, mock_post):
5050

5151
data = ab.post('the-url', data={'a': 'b'}, headers={'c': 'd'})
5252

53-
mock_post.assert_called_with(url='the-url', data='{"a": "b"}',
53+
mock_post.assert_called_with(url='the-url', json={'a': 'b'},
5454
headers={'c': 'd', 'Content-Type': 'application/json'})
5555

5656
self.assertEqual(data, {'x': 'y'})
57+
58+
@mock.patch('requests.post')
59+
def test_post_with_defaults(self, mock_post):
60+
ab = AuthenticationBase('auth0.com', telemetry=False)
61+
62+
mock_post.return_value.status_code = 200
63+
mock_post.return_value.text = '{"x": "y"}'
64+
65+
# Only required params are passed
66+
data = ab.post('the-url')
67+
68+
mock_post.assert_called_with(url='the-url', json=None,
69+
headers={'Content-Type': 'application/json'})
70+
71+
self.assertEqual(data, {'x': 'y'})
5772

5873
@mock.patch('requests.post')
5974
def test_post_includes_telemetry(self, mock_post):
@@ -67,7 +82,7 @@ def test_post_includes_telemetry(self, mock_post):
6782
self.assertEqual(mock_post.call_count, 1)
6883
call_kwargs = mock_post.call_args[1]
6984
self.assertEqual(call_kwargs['url'], 'the-url')
70-
self.assertEqual(call_kwargs['data'], '{"a": "b"}')
85+
self.assertEqual(call_kwargs['json'], {'a': 'b'})
7186
headers = call_kwargs['headers']
7287
self.assertEqual(headers['c'], 'd')
7388
self.assertEqual(headers['Content-Type'], 'application/json')
@@ -157,6 +172,35 @@ def test_post_error_with_no_response_text(self, mock_post):
157172
'a0.sdk.internal.unknown')
158173
self.assertEqual(context.exception.message, '')
159174

175+
@mock.patch('requests.get')
176+
def test_get(self, mock_get):
177+
ab = AuthenticationBase('auth0.com', telemetry=False)
178+
179+
mock_get.return_value.status_code = 200
180+
mock_get.return_value.text = '{"x": "y"}'
181+
182+
data = ab.get('the-url', params={'a': 'b'}, headers={'c': 'd'})
183+
184+
mock_get.assert_called_with(url='the-url', params={'a': 'b'},
185+
headers={'c': 'd', 'Content-Type': 'application/json'})
186+
187+
self.assertEqual(data, {'x': 'y'})
188+
189+
@mock.patch('requests.get')
190+
def test_get_with_defaults(self, mock_get):
191+
ab = AuthenticationBase('auth0.com', telemetry=False)
192+
193+
mock_get.return_value.status_code = 200
194+
mock_get.return_value.text = '{"x": "y"}'
195+
196+
# Only required params are passed
197+
data = ab.get('the-url')
198+
199+
mock_get.assert_called_with(url='the-url', params=None,
200+
headers={'Content-Type': 'application/json'})
201+
202+
self.assertEqual(data, {'x': 'y'})
203+
160204
@mock.patch('requests.get')
161205
def test_get_includes_telemetry(self, mock_get):
162206
ab = AuthenticationBase('auth0.com')
@@ -176,4 +220,4 @@ def test_get_includes_telemetry(self, mock_get):
176220
self.assertIn('User-Agent', headers)
177221
self.assertIn('Auth0-Client', headers)
178222

179-
self.assertEqual(data, '{"x": "y"}')
223+
self.assertEqual(data, {"x": "y"})

auth0/v3/test/authentication/test_database.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ def test_login(self, mock_post):
3232
'grant_type': 'gt',
3333
'scope': 'openid profile',
3434
})
35-
self.assertEqual(kwargs['headers'], {
36-
'Content-Type': 'application/json'
37-
})
3835

3936
@mock.patch('auth0.v3.authentication.database.Database.post')
4037
def test_signup(self, mock_post):
@@ -58,9 +55,6 @@ def test_signup(self, mock_post):
5855
'username': None,
5956
'user_metadata': None
6057
})
61-
self.assertEqual(kwargs['headers'], {
62-
'Content-Type': 'application/json'
63-
})
6458

6559

6660
# Using also username and metadata
@@ -88,9 +82,6 @@ def test_signup(self, mock_post):
8882
'username': 'usr',
8983
'user_metadata': sample_meta
9084
})
91-
self.assertEqual(kwargs['headers'], {
92-
'Content-Type': 'application/json'
93-
})
9485

9586
@mock.patch('auth0.v3.authentication.database.Database.post')
9687
def test_change_password(self, mock_post):
@@ -112,6 +103,3 @@ def test_change_password(self, mock_post):
112103
'password': 'pswd',
113104
'connection': 'conn',
114105
})
115-
self.assertEqual(kwargs['headers'], {
116-
'Content-Type': 'application/json'
117-
})

auth0/v3/test/authentication/test_delegated.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ def test_get_token_id_token(self, mock_post):
2828
'scope': 'openid profile',
2929
'api_type': 'apt',
3030
})
31-
self.assertEqual(kwargs['headers'], {
32-
'Content-Type': 'application/json'
33-
})
3431

3532
@mock.patch('auth0.v3.authentication.delegated.Delegated.post')
3633
def test_get_token_refresh_token(self, mock_post):
@@ -54,9 +51,6 @@ def test_get_token_refresh_token(self, mock_post):
5451
'scope': 'openid',
5552
'api_type': 'apt',
5653
})
57-
self.assertEqual(kwargs['headers'], {
58-
'Content-Type': 'application/json'
59-
})
6054

6155
@mock.patch('auth0.v3.authentication.delegated.Delegated.post')
6256
def test_get_token_value_error(self, mock_post):

auth0/v3/test/authentication/test_get_token.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ def test_authorization_code(self, mock_post):
2626
'grant_type': 'gt',
2727
'redirect_uri': 'idt'
2828
})
29-
self.assertEqual(kwargs['headers'], {
30-
'Content-Type': 'application/json'
31-
})
3229

3330
@mock.patch('auth0.v3.authentication.get_token.GetToken.post')
3431
def test_authorization_code_pkce(self, mock_post):
@@ -51,9 +48,6 @@ def test_authorization_code_pkce(self, mock_post):
5148
'grant_type': 'gt',
5249
'redirect_uri': 'idt'
5350
})
54-
self.assertEqual(kwargs['headers'], {
55-
'Content-Type': 'application/json'
56-
})
5751

5852
@mock.patch('auth0.v3.authentication.get_token.GetToken.post')
5953
def test_client_credentials(self, mock_post):
@@ -74,9 +68,6 @@ def test_client_credentials(self, mock_post):
7468
'audience': 'aud',
7569
'grant_type': 'gt'
7670
})
77-
self.assertEqual(kwargs['headers'], {
78-
'Content-Type': 'application/json'
79-
})
8071

8172
@mock.patch('auth0.v3.authentication.get_token.GetToken.post')
8273
def test_login(self, mock_post):
@@ -105,9 +96,6 @@ def test_login(self, mock_post):
10596
'audience': 'aud',
10697
'grant_type': 'gt'
10798
})
108-
self.assertEqual(kwargs['headers'], {
109-
'Content-Type': 'application/json'
110-
})
11199

112100
@mock.patch('auth0.v3.authentication.get_token.GetToken.post')
113101
def test_refresh_token(self, mock_post):
@@ -126,7 +114,4 @@ def test_refresh_token(self, mock_post):
126114
'client_secret': 'clsec',
127115
'refresh_token': 'rt',
128116
'grant_type': 'gt'
129-
})
130-
self.assertEqual(kwargs['headers'], {
131-
'Content-Type': 'application/json'
132-
})
117+
})

auth0/v3/test/authentication/test_logout.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ def test_logout(self, mock_get):
1414
return_to='rto')
1515

1616
args, kwargs = mock_get.call_args
17-
1817
self.assertEqual(args[0], 'https://my.domain.com/v2/logout?client_id=cid&returnTo=rto')
19-
self.assertEqual(kwargs['headers'], {
20-
'Content-Type': 'application/json'
21-
})
2218

2319
@mock.patch('auth0.v3.authentication.logout.Logout.get')
2420
def test_federated_logout(self, mock_get):
@@ -30,8 +26,4 @@ def test_federated_logout(self, mock_get):
3026
federated=True)
3127

3228
args, kwargs = mock_get.call_args
33-
3429
self.assertEqual(args[0], 'https://my.domain.com/v2/logout?federated&client_id=cid&returnTo=rto')
35-
self.assertEqual(kwargs['headers'], {
36-
'Content-Type': 'application/json'
37-
})

auth0/v3/test/authentication/test_passwordless.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ def test_email(self, mock_post):
2525
'authParams': {'a': 'b'},
2626
'connection': 'email',
2727
})
28-
self.assertEqual(kwargs['headers'], {
29-
'Content-Type': 'application/json'
30-
})
3128

3229
@mock.patch('auth0.v3.authentication.passwordless.Passwordless.post')
3330
def test_sms(self, mock_post):
@@ -43,10 +40,7 @@ def test_sms(self, mock_post):
4340
'phone_number': '123456',
4441
'connection': 'sms',
4542
})
46-
self.assertEqual(kwargs['headers'], {
47-
'Content-Type': 'application/json'
48-
})
49-
43+
5044
@mock.patch('auth0.v3.authentication.passwordless.Passwordless.post')
5145
def test_sms_login(self, mock_post):
5246

@@ -65,9 +59,6 @@ def test_sms_login(self, mock_post):
6559
'password': 'abcd',
6660
'scope': 'openid',
6761
})
68-
self.assertEqual(kwargs['headers'], {
69-
'Content-Type': 'application/json'
70-
})
7162

7263
@mock.patch('auth0.v3.authentication.passwordless.Passwordless.post')
7364
def test_sms_login_with_scope(self, mock_post):
@@ -87,7 +78,4 @@ def test_sms_login_with_scope(self, mock_post):
8778
'username': '123456',
8879
'password': 'abcd',
8980
'scope': 'openid profile',
90-
})
91-
self.assertEqual(kwargs['headers'], {
92-
'Content-Type': 'application/json'
93-
})
81+
})

auth0/v3/test/authentication/test_social.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ def test_login(self, mock_post):
1919
'connection': 'conn',
2020
'scope': 'openid',
2121
})
22-
self.assertEqual(kwargs['headers'], {
23-
'Content-Type': 'application/json'
24-
})
2522

2623
@mock.patch('auth0.v3.authentication.social.Social.post')
2724
def test_login_with_scope(self, mock_post):
@@ -38,6 +35,3 @@ def test_login_with_scope(self, mock_post):
3835
'connection': 'conn',
3936
'scope': 'openid profile',
4037
})
41-
self.assertEqual(kwargs['headers'], {
42-
'Content-Type': 'application/json'
43-
})

auth0/v3/test/authentication/test_users.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ def test_tokeninfo(self, mock_post):
2626

2727
mock_post.assert_called_with(
2828
url='https://my.domain.com/tokeninfo',
29-
data={'id_token': 'jwtoken'},
30-
headers={'Content-Type': 'application/json'}
29+
data={'id_token': 'jwtoken'}
3130
)

auth0/v3/test/management/test_rest.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def test_post(self, mock_post):
6464

6565
mock_post.return_value.status_code = 200
6666
response = rc.post('the/url', data=data)
67-
mock_post.assert_called_with('the/url', data=json.dumps(data),
67+
mock_post.assert_called_with('the/url', json=data,
6868
headers=headers)
6969

7070
self.assertEqual(response, {'a': 'b'})
@@ -215,6 +215,40 @@ def test_file_post_content_type_is_none(self, mock_post):
215215

216216
mock_post.assert_called_once_with('the-url', data=data, files=files, headers=headers)
217217

218+
219+
@mock.patch('requests.put')
220+
def test_put(self, mock_put):
221+
rc = RestClient(jwt='a-token', telemetry=False)
222+
headers = {'Authorization': 'Bearer a-token',
223+
'Content-Type': 'application/json'}
224+
225+
mock_put.return_value.text = '["a", "b"]'
226+
mock_put.return_value.status_code = 200
227+
228+
data = {'some': 'data'}
229+
230+
response = rc.put(url='the-url', data=data)
231+
mock_put.assert_called_with('the-url', json=data,
232+
headers=headers)
233+
234+
self.assertEqual(response, ['a', 'b'])
235+
236+
@mock.patch('requests.put')
237+
def test_put_errors(self, mock_put):
238+
rc = RestClient(jwt='a-token', telemetry=False)
239+
240+
mock_put.return_value.text = '{"statusCode": 999,' \
241+
' "errorCode": "code",' \
242+
' "message": "message"}'
243+
mock_put.return_value.status_code = 999
244+
245+
with self.assertRaises(Auth0Error) as context:
246+
rc.put(url='the/url')
247+
248+
self.assertEqual(context.exception.status_code, 999)
249+
self.assertEqual(context.exception.error_code, 'code')
250+
self.assertEqual(context.exception.message, 'message')
251+
218252
@mock.patch('requests.patch')
219253
def test_patch(self, mock_patch):
220254
rc = RestClient(jwt='a-token', telemetry=False)
@@ -227,7 +261,7 @@ def test_patch(self, mock_patch):
227261
data = {'some': 'data'}
228262

229263
response = rc.patch(url='the-url', data=data)
230-
mock_patch.assert_called_with('the-url', data=json.dumps(data),
264+
mock_patch.assert_called_with('the-url', json=data,
231265
headers=headers)
232266

233267
self.assertEqual(response, ['a', 'b'])

0 commit comments

Comments
 (0)