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();
}
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);
}
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
}
}
}
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
}
}
}
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;
}
Aggregations