Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add more unit tests #164

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions auth0/v3/test/authentication/test_base.py
Original file line number Diff line number Diff line change
@@ -100,3 +100,17 @@ def test_post_error_with_no_response_text(self, mock_post):
self.assertEqual(context.exception.error_code,
'a0.sdk.internal.unknown')
self.assertEqual(context.exception.message, '')

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

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

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

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

self.assertEqual(result, '{"x": "y"}')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this also assert the status code?

13 changes: 13 additions & 0 deletions auth0/v3/test/authentication/test_delegated.py
Original file line number Diff line number Diff line change
@@ -58,6 +58,18 @@ def test_get_token_refresh_token(self, mock_post):
'Content-Type': 'application/json'
})

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

d = Delegated('my.domain.com')

with self.assertRaises(ValueError):
d.get_token(client_id='cid',
target='tgt',
api_type='apt',
grant_type='gt')
mock_post.assert_not_called()

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

@@ -70,3 +82,4 @@ def test_get_token_value_error(self, mock_post):
grant_type='gt',
refresh_token='rtk',
id_token='idt')
mock_post.assert_not_called()
82 changes: 82 additions & 0 deletions auth0/v3/test/management/test_auth0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import unittest
from ...management.auth0 import Auth0
from ...management.blacklists import Blacklists
from ...management.clients import Clients
from ...management.client_grants import ClientGrants
from ...management.connections import Connections
from ...management.device_credentials import DeviceCredentials
from ...management.emails import Emails
from ...management.email_templates import EmailTemplates
from ...management.guardian import Guardian
from ...management.jobs import Jobs
from ...management.logs import Logs
from ...management.resource_servers import ResourceServers
from ...management.rules import Rules
from ...management.stats import Stats
from ...management.tenants import Tenants
from ...management.tickets import Tickets
from ...management.user_blocks import UserBlocks
from ...management.users import Users
from ...management.users_by_email import UsersByEmail


class TestAuth0(unittest.TestCase):

def setUp(self):
self.domain = 'user.some.domain'
self.token = 'a-token'
self.a0 = Auth0(self.domain, self.token)

def test_blacklists(self):
self.assertIsInstance(self.a0.blacklists, Blacklists)

def test_clients(self):
self.assertIsInstance(self.a0.clients, Clients)

def test_client_grants(self):
self.assertIsInstance(self.a0.client_grants, ClientGrants)

def test_connections(self):
self.assertIsInstance(self.a0.connections, Connections)

def test_device_credentials(self):
self.assertIsInstance(self.a0.device_credentials, DeviceCredentials)

def test_emails(self):
self.assertIsInstance(self.a0.emails, Emails)

def test_email_templates(self):
self.assertIsInstance(self.a0.email_templates, EmailTemplates)

def test_guardian(self):
self.assertIsInstance(self.a0.guardian, Guardian)

def test_jobs(self):
self.assertIsInstance(self.a0.jobs, Jobs)

def test_logs(self):
self.assertIsInstance(self.a0.logs, Logs)

def test_resource_servers(self):
self.assertIsInstance(self.a0.resource_servers, ResourceServers)

def test_rules(self):
self.assertIsInstance(self.a0.rules, Rules)

def test_stats(self):
self.assertIsInstance(self.a0.stats, Stats)

def test_tenants(self):
self.assertIsInstance(self.a0.tenants, Tenants)

def test_tickets(self):
self.assertIsInstance(self.a0.tickets, Tickets)

def test_user_blocks(self):
self.assertIsInstance(self.a0.user_blocks, UserBlocks)

def test_users(self):
self.assertIsInstance(self.a0.users, Users)

def test_users_by_email(self):
self.assertIsInstance(self.a0.users_by_email, UsersByEmail)
4 changes: 2 additions & 2 deletions auth0/v3/test/management/test_jobs.py
Original file line number Diff line number Diff line change
@@ -17,11 +17,11 @@ def test_get(self, mock_rc):
)

@mock.patch('auth0.v3.management.jobs.RestClient')
def get_failed_job(self, mock_rc):
def test_get_failed_job(self, mock_rc):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

mock_instance = mock_rc.return_value

j = Jobs(domain='domain', token='jwttoken')
j.get('an-id')
j.get_failed_job('an-id')

mock_instance.get.assert_called_with(
'https://domain/api/v2/jobs/an-id/errors',
83 changes: 83 additions & 0 deletions auth0/v3/test/management/test_rest.py
Original file line number Diff line number Diff line change
@@ -198,6 +198,56 @@ def test_post_error_with_no_response_text(self, mock_post):
self.assertEqual(context.exception.error_code, 'a0.sdk.internal.unknown')
self.assertEqual(context.exception.message, '')

@mock.patch('requests.post')
def test_post_error_with_error_but_no_error_code(self, mock_post):
rc = RestClient(jwt='a-token', telemetry=False)

for error_status in [400, 500, None]:
mock_post.return_value.status_code = error_status
mock_post.return_value.text = '{"error": "SomeError"}'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our errors typically come back with both an error property and an error_description one. I don't know OTOH but I'm guessing the exception that's returned will include the description as an error message. Might be good to check for that as well.


with self.assertRaises(Auth0Error) as context:
rc.post('the-url')

self.assertEqual(context.exception.status_code, error_status)
self.assertEqual(context.exception.error_code, 'SomeError')

@mock.patch('requests.post')
def test_file_post(self, mock_post):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we doing this anywhere in the SDK?

rc = RestClient(jwt='a-token', telemetry=False)
headers = {'Authorization': 'Bearer a-token'}

mock_post.return_value.text = '{"a": "b"}'

data = {'some': 'data'}
files = {'file': ('some.file', 'Has some text in it\n')}
mock_post.return_value.status_code = 200
response = rc.file_post('the/url', data=data, files=files)
mock_post.assert_called_with('the/url', data=data,
files=files, headers=headers)

self.assertEqual(response, {'a': 'b'})

@mock.patch('requests.post')
def test_file_post_errors(self, mock_post):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we doing this anywhere in the SDK?

rc = RestClient(jwt='a-token', telemetry=False)

mock_post.return_value.text = json.dumps({
'statusCode': 999,
'errorCode': 'some_code',
'error': 'some_error'
})
mock_post.return_value.status_code = 999

data = {'some': 'data'}
files = {'file': ('some.file', 'Has some text in it\n')}
with self.assertRaises(Auth0Error) as context:
rc.file_post('the-url', data=data, files=files)

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

@mock.patch('requests.patch')
def test_patch(self, mock_patch):
rc = RestClient(jwt='a-token', telemetry=False)
@@ -231,6 +281,39 @@ def test_patch_errors(self, mock_patch):
self.assertEqual(context.exception.error_code, 'code')
self.assertEqual(context.exception.message, 'message')

@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', data=json.dumps(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.delete')
def test_delete(self, mock_delete):
rc = RestClient(jwt='a-token', telemetry=False)
14 changes: 14 additions & 0 deletions auth0/v3/test/test_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest
from ..exceptions import Auth0Error


class TestAuth0Error(unittest.TestCase):

def test_construct(self):
err = Auth0Error(123, 456, 'some message')
self.assertIsInstance(err, Exception)

def test_str(self):
err = Auth0Error(123, 456, 'some message')
string_repr = str(err)
self.assertIn('some message', string_repr)