Search in sources :

Example 16 with TestUserSessionSource

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);
    }
}
Also used : ConstraintData(com.haulmont.cuba.security.global.ConstraintData) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) Server(com.haulmont.cuba.core.entity.Server) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) LoginWorker(com.haulmont.cuba.security.app.LoginWorker) UserSession(com.haulmont.cuba.security.global.UserSession) Test(org.junit.Test)

Example 17 with TestUserSessionSource

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);
    }
}
Also used : LoginWorker(com.haulmont.cuba.security.app.LoginWorker) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) UserSession(com.haulmont.cuba.security.global.UserSession) Test(org.junit.Test)

Example 18 with TestUserSessionSource

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);
    }
}
Also used : LoginWorker(com.haulmont.cuba.security.app.LoginWorker) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) UserSession(com.haulmont.cuba.security.global.UserSession) Test(org.junit.Test)

Example 19 with TestUserSessionSource

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);
    }
}
Also used : LoginWorker(com.haulmont.cuba.security.app.LoginWorker) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) UserSession(com.haulmont.cuba.security.global.UserSession) Test(org.junit.Test)

Example 20 with TestUserSessionSource

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);
    }
}
Also used : LoginWorker(com.haulmont.cuba.security.app.LoginWorker) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) TestUserSessionSource(com.haulmont.cuba.testsupport.TestUserSessionSource) UserSession(com.haulmont.cuba.security.global.UserSession) Test(org.junit.Test)

Aggregations

TestUserSessionSource (com.haulmont.cuba.testsupport.TestUserSessionSource)24 LoginWorker (com.haulmont.cuba.security.app.LoginWorker)23 UserSession (com.haulmont.cuba.security.global.UserSession)23 Test (org.junit.Test)15 List (java.util.List)8 ArrayList (java.util.ArrayList)7 ConfigStorageService (com.haulmont.cuba.core.app.ConfigStorageService)4 KeyValueEntity (com.haulmont.cuba.core.entity.KeyValueEntity)3 Server (com.haulmont.cuba.core.entity.Server)2 DataService (com.haulmont.cuba.core.app.DataService)1 Executions (com.haulmont.cuba.core.app.execution.Executions)1 UserSessionSource (com.haulmont.cuba.core.global.UserSessionSource)1 ConstraintData (com.haulmont.cuba.security.global.ConstraintData)1