View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.security;
8   
9   import org.mule.api.EncryptionStrategy;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.api.transport.SessionHandler;
13  import org.mule.module.client.MuleClient;
14  import org.mule.session.MuleSessionHandler;
15  import org.mule.tck.junit4.FunctionalTestCase;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.junit.Test;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertNotNull;
24  
25  /**
26   * Tests multi-user security against a security provider which only authenticates 
27   * a single user at a time (i.e., authentication of a new user overwrites the 
28   * previous authentication).  
29   * 
30   * see EE-979
31   */
32  public class MultiuserSecurityTestCase extends FunctionalTestCase
33  {
34  
35      @Override
36      protected String getConfigResources()
37      {
38          return "multiuser-security-test.xml, singleuser-security-provider.xml";
39      }
40  
41      @Test
42      public void testMultipleAuthentications() throws Exception
43      {
44          MuleClient client = new MuleClient(muleContext);
45          SessionHandler sessionHandler = new MuleSessionHandler();
46          MuleMessage reply;
47          Map props;
48  
49          EncryptionStrategy strategy = muleContext.getSecurityManager().getEncryptionStrategy("PBE");
50          
51          props = new HashMap();
52          props.put(MuleProperties.MULE_USER_PROPERTY, MuleCredentials.createHeader("marie", "marie", "PBE", strategy));
53          reply = client.send("vm://test", "Data1", props);
54          assertNotNull(reply);
55          assertEquals("user = marie, logins = 1, color = bright red", reply.getPayload());
56  
57          props = new HashMap();
58          props.put(MuleProperties.MULE_USER_PROPERTY, MuleCredentials.createHeader("stan", "stan", "PBE", strategy));
59          reply = client.send("vm://test", "Data2", props);
60          assertNotNull(reply);
61          assertEquals("user = stan, logins = 1, color = metallic blue", reply.getPayload());
62          
63          props = new HashMap();
64          props.put(MuleProperties.MULE_USER_PROPERTY, MuleCredentials.createHeader("cindy", "cindy", "PBE", strategy));
65          reply = client.send("vm://test", "Data3", props);
66          assertEquals("user = cindy, logins = 1, color = dark violet", reply.getPayload());
67  
68          props = new HashMap();
69          props.put(MuleProperties.MULE_USER_PROPERTY, MuleCredentials.createHeader("marie", "marie", "PBE", strategy));
70          reply = client.send("vm://test", "Data4", props);
71          assertNotNull(reply);
72          assertEquals("user = marie, logins = 2, color = bright red", reply.getPayload());
73  
74          props = new HashMap();
75          props.put(MuleProperties.MULE_USER_PROPERTY, MuleCredentials.createHeader("marie", "marie", "PBE", strategy));
76          reply = client.send("vm://test", "Data4", props);
77          assertNotNull(reply);
78          assertEquals("user = marie, logins = 3, color = bright red", reply.getPayload());
79  
80          props = new HashMap();
81          props.put(MuleProperties.MULE_USER_PROPERTY, MuleCredentials.createHeader("stan", "stan", "PBE", strategy));
82          reply = client.send("vm://test", "Data2", props);
83          assertNotNull(reply);
84          assertEquals("user = stan, logins = 2, color = metallic blue", reply.getPayload());        
85      }
86  }