Skip to content

Commit

Permalink
Finish fixing existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Walz authored and Jon Walz committed Mar 7, 2024
1 parent 6449992 commit b9065c4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
22 changes: 13 additions & 9 deletions addon_service/tests/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ class MockOSF:
def __init__(self, permissions=None):
'''A lightweight, configurable mock of OSF for testing remote permissions.
Accepts a mapping of arbitrary user_uris to their roles on arbitrary resource_uris.
i.e.
Accepts a mapping of arbitrary resource_uris to user permissiosn and `public` status
{
'osf.io/abcde': {'osf.io/bcdef': 'write', 'osf.io/cdefg': 'admin'},
'osf.io/zyxwv': {'osf.io/yxwvut': 'read'}
'osf.io/abcde': {'osf.io/bcdef': 'write', 'osf.io/cdefg': 'admin', 'public': True},
'osf.io/zyxwv': {'osf.io/yxwvut': 'read', 'public': False}
}
Intercepts 'get' requests and uses the request url and this mapping to generate a minimal
response required for testing GravyValet's behavior.
Expand All @@ -57,25 +56,30 @@ def configure_assumed_caller(self, caller_uri):
self._configured_caller_uri = caller_uri

def configure_user_role(self, user_uri, resource_uri, role):
self._permissions[user_uri][resource_uri] = role
self._permissions[resource_uri][user_uri] = role

def configure_resource_visibility(self, resource_uri, *, public=True):
self._permissions[resource_uri]['public'] = public

def _get_assumed_caller(self, cookies=None):
if self._configured_caller_uri:
return self._configured_caller_uri
if cookies is not None:
return cookies.get(settings.USER_REFERENCE_COOKIE)
raise ValueError('MockOSF cannot handle requests without configuring a user')
return None

def _get_user_permissions(self, user_uri, resource_uri):
role = self._permissions[user_uri][resource_uri]
if not role:
return []
# Use of defaultdict means this will always have some value
role = self._permissions[resource_uri][user_uri]
if role == 'read':
return ['read']
if role == 'write':
return ['read', 'write']
if role == 'admin':
return ['read', 'write', 'admin']
if self._permissions[resource_uri]['public']:
return ['read']
return []

def _mock_user_check(self, *args, **kwargs):
caller_uri = self._get_assumed_caller(cookies=kwargs.get('cookies'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,11 @@ def test_unauthorized(self):
self.assertEqual(_anon_resp.status_code, HTTPStatus.UNAUTHORIZED)

def test_wrong_user(self):
self._mock_osf.configure_assumed_caller('wrong/10')
_resp = self._view(
get_test_request(
cookies={settings.USER_REFERENCE_COOKIE: "wrong/10"}
),
pk=self._user.pk,
pk=self._asa.pk,
)
self.assertEqual(_resp.status_code, HTTPStatus.FORBIDDEN)

Expand Down
28 changes: 23 additions & 5 deletions addon_service/tests/test_by_type/test_resource_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def setUpTestData(cls):

def setUp(self):
super().setUp()
self._mock_osf = MockOSF(permissions={self._user.user_uri: {self._resource.resource_uri: 'admin'}})
self._mock_osf = MockOSF()
self._mock_osf.configure_user_role(user_uri=self._user.user_uri, resource_uri=self._resource.resource_uri, role='admin')
self.addCleanup(self._mock_osf.stop)
self.client.cookies[settings.USER_REFERENCE_COOKIE] = self._user.user_uri

Expand Down Expand Up @@ -137,19 +138,36 @@ def test_get(self):
},
)

def test_unauthorized(self):
def test_unauthorized__private_resource(self):
self._mock_osf.configure_resource_visibility(self._resource.resource_uri, public=False)
_anon_resp = self._view(get_test_request(), pk=self._resource.pk)
self.assertEqual(_anon_resp.status_code, HTTPStatus.FORBIDDEN)

def test_unauthorized__public_resource(self):
self._mock_osf.configure_resource_visibility(self._resource.resource_uri, public=True)
_anon_resp = self._view(get_test_request(), pk=self._resource.pk)
self.assertEqual(_anon_resp.status_code, HTTPStatus.UNAUTHORIZED)
self.assertEqual(_anon_resp.status_code, HTTPStatus.OK)

def test_wrong_user(self):
def test_wrong_user__pivate_resource(self):
self._mock_osf.configure_resource_visibility(self._resource.resource_uri, public=False)
_resp = self._view(
get_test_request(
cookies={settings.USER_REFERENCE_COOKIE: "this is wrong user auth"}
),
pk=self._user.pk,
pk=self._resource.pk,
)
self.assertEqual(_resp.status_code, HTTPStatus.FORBIDDEN)

def test_wrong_user__public_resource(self):
self._mock_osf.configure_resource_visibility(self._resource.resource_uri, public=True)
_resp = self._view(
get_test_request(
cookies={settings.USER_REFERENCE_COOKIE: "this is wrong user auth"}
),
pk=self._resource.pk,
)
self.assertEqual(_resp.status_code, HTTPStatus.OK)


class TestResourceReferenceRelatedView(TestCase):
@classmethod
Expand Down

0 comments on commit b9065c4

Please sign in to comment.