View Javadoc

1   /*
2    * $Id: AuthComponentSynchFunctionalTestCase.java 20477 2010-12-06 23:38:52Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.spring.security;
12  
13  import org.mule.api.EncryptionStrategy;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.module.client.MuleClient;
17  import org.mule.security.MuleCredentials;
18  import org.mule.tck.FunctionalTestCase;
19  import org.mule.transport.NullPayload;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.springframework.security.core.context.SecurityContextHolder;
25  import org.springframework.security.core.context.SecurityContextImpl;
26  
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      public void testCaseGoodAuthenticationGoodAuthorisation() throws Exception
44      {
45          MuleClient client = new MuleClient(muleContext);
46          Map props = new HashMap();
47  
48          EncryptionStrategy strategy = muleContext
49              .getSecurityManager()
50              .getEncryptionStrategy("PBE");
51          String header = MuleCredentials.createHeader("marie", "marie", "PBE", strategy);
52          props.put(MuleProperties.MULE_USER_PROPERTY, header);
53          MuleMessage m = client.send("vm://test", "Marie", props);
54          assertNotNull(m);
55          assertTrue(m.getPayload().equals("Marie"));
56      }
57  
58      public void testCaseGoodAuthenticationBadAuthorisation() throws Exception
59      {
60          MuleClient client = new MuleClient(muleContext);
61          Map props = new HashMap();
62  
63          EncryptionStrategy strategy = muleContext
64              .getSecurityManager()
65              .getEncryptionStrategy("PBE");
66          String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
67          props.put(MuleProperties.MULE_USER_PROPERTY, header);
68          MuleMessage m = client.send("vm://test", "Marie", props);
69          assertEquals(NullPayload.getInstance(), m.getPayload());
70      }
71  
72      public void testCaseBadAuthentication() throws Exception
73      {
74          MuleClient client = new MuleClient(muleContext);
75          Map props = new HashMap();
76  
77          EncryptionStrategy strategy = muleContext
78              .getSecurityManager()
79              .getEncryptionStrategy("PBE");
80          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
81          props.put(MuleProperties.MULE_USER_PROPERTY, header);
82          MuleMessage m = client.send("vm://test", "Marie", props);
83          assertNotNull(m.getPayload());
84      }
85  
86  }