Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Always include Content-Type header in management requests #158

Merged
merged 3 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion auth0/v3/authentication/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, domain, telemetry=True):
}).encode('utf-8')

self.base_headers.update({
'User-Agent': 'Python/%s' % py_version,
'User-Agent': 'Python/{}'.format(py_version),
'Auth0-Client': base64.b64encode(auth0_client),
})

Expand Down
32 changes: 6 additions & 26 deletions auth0/v3/management/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class RestClient(object):
def __init__(self, jwt, telemetry=True):
self.jwt = jwt

self.base_headers = {
'Authorization': 'Bearer {}'.format(self.jwt),
'Content-Type': 'application/json',
}
if telemetry:
py_version = platform.python_version()
version = sys.modules['auth0'].__version__
Expand All @@ -36,68 +40,44 @@ def __init__(self, jwt, telemetry=True):
]
}).encode('utf-8')

self.base_headers = {
self.base_headers.update({
'User-Agent': 'Python/{}'.format(py_version),
'Auth0-Client': base64.b64encode(auth0_client),
'Content-Type': 'application/json'
}
else:
self.base_headers = {}
})

def get(self, url, params=None):
headers = self.base_headers.copy()
headers.update({
'Authorization': 'Bearer {}'.format(self.jwt),
})

response = requests.get(url, params=params, headers=headers)
return self._process_response(response)

def post(self, url, data=None):
headers = self.base_headers.copy()
headers.update({
'Authorization': 'Bearer {}'.format(self.jwt),
'Content-Type': 'application/json'
})

response = requests.post(url, data=json.dumps(data or {}), headers=headers)
return self._process_response(response)

def file_post(self, url, data=None, files=None):
headers = self.base_headers.copy()
headers.pop('Content-Type', None)
headers.update({
'Authorization': 'Bearer {}'.format(self.jwt),
})

response = requests.post(url, data=data, files=files, headers=headers)
return self._process_response(response)

def patch(self, url, data=None):
headers = self.base_headers.copy()
headers.update({
'Authorization': 'Bearer {}'.format(self.jwt),
'Content-Type': 'application/json'
})

response = requests.patch(url, data=json.dumps(data or {}), headers=headers)
return self._process_response(response)

def put(self, url, data=None):
headers = self.base_headers.copy()
headers.update({
'Authorization': 'Bearer {}'.format(self.jwt),
'Content-Type': 'application/json'
})

response = requests.put(url, data=json.dumps(data or {}), headers=headers)
return self._process_response(response)

def delete(self, url, params=None):
headers = self.base_headers.copy()
headers.update({
'Authorization': 'Bearer {}'.format(self.jwt),
})

response = requests.delete(url, headers=headers, params=params or {})
return self._process_response(response)
Expand Down
30 changes: 27 additions & 3 deletions auth0/v3/test/management/test_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class TestRest(unittest.TestCase):
@mock.patch('requests.get')
def test_get(self, mock_get):
rc = RestClient(jwt='a-token', telemetry=False)
headers = {'Authorization': 'Bearer a-token'}
headers = {
'Authorization': 'Bearer a-token',
'Content-Type': 'application/json',
}

mock_get.return_value.text = '["a", "b"]'
mock_get.return_value.status_code = 200
Expand Down Expand Up @@ -198,6 +201,20 @@ 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_file_post_content_type_is_none(self, mock_post):
rc = RestClient(jwt='a-token', telemetry=False)
headers = {'Authorization': 'Bearer a-token'}
mock_post.return_value.status_code = 200
mock_post.return_value.text = 'Success'

data = {'some': 'data'}
files = [mock.Mock()]

rc.file_post('the-url', data=data, files=files)

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

@mock.patch('requests.patch')
def test_patch(self, mock_patch):
rc = RestClient(jwt='a-token', telemetry=False)
Expand Down Expand Up @@ -234,7 +251,10 @@ def test_patch_errors(self, mock_patch):
@mock.patch('requests.delete')
def test_delete(self, mock_delete):
rc = RestClient(jwt='a-token', telemetry=False)
headers = {'Authorization': 'Bearer a-token'}
headers = {
'Authorization': 'Bearer a-token',
'Content-Type': 'application/json',
}

mock_delete.return_value.text = '["a", "b"]'
mock_delete.return_value.status_code = 200
Expand Down Expand Up @@ -262,8 +282,12 @@ def test_delete_errors(self, mock_delete):

def test_disabled_telemetry(self):
rc = RestClient(jwt='a-token', telemetry=False)
expected_headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer a-token',
}

self.assertEqual(rc.base_headers, {})
self.assertEqual(rc.base_headers, expected_headers)

def test_enabled_telemetry(self):
rc = RestClient(jwt='a-token', telemetry=True)
Expand Down