Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions rorapi/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from rorapi.common.create_update import new_record_from_json, update_record_from_json
from rorapi.common.csv_bulk import process_csv
from rorapi.common.csv_utils import validate_csv
from django.conf import settings
from rorapi.settings import REST_FRAMEWORK, ES7, ES_VARS
from rorapi.common.matching import match_organizations
from rorapi.common.matching_single_search import match_organizations as single_search_match_organizations
Expand Down Expand Up @@ -153,6 +154,10 @@ def list(self, request, version=REST_FRAMEWORK["DEFAULT_VERSION"]):
if "affiliation" in params:
if "single_search" in params:
errors, organizations = single_search_match_organizations(params)
elif "multisearch" in params:
errors, organizations = match_organizations(params)
elif settings.SINGLE_SEARCH_DEFAULT:
errors, organizations = single_search_match_organizations(params)
else:
errors, organizations = match_organizations(params)
else:
Expand Down
4 changes: 4 additions & 0 deletions rorapi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@
# Toggle for behavior-based rate limiting
ENABLE_BEHAVIORAL_LIMITING = os.getenv("ENABLE_BEHAVIORAL_LIMITING", "False") == "True"

# When True, affiliation matching defaults to single search; otherwise multisearch.
# Request params single_search and multisearch override this.
SINGLE_SEARCH_DEFAULT = os.getenv("SINGLE_SEARCH_DEFAULT", "False") == "True"

# Email settings for Django
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
Expand Down
88 changes: 87 additions & 1 deletion rorapi/tests/tests_unit/tests_views_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import mock
import os

from django.test import SimpleTestCase, Client
from django.test import SimpleTestCase, Client, override_settings
from rest_framework.test import APIRequestFactory

from rorapi.common import views
Expand Down Expand Up @@ -88,6 +88,92 @@ def test_query_redirect(self, search_mock):
response = client.get('/v2/organizations?query.names=query')
self.assertRedirects(response, '/v2/organizations?query=query')


class _MockMatchingResult:
"""Minimal object matching MatchingResultSerializer expectations."""
number_of_results = 0
items = []


class AffiliationDefaultMatchingTestCase(SimpleTestCase):
"""Test SINGLE_SEARCH_DEFAULT and single_search/multisearch params."""
V2_VERSION = 'v2'

@override_settings(SINGLE_SEARCH_DEFAULT=False)
@mock.patch('rorapi.common.views.single_search_match_organizations')
@mock.patch('rorapi.common.views.match_organizations')
def test_affiliation_default_uses_multisearch_when_setting_off(
self, match_organizations_mock, single_search_mock):
mock_result = (None, _MockMatchingResult())
match_organizations_mock.return_value = mock_result
single_search_mock.return_value = mock_result

view = views.OrganizationViewSet.as_view({'get': 'list'})
request = factory.get('/v2/organizations', {'affiliation': 'Harvard University'})
response = view(request, version=self.V2_VERSION)
response.render()

match_organizations_mock.assert_called_once()
single_search_mock.assert_not_called()

@override_settings(SINGLE_SEARCH_DEFAULT=True)
@mock.patch('rorapi.common.views.single_search_match_organizations')
@mock.patch('rorapi.common.views.match_organizations')
def test_affiliation_default_uses_single_search_when_setting_on(
self, match_organizations_mock, single_search_mock):
mock_result = (None, _MockMatchingResult())
match_organizations_mock.return_value = mock_result
single_search_mock.return_value = mock_result

view = views.OrganizationViewSet.as_view({'get': 'list'})
request = factory.get('/v2/organizations', {'affiliation': 'Harvard University'})
response = view(request, version=self.V2_VERSION)
response.render()

single_search_mock.assert_called_once()
match_organizations_mock.assert_not_called()

@override_settings(SINGLE_SEARCH_DEFAULT=True)
@mock.patch('rorapi.common.views.single_search_match_organizations')
@mock.patch('rorapi.common.views.match_organizations')
def test_affiliation_multisearch_param_overrides_setting(
self, match_organizations_mock, single_search_mock):
mock_result = (None, _MockMatchingResult())
match_organizations_mock.return_value = mock_result
single_search_mock.return_value = mock_result

view = views.OrganizationViewSet.as_view({'get': 'list'})
request = factory.get(
'/v2/organizations',
{'affiliation': 'Harvard University', 'multisearch': ''}
)
response = view(request, version=self.V2_VERSION)
response.render()

match_organizations_mock.assert_called_once()
single_search_mock.assert_not_called()

@override_settings(SINGLE_SEARCH_DEFAULT=False)
@mock.patch('rorapi.common.views.single_search_match_organizations')
@mock.patch('rorapi.common.views.match_organizations')
def test_affiliation_single_search_param_uses_single_search(
self, match_organizations_mock, single_search_mock):
mock_result = (None, _MockMatchingResult())
match_organizations_mock.return_value = mock_result
single_search_mock.return_value = mock_result

view = views.OrganizationViewSet.as_view({'get': 'list'})
request = factory.get(
'/v2/organizations',
{'affiliation': 'Harvard University', 'single_search': ''}
)
response = view(request, version=self.V2_VERSION)
response.render()

single_search_mock.assert_called_once()
match_organizations_mock.assert_not_called()


class ViewRetrievalTestCase(SimpleTestCase):

V2_VERSION = 'v2'
Expand Down