From 85b101160fee573bac94636d9829c61683f5a654 Mon Sep 17 00:00:00 2001 From: Gavin Date: Thu, 2 Jul 2020 11:24:31 -0700 Subject: [PATCH] Support anon users in get links from data in core code --- lib/SampleService/core/samples.py | 5 ++--- test/core/samples_test.py | 17 ++++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/SampleService/core/samples.py b/lib/SampleService/core/samples.py index c4c9ef61..33b30110 100644 --- a/lib/SampleService/core/samples.py +++ b/lib/SampleService/core/samples.py @@ -394,14 +394,14 @@ def _resolve_timestamp(self, timestamp: datetime.datetime = None) -> datetime.da def get_links_from_data( self, - user: UserID, + user: Optional[UserID], upa: UPA, timestamp: datetime.datetime = None, as_admin: bool = False) -> Tuple[List[DataLink], datetime.datetime]: ''' Get a set of data links originating from a workspace object at a particular time. - :param user: the user requesting the links. + :param user: the user requesting the links, or None for an anonymous user. :param upa: the data from which the links originate. :param timestamp: the timestamp during which the links should be active, defaulting to the current time. @@ -413,7 +413,6 @@ def get_links_from_data( ''' # may need to make this independent of the workspace. YAGNI. # handle ref path? - _not_falsy(user, 'user') _not_falsy(upa, 'upa') timestamp = self._resolve_timestamp(timestamp) # NONE still checks that WS/obj exists. If it's deleted this method should fail diff --git a/test/core/samples_test.py b/test/core/samples_test.py index 18f972c9..80843918 100644 --- a/test/core/samples_test.py +++ b/test/core/samples_test.py @@ -1347,7 +1347,7 @@ def test_get_links_from_data(): storage.get_links_from_data.assert_called_once_with(UPA('2/4/6'), dt(6)) -def test_get_links_from_data_with_timestamp(): +def test_get_links_from_data_with_timestamp_and_anon_user(): storage = create_autospec(ArangoSampleStorage, spec_set=True, instance=True) lu = create_autospec(KBaseUserLookup, spec_set=True, instance=True) meta = create_autospec(MetadataValidatorSet, spec_set=True, instance=True) @@ -1364,10 +1364,10 @@ def test_get_links_from_data_with_timestamp(): storage.get_links_from_data.return_value = [dl1] - assert s.get_links_from_data(UserID('u1'), UPA('2/4/6'), timestamp=dt(700)) == ([dl1], dt(700)) + assert s.get_links_from_data(None, UPA('2/4/6'), timestamp=dt(700)) == ([dl1], dt(700)) ws.has_permission.assert_called_once_with( - UserID('u1'), WorkspaceAccessType.READ, upa=UPA('2/4/6')) + None, WorkspaceAccessType.READ, upa=UPA('2/4/6')) storage.get_links_from_data.assert_called_once_with(UPA('2/4/6'), dt(700)) @@ -1416,8 +1416,6 @@ def test_get_links_from_data_fail_bad_args(): up = UPA('1/1/1') bt = datetime.datetime.fromtimestamp(1) - _get_links_from_from_data_fail(s, None, up, None, ValueError( - 'user cannot be a value that evaluates to false')) _get_links_from_from_data_fail(s, u, None, None, ValueError( 'upa cannot be a value that evaluates to false')) _get_links_from_from_data_fail(s, u, up, bt, ValueError( @@ -1425,6 +1423,11 @@ def test_get_links_from_data_fail_bad_args(): def test_get_links_from_data_fail_no_ws_access(): + _get_links_from_data_fail_no_ws_access(UserID('u')) + _get_links_from_data_fail_no_ws_access(None) + + +def _get_links_from_data_fail_no_ws_access(user): storage = create_autospec(ArangoSampleStorage, spec_set=True, instance=True) lu = create_autospec(KBaseUserLookup, spec_set=True, instance=True) meta = create_autospec(MetadataValidatorSet, spec_set=True, instance=True) @@ -1433,11 +1436,11 @@ def test_get_links_from_data_fail_no_ws_access(): ws.has_permission.side_effect = UnauthorizedError('oh honey') - _get_links_from_from_data_fail(s, UserID('u'), UPA('1/1/1'), None, + _get_links_from_from_data_fail(s, user, UPA('1/1/1'), None, UnauthorizedError('oh honey')) ws.has_permission.assert_called_once_with( - UserID('u'), WorkspaceAccessType.READ, upa=UPA('1/1/1')) + user, WorkspaceAccessType.READ, upa=UPA('1/1/1')) def _get_links_from_from_data_fail(samples, user, upa, ts, expected):