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

Support 'verify' option #12

Merged
merged 9 commits into from
Nov 9, 2017
Merged

Support 'verify' option #12

merged 9 commits into from
Nov 9, 2017

Conversation

oliverlockwood
Copy link
Contributor

@oliverlockwood oliverlockwood commented Oct 17, 2017

Closes #11.

I have tested this manually as well with a local install and it does what I was looking for.

@kopf What do you think?

@coveralls
Copy link

coveralls commented Oct 17, 2017

Coverage Status

Coverage decreased (-2.4%) to 92.8% when pulling 8e9c9de on oliverlockwood:support-verify into 969f30c on kopf:master.

@oliverlockwood oliverlockwood force-pushed the support-verify branch 2 times, most recently from 3b972b3 to 8e9c9de Compare October 17, 2017 14:46
@kopf
Copy link
Owner

kopf commented Oct 17, 2017

Thanks for the contribution! I'll do the code review in-line

httsleep/main.py Outdated
@@ -34,6 +34,9 @@ class HttSleeper(object):
function that takes the response as an argument returning True.
:param auth: a (username, password) tuple for HTTP authentication.
:param headers: a dict of HTTP headers.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
Copy link
Owner

Choose a reason for hiding this comment

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

It should default to True, but at the moment it defaults to None. Can you fix this please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TL;DR: I copied this description from the requests library, which we delegate to; they default to None on the API, but then in the depths of the code they set verify to the actual default of True if not already set.

I'm inclined to stick with this description - and code setting! as-is, but happy to discuss further if you like?

References:

Copy link
Owner

Choose a reason for hiding this comment

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

I still feel it'd be more developer-friendly (especially for those using IDEs) if the kwarg matched the documentation. I don't see any downsides to setting the default value to True from the get-go.

Copy link
Owner

Choose a reason for hiding this comment

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

(Sorry, I added that comment weeks ago, but clicked the wrong button on github...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kopf it's your library 😃 I've changed this as requested in the most recent commit.

@@ -23,6 +23,17 @@ def test_run_success():


@httpretty.activate
def test_run_success_with_verify():
"""Should return response when a success criteria has been reached"""
Copy link
Owner

Choose a reason for hiding this comment

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

This test doesn't really do much - it would be better if it were """Should tell requests to skip SSL verification if verify==False""", testing whether session.send() was called with the verify kwarg set to False.

Checking the default behaviour would be great too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I'll have a play with doing this tomorrow. Thought I wouldn't spend too much time on it in the first pass just in case the library was unmaintained (which sometimes happens in OSS; it's nice to have a response so quickly after raising the PR - thanks!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've put something in for this. Feel free to point out ways to do it better; Python isn't my main language so I'm sure it could be nicer!

httsleep/main.py Outdated
@@ -34,6 +34,9 @@ class HttSleeper(object):
function that takes the response as an argument returning True.
:param auth: a (username, password) tuple for HTTP authentication.
:param headers: a dict of HTTP headers.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
Copy link
Owner

Choose a reason for hiding this comment

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

At the moment, the other kwargs (e.g. auth and headers and so on) aren't marked with "(optional)". It'd be best to stick to that style and I'll take care of them all at once.

(Unless you'd like to change them all :) I agree, marking it explicitly in the documentation is better.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How we handle this probably depends on the outcome of the other discussion thread, on the whole of this description being copied from the requests library. So I'll wait until you've had a chance to respond to that before I decide which way to go!

@kopf
Copy link
Owner

kopf commented Oct 17, 2017

One last thing - it would be great if you could make an entry in the changelog for this. You can simply put it under the heading "Next Version" at the very top.

Thanks again!

@coveralls
Copy link

coveralls commented Oct 18, 2017

Coverage Status

Coverage increased (+0.04%) to 95.2% when pulling 61ad4b6 on oliverlockwood:support-verify into 969f30c on kopf:master.

@coveralls
Copy link

coveralls commented Oct 18, 2017

Coverage Status

Coverage increased (+0.04%) to 95.2% when pulling 841c275 on oliverlockwood:support-verify into 969f30c on kopf:master.

@coveralls
Copy link

coveralls commented Oct 18, 2017

Coverage Status

Coverage increased (+0.04%) to 95.2% when pulling 0b3915f on oliverlockwood:support-verify into 969f30c on kopf:master.

httsleep.run()
assert mock_session_send.called
args, kwargs = mock_session_send.call_args
assert kwargs.get('verify') is None
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 prefer to keep as little within the mocked scope as possible. okResp and the asserts are a little unpythonic. Could you change these to:

def test_propagate_verify():
    """Should tell requests to skip SSL verification if verify==False"""
    resp = Response()
    resp.status_code = 200
    httsleep = HttSleeper(URL, {'status_code': 200}, verify=False)
    with mock.patch('requests.sessions.Session.send') as mock_session_send:
        mock_session_send.return_value = resp
        httsleep.run()
        assert mock_session_send.called
        args, kwargs = mock_session_send.call_args
    assert 'verify' in kwargs
    assert kwargs['verify'] == False


@httpretty.activate
def test_default_does_not_send_verify():
    """Should not send a value for 'verify' to requests by default"""
    resp = Response()
    resp.status_code = 200
    httsleep = HttSleeper(URL, {'status_code': 200})
    with mock.patch('requests.sessions.Session.send') as mock_session_send:
        mock_session_send.return_value = resp
        httsleep.run()
        assert mock_session_send.called
        args, kwargs = mock_session_send.call_args
    assert 'verify' not in kwargs

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes made. Thank you for the Pythonic education!

To satisfy the second test, I had to change the implementation to avoid it sending verify=None.

@coveralls
Copy link

coveralls commented Oct 26, 2017

Coverage Status

Coverage increased (+0.1%) to 95.276% when pulling 0dab89b on oliverlockwood:support-verify into 969f30c on kopf:master.

@coveralls
Copy link

coveralls commented Nov 8, 2017

Coverage Status

Coverage increased (+0.1%) to 95.276% when pulling d3a50da on oliverlockwood:support-verify into 969f30c on kopf:master.

@coveralls
Copy link

coveralls commented Nov 8, 2017

Coverage Status

Coverage increased (+0.1%) to 95.276% when pulling 0308a51 on oliverlockwood:support-verify into 969f30c on kopf:master.

httsleep/main.py Outdated
auth=None, headers=None,
def httsleep(url_or_request, until=None, alarms=None, status_code=None,
json=None, jsonpath=None, text=None, callback=None,
auth=None, headers=None, verify=None,
Copy link
Owner

Choose a reason for hiding this comment

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

verify=True

httsleep/main.py Outdated
@@ -84,6 +87,9 @@ def __init__(self, url_or_request, until=None, alarms=None,
'jsonpath': jsonpath, 'text': text, 'callback': callback}
until.append({k: v for k, v in condition.items() if v})

self.kwargs = {}
if verify is not None:
Copy link
Owner

Choose a reason for hiding this comment

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

This will now never happen.. I'd be fine with simply always passing verify to requests as a kwarg on L154 (no need for self.kwargs, which is a little ambiguous)

@kopf
Copy link
Owner

kopf commented Nov 8, 2017

Sorry that this is dragging on! I'm sure that was the last round of comments....

@coveralls
Copy link

coveralls commented Nov 8, 2017

Coverage Status

Coverage increased (+0.04%) to 95.2% when pulling 988ad5d on oliverlockwood:support-verify into 969f30c on kopf:master.

@oliverlockwood
Copy link
Contributor Author

@kopf no worries; I should have self-reviewed the last change better.

Hopefully with the commits I've just added, this is there now...!

@kopf
Copy link
Owner

kopf commented Nov 9, 2017

Nice one! Thanks again for the contribution!

@kopf kopf merged commit 1a5e76d into kopf:master Nov 9, 2017
@oliverlockwood
Copy link
Contributor Author

My pleasure, thanks for guiding me through it.

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.

None yet

3 participants