Skip to content

Commit

Permalink
update tests and add missing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
lbalmaceda committed Jun 14, 2019
1 parent ff72cce commit 3e670b4
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 69 deletions.
50 changes: 47 additions & 3 deletions auth0/v3/test/authentication/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,25 @@ def test_post(self, mock_post):

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

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

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

@mock.patch('requests.post')
def test_post_with_defaults(self, mock_post):
ab = AuthenticationBase('auth0.com', telemetry=False)

mock_post.return_value.status_code = 200
mock_post.return_value.text = '{"x": "y"}'

# Only required params are passed
data = ab.post('the-url')

mock_post.assert_called_with(url='the-url', json=None,
headers={'Content-Type': 'application/json'})

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

@mock.patch('requests.post')
def test_post_includes_telemetry(self, mock_post):
Expand All @@ -67,7 +82,7 @@ def test_post_includes_telemetry(self, mock_post):
self.assertEqual(mock_post.call_count, 1)
call_kwargs = mock_post.call_args[1]
self.assertEqual(call_kwargs['url'], 'the-url')
self.assertEqual(call_kwargs['data'], '{"a": "b"}')
self.assertEqual(call_kwargs['json'], {'a': 'b'})
headers = call_kwargs['headers']
self.assertEqual(headers['c'], 'd')
self.assertEqual(headers['Content-Type'], 'application/json')
Expand Down Expand Up @@ -157,6 +172,35 @@ def test_post_error_with_no_response_text(self, mock_post):
'a0.sdk.internal.unknown')
self.assertEqual(context.exception.message, '')

@mock.patch('requests.get')
def test_get(self, mock_get):
ab = AuthenticationBase('auth0.com', telemetry=False)

mock_get.return_value.status_code = 200
mock_get.return_value.text = '{"x": "y"}'

data = ab.get('the-url', params={'a': 'b'}, headers={'c': 'd'})

mock_get.assert_called_with(url='the-url', params={'a': 'b'},
headers={'c': 'd', 'Content-Type': 'application/json'})

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

@mock.patch('requests.get')
def test_get_with_defaults(self, mock_get):
ab = AuthenticationBase('auth0.com', telemetry=False)

mock_get.return_value.status_code = 200
mock_get.return_value.text = '{"x": "y"}'

# Only required params are passed
data = ab.get('the-url')

mock_get.assert_called_with(url='the-url', params=None,
headers={'Content-Type': 'application/json'})

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

@mock.patch('requests.get')
def test_get_includes_telemetry(self, mock_get):
ab = AuthenticationBase('auth0.com')
Expand All @@ -176,4 +220,4 @@ def test_get_includes_telemetry(self, mock_get):
self.assertIn('User-Agent', headers)
self.assertIn('Auth0-Client', headers)

self.assertEqual(data, '{"x": "y"}')
self.assertEqual(data, {"x": "y"})
12 changes: 0 additions & 12 deletions auth0/v3/test/authentication/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def test_login(self, mock_post):
'grant_type': 'gt',
'scope': 'openid profile',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.database.Database.post')
def test_signup(self, mock_post):
Expand All @@ -58,9 +55,6 @@ def test_signup(self, mock_post):
'username': None,
'user_metadata': None
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})


# Using also username and metadata
Expand Down Expand Up @@ -88,9 +82,6 @@ def test_signup(self, mock_post):
'username': 'usr',
'user_metadata': sample_meta
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.database.Database.post')
def test_change_password(self, mock_post):
Expand All @@ -112,6 +103,3 @@ def test_change_password(self, mock_post):
'password': 'pswd',
'connection': 'conn',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})
6 changes: 0 additions & 6 deletions auth0/v3/test/authentication/test_delegated.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ def test_get_token_id_token(self, mock_post):
'scope': 'openid profile',
'api_type': 'apt',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.delegated.Delegated.post')
def test_get_token_refresh_token(self, mock_post):
Expand All @@ -54,9 +51,6 @@ def test_get_token_refresh_token(self, mock_post):
'scope': 'openid',
'api_type': 'apt',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.delegated.Delegated.post')
def test_get_token_value_error(self, mock_post):
Expand Down
17 changes: 1 addition & 16 deletions auth0/v3/test/authentication/test_get_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ def test_authorization_code(self, mock_post):
'grant_type': 'gt',
'redirect_uri': 'idt'
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.get_token.GetToken.post')
def test_authorization_code_pkce(self, mock_post):
Expand All @@ -51,9 +48,6 @@ def test_authorization_code_pkce(self, mock_post):
'grant_type': 'gt',
'redirect_uri': 'idt'
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.get_token.GetToken.post')
def test_client_credentials(self, mock_post):
Expand All @@ -74,9 +68,6 @@ def test_client_credentials(self, mock_post):
'audience': 'aud',
'grant_type': 'gt'
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.get_token.GetToken.post')
def test_login(self, mock_post):
Expand Down Expand Up @@ -105,9 +96,6 @@ def test_login(self, mock_post):
'audience': 'aud',
'grant_type': 'gt'
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.get_token.GetToken.post')
def test_refresh_token(self, mock_post):
Expand All @@ -126,7 +114,4 @@ def test_refresh_token(self, mock_post):
'client_secret': 'clsec',
'refresh_token': 'rt',
'grant_type': 'gt'
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})
})
8 changes: 0 additions & 8 deletions auth0/v3/test/authentication/test_logout.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ def test_logout(self, mock_get):
return_to='rto')

args, kwargs = mock_get.call_args

self.assertEqual(args[0], 'https://my.domain.com/v2/logout?client_id=cid&returnTo=rto')
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

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

args, kwargs = mock_get.call_args

self.assertEqual(args[0], 'https://my.domain.com/v2/logout?federated&client_id=cid&returnTo=rto')
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})
16 changes: 2 additions & 14 deletions auth0/v3/test/authentication/test_passwordless.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ def test_email(self, mock_post):
'authParams': {'a': 'b'},
'connection': 'email',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.passwordless.Passwordless.post')
def test_sms(self, mock_post):
Expand All @@ -43,10 +40,7 @@ def test_sms(self, mock_post):
'phone_number': '123456',
'connection': 'sms',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})


@mock.patch('auth0.v3.authentication.passwordless.Passwordless.post')
def test_sms_login(self, mock_post):

Expand All @@ -65,9 +59,6 @@ def test_sms_login(self, mock_post):
'password': 'abcd',
'scope': 'openid',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.passwordless.Passwordless.post')
def test_sms_login_with_scope(self, mock_post):
Expand All @@ -87,7 +78,4 @@ def test_sms_login_with_scope(self, mock_post):
'username': '123456',
'password': 'abcd',
'scope': 'openid profile',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})
})
6 changes: 0 additions & 6 deletions auth0/v3/test/authentication/test_social.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ def test_login(self, mock_post):
'connection': 'conn',
'scope': 'openid',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})

@mock.patch('auth0.v3.authentication.social.Social.post')
def test_login_with_scope(self, mock_post):
Expand All @@ -38,6 +35,3 @@ def test_login_with_scope(self, mock_post):
'connection': 'conn',
'scope': 'openid profile',
})
self.assertEqual(kwargs['headers'], {
'Content-Type': 'application/json'
})
3 changes: 1 addition & 2 deletions auth0/v3/test/authentication/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,5 @@ def test_tokeninfo(self, mock_post):

mock_post.assert_called_with(
url='https://my.domain.com/tokeninfo',
data={'id_token': 'jwtoken'},
headers={'Content-Type': 'application/json'}
data={'id_token': 'jwtoken'}
)
38 changes: 36 additions & 2 deletions auth0/v3/test/management/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_post(self, mock_post):

mock_post.return_value.status_code = 200
response = rc.post('the/url', data=data)
mock_post.assert_called_with('the/url', data=json.dumps(data),
mock_post.assert_called_with('the/url', json=data,
headers=headers)

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

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


@mock.patch('requests.put')
def test_put(self, mock_put):
rc = RestClient(jwt='a-token', telemetry=False)
headers = {'Authorization': 'Bearer a-token',
'Content-Type': 'application/json'}

mock_put.return_value.text = '["a", "b"]'
mock_put.return_value.status_code = 200

data = {'some': 'data'}

response = rc.put(url='the-url', data=data)
mock_put.assert_called_with('the-url', json=data,
headers=headers)

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

@mock.patch('requests.put')
def test_put_errors(self, mock_put):
rc = RestClient(jwt='a-token', telemetry=False)

mock_put.return_value.text = '{"statusCode": 999,' \
' "errorCode": "code",' \
' "message": "message"}'
mock_put.return_value.status_code = 999

with self.assertRaises(Auth0Error) as context:
rc.put(url='the/url')

self.assertEqual(context.exception.status_code, 999)
self.assertEqual(context.exception.error_code, 'code')
self.assertEqual(context.exception.message, 'message')

@mock.patch('requests.patch')
def test_patch(self, mock_patch):
rc = RestClient(jwt='a-token', telemetry=False)
Expand All @@ -227,7 +261,7 @@ def test_patch(self, mock_patch):
data = {'some': 'data'}

response = rc.patch(url='the-url', data=data)
mock_patch.assert_called_with('the-url', data=json.dumps(data),
mock_patch.assert_called_with('the-url', json=data,
headers=headers)

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

0 comments on commit 3e670b4

Please sign in to comment.