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;
18  
19  import junit.framework.TestCase;
20  import org.apache.ws.security.WSUsernameTokenPrincipal;
21  import org.easymock.MockControl;
22  
23  import org.springframework.security.Authentication;
24  import org.springframework.security.GrantedAuthority;
25  import org.springframework.security.GrantedAuthorityImpl;
26  import org.springframework.security.context.SecurityContext;
27  import org.springframework.security.context.SecurityContextHolder;
28  import org.springframework.security.userdetails.User;
29  import org.springframework.security.userdetails.UserDetails;
30  import org.springframework.security.userdetails.UserDetailsService;
31  
32  /** @author tareq */
33  public class SpringDigestPasswordValidationCallbackHandlerTest extends TestCase {
34  
35      private SpringDigestPasswordValidationCallbackHandler callbackHandler;
36  
37      private GrantedAuthorityImpl grantedAuthority;
38  
39      private UserDetailsService userDetailsService;
40  
41      private MockControl control;
42  
43      private WSUsernameTokenPrincipal principal;
44  
45      private UsernameTokenPrincipalCallback callback;
46  
47      private UserDetails user;
48  
49      protected void setUp() throws Exception {
50          callbackHandler = new SpringDigestPasswordValidationCallbackHandler();
51  
52          grantedAuthority = new GrantedAuthorityImpl("ROLE_1");
53          user = new User("Ernie", "Bert", true, true, true, true, new GrantedAuthority[]{grantedAuthority});
54  
55          control = MockControl.createControl(UserDetailsService.class);
56          userDetailsService = (UserDetailsService) control.getMock();
57          userDetailsService.loadUserByUsername("Ernie");
58          control.setDefaultReturnValue(user);
59          control.replay();
60          callbackHandler.setUserDetailsService(userDetailsService);
61  
62          principal = new WSUsernameTokenPrincipal("Ernie", true);
63          callback = new UsernameTokenPrincipalCallback(principal);
64  
65      }
66  
67      public void testHandleUsernameTokenPrincipal() throws Exception {
68          callbackHandler.handleUsernameTokenPrincipal(callback);
69          SecurityContext context = SecurityContextHolder.getContext();
70          assertNotNull("SecurityContext must not be null", context);
71          Authentication authentication = context.getAuthentication();
72          assertNotNull("Authentication must not be null", authentication);
73          GrantedAuthority[] authorities = authentication.getAuthorities();
74          assertTrue("GrantedAuthority[] must not be null or empty", (authorities != null && authorities.length > 0));
75          assertEquals("Unexpected authority", grantedAuthority, authorities[0]);
76      }
77  }