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

[SVCS-475] Send hook request to OSF for GET requests #307

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
206 changes: 206 additions & 0 deletions tests/core/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
import time
from http import HTTPStatus
from unittest import mock

import pytest

from waterbutler.core.path import WaterButlerPath
from waterbutler.core.log_payload import LogPayload

from tests.utils import MockCoroutine
from tests.providers.osfstorage.fixtures import (auth, file_path, file_lineage, provider,
file_metadata_object, file_metadata)


@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': '[email protected]'
},
'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': '[email protected]'
},
'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': '[email protected]',
'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)


@pytest.fixture
def mock_signed_request():
return MockCoroutine(return_value=MockResponse())


class MockResponse:
status = HTTPStatus.OK
read = MockCoroutine(return_value=b'{"status": "success"}')


class MockBadResponse:
status = HTTPStatus.INTERNAL_SERVER_ERROR
read = MockCoroutine(return_value=b'{"status": "failure"}')
95 changes: 95 additions & 0 deletions tests/core/test_remote_logging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
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.core.fixtures import (MockBadResponse, log_payload, mock_time,
mock_signed_request, callback_log_payload_upload,
callback_log_payload_move, callback_log_payload_copy)
from tests.providers.osfstorage.fixtures import (auth, credentials, provider,
settings, file_path, file_lineage,
file_metadata, file_metadata_object)


class TestScrubPayloadForKeen:
Expand Down Expand Up @@ -74,3 +85,87 @@ 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', source=log_payload, destination=log_payload)
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', source=log_payload, destination=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', source=log_payload, destination=log_payload)
mock_signed_request.assert_called_with(
'PUT',
log_payload.auth['callback_url'],
callback_log_payload_upload
)

# TODO: should we fix or skip this? This test never passes for me locally but always takes a long time.
@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, 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')
expected_message = 'Callback for upload request failed with {},' \
' got {{"status": "failure"}}'.format(MockBadResponse())
assert exc.message == expected_message
1 change: 1 addition & 0 deletions tests/providers/osfstorage/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def auth():
'id': 'cat',
'name': 'cat',
'email': '[email protected]',
'callback_url': 'fakecallback.com',
}


Expand Down
Loading