Skip to content

Commit 42b3829

Browse files
authored
[ENG-8290] Allow collection search POST with token scope (#11201)
Purpose Add scope for POST collection search
1 parent 461aeba commit 42b3829

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

api/search/views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ class SearchCollections(BaseSearchView):
656656
doc_type = 'collectionSubmission'
657657
view_category = 'search'
658658
view_name = 'search-collected-metadata'
659+
required_write_scopes = [CoreScopes.ADVANCED_SEARCH]
659660

660661
@property
661662
def search_fields(self):

api_tests/search/views/test_views.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import pytest
22
import uuid
3+
from unittest import mock
34

45
from api.base.settings.defaults import API_BASE
56
from api_tests import utils
7+
from framework.auth.cas import CasResponse
68
from framework.auth.core import Auth
79
from osf.models import RegistrationSchema
810
from osf_tests.factories import (
@@ -1025,3 +1027,24 @@ def test_POST_search_collections_disease_data_type(
10251027
assert res.status_code == 200
10261028
assert res.json['links']['meta']['total'] == 2
10271029
assert len(res.json['data']) == 2
1030+
1031+
def test_POST_search_collections_scope(self, app, url_collection_search, user):
1032+
payload = self.post_payload(q='Collection')
1033+
1034+
token_invalid = CasResponse(
1035+
authenticated=True,
1036+
user=user._id,
1037+
attributes={'accessTokenScope': ['osf.full_read']}
1038+
)
1039+
with mock.patch('framework.auth.cas.CasClient.profile', return_value=token_invalid):
1040+
res = app.post_json_api(url_collection_search, payload, auth='some-invalid-token', expect_errors=True, auth_type='jwt')
1041+
assert res.status_code == 403
1042+
1043+
token_valid = CasResponse(
1044+
authenticated=True,
1045+
user=user._id,
1046+
attributes={'accessTokenScope': ['osf.full_read', 'osf.full_write']}
1047+
)
1048+
with mock.patch('framework.auth.cas.CasClient.profile', return_value=token_valid):
1049+
res = app.post_json_api(url_collection_search, payload, auth='some-valid-token', auth_type='jwt')
1050+
assert res.status_code == 200

framework/auth/oauth_scopes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ class CoreScopes:
210210
READ_COLLECTION_SUBMISSION = 'read_collection_submission'
211211
WRITE_COLLECTION_SUBMISSION = 'write_collection_submission'
212212

213+
ADVANCED_SEARCH = 'advanced_search'
214+
213215

214216
class ComposedScopes:
215217
"""
@@ -370,7 +372,8 @@ class ComposedScopes:
370372
CoreScopes.CEDAR_METADATA_RECORD_WRITE,
371373
CoreScopes.WRITE_COLLECTION_SUBMISSION_ACTION,
372374
CoreScopes.WRITE_COLLECTION_SUBMISSION,
373-
CoreScopes.USERS_MESSAGE_WRITE_EMAIL
375+
CoreScopes.USERS_MESSAGE_WRITE_EMAIL,
376+
CoreScopes.ADVANCED_SEARCH
374377
)
375378

376379
# Admin permissions- includes functionality not intended for third-party use

0 commit comments

Comments
 (0)