SamPermissionsImpl implements PermissionsInterface { @Override public Map<Role, List<String>> workflowsSharedWithUser(User user) { if (!hasGoogleToken(user)) { return Collections.emptyMap(); } ResourcesApi resourcesApi = getResourcesApi(user); try { List<ResourceAndAccessPolicy> resourceAndAccessPolicies = resourcesApi.listResourcesAndPolicies(SamConstants.RESOURCE_TYPE); return weedOutDuplicateResourceIds(resourceAndAccessPolicies).stream() .collect(Collectors.groupingBy(ResourceAndAccessPolicy::getAccessPolicyName)).entrySet().stream() .collect(Collectors.toMap(e -> samPolicyNameToRole(e.getKey()), e -> e.getValue().stream().map(r -> { try { return URLDecoder.decode(r.getResourceId().substring(SamConstants.ENCODED_WORKFLOW_PREFIX.length()), "UTF-8"); } catch (UnsupportedEncodingException e1) { return null; } }).collect(Collectors.toList()))); } catch (ApiException e) { LOG.error("Error getting shared workflows", e); if (e.getCode() == HttpStatus.SC_UNAUTHORIZED) { return Collections.emptyMap(); } throw new CustomWebApplicationException("Error getting shared workflows", e.getCode()); } } 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 testWorkflowsSharedWithUser() throws ApiException, UnsupportedEncodingException { ResourceAndAccessPolicy reader = new ResourceAndAccessPolicy(); reader.setResourceId(SamConstants.ENCODED_WORKFLOW_PREFIX + FOO_WORKFLOW_NAME); reader.setAccessPolicyName(SamConstants.READ_POLICY); ResourceAndAccessPolicy owner = new ResourceAndAccessPolicy(); owner.setResourceId(SamConstants.ENCODED_WORKFLOW_PREFIX + GOO_WORKFLOW_NAME); owner.setAccessPolicyName(SamConstants.OWNER_POLICY); ResourceAndAccessPolicy writer = new ResourceAndAccessPolicy(); writer.setResourceId(SamConstants.ENCODED_WORKFLOW_PREFIX + URLEncoder.encode(DOCKSTORE_ORG_WORKFLOW_NAME, StandardCharsets.UTF_8)); writer.setAccessPolicyName(SamConstants.WRITE_POLICY); when(resourcesApiMock.listResourcesAndPolicies(SamConstants.RESOURCE_TYPE)).thenReturn(Arrays.asList(reader, owner, writer)); final Map<Role, List<String>> sharedWithUser = samPermissionsImpl.workflowsSharedWithUser(userMock); Assert.assertEquals(3, sharedWithUser.size()); final List<String> ownerWorkflows = sharedWithUser.get(Role.OWNER); Assert.assertEquals(GOO_WORKFLOW_NAME, ownerWorkflows.get(0)); final List<String> readerWorkflows = sharedWithUser.get(Role.READER); Assert.assertEquals(FOO_WORKFLOW_NAME, readerWorkflows.get(0)); final List<String> writerWorkflows = sharedWithUser.get(Role.WRITER); Assert.assertEquals(DOCKSTORE_ORG_WORKFLOW_NAME, writerWorkflows.get(0)); }
@Test public void userNotInSamReturnsEmptyMap() throws ApiException { when(resourcesApiMock.listResourcesAndPolicies(SamConstants.RESOURCE_TYPE)).thenThrow(new ApiException(HttpStatus.SC_UNAUTHORIZED, "Unauthorized")); final Map<Role, List<String>> sharedWithUser = samPermissionsImpl.workflowsSharedWithUser(userMock); Assert.assertEquals(0, sharedWithUser.size()); }
@Test public void testUserInTwoPoliciesForSameResource() throws ApiException { final String resourceId = SamConstants.ENCODED_WORKFLOW_PREFIX + FOO_WORKFLOW_NAME; ResourceAndAccessPolicy reader = new ResourceAndAccessPolicy(); reader.setResourceId(resourceId); reader.setAccessPolicyName(SamConstants.READ_POLICY); ResourceAndAccessPolicy writer = new ResourceAndAccessPolicy(); writer.setResourceId(resourceId); writer.setAccessPolicyName(SamConstants.WRITE_POLICY); when(resourcesApiMock.listResourcesAndPolicies(SamConstants.RESOURCE_TYPE)) .thenReturn(Arrays.asList(reader, writer)) .thenReturn(Arrays.asList(writer, reader)); final Map<Role, List<String>> sharedWithUser = samPermissionsImpl.workflowsSharedWithUser(userMock); Assert.assertEquals(1, sharedWithUser.size()); Assert.assertEquals(Role.WRITER, sharedWithUser.entrySet().iterator().next().getKey()); final Map<Role, List<String>> sharedWithUser2 = samPermissionsImpl.workflowsSharedWithUser(userMock); Assert.assertEquals(1, sharedWithUser.size()); Assert.assertEquals(Role.WRITER, sharedWithUser.entrySet().iterator().next().getKey()); }