From df452a0fc452da461e2cc0058b43ae453aed4d4d Mon Sep 17 00:00:00 2001 From: Petri Kainulainen Date: Sat, 2 Nov 2013 12:49:22 +0200 Subject: [PATCH] Changed the API of the SecurityContextAssert class so that tests can actually fail --- .../security/util/SecurityContextAssert.java | 50 ++++++++++++++----- .../security/util/SecurityUtilTest.java | 7 ++- .../RegistrationControllerTest.java | 12 +++-- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/security/util/SecurityContextAssert.java b/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/security/util/SecurityContextAssert.java index 90b929b..b8a2050 100644 --- a/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/security/util/SecurityContextAssert.java +++ b/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/security/util/SecurityContextAssert.java @@ -2,7 +2,6 @@ import net.petrikainulainen.spring.social.signinmvc.security.dto.ExampleUserDetails; import net.petrikainulainen.spring.social.signinmvc.security.dto.ExampleUserDetailsAssert; -import net.petrikainulainen.spring.social.signinmvc.security.dto.ExampleUserDetailsAssert; import net.petrikainulainen.spring.social.signinmvc.user.model.SocialMediaService; import net.petrikainulainen.spring.social.signinmvc.user.model.User; import org.fest.assertions.Assertions; @@ -22,7 +21,7 @@ public static SecurityContextAssert assertThat(SecurityContext actual) { return new SecurityContextAssert(actual); } - public SecurityContextAssert loggedInUserIsRegisteredByUsingRegistrationForm(User user) { + public SecurityContextAssert loggedInUserIs(User user) { isNotNull(); ExampleUserDetails loggedIn = (ExampleUserDetails) actual.getAuthentication().getPrincipal(); @@ -36,33 +35,58 @@ public SecurityContextAssert loggedInUserIsRegisteredByUsingRegistrationForm(Use .hasFirstName(user.getFirstName()) .hasId(user.getId()) .hasLastName(user.getLastName()) - .hasPassword(user.getPassword()) .hasUsername(user.getEmail()) .isActive() - .isRegisteredUser() + .isRegisteredUser(); + + return this; + } + + public SecurityContextAssert loggedInUserHasPassword(String password) { + isNotNull(); + + ExampleUserDetails loggedIn = (ExampleUserDetails) actual.getAuthentication().getPrincipal(); + + String errorMessage = String.format("Expected logged in user to be but was "); + Assertions.assertThat(loggedIn) + .overridingErrorMessage(errorMessage) + .isNotNull(); + + ExampleUserDetailsAssert.assertThat(loggedIn) + .hasPassword(password); + + return this; + } + + public SecurityContextAssert loggedInUserIsRegisteredByUsingNormalRegistration() { + isNotNull(); + + ExampleUserDetails loggedIn = (ExampleUserDetails) actual.getAuthentication().getPrincipal(); + + String errorMessage = String.format("Expected logged in user to be but was "); + Assertions.assertThat(loggedIn) + .overridingErrorMessage(errorMessage) + .isNotNull(); + + ExampleUserDetailsAssert.assertThat(loggedIn) .isRegisteredByUsingFormRegistration(); return this; } - public SecurityContextAssert loggedInUserIsSignedInByUsingSocialProvider(User user) { + public SecurityContextAssert loggedInUserIsSignedInByUsingSocialProvider(SocialMediaService signInProvider) { isNotNull(); ExampleUserDetails loggedIn = (ExampleUserDetails) actual.getAuthentication().getPrincipal(); - String errorMessage = String.format("Expected logged in user to be <%s> but was ", user); + String errorMessage = String.format("Expected logged in user to be but was "); Assertions.assertThat(loggedIn) .overridingErrorMessage(errorMessage) .isNotNull(); ExampleUserDetailsAssert.assertThat(loggedIn) - .hasFirstName(user.getFirstName()) - .hasId(user.getId()) - .hasLastName(user.getLastName()) - .hasUsername(user.getEmail()) - .isActive() - .isRegisteredUser() - .isSignedInByUsingSocialSignInProvider(user.getSignInProvider()); + .hasPassword("SocialUser") + .isSignedInByUsingSocialSignInProvider(signInProvider); return this; } diff --git a/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/security/util/SecurityUtilTest.java b/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/security/util/SecurityUtilTest.java index b4f8ed2..dd98fa5 100644 --- a/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/security/util/SecurityUtilTest.java +++ b/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/security/util/SecurityUtilTest.java @@ -32,7 +32,9 @@ public void logInUser_UserRegisteredByUsingFormRegistration_ShouldAddUserDetails SecurityUtil.logInUser(user); assertThat(SecurityContextHolder.getContext()) - .loggedInUserIsRegisteredByUsingRegistrationForm(user); + .loggedInUserIs(user) + .loggedInUserHasPassword(PASSWORD) + .loggedInUserIsRegisteredByUsingNormalRegistration(); } @Test @@ -48,6 +50,7 @@ public void logInUser_UserSignInByUsingSocialSignInProvider_ShouldAddUserDetails SecurityUtil.logInUser(user); assertThat(SecurityContextHolder.getContext()) - .loggedInUserIsSignedInByUsingSocialProvider(user); + .loggedInUserIs(user) + .loggedInUserIsSignedInByUsingSocialProvider(SocialMediaService.TWITTER); } } diff --git a/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/RegistrationControllerTest.java b/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/RegistrationControllerTest.java index 5b958a7..63cad92 100644 --- a/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/RegistrationControllerTest.java +++ b/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/RegistrationControllerTest.java @@ -20,8 +20,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.social.connect.UserProfile; -import org.springframework.social.connect.UserProfileBuilder; import org.springframework.social.connect.web.ProviderSignInAttempt; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -337,7 +335,10 @@ public void registerUserAccount_NormalRegistration_ShouldCreateNewUserAccountAnd .andExpect(status().isMovedTemporarily()) .andExpect(redirectedUrl("/")); - assertThat(SecurityContextHolder.getContext()).loggedInUserIsRegisteredByUsingRegistrationForm(registered); + assertThat(SecurityContextHolder.getContext()) + .loggedInUserIs(registered) + .loggedInUserHasPassword(registered.getPassword()) + .loggedInUserIsRegisteredByUsingNormalRegistration(); verify(userServiceMock, times(1)).registerNewUserAccount(userAccountData); verifyNoMoreInteractions(userServiceMock); @@ -540,7 +541,6 @@ public void registerUserAccount_SocialSignIn_ShouldCreateNewUserAccountAndRender .email(EMAIL) .firstName(FIRST_NAME) .lastName(LAST_NAME) - .password(PASSWORD) .signInProvider(SIGN_IN_PROVIDER) .build(); @@ -555,7 +555,9 @@ public void registerUserAccount_SocialSignIn_ShouldCreateNewUserAccountAndRender .andExpect(status().isMovedTemporarily()) .andExpect(redirectedUrl("/")); - assertThat(SecurityContextHolder.getContext()).loggedInUserIsSignedInByUsingSocialProvider(registered); + assertThat(SecurityContextHolder.getContext()) + .loggedInUserIs(registered) + .loggedInUserIsSignedInByUsingSocialProvider(SIGN_IN_PROVIDER); assertThatSignIn(socialSignIn).createdConnectionForUserId(EMAIL); verify(userServiceMock, times(1)).registerNewUserAccount(userAccountData);