|
8 | 8 |
|
9 | 9 | import org.junit.Test; |
10 | 10 | import org.mockito.Mockito; |
| 11 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
11 | 12 | import org.springframework.security.core.Authentication; |
12 | 13 | import org.springframework.security.core.authority.AuthorityUtils; |
13 | 14 | import org.springframework.security.core.userdetails.User; |
@@ -59,4 +60,50 @@ public void shouldExtractAuthenticationWhenUserDetailsProvided() throws Exceptio |
59 | 60 |
|
60 | 61 | assertEquals("ROLE_SPAM", authentication.getAuthorities().iterator().next().toString()); |
61 | 62 | } |
| 63 | + |
| 64 | +@Test |
| 65 | +public void shouldExtractWithDefaultUsernameClaimWhenNotSet() throws Exception { |
| 66 | +Map<String, Object> map = new HashMap<String, Object>(); |
| 67 | +map.put(UserAuthenticationConverter.USERNAME, "test_user"); |
| 68 | + |
| 69 | +Authentication authentication = converter.extractAuthentication(map); |
| 70 | + |
| 71 | +assertEquals("test_user", authentication.getPrincipal()); |
| 72 | +} |
| 73 | + |
| 74 | +@Test |
| 75 | +public void shouldConvertUserWithDefaultUsernameClaimWhenNotSet() throws Exception { |
| 76 | +Authentication authentication = new UsernamePasswordAuthenticationToken("test_user", ""); |
| 77 | + |
| 78 | +Map<String, ?> map = converter.convertUserAuthentication(authentication); |
| 79 | + |
| 80 | +assertEquals("test_user", map.get(UserAuthenticationConverter.USERNAME)); |
| 81 | +} |
| 82 | + |
| 83 | +@Test |
| 84 | +public void shouldExtractWithCustomUsernameClaimWhenSet() throws Exception { |
| 85 | +String customUserClaim = "custom_user_name"; |
| 86 | +DefaultUserAuthenticationConverter converter = new DefaultUserAuthenticationConverter(); |
| 87 | +converter.setUserClaimName(customUserClaim); |
| 88 | + |
| 89 | +Map<String, Object> map = new HashMap<String, Object>(); |
| 90 | +map.put(customUserClaim, "test_user"); |
| 91 | + |
| 92 | +Authentication authentication = converter.extractAuthentication(map); |
| 93 | + |
| 94 | +assertEquals("test_user", authentication.getPrincipal()); |
| 95 | +} |
| 96 | + |
| 97 | +@Test |
| 98 | +public void shouldConvertUserWithCustomUsernameClaimWhenSet() throws Exception { |
| 99 | +String customUserClaim = "custom_user_name"; |
| 100 | +DefaultUserAuthenticationConverter converter = new DefaultUserAuthenticationConverter(); |
| 101 | +converter.setUserClaimName(customUserClaim); |
| 102 | + |
| 103 | +Authentication authentication = new UsernamePasswordAuthenticationToken("test_user", ""); |
| 104 | + |
| 105 | +Map<String, ?> map = converter.convertUserAuthentication(authentication); |
| 106 | + |
| 107 | +assertEquals("test_user", map.get(customUserClaim)); |
| 108 | +} |
62 | 109 | } |
0 commit comments