|
| 1 | +package samples.mixins; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | +import org.junit.runner.RunWith; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.boot.test.SpringApplicationConfiguration; |
| 7 | +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; |
| 8 | +import org.springframework.data.redis.serializer.SerializationException; |
| 9 | +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
| 10 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 11 | +import org.springframework.security.core.userdetails.User; |
| 12 | +import org.springframework.security.web.authentication.WebAuthenticationDetails; |
| 13 | +import org.springframework.security.web.csrf.DefaultCsrfToken; |
| 14 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 15 | +import sample.Application; |
| 16 | + |
| 17 | +import javax.servlet.http.Cookie; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +/** |
| 22 | + * @author jitendra on 28/3/16. |
| 23 | + */ |
| 24 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 25 | +@SpringApplicationConfiguration(Application.class) |
| 26 | +public class MixinsDeserilizeTest { |
| 27 | + |
| 28 | + @Autowired |
| 29 | + GenericJackson2JsonRedisSerializer redisSerializer; |
| 30 | + |
| 31 | + @Test |
| 32 | + public void defaultCsrfTokenMixin() { |
| 33 | + String tokenJson = "{\"@class\": \"org.springframework.security.web.csrf.DefaultCsrfToken\", \"token\": \"123456\", \"parameterName\": \"_csrf\", \"headerName\": \"x-csrf-header\"}"; |
| 34 | + DefaultCsrfToken token = redisSerializer.deserialize(tokenJson.getBytes(), DefaultCsrfToken.class); |
| 35 | + assertThat(token) |
| 36 | + .hasFieldOrPropertyWithValue("token", "123456") |
| 37 | + .hasFieldOrPropertyWithValue("parameterName", "_csrf") |
| 38 | + .hasFieldOrPropertyWithValue("headerName", "x-csrf-header"); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void httpCookieTest() { |
| 43 | + String httpCookie = "{\"@class\": \"javax.servlet.http.Cookie\", \"name\": \"SESSION\", \"value\": \"123456789\", \"maxAge\": 1000, \"path\": \"/\", \"secure\": true, \"version\": 0, \"httpOnly\": true}"; |
| 44 | + Cookie cookie = redisSerializer.deserialize(httpCookie.getBytes(), Cookie.class); |
| 45 | + assertThat(cookie).hasFieldOrPropertyWithValue("name", "SESSION") |
| 46 | + .hasFieldOrPropertyWithValue("value", "123456789") |
| 47 | + .hasFieldOrPropertyWithValue("secure", true) |
| 48 | + .hasFieldOrPropertyWithValue("comment", "") |
| 49 | + .hasFieldOrPropertyWithValue("path", "/") |
| 50 | + .hasFieldOrPropertyWithValue("maxAge", 1000) |
| 51 | + .hasFieldOrPropertyWithValue("httpOnly", true); |
| 52 | + } |
| 53 | + |
| 54 | + @Test(expected = SerializationException.class) |
| 55 | + public void simpleGrantedAuthorityWithoutTypeIdTest() { |
| 56 | + String authorityJson = "{\"authority\": \"ROLE_USER\"}"; |
| 57 | + SimpleGrantedAuthority authority = redisSerializer.deserialize(authorityJson.getBytes(), SimpleGrantedAuthority.class); |
| 58 | + assertThat(authority.getAuthority()).isEqualTo("ROLE_USER"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void simpleGrantedAuthorityWithTypeIdTest() { |
| 63 | + String authorityJson = "{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"role\": \"ROLE_USER\"}"; |
| 64 | + SimpleGrantedAuthority authority = redisSerializer.deserialize(authorityJson.getBytes(), SimpleGrantedAuthority.class); |
| 65 | + assertThat(authority.getAuthority()).isEqualTo("ROLE_USER"); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void userTest() { |
| 70 | + String userJson = "{\"@class\": \"org.springframework.security.core.userdetails.User\", \"username\": \"user\", \"password\": \"password\", \"authorities\": [\"java.util.Collections$UnmodifiableSet\", [{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"role\": \"ROLE_USER\"}]], \"accountNonExpired\": true, \"accountNonLocked\": true, \"credentialsNonExpired\": true, \"enabled\": true}"; |
| 71 | + User user = redisSerializer.deserialize(userJson.getBytes(), User.class); |
| 72 | + assertThat(user.getUsername()).isEqualTo("user"); |
| 73 | + assertThat(user.getPassword()).isEqualTo("password"); |
| 74 | + assertThat(user.getAuthorities()).contains(new SimpleGrantedAuthority("ROLE_USER")); |
| 75 | + assertThat(user.isEnabled()).isEqualTo(true); |
| 76 | + assertThat(user.isAccountNonExpired()).isEqualTo(true); |
| 77 | + assertThat(user.isAccountNonLocked()).isEqualTo(true); |
| 78 | + assertThat(user.isCredentialsNonExpired()).isEqualTo(true); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void unauthenticatedUsernamePasswordAuthenticationTokenTest() { |
| 83 | + String unauthenticatedTokenJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\"," + |
| 84 | + "\"principal\": \"user\", \"credentials\": \"password\", \"details\": null, \"authorities\": [\"java.util.ArrayList\", []]," + |
| 85 | + "\"authenticated\": false}"; |
| 86 | + UsernamePasswordAuthenticationToken token = redisSerializer.deserialize(unauthenticatedTokenJson.getBytes(), UsernamePasswordAuthenticationToken.class); |
| 87 | + assertThat(token.getPrincipal()).isEqualTo("user"); |
| 88 | + assertThat(token.getCredentials()).isEqualTo("password"); |
| 89 | + assertThat(token.isAuthenticated()).isEqualTo(false); |
| 90 | + assertThat(token.getAuthorities()).hasSize(0); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void unauthenticatedUsernamePasswordAuthenticationTokenWithUserAsPrincipalTest() { |
| 95 | + String unauthenticatedTokenJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\"," + |
| 96 | + "\"principal\": {\"@class\": \"org.springframework.security.core.userdetails.User\", \"username\": \"user\", \"password\": \"password\", " + |
| 97 | + "\"authorities\": [\"java.util.Collections$UnmodifiableSet\", [{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\"," + |
| 98 | + " \"role\": \"ROLE_USER\"}]], \"accountNonExpired\": true, \"accountNonLocked\": true, \"credentialsNonExpired\": true, \"enabled\": true}, " + |
| 99 | + "\"credentials\": \"password\", \"details\": null, \"authorities\": [\"java.util.ArrayList\", []], \"authenticated\": false}"; |
| 100 | + UsernamePasswordAuthenticationToken token = redisSerializer.deserialize(unauthenticatedTokenJson.getBytes(), UsernamePasswordAuthenticationToken.class); |
| 101 | + assertThat(token.getPrincipal()).isInstanceOf(User.class); |
| 102 | + User user = (User) token.getPrincipal(); |
| 103 | + assertThat(user.getUsername()).isEqualTo("user"); |
| 104 | + assertThat(user.getPassword()).isEqualTo("password"); |
| 105 | + assertThat(user.getAuthorities()).contains(new SimpleGrantedAuthority("ROLE_USER")); |
| 106 | + assertThat(user.isEnabled()).isEqualTo(true); |
| 107 | + assertThat(user.isAccountNonExpired()).isEqualTo(true); |
| 108 | + assertThat(user.isAccountNonLocked()).isEqualTo(true); |
| 109 | + assertThat(user.isCredentialsNonExpired()).isEqualTo(true); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void authenticatedUsernamePasswordAuthenticationTokenTest() { |
| 114 | + String unauthenticatedTokenJson = "{\"@class\": \"org.springframework.security.authentication.UsernamePasswordAuthenticationToken\"," + |
| 115 | + "\"principal\": \"user\", \"credentials\": \"password\", \"details\": null, \"authorities\": [\"java.util.ArrayList\", " + |
| 116 | + "[{\"@class\": \"org.springframework.security.core.authority.SimpleGrantedAuthority\", \"role\": \"ROLE_USER\"}]]," + |
| 117 | + "\"authenticated\": true}"; |
| 118 | + UsernamePasswordAuthenticationToken authenticationToken = redisSerializer.deserialize(unauthenticatedTokenJson.getBytes(), UsernamePasswordAuthenticationToken.class); |
| 119 | + assertThat(authenticationToken.getPrincipal()).isEqualTo("user"); |
| 120 | + assertThat(authenticationToken.getCredentials()).isEqualTo("password"); |
| 121 | + assertThat(authenticationToken.isAuthenticated()).isEqualTo(true); |
| 122 | + assertThat(authenticationToken.getAuthorities()).hasSize(1); |
| 123 | + assertThat(authenticationToken.getAuthorities()).contains(new SimpleGrantedAuthority("ROLE_USER")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void webAuthenticationDetailTest() { |
| 128 | + String authenticationDetailJson = "{\"@class\": \"org.springframework.security.web.authentication.WebAuthenticationDetails\"," + |
| 129 | + "\"remoteAddress\": \"http://localhost/login\", \"sessionId\": \"123456789\"}"; |
| 130 | + WebAuthenticationDetails details = redisSerializer.deserialize(authenticationDetailJson.getBytes(), WebAuthenticationDetails.class); |
| 131 | + assertThat(details.getRemoteAddress()).isEqualTo("http://localhost/login"); |
| 132 | + assertThat(details.getSessionId()).isEqualTo("123456789"); |
| 133 | + } |
| 134 | +} |
0 commit comments