use of org.pmiops.workbench.model.UserRole in project workbench by all-of-us.
the class WorkspacesControllerTest method testCloneWorkspace.
@Test
public void testCloneWorkspace() throws Exception {
Workspace workspace = createDefaultWorkspace();
workspace = workspacesController.createWorkspace(workspace).getBody();
// The original workspace is shared with one other user.
User writerUser = new User();
writerUser.setEmail("writerfriend@gmail.com");
writerUser.setUserId(124L);
writerUser.setFreeTierBillingProjectName("TestBillingProject2");
writerUser.setDisabled(false);
writerUser = userDao.save(writerUser);
ShareWorkspaceRequest shareWorkspaceRequest = new ShareWorkspaceRequest();
shareWorkspaceRequest.setWorkspaceEtag(workspace.getEtag());
UserRole creator = new UserRole();
creator.setEmail(LOGGED_IN_USER_EMAIL);
creator.setRole(WorkspaceAccessLevel.OWNER);
shareWorkspaceRequest.addItemsItem(creator);
UserRole writer = new UserRole();
writer.setEmail(writerUser.getEmail());
writer.setRole(WorkspaceAccessLevel.WRITER);
shareWorkspaceRequest.addItemsItem(writer);
when(fireCloudService.updateWorkspaceACL(anyString(), anyString(), anyListOf(WorkspaceACLUpdate.class))).thenReturn(new WorkspaceACLUpdateResponseList());
workspacesController.shareWorkspace(workspace.getNamespace(), workspace.getName(), shareWorkspaceRequest);
stubGetWorkspace(workspace.getNamespace(), workspace.getName(), LOGGED_IN_USER_EMAIL, WorkspaceAccessLevel.OWNER);
CloneWorkspaceRequest req = new CloneWorkspaceRequest();
Workspace modWorkspace = new Workspace();
modWorkspace.setName("cloned");
modWorkspace.setNamespace("cloned-ns");
ResearchPurpose modPurpose = new ResearchPurpose();
modPurpose.setAncestry(true);
modWorkspace.setResearchPurpose(modPurpose);
req.setWorkspace(modWorkspace);
stubGetWorkspace(modWorkspace.getNamespace(), modWorkspace.getName(), LOGGED_IN_USER_EMAIL, WorkspaceAccessLevel.OWNER);
Workspace workspace2 = workspacesController.cloneWorkspace(workspace.getNamespace(), workspace.getId(), req).getBody().getWorkspace();
assertWithMessage("get and clone responses are inconsistent").that(workspace2).isEqualTo(workspacesController.getWorkspace(workspace2.getNamespace(), workspace2.getId()).getBody().getWorkspace());
assertThat(workspace2.getName()).isEqualTo(modWorkspace.getName());
assertThat(workspace2.getNamespace()).isEqualTo(modWorkspace.getNamespace());
assertThat(workspace2.getResearchPurpose()).isEqualTo(modPurpose);
// Original description should have been copied.
assertThat(workspace2.getDescription()).isEqualTo(workspace.getDescription());
// User roles should *not* be copied.
assertThat(workspace2.getUserRoles().size()).isEqualTo(1);
assertThat(workspace2.getUserRoles().get(0).getRole()).isEqualTo(WorkspaceAccessLevel.OWNER);
}
use of org.pmiops.workbench.model.UserRole in project workbench by all-of-us.
the class WorkspacesController method shareWorkspace.
@Override
public ResponseEntity<ShareWorkspaceResponse> shareWorkspace(String workspaceNamespace, String workspaceId, ShareWorkspaceRequest request) {
if (Strings.isNullOrEmpty(request.getWorkspaceEtag())) {
throw new BadRequestException("Missing required update field 'workspaceEtag'");
}
org.pmiops.workbench.db.model.Workspace dbWorkspace = workspaceService.getRequired(workspaceNamespace, workspaceId);
int version = Etags.toVersion(request.getWorkspaceEtag());
if (dbWorkspace.getVersion() != version) {
throw new ConflictException("Attempted to modify user roles with outdated workspace etag");
}
Set<WorkspaceUserRole> dbUserRoles = new HashSet<WorkspaceUserRole>();
for (UserRole user : request.getItems()) {
WorkspaceUserRole newUserRole = new WorkspaceUserRole();
User newUser = userDao.findUserByEmail(user.getEmail());
if (newUser == null) {
throw new BadRequestException(String.format("User %s doesn't exist", user.getEmail()));
}
newUserRole.setUser(newUser);
newUserRole.setRole(user.getRole());
dbUserRoles.add(newUserRole);
}
// This automatically enforces owner role.
dbWorkspace = workspaceService.updateUserRoles(dbWorkspace, dbUserRoles);
ShareWorkspaceResponse resp = new ShareWorkspaceResponse();
resp.setWorkspaceEtag(Etags.fromVersion(dbWorkspace.getVersion()));
return ResponseEntity.ok(resp);
}
Aggregations