Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 210 additions & 0 deletions tests/core/fixtures.py
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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)

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())
97 changes: 97 additions & 0 deletions tests/core/test_remote_logging.py
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 (

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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:
Expand Down Expand Up @@ -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)

@cslzchen cslzchen Jan 3, 2018

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In remote_logging.py, the signature for log_to_callback() is:

async def log_to_callback(action, source=None, destination=None, start_time=None, errors=[]):

This doesn't look right. I removed the two log_payload arguments and tests passed as expected. No, they failed.

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())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Curly braces are escaped by using {{ and }} in python.

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': 'cat@cat.com',
'callback_url': 'fakecallback.com',
}


Expand Down
Loading