Finally fixing users and profiles related tests changing the way to define default users
FEA: ItEr76S30PermissionsEnhancements
This commit is contained in:
parent
36414465cf
commit
e91faa776b
6 changed files with 67 additions and 81 deletions
|
|
@ -59,9 +59,8 @@ import static org.libreplan.business.users.entities.UserRole.ROLE_WORK_AND_PROGR
|
|||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.libreplan.business.common.Registry;
|
||||
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.libreplan.business.users.entities.Profile;
|
||||
import org.libreplan.business.users.entities.UserRole;
|
||||
|
||||
|
|
@ -129,12 +128,12 @@ public enum PredefinedProfiles {
|
|||
.create(name, new HashSet<UserRole>(Arrays.asList(roles)));
|
||||
}
|
||||
|
||||
public Profile getFromDB() {
|
||||
try {
|
||||
return Registry.getProfileDAO().findByProfileNameLoadingRoles(name);
|
||||
} catch (InstanceNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Set<UserRole> getRoles() {
|
||||
return new HashSet<UserRole>(Arrays.asList(roles));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,6 @@ import org.libreplan.business.common.Configuration;
|
|||
import org.libreplan.business.common.Registry;
|
||||
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.libreplan.business.users.bootstrap.PredefinedProfiles;
|
||||
import org.libreplan.business.users.entities.Profile;
|
||||
import org.libreplan.business.users.entities.UserRole;
|
||||
|
||||
/**
|
||||
|
|
@ -81,8 +80,7 @@ public enum PredefinedUsers {
|
|||
.getChangedDefaultWssubcontractingPassword();
|
||||
}
|
||||
},
|
||||
MANAGER(null,
|
||||
Arrays.asList(PredefinedProfiles.PROJECT_MANAGER.getFromDB()),
|
||||
MANAGER(null, Arrays.asList(PredefinedProfiles.PROJECT_MANAGER),
|
||||
Configuration.isExampleUsersDisabled()) {
|
||||
@Override
|
||||
public boolean hasChangedDefaultPassword() {
|
||||
|
|
@ -91,22 +89,22 @@ public enum PredefinedUsers {
|
|||
}
|
||||
},
|
||||
HRESOURCES(null, Arrays
|
||||
.asList(PredefinedProfiles.HUMAN_RESOURCES_AND_COSTS_MANAGER
|
||||
.getFromDB()), Configuration.isExampleUsersDisabled()) {
|
||||
.asList(PredefinedProfiles.HUMAN_RESOURCES_AND_COSTS_MANAGER),
|
||||
Configuration.isExampleUsersDisabled()) {
|
||||
@Override
|
||||
public boolean hasChangedDefaultPassword() {
|
||||
return getConfiguration().getChangedDefaultManagerPassword();
|
||||
}
|
||||
},
|
||||
OUTSOURCING(null, Arrays.asList(PredefinedProfiles.OUTSOURCING_MANAGER
|
||||
.getFromDB()), Configuration.isExampleUsersDisabled()) {
|
||||
OUTSOURCING(null, Arrays.asList(PredefinedProfiles.OUTSOURCING_MANAGER),
|
||||
Configuration.isExampleUsersDisabled()) {
|
||||
@Override
|
||||
public boolean hasChangedDefaultPassword() {
|
||||
return getConfiguration().getChangedDefaultManagerPassword();
|
||||
}
|
||||
},
|
||||
REPORTS(null, Arrays.asList(PredefinedProfiles.REPORTS_RESPONSIBLE
|
||||
.getFromDB()), Configuration.isExampleUsersDisabled()) {
|
||||
REPORTS(null, Arrays.asList(PredefinedProfiles.REPORTS_RESPONSIBLE),
|
||||
Configuration.isExampleUsersDisabled()) {
|
||||
@Override
|
||||
public boolean hasChangedDefaultPassword() {
|
||||
return getConfiguration().getChangedDefaultManagerPassword();
|
||||
|
|
@ -135,7 +133,7 @@ public enum PredefinedUsers {
|
|||
|
||||
private Set<UserRole> initialRoles = new HashSet<UserRole>();
|
||||
|
||||
private Set<Profile> initialProfiles = new HashSet<Profile>();
|
||||
private Set<PredefinedProfiles> initialProfiles = new HashSet<PredefinedProfiles>();
|
||||
|
||||
private final boolean userDisabled;
|
||||
|
||||
|
|
@ -145,12 +143,13 @@ public enum PredefinedUsers {
|
|||
}
|
||||
|
||||
private PredefinedUsers(Collection<UserRole> initialUserRoles,
|
||||
Collection<Profile> initialProfiles, boolean userDisabled) {
|
||||
Collection<PredefinedProfiles> initialProfiles, boolean userDisabled) {
|
||||
if (initialUserRoles != null) {
|
||||
this.initialRoles = new HashSet<UserRole>(initialUserRoles);
|
||||
}
|
||||
if (initialProfiles != null) {
|
||||
this.initialProfiles = new HashSet<Profile>(initialProfiles);
|
||||
this.initialProfiles = new HashSet<PredefinedProfiles>(
|
||||
initialProfiles);
|
||||
}
|
||||
this.userDisabled = userDisabled;
|
||||
}
|
||||
|
|
@ -177,7 +176,7 @@ public enum PredefinedUsers {
|
|||
return initialRoles;
|
||||
}
|
||||
|
||||
public Set<Profile> getInitialProfiles() {
|
||||
public Set<PredefinedProfiles> getInitialProfiles() {
|
||||
return initialProfiles;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,8 +21,15 @@
|
|||
|
||||
package org.libreplan.web.users.bootstrap;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.libreplan.business.BootstrapOrder;
|
||||
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.libreplan.business.users.bootstrap.PredefinedProfiles;
|
||||
import org.libreplan.business.users.daos.IProfileDAO;
|
||||
import org.libreplan.business.users.daos.IUserDAO;
|
||||
import org.libreplan.business.users.entities.Profile;
|
||||
import org.libreplan.business.users.entities.User;
|
||||
import org.libreplan.web.users.services.IDBPasswordEncoderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -41,6 +48,9 @@ public class UsersBootstrapInDB implements IUsersBootstrapInDB {
|
|||
@Autowired
|
||||
private IUserDAO userDAO;
|
||||
|
||||
@Autowired
|
||||
private IProfileDAO profileDAO;
|
||||
|
||||
private IDBPasswordEncoderService dbPasswordEncoderService;
|
||||
|
||||
public void setDbPasswordEncoderService(
|
||||
|
|
@ -57,7 +67,7 @@ public class UsersBootstrapInDB implements IUsersBootstrapInDB {
|
|||
for (PredefinedUsers u : PredefinedUsers.values()) {
|
||||
User user = User.create(u.getLoginName(),
|
||||
getEncodedPassword(u), u.getInitialRoles(),
|
||||
u.getInitialProfiles());
|
||||
getProfiles(u.getInitialProfiles()));
|
||||
user.setDisabled(u.isUserDisabled());
|
||||
|
||||
userDAO.save(user);
|
||||
|
|
@ -66,6 +76,18 @@ public class UsersBootstrapInDB implements IUsersBootstrapInDB {
|
|||
|
||||
}
|
||||
|
||||
private Set<Profile> getProfiles(Set<PredefinedProfiles> initialProfiles) {
|
||||
Set<Profile> profiles = new HashSet<Profile>();
|
||||
for (PredefinedProfiles each : initialProfiles) {
|
||||
try {
|
||||
profiles.add(profileDAO.findByProfileName(each.getName()));
|
||||
} catch (InstanceNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return profiles;
|
||||
}
|
||||
|
||||
private String getEncodedPassword(PredefinedUsers u) {
|
||||
|
||||
return dbPasswordEncoderService.encodePassword(u.getClearPassword(),
|
||||
|
|
|
|||
|
|
@ -30,8 +30,6 @@ import static org.libreplan.web.test.WebappGlobalNames.WEBAPP_SPRING_SECURITY_CO
|
|||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.libreplan.business.common.IAdHocTransactionService;
|
||||
import org.libreplan.business.common.IOnTransaction;
|
||||
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
|
||||
import org.libreplan.business.users.bootstrap.IProfileBootstrap;
|
||||
import org.libreplan.business.users.daos.IUserDAO;
|
||||
|
|
@ -39,7 +37,6 @@ import org.libreplan.business.users.entities.User;
|
|||
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
|
||||
import org.libreplan.web.users.bootstrap.PredefinedUsers;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
|
@ -58,62 +55,27 @@ import org.springframework.transaction.annotation.Transactional;
|
|||
public class UsersBootstrapInDBTest {
|
||||
|
||||
@Autowired
|
||||
private IProfileBootstrap profileBootstrap;
|
||||
private IUsersBootstrapInDB usersBootstrap;
|
||||
|
||||
@Autowired
|
||||
private IUsersBootstrapInDB usersBootstrap;
|
||||
private IProfileBootstrap profileBootstrap;
|
||||
|
||||
@Autowired
|
||||
private IUserDAO userDAO;
|
||||
|
||||
@Autowired
|
||||
private IAdHocTransactionService transactionService;
|
||||
|
||||
@Test
|
||||
@Rollback(false)
|
||||
public void testLoadProfiles() {
|
||||
transactionService.runOnAnotherTransaction(new IOnTransaction<Void>() {
|
||||
@Override
|
||||
public Void execute() {
|
||||
profileBootstrap.loadRequiredData();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
public void testMandatoryUsersCreated() throws InstanceNotFoundException {
|
||||
profileBootstrap.loadRequiredData();
|
||||
usersBootstrap.loadRequiredData();
|
||||
|
||||
@Test
|
||||
@Rollback(false)
|
||||
public void testLoadUsers() {
|
||||
transactionService.runOnAnotherTransaction(new IOnTransaction<Void>() {
|
||||
@Override
|
||||
public Void execute() {
|
||||
usersBootstrap.loadRequiredData();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
for (PredefinedUsers u : PredefinedUsers.values()) {
|
||||
|
||||
@Test
|
||||
public void testMandatoryUsersCreated() {
|
||||
transactionService.runOnAnotherTransaction(new IOnTransaction<Void>() {
|
||||
@Override
|
||||
public Void execute() {
|
||||
for (PredefinedUsers u : PredefinedUsers.values()) {
|
||||
try {
|
||||
User user = userDAO.findByLoginName(u.getLoginName());
|
||||
|
||||
User user = userDAO.findByLoginName(u.getLoginName());
|
||||
|
||||
assertEquals(u.getLoginName(), user.getLoginName());
|
||||
assertEquals(u.getInitialRoles(), user.getRoles());
|
||||
|
||||
} catch (InstanceNotFoundException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
assertEquals(u.getLoginName(), user.getLoginName());
|
||||
assertEquals(u.getInitialRoles(), user.getRoles());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,8 +37,8 @@ import org.junit.runner.RunWith;
|
|||
import org.libreplan.business.common.IAdHocTransactionService;
|
||||
import org.libreplan.business.common.IOnTransaction;
|
||||
import org.libreplan.business.scenarios.bootstrap.IScenariosBootstrap;
|
||||
import org.libreplan.business.users.daos.IProfileDAO;
|
||||
import org.libreplan.business.users.entities.Profile;
|
||||
import org.libreplan.business.users.bootstrap.IProfileBootstrap;
|
||||
import org.libreplan.business.users.bootstrap.PredefinedProfiles;
|
||||
import org.libreplan.business.users.entities.UserRole;
|
||||
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
|
||||
import org.libreplan.web.users.bootstrap.PredefinedUsers;
|
||||
|
|
@ -72,10 +72,10 @@ public class DBUserDetailsServiceTest {
|
|||
private IScenariosBootstrap scenariosBootstrap;
|
||||
|
||||
@Autowired
|
||||
private IAdHocTransactionService transactionService;
|
||||
private IProfileBootstrap profileBootstrap;
|
||||
|
||||
@Autowired
|
||||
private IProfileDAO profileDAO;
|
||||
private IAdHocTransactionService transactionService;
|
||||
|
||||
@Before
|
||||
public void loadScenariosBootsrap() {
|
||||
|
|
@ -90,6 +90,7 @@ public class DBUserDetailsServiceTest {
|
|||
@Override
|
||||
public Void execute() {
|
||||
scenariosBootstrap.loadRequiredData();
|
||||
profileBootstrap.loadRequiredData();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
|
@ -116,9 +117,8 @@ public class DBUserDetailsServiceTest {
|
|||
|
||||
userRoles.addAll(u.getInitialRoles());
|
||||
|
||||
Set<Profile> initialProfiles = u.getInitialProfiles();
|
||||
for (Profile profile : initialProfiles) {
|
||||
userRoles.addAll(profile.getRoles());
|
||||
for (PredefinedProfiles each : u.getInitialProfiles()) {
|
||||
userRoles.addAll(each.getRoles());
|
||||
}
|
||||
|
||||
return userRoles;
|
||||
|
|
|
|||
|
|
@ -35,7 +35,8 @@ import org.junit.runner.RunWith;
|
|||
import org.libreplan.business.common.IAdHocTransactionService;
|
||||
import org.libreplan.business.common.IOnTransaction;
|
||||
import org.libreplan.business.scenarios.bootstrap.IScenariosBootstrap;
|
||||
import org.libreplan.business.users.entities.Profile;
|
||||
import org.libreplan.business.users.bootstrap.IProfileBootstrap;
|
||||
import org.libreplan.business.users.bootstrap.PredefinedProfiles;
|
||||
import org.libreplan.business.users.entities.UserRole;
|
||||
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
|
||||
import org.libreplan.web.users.bootstrap.PredefinedUsers;
|
||||
|
|
@ -73,6 +74,9 @@ public class LDAPUserDetailsServiceTest {
|
|||
@Autowired
|
||||
private IScenariosBootstrap scenariosBootstrap;
|
||||
|
||||
@Autowired
|
||||
private IProfileBootstrap profileBootstrap;
|
||||
|
||||
@Autowired
|
||||
private IAdHocTransactionService transactionService;
|
||||
|
||||
|
|
@ -89,6 +93,7 @@ public class LDAPUserDetailsServiceTest {
|
|||
@Override
|
||||
public Void execute() {
|
||||
scenariosBootstrap.loadRequiredData();
|
||||
profileBootstrap.loadRequiredData();
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
|
@ -116,9 +121,8 @@ public class LDAPUserDetailsServiceTest {
|
|||
|
||||
userRoles.addAll(u.getInitialRoles());
|
||||
|
||||
Set<Profile> initialProfiles = u.getInitialProfiles();
|
||||
for (Profile profile : initialProfiles) {
|
||||
userRoles.addAll(profile.getRoles());
|
||||
for (PredefinedProfiles each : u.getInitialProfiles()) {
|
||||
userRoles.addAll(each.getRoles());
|
||||
}
|
||||
|
||||
return userRoles;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue