View Javadoc

1   /*
2    * $Id: AuthComponentSynchFunctionalTestCase.java 22735 2011-08-25 16:02:35Z dfeist $
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 static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertTrue;
16  
17  import org.mule.api.EncryptionStrategy;
18  import org.mule.api.MuleMessage;
19  import org.mule.api.config.MuleProperties;
20  import org.mule.api.security.UnauthorisedException;
21  import org.mule.component.ComponentException;
22  import org.mule.module.client.MuleClient;
23  import org.mule.security.MuleCredentials;
24  import org.mule.tck.junit4.FunctionalTestCase;
25  
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.junit.Test;
30  import org.springframework.security.core.context.SecurityContextHolder;
31  import org.springframework.security.core.context.SecurityContextImpl;
32  
33  public class AuthComponentSynchFunctionalTestCase extends FunctionalTestCase
34  {
35  
36      @Override
37      protected String getConfigResources()
38      {
39          return "auth-component-synch-test.xml";
40      }
41  
42      @Override
43      // Clear the security context after each test.
44      public void doTearDown()
45      {
46          SecurityContextHolder.setContext(new SecurityContextImpl());
47      }
48  
49      @Test
50      public void testCaseGoodAuthenticationGoodAuthorisation() throws Exception
51      {
52          MuleClient client = new MuleClient(muleContext);
53          Map props = new HashMap();
54  
55          EncryptionStrategy strategy = muleContext
56              .getSecurityManager()
57              .getEncryptionStrategy("PBE");
58          String header = MuleCredentials.createHeader("marie", "marie", "PBE", strategy);
59          props.put(MuleProperties.MULE_USER_PROPERTY, header);
60          MuleMessage m = client.send("vm://test", "Marie", props);
61          assertNotNull(m);
62          assertTrue(m.getPayload().equals("Marie"));
63      }
64  
65      @Test
66      public void testCaseGoodAuthenticationBadAuthorisation() throws Exception
67      {
68          MuleClient client = new MuleClient(muleContext);
69          Map props = new HashMap();
70  
71          EncryptionStrategy strategy = muleContext
72              .getSecurityManager()
73              .getEncryptionStrategy("PBE");
74          String header = MuleCredentials.createHeader("anon", "anon", "PBE", strategy);
75          props.put(MuleProperties.MULE_USER_PROPERTY, header);
76          MuleMessage result = client.send("vm://test", "Marie", props);
77          assertNotNull(result);
78          assertNotNull(result.getExceptionPayload());
79          assertEquals(ComponentException.class, result.getExceptionPayload().getException().getClass());
80      }
81  
82      @Test
83      public void testCaseBadAuthentication() throws Exception
84      {
85          MuleClient client = new MuleClient(muleContext);
86          Map props = new HashMap();
87  
88          EncryptionStrategy strategy = muleContext
89              .getSecurityManager()
90              .getEncryptionStrategy("PBE");
91          String header = MuleCredentials.createHeader("anonX", "anonX", "PBE", strategy);
92          props.put(MuleProperties.MULE_USER_PROPERTY, header);
93          MuleMessage result = client.send("vm://test", "Marie", props);
94          assertNotNull(result);
95          assertNotNull(result.getExceptionPayload());
96          assertEquals(UnauthorisedException.class, result.getExceptionPayload().getException().getClass());
97      }
98  
99  }