fix test in ProgressMonitorServiceTest, extraUser needs to be in group editor
authorKatja Luther <k.luther@bgbm.org>
Tue, 26 Feb 2019 12:58:22 +0000 (13:58 +0100)
committerKatja Luther <k.luther@bgbm.org>
Tue, 26 Feb 2019 12:58:22 +0000 (13:58 +0100)
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/BaseRemotingTest.java
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/RemotingSessionAwareTest.java
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/service/ProgressMonitorServiceTest.java

index b7599c5ed2bae219fcba57bc234da24460ea3dfe..9eaa9a335eae706427228fa435916f7d2c9671d8 100644 (file)
@@ -16,7 +16,6 @@ import org.junit.BeforeClass;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.context.SecurityContextHolder;
-import org.springframework.security.core.context.SecurityContextImpl;
 import org.unitils.database.DatabaseUnitils;
 import org.unitils.database.annotations.Transactional;
 import org.unitils.database.util.TransactionMode;
@@ -27,6 +26,7 @@ import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.application.CdmDataChangeService;
 import eu.etaxonomy.cdm.api.cache.CdmServiceCacher;
 import eu.etaxonomy.cdm.cache.CdmRemoteCacheManager;
+import eu.etaxonomy.cdm.model.common.User;
 import eu.etaxonomy.taxeditor.remoting.cache.ConversationalTransientEntityCacher;
 import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
 import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSource;
@@ -67,6 +67,7 @@ public abstract class BaseRemotingTest extends ThreadedTest {
                     user,
                     password);
         } catch (Exception e) {
+            System.err.println("user: " + user + " password: " + password);
             e.printStackTrace();
            // Assert.fail("Server failed to start. Reason : " + e.getMessage());
         }
@@ -111,15 +112,34 @@ public abstract class BaseRemotingTest extends ThreadedTest {
         //FIXME:Remoting the authentication code should be replaced by a method call which actually
         // does the authentication in the editor code so that the 'real' authentication can be tested
         SecurityContextHolder.clearContext();
-        SecurityContextImpl sc = new SecurityContextImpl();
-        Authentication token = new UsernamePasswordAuthenticationToken(username,password);
+
+        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
         Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
-        authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
-        sc.setAuthentication(authentication);
 
-        SecurityContextHolder.setContext(sc);
+
+        User user = (User) authentication.getPrincipal();
+        /* circumventing problem with hibernate not refreshing the transient collection authorities in this case,
+         * see http://dev.e-taxonomy.eu/trac/ticket/4053 */
+        user.initAuthorities();
+        authentication = new UsernamePasswordAuthenticationToken(user,password, authentication.getAuthorities());
+        SecurityContextHolder.getContext().setAuthentication(authentication);
         CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
 
+
+
+
+
+//        SecurityContextHolder.clearContext();
+//        SecurityContextImpl sc = new SecurityContextImpl();
+//        Authentication token = new UsernamePasswordAuthenticationToken(username,password);
+//        Authentication authentication = getRemoteApplicationController().getAuthenticationManager().authenticate(token);
+//
+//        authentication = new UsernamePasswordAuthenticationToken(username,password, authentication.getAuthorities());
+//        sc.setAuthentication(authentication);
+//
+//        SecurityContextHolder.setContext(sc);
+//        CdmApplicationState.setCurrentSecurityContext(SecurityContextHolder.getContext());
+
     }
 
 
index 2497820b963829ead4f96382bfee2275d48d3415..1ec14d6137a4b15685f17f53177eb6fe0fb3ebc9 100644 (file)
@@ -13,13 +13,13 @@ import java.util.UUID;
 import org.apache.log4j.Logger;
 import org.junit.After;
 import org.junit.Before;
+import org.springframework.security.core.userdetails.UserDetails;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
 
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
 import eu.etaxonomy.cdm.api.service.IUserService;
 import eu.etaxonomy.cdm.model.common.Group;
 import eu.etaxonomy.cdm.model.common.User;
-import eu.etaxonomy.cdm.persistence.dto.MergeResult;
 import eu.etaxonomy.taxeditor.remoting.cache.ConversationalTransientEntityCacher;
 import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
 import eu.etaxonomy.taxeditor.session.MockSessionOwner;
@@ -53,23 +53,25 @@ public abstract class RemotingSessionAwareTest extends BaseRemotingTest {
         sessionOwner.setCdmEntitySession(cdmEntitySession);
         cacher = getCacher(sessionOwner);
 
-        User extraUser = null;
+        UserDetails extraUser = null;
 
         try {
-            extraUser = (User) userService.loadUserByUsername(extraUsername);
+            extraUser = userService.loadUserByUsername(extraUsername);
             Group editorGroup = getRemoteApplicationController().getGroupService().find(editorGroupUuid);
-            if (!extraUser.getGroups().contains(editorGroup)){
-                extraUser.getGroups().add(editorGroup);
-                userService.merge(extraUser, true);
+            if (!((User)extraUser).getGroups().contains(editorGroup)){
+                ((User)extraUser).getGroups().add(editorGroup);
+                userService.merge(((User)extraUser), true);
+
             }
         } catch (UsernameNotFoundException unfe) {
-            User user = User.NewInstance(extraUsername, extraPassword);
+            UserDetails user = User.NewInstance(extraUsername, extraPassword);
             userService.createUser(user);
+            user = userService.load(((User)user).getUuid());
             Group editorGroup = getRemoteApplicationController().getGroupService().find(editorGroupUuid);
-            user = userService.load(user.getUuid());
-            user.getGroups().add(editorGroup);
-            MergeResult<User> result = userService.merge(user, true);
-            user = result.getMergedEntity();
+            ((User)user).getGroups().add(editorGroup);
+            userService.merge(((User)user), true);
+
+
         }
 
 
@@ -84,6 +86,7 @@ public abstract class RemotingSessionAwareTest extends BaseRemotingTest {
 
 
     protected static void authenticateExtraUser() {
+
         authenticate(extraUsername, extraPassword);
     }
 
index 86b8cd180467c797f89cc872bf3e7d30c24ce026..ee0e4c0ea740e2bcdacd471f153068a33b1b52e2 100644 (file)
@@ -18,7 +18,6 @@ import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.core.runtime.SubMonitor;
 import org.junit.After;
 import org.junit.Assert;
-import org.junit.Ignore;
 import org.junit.Test;
 
 import eu.etaxonomy.cdm.api.application.CdmApplicationState;
@@ -69,12 +68,13 @@ public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
     }
 
     @Test
-    @Ignore
+  //  @Ignore
     public void testMonitLongRunningMethodByChangingUser() throws InterruptedException {
 
         IllegalStateException ise = new IllegalStateException("IllegalStateException");
 
         UUID uuid = testService.monitLongRunningMethod(ise, null, 0);
+
         authenticateExtraUser();
         IRemotingProgressMonitor monitor = progressMonitorService.getRemotingMonitor(uuid);
         Assert.assertNull(monitor);