Skip to content

Commit

Permalink
fix: mock sharev2-push correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
aaxelb committed Aug 21, 2023
1 parent eaf4912 commit 7cf4422
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
6 changes: 5 additions & 1 deletion api/share/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def shtrove_ingest_url():
return f'{settings.SHARE_URL}api/v3/ingest'


def sharev2_push_url():
return f'{settings.SHARE_URL}api/v2/normalizeddata/'


def is_qa_resource(resource):
"""
QA puts tags and special titles on their project to stop them from appearing in the search results. This function
Expand Down Expand Up @@ -350,7 +354,7 @@ def send_share_json(resource, data):
access_token = settings.SHARE_API_TOKEN

return requests.post(
f'{settings.SHARE_URL}api/v2/normalizeddata/',
sharev2_push_url(),
json=data,
headers={
'Authorization': f'Bearer {access_token}',
Expand Down
14 changes: 10 additions & 4 deletions api_tests/share/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
postcommit_queue,
)
from website import settings as website_settings
from api.share.utils import shtrove_ingest_url
from api.share.utils import shtrove_ingest_url, sharev2_push_url


@contextlib.contextmanager
Expand All @@ -27,6 +27,8 @@ def mock_share_responses():
_ingest_url = shtrove_ingest_url()
_rsps.add(responses.POST, _ingest_url, status=200)
_rsps.add(responses.DELETE, _ingest_url, status=200)
# for legacy sharev2 support:
_rsps.add(responses.POST, sharev2_push_url(), status=200)
yield _rsps


Expand All @@ -41,11 +43,15 @@ def mock_update_share():
def expect_ingest_request(mock_share_responses, osfguid, *, token=None, delete=False, count=1):
mock_share_responses._calls.reset()
yield
assert len(mock_share_responses.calls) == count, (
f'expected {count} call(s), got {len(mock_share_responses.calls)}: {list(mock_share_responses.calls)}'
_double_count = count * 2 # pushing to share two ways
assert len(mock_share_responses.calls) == _double_count, (
f'expected {_double_count} call(s), got {len(mock_share_responses.calls)}: {list(mock_share_responses.calls)}'
)
for _call in mock_share_responses.calls:
assert_ingest_request(_call.request, osfguid, token=token, delete=delete)
if _call.request.url.startswith(shtrove_ingest_url()):
assert_ingest_request(_call.request, osfguid, token=token, delete=delete)
else:
assert _call.request.url.startswith(sharev2_push_url())


def assert_ingest_request(request, expected_osfguid, *, token=None, delete=False):
Expand Down
7 changes: 5 additions & 2 deletions api_tests/share/test_share_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from website.project.tasks import on_node_updated

from framework.auth.core import Auth
from api.share.utils import shtrove_ingest_url
from api.share.utils import shtrove_ingest_url, sharev2_push_url
from ._utils import expect_ingest_request


Expand Down Expand Up @@ -188,17 +188,20 @@ def test_call_async_update_on_500_retry(self, mock_share_responses, node, user):
"""This is meant to simulate a temporary outage, so the retry mechanism should kick in and complete it."""
mock_share_responses.replace(responses.POST, shtrove_ingest_url(), status=500)
mock_share_responses.add(responses.POST, shtrove_ingest_url(), status=200)
mock_share_responses.replace(responses.POST, sharev2_push_url(), status=500)
mock_share_responses.add(responses.POST, sharev2_push_url(), status=200)
with expect_ingest_request(mock_share_responses, node._id, count=2):
on_node_updated(node._id, user._id, False, {'is_public'})

def test_call_async_update_on_500_failure(self, mock_share_responses, node, user):
"""This is meant to simulate a total outage, so the retry mechanism should try X number of times and quit."""
mock_share_responses.assert_all_requests_are_fired = False # allows it to retry indefinitely
mock_share_responses.replace(responses.POST, shtrove_ingest_url(), status=500)
mock_share_responses.replace(responses.POST, sharev2_push_url(), status=500)
with expect_ingest_request(mock_share_responses, node._id, count=5): # tries five times
on_node_updated(node._id, user._id, False, {'is_public'})

def test_no_call_async_update_on_400_failure(self, mock_share_responses, node, user):
mock_share_responses.replace(responses.POST, shtrove_ingest_url(), status=400)
mock_share_responses.replace(responses.POST, sharev2_push_url(), status=400)
with expect_ingest_request(mock_share_responses, node._id):
on_node_updated(node._id, user._id, False, {'is_public'})
4 changes: 3 additions & 1 deletion api_tests/share/test_share_preprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest
import responses

from api.share.utils import shtrove_ingest_url
from api.share.utils import shtrove_ingest_url, sharev2_push_url
from framework.auth.core import Auth
from osf.models.spam import SpamStatus
from osf.utils.permissions import READ, WRITE, ADMIN
Expand Down Expand Up @@ -123,12 +123,14 @@ def test_preprint_contributor_changes_updates_preprints_share(self, mock_share_r

def test_call_async_update_on_500_failure(self, mock_share_responses, preprint, auth):
mock_share_responses.replace(responses.POST, shtrove_ingest_url(), status=500)
mock_share_responses.replace(responses.POST, sharev2_push_url(), status=500)
preprint.set_published(True, auth=auth, save=True)
with expect_preprint_ingest_request(mock_share_responses, preprint, count=5):
preprint.update_search()

def test_no_call_async_update_on_400_failure(self, mock_share_responses, preprint, auth):
mock_share_responses.replace(responses.POST, shtrove_ingest_url(), status=400)
mock_share_responses.replace(responses.POST, sharev2_push_url(), status=400)
preprint.set_published(True, auth=auth, save=True)
with expect_preprint_ingest_request(mock_share_responses, preprint, count=1):
preprint.update_search()
Expand Down

0 comments on commit 7cf4422

Please sign in to comment.