-
Notifications
You must be signed in to change notification settings - Fork 687
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
Added base OS info to metadata endpoint #4097
Conversation
@redshiftzero Please review this and suggest changes if any |
securedrop/tests/test_source.py
Outdated
@@ -522,6 +522,7 @@ def test_metadata_route(source_app): | |||
assert resp.headers.get('Content-Type') == 'application/json' | |||
assert json.loads(resp.data.decode('utf-8')).get('sd_version') \ | |||
== version.__version__ | |||
assert resp.get('server_os') == 16.04 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we want these tests to pass on both trusty/14.04 and xenial/16.04 (we run tests in both environments), can you mock out platform.linux_distribution
? Check out this commit for a example of how to do that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, will try my best
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@redshiftzero Why is it failing ? Is it the because of mock .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@redshiftzero Why is it failing ? Is it the because of mock .
Did you test the test case locally by running all of the tests?
Check https://circleci.com/gh/freedomofpress/securedrop/21953 and see the error message in red
. If you still have questions, ask in gitter. Being able to read error messages will help in long run. Feel free to ping me on IRC if you want.
@kushaldas Please have a look at this. I did some changes and then tested locally again on my system. |
Yes, you are running in a |
hey @nightwarrior-xxx, just to elaborate a bit more on what @kushaldas and I were saying above: we don't want to actually call This is a good place for a mock object (well, it's a stub): which is just a dummy object for test purposes. For example in our case, we want to replace Check out the following diff: diff --git a/securedrop/tests/test_source.py b/securedrop/tests/test_source.py
index 5ce6a661..c4d53989 100644
--- a/securedrop/tests/test_source.py
+++ b/securedrop/tests/test_source.py
@@ -515,14 +515,17 @@ def test_why_journalist_key(source_app):
assert "Why download the journalist's public key?" in text
-def test_metadata_route(source_app):
+def test_metadata_route(source_app, mocker):
with source_app.test_client() as app:
+ mocked_platform = mocker.patch('platform.linux_distribution')
+ mocked_platform.return_value = ('Ubuntu', '16.04', 'xenial')
+
resp = app.get(url_for('api.metadata'))
assert resp.status_code == 200
assert resp.headers.get('Content-Type') == 'application/json'
- assert json.loads(resp.data.decode('utf-8')).get('sd_version') \
- == version.__version__
- assert resp.get('server_os') == 16.04
+ data = json.loads(resp.data.decode('utf-8'))
+ assert data.get('sd_version') == version.__version__
+ assert data.get('server_os') == '16.04'
Here we are patching PS: btw this is one of my favorite talks on testing in Python, there are some really clear examples of various situations where one would want to use mocks towards the middle/end of the talk. |
@redshiftzero Still test failed . Here are the logs. |
This line mocked_platform = mocker.patch('platform.linux_distribution') Needs to be this mocked_platform = mocker.patch('source_app.api.platform.linux_distribution') (I think) |
But regardless, what's happening is the call to |
That is because you took half of @redshiftzero's original patch. Here is one more example following the other examples in the same file:
|
(the patch I posted above I did test and does work if applied in total, one can directly patch |
@redshiftzero Please review !! |
hi @nightwarrior-xxx, excellent - tests are passing now. The final thing is to resolve the linting errors:
You can run |
@redshiftzero Done but still lint fails
Are these causing problem ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks @nightwarrior-xxx!
(for posterity: the lint failures are fixed on |
securedrop/tests/test_source.py
Outdated
assert json.loads(resp.data.decode('utf-8')).get('sd_version') \ | ||
== version.__version__ | ||
assert resp.get('server_os') == 16.04 | ||
def test_metadata_route(source_app, mocker): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nightwarrior-xxx Why do we have this mocker
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's from pytest-mock
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh yeah this is doubly mocked...
dismissing because the mocking needs fixing
@redshiftzero @heartsucker Please review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is finally good.
This PR contains the code and corresponding test for OS information in the metadata endpoint.
Status
Ready for review
Description of Changes
Fixes #4059