SamPermissionsImpl implements PermissionsInterface { @Override public boolean isSharing(User user) { if (!hasGoogleToken(user)) { return false; } final ResourcesApi resourcesApi = getResourcesApi(user); try { final List<String> resourceIds = ownedResourceIds(resourcesApi); return !userIsOnlyMember(resourceIds, resourcesApi); } catch (ApiException e) { if (e.getCode() == HttpStatus.SC_UNAUTHORIZED) { return false; } LOG.error("Error fetching user's shared resources", e); return true; } } SamPermissionsImpl(TokenDAO tokenDAO, DockstoreWebserviceConfiguration config); @Override List<Permission> setPermission(User requester, Workflow workflow, Permission permission); @Override Map<Role, List<String>> workflowsSharedWithUser(User user); @Override List<Permission> getPermissionsForWorkflow(User user, Workflow workflow); @Override List<Role.Action> getActionsForWorkflow(User user, Workflow workflow); @Override void removePermission(User user, Workflow workflow, String email, Role role); @Override boolean canDoAction(User user, Workflow workflow, Role.Action action); @Override void selfDestruct(User user); @Override boolean isSharing(User user);  }
@Test public void testIsSharingNotInSam() throws ApiException { when(resourcesApiMock.listResourcesAndPolicies(SamConstants.RESOURCE_TYPE)) .thenThrow(new ApiException(HttpStatus.SC_UNAUTHORIZED, "Unauthorized")); Assert.assertFalse(samPermissionsImpl.isSharing(userMock)); }
@Test public void testIsSharingUserInSam() throws ApiException { final String resourceId = SamConstants.ENCODED_WORKFLOW_PREFIX + FOO_WORKFLOW_NAME; ResourceAndAccessPolicy reader = resourceAndAccessPolicyHelper(SamConstants.READ_POLICY); ResourceAndAccessPolicy writer = resourceAndAccessPolicyHelper(SamConstants.WRITE_POLICY); ResourceAndAccessPolicy owner = resourceAndAccessPolicyHelper(SamConstants.OWNER_POLICY); when(resourcesApiMock.listResourcesAndPolicies(SamConstants.RESOURCE_TYPE)) .thenReturn(Collections.emptyList()) .thenReturn(Arrays.asList(reader, writer)) .thenReturn(Collections.singletonList(owner)) .thenReturn(Collections.singletonList(owner)); Assert.assertFalse(samPermissionsImpl.isSharing(userMock)); Assert.assertFalse(samPermissionsImpl.isSharing(userMock)); AccessPolicyResponseEntry onlyOwner = new AccessPolicyResponseEntry(); onlyOwner.setPolicy(new AccessPolicyMembership()); onlyOwner.setPolicyName(SamConstants.OWNER_POLICY); onlyOwner.getPolicy().getMemberEmails().add(JANE_DOE_GMAIL_COM); when(resourcesApiMock.listResourcePolicies(SamConstants.RESOURCE_TYPE, resourceId)) .thenReturn(Collections.singletonList(onlyOwner)); Assert.assertFalse(samPermissionsImpl.isSharing(userMock)); onlyOwner.getPolicy().getMemberEmails().add("jdoe@ucsc.edu"); Assert.assertTrue(samPermissionsImpl.isSharing(userMock)); }