From 6b0981549bbcfb455feadf519e61ff021e5242b5 Mon Sep 17 00:00:00 2001 From: Tao Sun Date: Sat, 14 Dec 2019 11:39:25 +0800 Subject: [PATCH] Add test for details deserialization --- ...amePasswordAuthenticationTokenMixinTests.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/src/test/java/org/springframework/security/jackson2/UsernamePasswordAuthenticationTokenMixinTests.java b/core/src/test/java/org/springframework/security/jackson2/UsernamePasswordAuthenticationTokenMixinTests.java index 505c34b3ce..8818492048 100644 --- a/core/src/test/java/org/springframework/security/jackson2/UsernamePasswordAuthenticationTokenMixinTests.java +++ b/core/src/test/java/org/springframework/security/jackson2/UsernamePasswordAuthenticationTokenMixinTests.java @@ -64,6 +64,10 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin + "}"; // @formatter:on + // @formatter:off + private static final String AUTHENTICATED_STRINGDETAILS_JSON = AUTHENTICATED_JSON.replace("\"details\": null, ", "\"details\": \"details\", "); + // @formatter:on + // @formatter:off private static final String AUTHENTICATED_NON_USER_PRINCIPAL_JSON = AUTHENTICATED_JSON .replace(UserDeserializerTests.USER_JSON, NON_USER_PRINCIPAL_JSON) @@ -155,6 +159,18 @@ public class UsernamePasswordAuthenticationTokenMixinTests extends AbstractMixin assertThat(token.getPrincipal()).isNotNull().isInstanceOf(NonUserPrincipal.class); } + @Test + public void deserializeAuthenticatedUsernamePasswordAuthenticationTokenWithDetailsTest() throws IOException { + UsernamePasswordAuthenticationToken token = mapper + .readValue(AUTHENTICATED_STRINGDETAILS_JSON, UsernamePasswordAuthenticationToken.class); + assertThat(token).isNotNull(); + assertThat(token.getPrincipal()).isNotNull().isInstanceOf(User.class); + assertThat(((User) token.getPrincipal()).getAuthorities()).isNotNull().hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER")); + assertThat(token.isAuthenticated()).isEqualTo(true); + assertThat(token.getAuthorities()).hasSize(1).contains(new SimpleGrantedAuthority("ROLE_USER")); + assertThat(token.getDetails()).isExactlyInstanceOf(String.class); + } + @Test public void serializingThenDeserializingWithNoCredentialsOrDetailsShouldWork() throws IOException { // given