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 verification tool for later Py/Dj versions and add tests #41

Merged
merged 19 commits into from
Feb 21, 2018

Conversation

okayjeff
Copy link
Contributor

@okayjeff okayjeff commented Feb 11, 2018

Resolves #33 and #40 and adds unit tests for the management command

Copy link
Owner

@kumar303 kumar303 left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this up and adding a test! I have some minor change requests.

try:
res = request(url, method.lower(), data=qs, headers=headers)
except ImportError:
raise CommandError('To use this command you first need to '
Copy link
Owner

Choose a reason for hiding this comment

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

I'd like to see this try/except/raise block moved back to the exact import requests statement within request() because otherwise it could obscure other imports.

For testing purposes you could create a get_requests_module() that does the import so you can mock it out.

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 makes perfect sense. Done.


def handle(self, *args, **options):
hawk_log = logging.getLogger('mohawk')
hawk_log.setLevel(logging.DEBUG)
Copy link
Owner

Choose a reason for hiding this comment

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

It looks like you removed this. Was the logging too noisy? I had found it helpful in the past because it shows exactly what was used to create MACs, etc. If you still want to remove it, maybe there can be an option to re-enable it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. It actually has more to do with me not fully grasping how getLogger works. At the time, I thought we were creating a new logger and that it wasn't being used, but now it's clear to me that we're listening in on the logger of the underlying mohawk lib.

I added it back in; however, I thought it more conventional to add this logging config to the top of the module beneath the imports.

'Server-Authorization': 'xyz',
'Content-Type': 'text/plain'
}
self.text = 'Authorized'
Copy link
Owner

Choose a reason for hiding this comment

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

This looks like it might be easy enough with a real Response. If it's not easy then faking it is in fine but this is worth a try:

response = Response()
response.headers['Server-Authorization'] = 'xyz'
response.headers['Content-Type'] = 'text/plain'
response._content = 'Authorized'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simpler, more realistic, and works great. Thank you.

Copy link
Contributor Author

@okayjeff okayjeff left a comment

Choose a reason for hiding this comment

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

Thanks for the comments @kumar303. I addressed them all.


def handle(self, *args, **options):
hawk_log = logging.getLogger('mohawk')
hawk_log.setLevel(logging.DEBUG)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for pointing this out. It actually has more to do with me not fully grasping how getLogger works. At the time, I thought we were creating a new logger and that it wasn't being used, but now it's clear to me that we're listening in on the logger of the underlying mohawk lib.

I added it back in; however, I thought it more conventional to add this logging config to the top of the module beneath the imports.

try:
res = request(url, method.lower(), data=qs, headers=headers)
except ImportError:
raise CommandError('To use this command you first need to '
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 makes perfect sense. Done.

'Server-Authorization': 'xyz',
'Content-Type': 'text/plain'
}
self.text = 'Authorized'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Simpler, more realistic, and works great. Thank you.

Copy link
Owner

@kumar303 kumar303 left a comment

Choose a reason for hiding this comment

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

It's looking good. I have a few more requests.



hawk_log = logging.getLogger('mohawk')
hawk_log.setLevel(logging.DEBUG)
Copy link
Owner

Choose a reason for hiding this comment

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

This shouldn't execute at import time because debug logging might get turned on through regular usage of the hawrest middleware which is not what we want. It should only get turned on when the management command script runs so it should move back to the handle() function.

Maybe it would help to add a comment to this code. I'd suggest something like: This will configure the mohawk library for debug logging so you can see inputs to signature functions and other useful stuff.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I added it back.

@mock.patch('mohawk.Sender.accept_response')
def test_response_verified_with_auth_header(self, mk_accept, mk_resp):
response = Response()
response.headers = {
Copy link
Owner

Choose a reason for hiding this comment

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

It might not be a big deal but I'd feel better if you re-used the existing dict like this:

response.headers['Server-Authorization'] = 'xyz'
response.headers['Content-Type'] = 'text/plain'

Overwriting the dict makes me nervous because it's a customized case-insensitive mapping.

@okayjeff
Copy link
Contributor Author

Addressed your comments. Thanks for the feedback, @kumar303!

@kumar303 kumar303 self-requested a review February 21, 2018 16:27
Copy link
Owner

@kumar303 kumar303 left a comment

Choose a reason for hiding this comment

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

Thanks again for fixing up this command! Looks good

@kumar303 kumar303 merged commit dce22be into kumar303:master Feb 21, 2018
@kumar303 kumar303 mentioned this pull request Feb 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants