-
Notifications
You must be signed in to change notification settings - Fork 92
[SVCS-475] Send hook request to OSF for GET requests #307
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
base: develop
Are you sure you want to change the base?
Changes from 4 commits
4d68cfb
b9dd890
307b9e8
f722ad4
6ff7a99
da471b1
b072577
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| import time | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from tests.utils import MockCoroutine | ||
| from waterbutler.core.path import WaterButlerPath | ||
| from waterbutler.core.log_payload import LogPayload | ||
| from tests.providers.osfstorage.fixtures import ( | ||
| file_metadata_object, | ||
| file_path, | ||
| file_metadata, | ||
| file_lineage, | ||
| provider, | ||
| auth | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def log_payload(file_metadata_object, file_path, provider): | ||
| return LogPayload('guid0', provider, file_metadata_object, file_path) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def callback_log_payload_move(): | ||
| return { | ||
| 'auth': { | ||
| 'callback_url': 'fakecallback.com', | ||
| 'id': 'cat', | ||
| 'name': 'cat', | ||
| 'email': 'cat@cat.com' | ||
| }, | ||
| 'time': 70, | ||
| 'action': 'move', | ||
| 'source': { | ||
| 'materialized': WaterButlerPath('/doc.rst', prepend=None), | ||
| 'path': '/59a9b628b7d1c903ab5a8f52', | ||
| 'kind': 'file', | ||
| 'extra': { | ||
| 'checkout': None, | ||
| 'downloads': 0, | ||
| 'guid': None, | ||
| 'hashes': { | ||
| 'sha256': '043be9ff919762f0dc36fff0222cd90c753ce28b39feb52112be9360c476ef88', | ||
| 'md5': 'eb3f7cc15ba7b6effb2186284185c5cf' | ||
| }, | ||
| 'version': 1 | ||
| }, | ||
| 'nid': 'guid0', | ||
| 'etag': 'eccd2270585257f4b48d8493bed863c01cf0b6dc0bb590101407c9b5e10b8e08', | ||
| 'contentType': None, | ||
| 'created_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'provider': 'osfstorage', | ||
| 'modified': '2017-09-01T19:34:00.175741+00:00', | ||
| 'modified_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'name': 'doc.rst', | ||
| 'size': 5596, | ||
| 'resource': 'guid0' | ||
| }, | ||
| 'errors': [], | ||
| 'destination': { | ||
| 'materialized': WaterButlerPath('/doc.rst', prepend=None), | ||
| 'path': '/59a9b628b7d1c903ab5a8f52', | ||
| 'kind': 'file', | ||
| 'extra': { | ||
| 'checkout': None, | ||
| 'downloads': 0, | ||
| 'guid': None, | ||
| 'hashes': { | ||
| 'sha256': '043be9ff919762f0dc36fff0222cd90c753ce28b39feb52112be9360c476ef88', | ||
| 'md5': 'eb3f7cc15ba7b6effb2186284185c5cf' | ||
| }, | ||
| 'version': 1 | ||
| }, | ||
| 'nid': 'guid0', | ||
| 'etag': 'eccd2270585257f4b48d8493bed863c01cf0b6dc0bb590101407c9b5e10b8e08', | ||
| 'contentType': None, 'created_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'provider': 'osfstorage', | ||
| 'modified': '2017-09-01T19:34:00.175741+00:00', | ||
| 'modified_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'name': 'doc.rst', | ||
| 'size': 5596, | ||
| 'resource': 'guid0' | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def callback_log_payload_copy(): | ||
| return { | ||
| 'auth': { | ||
| 'callback_url': 'fakecallback.com', | ||
| 'id': 'cat', | ||
| 'name': 'cat', | ||
| 'email': 'cat@cat.com' | ||
| }, | ||
| 'time': 70, | ||
| 'action': 'copy', | ||
| 'source': { | ||
| 'materialized': WaterButlerPath('/doc.rst', prepend=None), | ||
| 'path': '/59a9b628b7d1c903ab5a8f52', | ||
| 'kind': 'file', | ||
| 'extra': { | ||
| 'checkout': None, | ||
| 'downloads': 0, | ||
| 'guid': None, | ||
| 'hashes': { | ||
| 'sha256': '043be9ff919762f0dc36fff0222cd90c753ce28b39feb52112be9360c476ef88', | ||
| 'md5': 'eb3f7cc15ba7b6effb2186284185c5cf' | ||
| }, | ||
| 'version': 1 | ||
| }, | ||
| 'nid': 'guid0', | ||
| 'etag': 'eccd2270585257f4b48d8493bed863c01cf0b6dc0bb590101407c9b5e10b8e08', | ||
| 'contentType': None, | ||
| 'created_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'provider': 'osfstorage', | ||
| 'modified': '2017-09-01T19:34:00.175741+00:00', | ||
| 'modified_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'name': 'doc.rst', | ||
| 'size': 5596, | ||
| 'resource': 'guid0' | ||
| }, | ||
| 'errors': [], | ||
| 'destination': { | ||
| 'materialized': WaterButlerPath('/doc.rst', prepend=None), | ||
| 'path': '/59a9b628b7d1c903ab5a8f52', | ||
| 'kind': 'file', | ||
| 'extra': { | ||
| 'checkout': None, | ||
| 'downloads': 0, | ||
| 'guid': None, | ||
| 'hashes': { | ||
| 'sha256': '043be9ff919762f0dc36fff0222cd90c753ce28b39feb52112be9360c476ef88', | ||
| 'md5': 'eb3f7cc15ba7b6effb2186284185c5cf' | ||
| }, | ||
| 'version': 1 | ||
| }, | ||
| 'nid': 'guid0', | ||
| 'etag': 'eccd2270585257f4b48d8493bed863c01cf0b6dc0bb590101407c9b5e10b8e08', | ||
| 'contentType': None, 'created_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'provider': 'osfstorage', | ||
| 'modified': '2017-09-01T19:34:00.175741+00:00', | ||
| 'modified_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'name': 'doc.rst', | ||
| 'size': 5596, | ||
| 'resource': 'guid0' | ||
| } | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def callback_log_payload_upload(): | ||
| return { | ||
| 'auth': { | ||
| 'id': 'cat', | ||
| 'email': 'cat@cat.com', | ||
| 'name': 'cat', | ||
| 'callback_url': 'fakecallback.com' | ||
| }, | ||
| 'errors': [], | ||
| 'time': 70, | ||
| 'action': 'upload', | ||
| 'provider': 'osfstorage', | ||
| 'metadata': { | ||
| 'kind': 'file', | ||
| 'name': 'doc.rst', | ||
| 'resource': 'guid0', | ||
| 'modified_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'created_utc': '2017-09-01T19:34:00.175741+00:00', | ||
| 'provider': 'osfstorage', | ||
| 'modified': '2017-09-01T19:34:00.175741+00:00', | ||
| 'size': 5596, | ||
| 'path': '/59a9b628b7d1c903ab5a8f52', | ||
| 'etag': 'eccd2270585257f4b48d8493bed863c01cf0b6dc0bb590101407c9b5e10b8e08', | ||
| 'materialized': WaterButlerPath('/doc.rst', prepend=None), | ||
| 'extra': { | ||
| 'downloads': 0, | ||
| 'guid': None, | ||
| 'hashes': { | ||
| 'sha256': '043be9ff919762f0dc36fff0222cd90c753ce28b39feb52112be9360c476ef88', | ||
| 'md5': 'eb3f7cc15ba7b6effb2186284185c5cf'}, | ||
| 'checkout': None, | ||
| 'version': 1 | ||
| }, | ||
| 'contentType': None, | ||
| 'nid': 'guid0'} | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_time(monkeypatch): | ||
| mock_time = mock.Mock() | ||
| mock_time.return_value = 10 | ||
| monkeypatch.setattr(time, 'time', mock_time) | ||
|
|
||
|
|
||
| class MockResponse(): | ||
| status = 200 | ||
| read = MockCoroutine(return_value=b'{"status": "success"}') | ||
|
|
||
|
|
||
| class MockBadResponse(): | ||
| status = 500 | ||
| read = MockCoroutine(return_value=b'{"status": "failure"}') | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_signed_request(): | ||
| return MockCoroutine(return_value=MockResponse()) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,30 @@ | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from waterbutler.core import remote_logging | ||
| from waterbutler.core.log_payload import LogPayload | ||
| from waterbutler.core.remote_logging import log_to_callback | ||
|
|
||
| from tests.providers.osfstorage.fixtures import ( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix import style as shown in previous examples. |
||
| file_metadata_object, | ||
| file_path, | ||
| file_metadata, | ||
| file_lineage, | ||
| provider, | ||
| auth, | ||
| credentials, | ||
| settings | ||
| ) | ||
| from tests.core.fixtures import ( | ||
| log_payload, | ||
| MockBadResponse, | ||
| mock_time, | ||
| callback_log_payload_move, | ||
| callback_log_payload_copy, | ||
| callback_log_payload_upload, | ||
| mock_signed_request | ||
| ) | ||
|
|
||
|
|
||
| class TestScrubPayloadForKeen: | ||
|
|
@@ -74,3 +98,76 @@ def test_max_iteration(self): | |
| 'key-test': 'value2', | ||
| 'key-test-1': 'value3' | ||
| } | ||
|
|
||
|
|
||
| class TestLogPayLoad: | ||
|
|
||
| def test_log_payload(self, log_payload, file_metadata_object, file_path, provider): | ||
| assert log_payload.resource == 'guid0' | ||
| assert log_payload.provider == provider | ||
| assert log_payload.metadata == file_metadata_object | ||
| assert log_payload.path == file_path | ||
|
|
||
| with pytest.raises(Exception) as exc: | ||
| LogPayload('guid0', 'osfstorage') | ||
| assert exc.message == 'Log payload needs either a path or metadata.' | ||
|
|
||
|
|
||
| class TestLogToCallback: | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_log_to_callback_no_logging(self): | ||
| assert (await log_to_callback('download_file')) is None | ||
| assert (await log_to_callback('download_zip')) is None | ||
| assert (await log_to_callback('metadata')) is None | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_log_to_callback_move(self, | ||
| log_payload, | ||
| callback_log_payload_move, | ||
| mock_signed_request, | ||
| mock_time): | ||
|
|
||
| with mock.patch('waterbutler.core.utils.send_signed_request', mock_signed_request): | ||
| await log_to_callback('move', log_payload, log_payload) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In async def log_to_callback(action, source=None, destination=None, start_time=None, errors=[]):This doesn't look right. |
||
| mock_signed_request.assert_called_with('PUT', | ||
| log_payload.auth['callback_url'], | ||
| callback_log_payload_move) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_log_to_callback_copy(self, | ||
| log_payload, | ||
| callback_log_payload_copy, | ||
| mock_signed_request, | ||
| mock_time): | ||
|
|
||
| with mock.patch('waterbutler.core.utils.send_signed_request', mock_signed_request): | ||
| await log_to_callback('copy', log_payload, log_payload) | ||
| mock_signed_request.assert_called_with('PUT', | ||
| log_payload.auth['callback_url'], | ||
| callback_log_payload_copy) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_log_to_callback_upload(self, | ||
| log_payload, | ||
| callback_log_payload_upload, | ||
| mock_signed_request, | ||
| mock_time): | ||
|
|
||
| with mock.patch('waterbutler.core.utils.send_signed_request', mock_signed_request): | ||
| await log_to_callback('upload', log_payload, log_payload) | ||
| mock_signed_request.assert_called_with('PUT', | ||
| log_payload.auth['callback_url'], | ||
| callback_log_payload_upload) | ||
|
|
||
| @pytest.mark.skipif(reason="This test takes too much time because it has 5 retries before " | ||
| "throwing the desired exception, it should take around 50-60 " | ||
| "seconds") | ||
| @pytest.mark.asyncio | ||
| async def test_log_to_callback_throws_exception(self, log_payload, mock_signed_request): | ||
|
|
||
| with mock.patch('waterbutler.core.utils.send_signed_request', mock_signed_request): | ||
| with pytest.raises(Exception) as exc: | ||
| await log_to_callback('upload', log_payload, log_payload) | ||
| assert exc.message == 'Callback for upload request failed with {},' \ | ||
| ' got {"status": "failure"}'.format(MockBadResponse()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curly braces are escaped by using |
||
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.