Search in sources :

Example 11 with BadRequestException

use of org.pmiops.workbench.exceptions.BadRequestException in project workbench by all-of-us.

the class CohortReviewServiceImplTest method assertParticipantCohortAnnotationModifyBadRequest.

private void assertParticipantCohortAnnotationModifyBadRequest(Long annotationId, ParticipantCohortAnnotation participantCohortAnnotation, AnnotationType annotationType) {
    Long cohortAnnotationDefinitionId = participantCohortAnnotation.getCohortAnnotationDefinitionId();
    Long cohortReviewId = participantCohortAnnotation.getCohortReviewId();
    Long participantId = participantCohortAnnotation.getParticipantId();
    CohortAnnotationDefinition cohortAnnotationDefinition = createCohortAnnotationDefinition(cohortAnnotationDefinitionId, annotationType);
    when(participantCohortAnnotationDao.findByAnnotationIdAndCohortReviewIdAndParticipantId(annotationId, cohortReviewId, participantId)).thenReturn(participantCohortAnnotation);
    when(cohortAnnotationDefinitionDao.findOne(cohortAnnotationDefinitionId)).thenReturn(cohortAnnotationDefinition);
    try {
        cohortReviewService.updateParticipantCohortAnnotation(annotationId, cohortReviewId, participantId, new ModifyParticipantCohortAnnotationRequest());
        fail("Should have thrown BadRequestExcpetion!");
    } catch (BadRequestException e) {
        assertEquals("Invalid Request: Please provide a valid " + cohortAnnotationDefinition.getAnnotationType().name() + " value for annotation defintion id: " + cohortAnnotationDefinition.getCohortAnnotationDefinitionId(), e.getMessage());
    }
    verify(participantCohortAnnotationDao, atLeastOnce()).findByAnnotationIdAndCohortReviewIdAndParticipantId(annotationId, cohortReviewId, participantId);
    verify(cohortAnnotationDefinitionDao, atLeastOnce()).findOne(cohortAnnotationDefinitionId);
    verifyNoMoreMockInteractions();
}
Also used : CohortAnnotationDefinition(org.pmiops.workbench.db.model.CohortAnnotationDefinition) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ModifyParticipantCohortAnnotationRequest(org.pmiops.workbench.model.ModifyParticipantCohortAnnotationRequest)

Example 12 with BadRequestException

use of org.pmiops.workbench.exceptions.BadRequestException in project workbench by all-of-us.

the class WorkspaceServiceImpl method updateUserRoles.

@Override
public Workspace updateUserRoles(Workspace workspace, Set<WorkspaceUserRole> userRoleSet) {
    Map<Long, WorkspaceUserRole> userRoleMap = new HashMap<Long, WorkspaceUserRole>();
    for (WorkspaceUserRole userRole : userRoleSet) {
        userRole.setWorkspace(workspace);
        userRoleMap.put(userRole.getUser().getUserId(), userRole);
    }
    ArrayList<WorkspaceACLUpdate> updateACLRequestList = new ArrayList<WorkspaceACLUpdate>();
    Iterator<WorkspaceUserRole> dbUserRoles = workspace.getWorkspaceUserRoles().iterator();
    while (dbUserRoles.hasNext()) {
        WorkspaceUserRole currentUserRole = dbUserRoles.next();
        WorkspaceUserRole mapValue = userRoleMap.get(currentUserRole.getUser().getUserId());
        if (mapValue != null) {
            currentUserRole.setRole(mapValue.getRole());
            userRoleMap.remove(currentUserRole.getUser().getUserId());
        } else {
            // This is how to remove a user from the FireCloud ACL:
            // Pass along an update request with NO ACCESS as the given access level.
            WorkspaceACLUpdate removedUser = new WorkspaceACLUpdate();
            removedUser.setEmail(currentUserRole.getUser().getEmail());
            removedUser.setCanCompute(false);
            removedUser.setCanShare(false);
            removedUser.setAccessLevel(WorkspaceAccessLevel.NO_ACCESS.toString());
            updateACLRequestList.add(removedUser);
            dbUserRoles.remove();
        }
    }
    for (Entry<Long, WorkspaceUserRole> remainingRole : userRoleMap.entrySet()) {
        workspace.getWorkspaceUserRoles().add(remainingRole.getValue());
    }
    for (WorkspaceUserRole currentWorkspaceUser : workspace.getWorkspaceUserRoles()) {
        WorkspaceACLUpdate currentUpdate = new WorkspaceACLUpdate();
        currentUpdate.setEmail(currentWorkspaceUser.getUser().getEmail());
        currentUpdate.setCanCompute(false);
        if (currentWorkspaceUser.getRole() == WorkspaceAccessLevel.OWNER) {
            currentUpdate.setCanShare(true);
            currentUpdate.setAccessLevel(WorkspaceAccessLevel.OWNER.toString());
        } else if (currentWorkspaceUser.getRole() == WorkspaceAccessLevel.WRITER) {
            currentUpdate.setCanShare(false);
            currentUpdate.setAccessLevel(WorkspaceAccessLevel.WRITER.toString());
        } else {
            currentUpdate.setCanShare(false);
            currentUpdate.setAccessLevel(WorkspaceAccessLevel.READER.toString());
        }
        updateACLRequestList.add(currentUpdate);
    }
    try {
        WorkspaceACLUpdateResponseList fireCloudResponse = fireCloudService.updateWorkspaceACL(workspace.getWorkspaceNamespace(), workspace.getFirecloudName(), updateACLRequestList);
        if (fireCloudResponse.getUsersNotFound().size() != 0) {
            String usersNotFound = "";
            for (int i = 0; i < fireCloudResponse.getUsersNotFound().size(); i++) {
                if (i > 0) {
                    usersNotFound += ", ";
                }
                usersNotFound += fireCloudResponse.getUsersNotFound().get(i).getEmail();
            }
            throw new BadRequestException(usersNotFound);
        }
    } catch (ApiException e) {
        if (e.getCode() == 400) {
            throw new BadRequestException(e.getResponseBody());
        } else if (e.getCode() == 404) {
            throw new NotFoundException("Workspace not found.");
        } else if (e.getCode() == 500) {
            throw new ServerErrorException(e);
        } else {
            throw new ServerUnavailableException(e);
        }
    }
    return this.saveWithLastModified(workspace);
}
Also used : HashMap(java.util.HashMap) WorkspaceACLUpdateResponseList(org.pmiops.workbench.firecloud.model.WorkspaceACLUpdateResponseList) ArrayList(java.util.ArrayList) ServerUnavailableException(org.pmiops.workbench.exceptions.ServerUnavailableException) NotFoundException(org.pmiops.workbench.exceptions.NotFoundException) WorkspaceUserRole(org.pmiops.workbench.db.model.WorkspaceUserRole) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) ServerErrorException(org.pmiops.workbench.exceptions.ServerErrorException) WorkspaceACLUpdate(org.pmiops.workbench.firecloud.model.WorkspaceACLUpdate) ApiException(org.pmiops.workbench.firecloud.ApiException)

Example 13 with BadRequestException

use of org.pmiops.workbench.exceptions.BadRequestException in project workbench by all-of-us.

the class CohortsControllerTest method testUpdateCohortInvalidEtagsThrow.

@Test
public void testUpdateCohortInvalidEtagsThrow() throws Exception {
    Cohort cohort = createDefaultCohort();
    cohort = cohortsController.createCohort(workspace.getNamespace(), workspace.getId(), cohort).getBody();
    // TODO: Refactor to be a @Parameterized test case.
    List<String> cases = ImmutableList.of("", "hello, world", "\"\"", "\"\"1234\"\"", "\"-1\"");
    for (String etag : cases) {
        try {
            cohortsController.updateCohort(workspace.getNamespace(), workspace.getId(), cohort.getId(), new Cohort().name("updated-name").etag(etag));
            fail(String.format("expected BadRequestException for etag: %s", etag));
        } catch (BadRequestException e) {
        // expected
        }
    }
}
Also used : Cohort(org.pmiops.workbench.model.Cohort) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.Test)

Example 14 with BadRequestException

use of org.pmiops.workbench.exceptions.BadRequestException in project workbench by all-of-us.

the class WorkspacesControllerTest method testUpdateWorkspaceInvalidEtagsThrow.

@Test
public void testUpdateWorkspaceInvalidEtagsThrow() throws Exception {
    Workspace ws = createDefaultWorkspace();
    ws = workspacesController.createWorkspace(ws).getBody();
    // TODO: Refactor to be a @Parameterized test case.
    List<String> cases = ImmutableList.of("", "hello, world", "\"\"", "\"\"1234\"\"", "\"-1\"");
    for (String etag : cases) {
        try {
            stubGetWorkspace(ws.getNamespace(), ws.getId(), ws.getCreator(), WorkspaceAccessLevel.OWNER);
            UpdateWorkspaceRequest request = new UpdateWorkspaceRequest();
            request.setWorkspace(new Workspace().name("updated-name").etag(etag));
            workspacesController.updateWorkspace(ws.getNamespace(), ws.getId(), request);
            fail(String.format("expected BadRequestException for etag: %s", etag));
        } catch (BadRequestException e) {
        // expected
        }
    }
}
Also used : BadRequestException(org.pmiops.workbench.exceptions.BadRequestException) UpdateWorkspaceRequest(org.pmiops.workbench.model.UpdateWorkspaceRequest) Matchers.anyString(org.mockito.Matchers.anyString) Workspace(org.pmiops.workbench.model.Workspace) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest) Test(org.junit.Test)

Example 15 with BadRequestException

use of org.pmiops.workbench.exceptions.BadRequestException in project workbench by all-of-us.

the class AuthInterceptor method hasRequiredAuthority.

boolean hasRequiredAuthority(Method controllerMethod, User user) {
    String controllerMethodName = controllerMethod.getDeclaringClass().getName() + "." + controllerMethod.getName();
    AuthorityRequired req = controllerMethod.getAnnotation(AuthorityRequired.class);
    if (req != null) {
        if (user == null) {
            throw new BadRequestException("User is not initialized; please register");
        }
        // Fetch the user with authorities, since they aren't loaded during normal
        user = userDao.findUserWithAuthorities(user.getUserId());
        Collection<Authority> granted = user.getAuthorities();
        if (granted.containsAll(Arrays.asList(req.value()))) {
            return true;
        } else {
            log.log(Level.INFO, "{0} required authorities {1} but user had only {2}.", new Object[] { controllerMethodName, Arrays.toString(req.value()), Arrays.toString(granted.toArray()) });
            return false;
        }
    }
    // No @AuthorityRequired annotation found at runtime, default to allowed.
    return true;
}
Also used : Authority(org.pmiops.workbench.model.Authority) AuthorityRequired(org.pmiops.workbench.annotations.AuthorityRequired) BadRequestException(org.pmiops.workbench.exceptions.BadRequestException)

Aggregations

BadRequestException (org.pmiops.workbench.exceptions.BadRequestException)27 Timestamp (java.sql.Timestamp)5 CohortReview (org.pmiops.workbench.db.model.CohortReview)5 Workspace (org.pmiops.workbench.db.model.Workspace)5 NotFoundException (org.pmiops.workbench.exceptions.NotFoundException)5 Test (org.junit.Test)4 CohortAnnotationDefinition (org.pmiops.workbench.db.model.CohortAnnotationDefinition)4 WorkspaceUserRole (org.pmiops.workbench.db.model.WorkspaceUserRole)4 ConflictException (org.pmiops.workbench.exceptions.ConflictException)4 User (org.pmiops.workbench.db.model.User)3 Workspace (org.pmiops.workbench.model.Workspace)3 QueryResult (com.google.cloud.bigquery.QueryResult)2 Gson (com.google.gson.Gson)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ParticipantCriteria (org.pmiops.workbench.cohortbuilder.ParticipantCriteria)2 TableQueryAndConfig (org.pmiops.workbench.cohortbuilder.TableQueryAndConfig)2 CdrVersion (org.pmiops.workbench.db.model.CdrVersion)2