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

Fix request creation when headers are the default #198

Merged
merged 5 commits into from
Jun 14, 2019
Merged

Conversation

lbalmaceda
Copy link
Contributor

Changes

There was an error affecting only the authentication API client and methods exposed on this SDK. When the call was not specifying custom headers (headers=None) a call to dictionary#update would fail.

This PR fixes that issue, as well as a response converting issue.

Testing

I manually run this directly calling the requests library. Also run the AuthenticationBase#post/get calls.

  • Added missing tests for the "default" scenario on those 2 methods.

  • Added missing tests for the management#put method.

  • This change adds unit test coverage

  • This change has been tested on the latest version of the platform/language or why not

Checklist

@lbalmaceda lbalmaceda requested a review from a team June 13, 2019 18:36
@@ -42,16 +42,15 @@ def __init__(self, domain, telemetry=True):

def post(self, url, data=None, headers=None):
request_headers = self.base_headers.copy()
request_headers.update(headers)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this was the bug. When headers was None this call would throw.

response = requests.get(url=url, params=params, headers=request_headers)
return response.text
return self._process_response(response)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some response formats where not taken into account. This was the only method not calling _process_response. Now it handles text responses (what was there before), JSON and empty body responses.

@@ -32,8 +32,7 @@ def login(self, client_id, username, password, connection, id_token=None,
'device': device,
'grant_type': grant_type,
'scope': scope,
},
headers={'Content-Type': 'application/json'}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

All these args were removed because they match the default behavior https://github.com/auth0/auth0-python/blob/master/auth0/v3/authentication/base.py#L24

@@ -46,7 +46,7 @@ def get(self, url, params=None):
def post(self, url, data=None):
headers = self.base_headers.copy()

response = requests.post(url, data=json.dumps(data or {}), headers=headers)
response = requests.post(url, json=data, headers=headers)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No longer need to explicitly convert python dictionaries to "JSON text"

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

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

Choose a reason for hiding this comment

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

no headers value is passed, so default of None will kick in. This asserts the bug is no longer present

@@ -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):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

missing tests for the put HTTP method

@lbalmaceda lbalmaceda added CH: Fixed small Small review labels Jun 13, 2019
@lbalmaceda lbalmaceda added this to the v3-Next milestone Jun 13, 2019
Copy link
Contributor

@stevehobbsdev stevehobbsdev left a comment

Choose a reason for hiding this comment

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

Looks good. Does this have anything to do with the issue we were discussing yesterday, regarding downloading the sample?

@lbalmaceda lbalmaceda merged commit 3e670b4 into master Jun 14, 2019
@lbalmaceda lbalmaceda deleted the fix-headers branch June 14, 2019 13:59
@lbalmaceda
Copy link
Contributor Author

@elkdanger I can't tell. You never told me what the actual issue was! But we can discuss it. This was affecting only the authentication client from this SDK

@lbalmaceda lbalmaceda modified the milestones: v3-Next, 3.8.1 Jun 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CH: Fixed small Small review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants