1   /*
2    * Copyright 2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.wss4j.callback.acegi;
18  
19  import junit.framework.TestCase;
20  import org.acegisecurity.Authentication;
21  import org.acegisecurity.GrantedAuthority;
22  import org.acegisecurity.GrantedAuthorityImpl;
23  import org.acegisecurity.DisabledException;
24  import org.acegisecurity.context.SecurityContext;
25  import org.acegisecurity.context.SecurityContextHolder;
26  import org.acegisecurity.userdetails.User;
27  import org.acegisecurity.userdetails.UserDetails;
28  import org.acegisecurity.userdetails.UserDetailsService;
29  import org.apache.ws.security.WSUsernameTokenPrincipal;
30  import org.apache.ws.security.WSPasswordCallback;
31  import org.easymock.MockControl;
32  
33  import org.springframework.ws.soap.security.wss4j.callback.UsernameTokenPrincipalCallback;
34  
35  /** @author tareq */
36  public class AcegiDigestPasswordValidationCallbackHandlerTest extends TestCase {
37  
38      private AcegiDigestPasswordValidationCallbackHandler callbackHandler;
39  
40      private GrantedAuthorityImpl grantedAuthority;
41  
42      private UserDetailsService userDetailsService;
43  
44      private MockControl control;
45  
46      private UserDetails user;
47  
48      protected void setUp() throws Exception {
49          callbackHandler = new AcegiDigestPasswordValidationCallbackHandler();
50  
51          grantedAuthority = new GrantedAuthorityImpl("ROLE_1");
52  
53          control = MockControl.createControl(UserDetailsService.class);
54          userDetailsService = (UserDetailsService) control.getMock();
55          userDetailsService.loadUserByUsername("Ernie");
56          callbackHandler.setUserDetailsService(userDetailsService);
57      }
58  
59      protected void tearDown() throws Exception {
60          control.reset();
61      }
62  
63      public void testHandleUsernameTokenPrincipal() throws Exception {
64          user = new User("Ernie", "Bert", true, true, true, true, new GrantedAuthority[]{grantedAuthority});
65          WSUsernameTokenPrincipal principal = new WSUsernameTokenPrincipal("Ernie", true);
66          UsernameTokenPrincipalCallback callback = new UsernameTokenPrincipalCallback(principal);
67          control.setDefaultReturnValue(user);
68          control.replay();
69          callbackHandler.handleUsernameTokenPrincipal(callback);
70          SecurityContext context = SecurityContextHolder.getContext();
71          assertNotNull("SecurityContext must not be null", context);
72          Authentication authentication = context.getAuthentication();
73          assertNotNull("Authentication must not be null", authentication);
74          GrantedAuthority[] authorities = authentication.getAuthorities();
75          assertTrue("GrantedAuthority[] must not be null or empty", (authorities != null && authorities.length > 0));
76          assertEquals("Unexpected authority", grantedAuthority, authorities[0]);
77      }
78  
79      public void testHandleUsernameTokenWithDisabledUser() throws Exception {
80          user = new User("Ernie", "Bert", false, true, true, true, new GrantedAuthority[]{grantedAuthority});
81          WSPasswordCallback callback = new WSPasswordCallback("ID", WSPasswordCallback.USERNAME_TOKEN);
82          control.setDefaultReturnValue(user);
83          control.replay();
84          try {
85              callbackHandler.handleUsernameToken(callback);
86              fail("disabled user authenticated");
87          } catch (DisabledException expected) {
88          }
89      }
90  }