View Javadoc

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