@Test public void canUserEditApplicationTest() { // Initial setup User user = getUser(); userService.add(user); Organization organization = createOrganization(); Category category = createCategory(organization); organization.getCategories().add(category); Application application = createApplication(category, "Test Application", AppState.GROUP_PUBLISH); Group group = createGroup(organization); group.getOwnedApplications().add(application); entityManager.flush(); createUserDomain(user, group.getId(), DomainType.GROUP, UserRole.ROLE_GROUP_ADMIN); entityManager.flush(); assertTrue(userService.canUserEditApplication(user.getId(), application.getId())); // Reset userService.delete(user.getId()); ReflectionTestUtils.setField(this, "user", null); entityManager.flush(); // Test if org admin can edit application user = getUser(); userService.add(user); entityManager.flush(); createUserDomain(user, organization.getId(), DomainType.ORGANIZATION, UserRole.ROLE_ORG_ADMIN); entityManager.flush(); assertTrue(userService.canUserEditApplication(user.getId(), application.getId())); // Reset userService.delete(user.getId()); ReflectionTestUtils.setField(this, "user", null); entityManager.flush(); // Test user is org user user = getUser(); userService.add(user); entityManager.flush(); createUserDomain(user, organization.getId(), DomainType.ORGANIZATION, UserRole.ROLE_ORG_USER); entityManager.flush(); assertFalse(userService.canUserEditApplication(user.getId(), application.getId())); // Reset userService.delete(user.getId()); ReflectionTestUtils.setField(this, "user", null); entityManager.flush(); // Test user is not part of organization and not group admin user = getUser(); userService.add(user); entityManager.flush(); assertFalse(userService.canUserEditApplication(user.getId(), application.getId())); }
@Test public void addUserToOrganizationTest() { User user = getUser(UserRole.ROLE_ORG_USER, UserRole.ROLE_GROUP_USER); Organization newOrganization = new Organization(); newOrganization.setName("New Organization"); organizationService.add(newOrganization); organizationService.getAll(); userService.addUserToOrganization(user, newOrganization.getId(), UserRole.ROLE_ORG_USER); List<Organization> organizations = userService.getOrganizations(user); assertTrue(organizations.size() == 2); assertTrue(userService.isUserInOrganization(user, newOrganization, UserRole.ROLE_ORG_USER)); }
@Test public void isUserInDomainTest() { User user = getUser(UserRole.ROLE_ORG_USER, UserRole.ROLE_GROUP_USER); List<Group> groups = userService.getGroups(user); for (Group group : groups) { boolean isInDomain = userService.isUserInDomain( user, group.getId(), DomainType.GROUP, UserRole.ROLE_GROUP_USER); assertTrue(isInDomain); } List<Organization> organizations = userService.getOrganizations(user); for (Organization organization : organizations) { boolean isInDomain = userService.isUserInDomain( user, organization.getId(), DomainType.ORGANIZATION, UserRole.ROLE_ORG_USER); assertTrue(isInDomain); } }
private User getUser(UserRole organizationUserRole, UserRole groupUserRole) { User user = getUser(); Role orgRole = null; if (organizationUserRole != null) { orgRole = roleService.getRoleByAuthority(organizationUserRole.name()); user.getRoles().add(orgRole); } Role groupRole = null; if (groupUserRole != null) { groupRole = roleService.getRoleByAuthority(groupUserRole.name()); user.getRoles().add(groupRole); } Organization organization = createOrganization(); Application application = createApplication( organization.getCategories().get(0), "Test Application", AppState.GROUP_PUBLISH); Application application2 = createApplication( organization.getCategories().get(0), "Test Application 2", AppState.ORGANIZATION_PUBLISH); Group group = createGroup(organization); group.getOwnedApplications().add(application); group.getOwnedApplications().add(application2); organization.getGroups().add(group); userService.save(user); organizationService.getAll(); if (groupRole != null) { UserDomain userDomainGroup = new UserDomain(); userDomainGroup.setUser(user); userDomainGroup.setDomainId(group.getId()); userDomainGroup.setDomainType(DomainType.GROUP); userDomainGroup.setRole(groupRole); userDomainGroup.setDomainId(group.getId()); user.getUserDomains().add(userDomainGroup); } if (orgRole != null) { UserDomain userDomainOrg = new UserDomain(); userDomainOrg.setUser(user); userDomainOrg.setDomainId(group.getId()); userDomainOrg.setDomainType(DomainType.ORGANIZATION); userDomainOrg.setRole(orgRole); userDomainOrg.setDomainId(organization.getId()); user.getUserDomains().add(userDomainOrg); } userService.save(user); entityManager.flush(); return user; }