From 942979099d58254e5238408318cee8fb07882f54 Mon Sep 17 00:00:00 2001 From: Igor Dianov Date: Mon, 26 Aug 2019 00:16:50 -0700 Subject: [PATCH] fix: use ProcessRuntime wrapper to start process instance --- .../runtime/RuntimeBundleApplicationTest.java | 134 +++++++++++++++++- 1 file changed, 129 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/activiti/cloud/runtime/RuntimeBundleApplicationTest.java b/src/test/java/org/activiti/cloud/runtime/RuntimeBundleApplicationTest.java index 951e687e..2442aee5 100644 --- a/src/test/java/org/activiti/cloud/runtime/RuntimeBundleApplicationTest.java +++ b/src/test/java/org/activiti/cloud/runtime/RuntimeBundleApplicationTest.java @@ -3,7 +3,10 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -12,15 +15,22 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import org.activiti.api.process.model.ProcessInstance; +import org.activiti.api.process.model.builders.StartProcessPayloadBuilder; +import org.activiti.api.process.model.payloads.StartProcessPayload; +import org.activiti.api.process.runtime.ProcessRuntime; +import org.activiti.api.runtime.shared.security.SecurityManager; import org.activiti.cloud.api.process.model.IntegrationRequest; import org.activiti.cloud.api.process.model.IntegrationResult; import org.activiti.cloud.api.process.model.impl.IntegrationResultImpl; +import org.activiti.core.common.spring.identity.ExtendedInMemoryUserDetailsManager; import org.activiti.engine.RuntimeService; -import org.activiti.engine.runtime.ProcessInstance; import org.awaitility.Awaitility; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.TestConfiguration; @@ -33,6 +43,14 @@ import org.springframework.messaging.Message; import org.springframework.messaging.SubscribableChannel; import org.springframework.messaging.support.MessageBuilder; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.context.SecurityContextImpl; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @@ -45,9 +63,15 @@ public class RuntimeBundleApplicationTest { private static final String CONNECTOR_PROCESS = "ConnectorProcess"; private static final String BUSINESS_KEY = "businessKey"; + @Autowired + private ProcessRuntime processRuntime; + @Autowired private RuntimeService runtimeService; + @Autowired + private TestSecurityUtil securityUtil; + private static CountDownLatch exampleConnectorConsumer; @Before @@ -58,13 +82,15 @@ public void setUp() { @Test public void testExampleConnectorProcess() throws InterruptedException { // given + securityUtil.logInAs("user"); + String businessKey = BUSINESS_KEY; + StartProcessPayload payload = new StartProcessPayloadBuilder().withProcessDefinitionKey(CONNECTOR_PROCESS) + .withBusinessKey(businessKey) + .build(); // when - ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder() - .processDefinitionKey(CONNECTOR_PROCESS) - .businessKey(businessKey) - .start(); + ProcessInstance processInstance = processRuntime.start(payload); // then assertThat(processInstance).as("Should start process instance") .isNotNull(); @@ -130,8 +156,106 @@ public TestIntegrationResultSender testIntegrationResultSender() { return new TestIntegrationResultSender(); } + @Bean + public UserDetailsService myUserDetailsService() { + ExtendedInMemoryUserDetailsManager extendedInMemoryUserDetailsManager = new ExtendedInMemoryUserDetailsManager(); + + List userAuthorities = new ArrayList<>(); + userAuthorities.add(new SimpleGrantedAuthority("ROLE_ACTIVITI_USER")); + userAuthorities.add(new SimpleGrantedAuthority("GROUP_activitiTeam")); + + extendedInMemoryUserDetailsManager.createUser(new User("user", + "password", + userAuthorities)); + + + List adminAuthorities = new ArrayList<>(); + adminAuthorities.add(new SimpleGrantedAuthority("ROLE_ACTIVITI_ADMIN")); + + extendedInMemoryUserDetailsManager.createUser(new User("admin", + "password", + adminAuthorities)); + + List garthAuthorities = new ArrayList<>(); + garthAuthorities.add(new SimpleGrantedAuthority("ROLE_ACTIVITI_USER")); + garthAuthorities.add(new SimpleGrantedAuthority("GROUP_doctor")); + + extendedInMemoryUserDetailsManager.createUser(new User("garth", + "password", + garthAuthorities)); + + //dean has role but no group + List deanAuthorities = new ArrayList<>(); + deanAuthorities.add(new SimpleGrantedAuthority("ROLE_ACTIVITI_USER")); + extendedInMemoryUserDetailsManager.createUser(new User("dean", + "password", + deanAuthorities)); + + return extendedInMemoryUserDetailsManager; + } + } + + @TestConfiguration + public static class TestSecurityUtil { + + private Logger logger = LoggerFactory.getLogger(TestSecurityUtil.class); + + @Autowired + private UserDetailsService userDetailsService; + + @Autowired + private SecurityManager securityManager; + + public void logInAs(String username) { + + UserDetails user = userDetailsService.loadUserByUsername(username); + if (user == null) { + throw new IllegalStateException("User " + username + " doesn't exist, please provide a valid user"); + } + logger.info("> Logged in as: " + username); + SecurityContextHolder.setContext(new SecurityContextImpl(new Authentication() { + @Override + public Collection getAuthorities() { + return user.getAuthorities(); + } + + @Override + public Object getCredentials() { + return user.getPassword(); + } + + @Override + public Object getDetails() { + return user; + } + + @Override + public Object getPrincipal() { + return user; + } + + @Override + public boolean isAuthenticated() { + return true; + } + + @Override + public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { + + } + + @Override + public String getName() { + return user.getUsername(); + } + })); + org.activiti.engine.impl.identity.Authentication.setAuthenticatedUserId(username); + + assertThat(securityManager.getAuthenticatedUserId()).isEqualTo(username); + } } + public static class TestIntegrationResultSender { @Autowired