1   /*
2    * Copyright 2005-2012 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 java.util.Collection;
20  import java.util.Collections;
21  
22  import org.springframework.security.core.Authentication;
23  import org.springframework.security.core.GrantedAuthority;
24  import org.springframework.security.core.authority.SimpleGrantedAuthority;
25  import org.springframework.security.core.context.SecurityContext;
26  import org.springframework.security.core.context.SecurityContextHolder;
27  import org.springframework.security.core.userdetails.User;
28  import org.springframework.security.core.userdetails.UserDetails;
29  import org.springframework.security.core.userdetails.UserDetailsService;
30  
31  import org.apache.ws.security.WSUsernameTokenPrincipal;
32  import org.junit.Assert;
33  import org.junit.Before;
34  import org.junit.Test;
35  
36  import static org.easymock.EasyMock.*;
37  
38  /** @author tareq */
39  public class SpringSecurityPasswordValidationCallbackHandlerTest {
40  
41      private SpringSecurityPasswordValidationCallbackHandler callbackHandler;
42  
43      private SimpleGrantedAuthority grantedAuthority;
44  
45      private UsernameTokenPrincipalCallback callback;
46  
47      private UserDetails user;
48  
49      @Before
50      public void setUp() throws Exception {
51          callbackHandler = new SpringSecurityPasswordValidationCallbackHandler();
52  
53          grantedAuthority = new SimpleGrantedAuthority("ROLE_1");
54          user = new User("Ernie", "Bert", true, true, true, true, Collections.singleton(grantedAuthority));
55  
56          WSUsernameTokenPrincipal principal = new WSUsernameTokenPrincipal("Ernie", true);
57          callback = new UsernameTokenPrincipalCallback(principal);
58      }
59  
60      @Test
61      public void testHandleUsernameTokenPrincipal() throws Exception {
62          UserDetailsService userDetailsService = createMock(UserDetailsService.class);
63          callbackHandler.setUserDetailsService(userDetailsService);
64  
65          expect(userDetailsService.loadUserByUsername("Ernie")).andReturn(user).anyTimes();
66  
67          replay(userDetailsService);
68  
69          callbackHandler.handleUsernameTokenPrincipal(callback);
70          SecurityContext context = SecurityContextHolder.getContext();
71          Assert.assertNotNull("SecurityContext must not be null", context);
72          Authentication authentication = context.getAuthentication();
73          Assert.assertNotNull("Authentication must not be null", authentication);
74          Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
75          Assert.assertTrue("GrantedAuthority[] must not be null or empty",
76                  (authorities != null && authorities.size() > 0));
77          Assert.assertEquals("Unexpected authority", grantedAuthority, authorities.iterator().next());
78  
79          verify(userDetailsService);
80      }
81  }