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.module.spring.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.module.client.MuleClient;
13  import org.mule.security.MuleCredentials;
14  import org.mule.tck.junit4.FunctionalTestCase;
15  import org.mule.transport.NullPayload;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  
20  import org.junit.Test;
21  import org.springframework.security.core.context.SecurityContextHolder;
22  import org.springframework.security.core.context.SecurityContextImpl;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  
28  
29  public class AuthComponentSynchFunctionalTestCase extends FunctionalTestCase
30  {
31  
32      @Override
33      protected String getConfigResources()
34      {
35          return "auth-component-synch-test.xml";
36      }
37  
38      @Override
39      // Clear the security context after each test.
40      public void doTearDown()
41      {
42          SecurityContextHolder.setContext(new SecurityContextImpl());
43      }
44  
45      @Test
46      public void testCaseGoodAuthenticationGoodAuthorisation() throws Exception
47      {
48          MuleClient client = new MuleClient(muleContext);
49          Map props = new HashMap();
50  
51          EncryptionStrategy strategy = muleContext
52              .getSecurityManager()
53              .getEncryptionStrategy("PBE");
54          String header = MuleCredentials.createHeader("marie", "marie", "PBE", strategy);
55          props.put(MuleProperties.MULE_USER_PROPERTY, header);
56          MuleMessage m = client.send("vm://test", "Marie", props);
57          assertNotNull(m);
58          assertTrue(m.getPayload().equals("Marie"));
59      }
60  
61      @Test
62      public void testCaseGoodAuthenticationBadAuthorisation() throws Exception
63      {
64          MuleClient client = new MuleClient(muleContext);
65          Map props = new HashMap();
66  
67          EncryptionStrategy strategy = muleContext
68              .getSecurityManager()
69              .getEncryptionStrategy("PBE");
70          String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
71          props.put(MuleProperties.MULE_USER_PROPERTY, header);
72          MuleMessage m = client.send("vm://test", "Marie", props);
73          assertEquals(NullPayload.getInstance(), m.getPayload());
74      }
75  
76      @Test
77      public void testCaseBadAuthentication() throws Exception
78      {
79          MuleClient client = new MuleClient(muleContext);
80          Map props = new HashMap();
81  
82          EncryptionStrategy strategy = muleContext
83              .getSecurityManager()
84              .getEncryptionStrategy("PBE");
85          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
86          props.put(MuleProperties.MULE_USER_PROPERTY, header);
87          MuleMessage m = client.send("vm://test", "Marie", props);
88          assertNotNull(m.getPayload());
89      }
90  
91  }