SamPermissionsImpl implements PermissionsInterface { @Override public List<Permission> setPermission(User requester, Workflow workflow, Permission permission) { checkEmailNotOriginalOwner(permission.getEmail(), workflow); ResourcesApi resourcesApi = getResourcesApi(requester); try { final String encodedPath = encodedWorkflowResource(workflow, resourcesApi.getApiClient()); final List<AccessPolicyResponseEntry> resourcePolicies = ensureResourceExists(workflow, requester, resourcesApi, encodedPath); final List<AccessPolicyResponseEntry> policiesNewUserBelongsTo = resourcePolicies .stream().filter(entry -> entry.getPolicy().getMemberEmails().contains(permission.getEmail())) .collect(Collectors.toList()); final String samPolicyName = permissionSamMap.get(permission.getRole()); ensurePolicyExists(resourcePolicies, samPolicyName, encodedPath, resourcesApi); if (!policiesNewUserBelongsTo.stream().anyMatch(entry -> entry.getPolicyName().equals(samPolicyName))) { resourcesApi.addUserToPolicy(SamConstants.RESOURCE_TYPE, encodedPath, samPolicyName, permission.getEmail()); } for (AccessPolicyResponseEntry entry : policiesNewUserBelongsTo) { if (!entry.getPolicyName().equals(samPolicyName)) { resourcesApi.removeUserFromPolicy(SamConstants.RESOURCE_TYPE, encodedPath, entry.getPolicyName(), permission.getEmail()); } } return getPermissionsForWorkflow(requester, workflow); } catch (ApiException e) { String errorMessage = readValue(e, ErrorReport.class).map(errorReport -> errorReport.getMessage()) .orElse("Error setting permission"); LOG.error(errorMessage, e); throw new CustomWebApplicationException(errorMessage, 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 testSetPermission() throws ApiException { when(resourcesApiMock.listResourcePolicies(SamConstants.RESOURCE_TYPE, SamConstants.WORKFLOW_PREFIX + FOO_WORKFLOW_NAME)) .thenReturn(Collections.emptyList(), Collections.singletonList(readerAccessPolicyResponseEntry)); Permission permission = new Permission(); permission.setEmail(JANE_DOE_GMAIL_COM); permission.setRole(Role.READER); List<Permission> permissions = samPermissionsImpl.setPermission(userMock, workflowInstance, permission); Assert.assertEquals(permissions.size(), 1); }
@Test public void setPermissionTest1() { try { when(resourcesApiMock.listResourcePolicies(SamConstants.RESOURCE_TYPE, SamConstants.WORKFLOW_PREFIX + FOO_WORKFLOW_NAME)).thenThrow(new ApiException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Server error")); final Permission permission = new Permission(); permission.setEmail("jdoe@example.com"); permission.setRole(Role.WRITER); thrown.expect(CustomWebApplicationException.class); samPermissionsImpl.setPermission(userMock, workflowInstance, permission); Assert.fail("setPermissions did not throw Exception"); } catch (ApiException e) { Assert.fail(); e.printStackTrace(); } }
@Test public void setPermissionTest2() { try { final Permission permission = new Permission(); permission.setEmail(JANE_DOE_GMAIL_COM); permission.setRole(Role.READER); when(resourcesApiMock.listResourcePolicies(SamConstants.RESOURCE_TYPE, SamConstants.WORKFLOW_PREFIX + FOO_WORKFLOW_NAME)) .thenThrow(new ApiException(404, "Not Found")) .thenReturn(Collections.singletonList(readerAccessPolicyResponseEntry)); try { setupInitializePermissionsMocks(SamConstants.WORKFLOW_PREFIX + FOO_WORKFLOW_NAME); final List<Permission> permissions = samPermissionsImpl.setPermission(userMock, workflowInstance, permission); Assert.assertEquals(permissions.size(), 1); } catch (CustomWebApplicationException ex) { Assert.fail("setPermissions threw Exception"); } } catch (ApiException e) { Assert.fail(); e.printStackTrace(); } }
@Test public void setPermissionRemovesExistingPermission() throws ApiException { final Permission localReaderPermission = new Permission(JANE_DOE_GMAIL_COM, Role.READER); final String encodedPath = SamConstants.WORKFLOW_PREFIX + FOO_WORKFLOW_NAME; when(resourcesApiMock.listResourcePolicies(SamConstants.RESOURCE_TYPE, encodedPath)) .thenReturn(Collections.singletonList(writerPolicy)); setupInitializePermissionsMocks(encodedPath); doNothing().when(resourcesApiMock) .removeUserFromPolicy(SamConstants.RESOURCE_TYPE, encodedPath, SamConstants.WRITE_POLICY, JANE_DOE_GMAIL_COM); samPermissionsImpl.setPermission(userMock, workflowInstance, localReaderPermission); verify(resourcesApiMock, times(1)) .removeUserFromPolicy(SamConstants.RESOURCE_TYPE, encodedPath, SamConstants.WRITE_POLICY, JANE_DOE_GMAIL_COM); }
@Test public void testSetPermissionWhenResourceCreatedWithoutAllPolicies() throws ApiException { when(workflowInstance.getUsers()).thenReturn(new HashSet<>(Collections.singletonList(userMock))); final String resourceId = SamConstants.ENCODED_WORKFLOW_PREFIX + FOO_WORKFLOW_NAME; ResourceAndAccessPolicy owner = resourceAndAccessPolicyHelper(SamConstants.OWNER_POLICY); when(resourcesApiMock.listResourcesAndPolicies(SamConstants.RESOURCE_TYPE)) .thenReturn(Collections.singletonList(owner)); setupInitializePermissionsMocks(resourceId); samPermissionsImpl.setPermission(userMock, workflowInstance, new Permission("johndoe@example.com", Role.WRITER)); verify(resourcesApiMock, times(1)).overwritePolicy(eq(SamConstants.RESOURCE_TYPE), anyString(), eq(SamConstants.WRITE_POLICY), any()); }
@Test public void testSamPolicyOwnedbyAnother() throws ApiException { when(workflowInstance.getUsers()).thenReturn(new HashSet<>(Collections.singletonList(userMock))); final String resourceId = SamConstants.WORKFLOW_PREFIX + FOO_WORKFLOW_NAME; when(resourcesApiMock.listResourcePolicies(SamConstants.RESOURCE_TYPE, resourceId)) .thenThrow(new ApiException(HttpStatus.SC_NOT_FOUND, "")); doThrow(new ApiException(HttpStatus.SC_CONFLICT, "")) .when(resourcesApiMock) .createResourceWithDefaults(SamConstants.RESOURCE_TYPE, resourceId); try { samPermissionsImpl.setPermission(userMock, workflowInstance, new Permission("johndoe@example.com", Role.WRITER)); Assert.fail("Expected setPermission to throw exception"); } catch (CustomWebApplicationException ex) { Assert.assertTrue(ex.getMessage().contains(" 409 ")); } }
@Test public void testAddingOwnerDoesNotCreateSecondOwnerPolicy() throws ApiException { when(workflowInstance.getUsers()).thenReturn(new HashSet<>(Collections.singletonList(userMock))); final String resourceId = SamConstants.ENCODED_WORKFLOW_PREFIX + FOO_WORKFLOW_NAME; ResourceAndAccessPolicy owner = resourceAndAccessPolicyHelper(SamConstants.OWNER_POLICY); when(resourcesApiMock.listResourcesAndPolicies(SamConstants.RESOURCE_TYPE)) .thenReturn(Collections.singletonList(owner)); samPermissionsImpl.setPermission(userMock, workflowInstance, new Permission("johndoe@example.com", Role.OWNER)); verify(resourcesApiMock, times(0)).overwritePolicy(eq(SamConstants.RESOURCE_TYPE), anyString(), eq(SamConstants.OWNER_POLICY), any()); }