Fixed some users related tests due to previous patch

FEA: ItEr76S30PermissionsEnhancements
This commit is contained in:
Manuel Rego Casasnovas 2012-06-20 16:55:59 +02:00
parent b26430979c
commit 909a007042
7 changed files with 55 additions and 22 deletions

View file

@ -43,8 +43,10 @@ public class ProfilesBootstrap implements IProfileBootstrap {
@Transactional
public void loadRequiredData() {
if (profileDAO.list(Profile.class).isEmpty()) {
for (PredefinedProfiles profile : PredefinedProfiles.values()) {
profileDAO.save(profile.createProfile());
for (PredefinedProfiles each : PredefinedProfiles.values()) {
Profile profile = each.createProfile();
profileDAO.save(profile);
profile.dontPoseAsTransientObjectAnymore();
}
}
}

View file

@ -21,6 +21,7 @@
package org.libreplan.web.users.bootstrap;
import org.libreplan.business.BootstrapOrder;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.entities.User;
import org.libreplan.web.users.services.IDBPasswordEncoderService;
@ -34,6 +35,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Manuel Rego Casasnovas <rego@igalia.com>
*/
@Transactional
@BootstrapOrder(1)
public class UsersBootstrapInDB implements IUsersBootstrapInDB {
@Autowired

View file

@ -21,8 +21,7 @@
package org.libreplan.web.users.services;
import static org.libreplan.web.I18nHelper._;
import java.text.MessageFormat;
import java.util.Set;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
@ -63,8 +62,8 @@ public class DBUserDetailsService implements UserDetailsService {
try {
user = userDAO.findByLoginName(loginName);
} catch (InstanceNotFoundException e) {
throw new UsernameNotFoundException(_(
"User with username '{0}': not found", loginName));
throw new UsernameNotFoundException(MessageFormat.format(
"User with username {0}: not found", loginName));
}
Scenario scenario = user.getLastConnectedScenario();

View file

@ -18,8 +18,7 @@
*/
package org.libreplan.web.users.services;
import static org.libreplan.web.I18nHelper._;
import java.text.MessageFormat;
import java.util.Set;
import org.libreplan.business.common.exceptions.InstanceNotFoundException;
@ -62,8 +61,8 @@ public class LDAPUserDetailsService implements UserDetailsService {
try {
user = userDAO.findByLoginName(loginName);
} catch (InstanceNotFoundException e) {
throw new UsernameNotFoundException(_("User with username "
+ "'{0}': not found", loginName));
throw new UsernameNotFoundException(MessageFormat.format(
"User with username {0}: not found", loginName));
}
Scenario scenario = user.getLastConnectedScenario();

View file

@ -31,11 +31,13 @@ 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.exceptions.InstanceNotFoundException;
import org.libreplan.business.users.bootstrap.IProfileBootstrap;
import org.libreplan.business.users.daos.IUserDAO;
import org.libreplan.business.users.entities.User;
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
import org.libreplan.web.users.bootstrap.MandatoryUser;
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;
@ -53,6 +55,9 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional
public class UsersBootstrapInDBTest {
@Autowired
private IProfileBootstrap profileBootstrap;
@Autowired
private IUsersBootstrapInDB usersBootstrap;
@ -60,16 +65,14 @@ public class UsersBootstrapInDBTest {
private IUserDAO userDAO;
@Test
@Rollback(false)
public void testMandatoryUsersCreated() throws InstanceNotFoundException {
profileBootstrap.loadRequiredData();
checkLoadRequiredData();
/*
* Load data again to verify that a second load does not cause
* problems.
*/
checkLoadRequiredData();
checkLoadRequiredData();
// Load data again to verify that a second load does not cause problems
checkLoadRequiredData();
}
private void checkLoadRequiredData() throws InstanceNotFoundException {
@ -82,6 +85,7 @@ public class UsersBootstrapInDBTest {
assertEquals(u.getLoginName(), user.getLoginName());
assertEquals(u.getInitialRoles(), user.getRoles());
assertEquals(u.getInitialProfiles(), user.getProfiles());
}

View file

@ -37,6 +37,7 @@ 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.entities.UserRole;
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
import org.libreplan.web.users.bootstrap.MandatoryUser;
@ -96,17 +97,29 @@ public class DBUserDetailsServiceTest {
for (MandatoryUser u : MandatoryUser.values()) {
UserDetails userDetails =
userDetailsService.loadUserByUsername(u.getLoginName());
UserDetails userDetails = userDetailsService.loadUserByUsername(u
.getLoginName());
assertEquals(u.getLoginName(), userDetails.getUsername());
assertEquals(u.getInitialRoles(), getUserRoles(userDetails));
assertEquals(getUserRoles(u), getUserRoles(userDetails));
}
}
private Object getUserRoles(MandatoryUser u) {
Set<UserRole> userRoles = new HashSet<UserRole>();
userRoles.addAll(u.getInitialRoles());
Set<Profile> initialProfiles = u.getInitialProfiles();
for (Profile profile : initialProfiles) {
userRoles.addAll(profile.getRoles());
}
return userRoles;
}
private Set<UserRole> getUserRoles(UserDetails userDetails) {
Set<UserRole> userRoles = new HashSet<UserRole>();

View file

@ -35,6 +35,7 @@ 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.entities.UserRole;
import org.libreplan.web.users.bootstrap.IUsersBootstrapInDB;
import org.libreplan.web.users.bootstrap.MandatoryUser;
@ -104,12 +105,25 @@ public class LDAPUserDetailsServiceTest {
assertEquals(u.getLoginName(), userDetails.getUsername());
assertEquals(u.getInitialRoles(), getUserRoles(userDetails));
assertEquals(getUserRoles(u), getUserRoles(userDetails));
}
}
private Object getUserRoles(MandatoryUser u) {
Set<UserRole> userRoles = new HashSet<UserRole>();
userRoles.addAll(u.getInitialRoles());
Set<Profile> initialProfiles = u.getInitialProfiles();
for (Profile profile : initialProfiles) {
userRoles.addAll(profile.getRoles());
}
return userRoles;
}
private Set<UserRole> getUserRoles(UserDetails userDetails) {
Set<UserRole> userRoles = new HashSet<UserRole>();