use of com.haulmont.cuba.testsupport.TestUserSessionSource in project cuba by cuba-platform.
the class ConstraintTest method test.
@Test
public void test() throws LoginException {
LoginWorker lw = AppBeans.get(LoginWorker.NAME);
UserSession userSession = lw.login(USER_LOGIN, passwordEncryption.getPlainHash(USER_PASSW), Locale.getDefault());
assertNotNull(userSession);
List<ConstraintData> constraints = userSession.getConstraints("sys$Server");
assertEquals(2, constraints.size());
List<ConstraintData> roleConstraints = userSession.getConstraints("sec$UserRole");
assertEquals(1, roleConstraints.size());
UserSessionSource uss = AppBeans.get(UserSessionSource.class);
UserSession savedUserSession = uss.getUserSession();
((TestUserSessionSource) uss).setUserSession(userSession);
try {
DataManager dm = AppBeans.get(DataManager.NAME);
LoadContext loadContext = new LoadContext(Server.class).setQuery(new LoadContext.Query("select s from sys$Server s"));
List<Server> list = dm.loadList(loadContext);
for (Server server : list) {
if (server.getId().equals(serverId))
fail("Constraints have not taken effect for some reason");
}
// test constraint that contains session parameter
loadContext = new LoadContext(UserRole.class).setQuery(new LoadContext.Query("select ur from sec$UserRole ur"));
List<UserRole> userRoles = dm.loadList(loadContext);
if (!userRoles.isEmpty()) {
fail("Constraint with session attribute failed");
}
} finally {
((TestUserSessionSource) uss).setUserSession(savedUserSession);
}
}
use of com.haulmont.cuba.testsupport.TestUserSessionSource in project cuba by cuba-platform.
the class DataManagerCommitConstraintTest method testUpdateConstraintNotPassed.
@Test
public void testUpdateConstraintNotPassed() throws LoginException {
LoginWorker lw = AppBeans.get(LoginWorker.NAME);
DataManager dataManager = AppBeans.get(DataManager.NAME);
UserSession userSession = lw.login("constraintuserupdate", passwordEncryption.getPlainHash(PASSWORD), Locale.getDefault());
assertNotNull(userSession);
UserSessionSource uss = AppBeans.get(UserSessionSource.class);
UserSession savedUserSession = uss.getUserSession();
((TestUserSessionSource) uss).setUserSession(userSession);
try {
dataManager = dataManager.secure();
User user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate1.getId()).setView(View.LOCAL));
user.setName("newName");
dataManager.commit(user);
fail();
} catch (RowLevelSecurityException e) {
User user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate1.getId()).setView(View.LOCAL));
assertEquals(user.getName(), "oldName");
} finally {
((TestUserSessionSource) uss).setUserSession(savedUserSession);
}
}
use of com.haulmont.cuba.testsupport.TestUserSessionSource in project cuba by cuba-platform.
the class DataManagerCommitConstraintTest method testUpdateConstraintPassed.
@Test
public void testUpdateConstraintPassed() throws LoginException {
LoginWorker lw = AppBeans.get(LoginWorker.NAME);
DataManager dataManager = AppBeans.get(DataManager.NAME);
UserSession userSession = lw.login("constraintuserupdate", passwordEncryption.getPlainHash(PASSWORD), Locale.getDefault());
assertNotNull(userSession);
UserSessionSource uss = AppBeans.get(UserSessionSource.class);
UserSession savedUserSession = uss.getUserSession();
((TestUserSessionSource) uss).setUserSession(userSession);
try {
dataManager = dataManager.secure();
User user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate2.getId()).setView(View.LOCAL));
user.setName("newName");
dataManager.commit(user);
user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate2.getId()).setView(View.LOCAL));
assertEquals(user.getName(), "newName");
user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate3.getId()).setView(View.LOCAL));
user.setName("newName");
user.setActive(true);
dataManager.commit(user);
user = dataManager.load(new LoadContext<>(User.class).setId(testUserUpdate3.getId()).setView(View.LOCAL));
assertEquals(user.getName(), "newName");
} finally {
((TestUserSessionSource) uss).setUserSession(savedUserSession);
}
}
use of com.haulmont.cuba.testsupport.TestUserSessionSource in project cuba by cuba-platform.
the class DataManagerCommitConstraintTest method testDeleteConstraintPassed.
@Test
public void testDeleteConstraintPassed() throws LoginException {
LoginWorker lw = AppBeans.get(LoginWorker.NAME);
DataManager dataManager = AppBeans.get(DataManager.NAME);
UserSession userSession = lw.login("constraintuserupdate", passwordEncryption.getPlainHash(PASSWORD), Locale.getDefault());
assertNotNull(userSession);
UserSessionSource uss = AppBeans.get(UserSessionSource.class);
UserSession savedUserSession = uss.getUserSession();
((TestUserSessionSource) uss).setUserSession(userSession);
try {
dataManager = dataManager.secure();
User user = dataManager.load(new LoadContext<>(User.class).setId(testUserDelete2.getId()).setView(View.LOCAL));
dataManager.remove(user);
user = dataManager.load(new LoadContext<>(User.class).setId(testUserDelete2.getId()).setView(View.LOCAL));
assertNull(user);
} finally {
((TestUserSessionSource) uss).setUserSession(savedUserSession);
}
}
use of com.haulmont.cuba.testsupport.TestUserSessionSource in project cuba by cuba-platform.
the class DistinctConstraintTest method test.
@Test
public void test() throws LoginException {
LoginWorker lw = AppBeans.get(LoginWorker.NAME);
UserSession userSession = lw.login(USER_LOGIN, passwordEncryption.getPlainHash(USER_PASSW), Locale.getDefault());
assertNotNull(userSession);
UserSessionSource uss = AppBeans.get(UserSessionSource.class);
UserSession savedUserSession = uss.getUserSession();
((TestUserSessionSource) uss).setUserSession(userSession);
try {
DataManager dm = AppBeans.get(DataManager.NAME);
LoadContext<User> loadContext = new LoadContext<>(User.class).setId(user2Id).setView(View.LOCAL);
User user = dm.load(loadContext);
assertNotNull(user);
} finally {
((TestUserSessionSource) uss).setUserSession(savedUserSession);
}
}
Aggregations