1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }