1   /*
2    * $Id: AuthComponentAsynchFunctionalTestCase.java 10789 2008-02-12 20:04:43Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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  
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.acegisecurity.context.SecurityContextHolder;
24  import org.acegisecurity.context.SecurityContextImpl;
25  
26  public class AuthComponentAsynchFunctionalTestCase extends FunctionalTestCase
27  {
28  
29      protected String getConfigResources()
30      {
31          return "auth-component-asynch-test.xml";
32      }
33  
34      // @Override
35      // Clear the security context after each test.
36      public void doTearDown()
37      {
38          SecurityContextHolder.setContext(new SecurityContextImpl());
39      }
40  
41      public void testCaseGoodAuthenticationGoodAuthorisation() throws Exception
42      {
43          MuleClient client = new MuleClient();
44          Map props = new HashMap();
45  
46          EncryptionStrategy strategy = muleContext
47              .getSecurityManager()
48              .getEncryptionStrategy("PBE");
49          String header = MuleCredentials.createHeader("marie", "marie", "PBE", strategy);
50          props.put(MuleProperties.MULE_USER_PROPERTY, header);
51          client.dispatch("vm://test", "Marie", props);
52          MuleMessage m = client.request("vm://output", 3000);
53          assertNotNull(m);
54          assertEquals((String)m.getPayload(), "Marie");
55      }
56  
57      public void testCaseGoodAuthenticationBadAuthorisation() throws Exception
58      {
59          MuleClient client = new MuleClient();
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          client.dispatch("vm://test", "Marie", props);
68          MuleMessage m = client.request("vm://output", 3000);
69          assertNull(m);
70      }
71  
72      public void testCaseBadAuthentication() throws Exception
73      {
74          MuleClient client = new MuleClient();
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          client.dispatch("vm://test", "USD,MTL", props);
83          MuleMessage m = client.request("vm://output", 3000);
84          assertNull(m);
85      }
86  
87  }