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  
16  import java.util.HashMap;
17  import java.util.Map;
18  
19  import org.junit.Test;
20  import org.springframework.security.core.context.SecurityContextHolder;
21  import org.springframework.security.core.context.SecurityContextImpl;
22  
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNull;
26  
27  
28  public class AuthComponentAsynchFunctionalTestCase extends FunctionalTestCase
29  {
30  
31      @Override
32      protected String getConfigResources()
33      {
34          return "auth-component-asynch-test.xml";
35      }
36  
37      @Override
38      // Clear the security context after each test.
39      public void doTearDown()
40      {
41          SecurityContextHolder.setContext(new SecurityContextImpl());
42      }
43  
44      @Test
45      public void testCaseGoodAuthenticationGoodAuthorisation() throws Exception
46      {
47          MuleClient client = new MuleClient(muleContext);
48          Map props = new HashMap();
49  
50          EncryptionStrategy strategy = muleContext
51              .getSecurityManager()
52              .getEncryptionStrategy("PBE");
53          String header = MuleCredentials.createHeader("marie", "marie", "PBE", strategy);
54          props.put(MuleProperties.MULE_USER_PROPERTY, header);
55          client.dispatch("vm://test", "Marie", props);
56          MuleMessage m = client.request("vm://output", 3000);
57          assertNotNull(m);
58          assertEquals((String)m.getPayload(), "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          client.dispatch("vm://test", "Marie", props);
73          MuleMessage m = client.request("vm://output", 3000);
74          assertNull(m);
75      }
76  
77      @Test
78      public void testCaseBadAuthentication() throws Exception
79      {
80          MuleClient client = new MuleClient(muleContext);
81          Map props = new HashMap();
82  
83          EncryptionStrategy strategy = muleContext
84              .getSecurityManager()
85              .getEncryptionStrategy("PBE");
86          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
87          props.put(MuleProperties.MULE_USER_PROPERTY, header);
88          client.dispatch("vm://test", "USD,MTL", props);
89          MuleMessage m = client.request("vm://output", 3000);
90          assertNull(m);
91      }
92  
93  }