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