diff --git a/sign-in/spring-mvc-normal/pom.xml b/sign-in/spring-mvc-normal/pom.xml index b749c8e..d2bc66d 100644 --- a/sign-in/spring-mvc-normal/pom.xml +++ b/sign-in/spring-mvc-normal/pom.xml @@ -532,7 +532,7 @@ com.googlecode.catch-exception catch-exception - 1.0.4 + 1.2.0 org.mockito 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 b8a2050..f5eeb74 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 @@ -6,6 +6,7 @@ import net.petrikainulainen.spring.social.signinmvc.user.model.User; import org.fest.assertions.Assertions; import org.fest.assertions.GenericAssert; +import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContext; /** @@ -21,6 +22,19 @@ public static SecurityContextAssert assertThat(SecurityContext actual) { return new SecurityContextAssert(actual); } + public SecurityContextAssert userIsAnonymous() { + isNotNull(); + + Authentication authentication = actual.getAuthentication(); + + String errorMessage = String.format("Expected authentication to be but was <%s>.", authentication); + Assertions.assertThat(authentication) + .overridingErrorMessage(errorMessage) + .isNull(); + + return this; + } + public SecurityContextAssert loggedInUserIs(User user) { isNotNull(); 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 63cad92..91224e4 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 @@ -5,9 +5,8 @@ import net.petrikainulainen.spring.social.signinmvc.config.WebAppContext; import net.petrikainulainen.spring.social.signinmvc.user.model.SocialMediaService; import net.petrikainulainen.spring.social.signinmvc.user.service.DuplicateEmailException; -import org.springframework.social.connect.web.ProviderSignInAttemptStub; -import org.springframework.social.connect.support.TestConnection; -import org.springframework.social.connect.support.TestConnectionBuilder; +import org.springframework.social.connect.support.TestProviderSignInAttemptBuilder; +import org.springframework.social.connect.web.TestProviderSignInAttempt; import net.petrikainulainen.spring.social.signinmvc.user.dto.RegistrationForm; import net.petrikainulainen.spring.social.signinmvc.user.dto.RegistrationFormBuilder; import net.petrikainulainen.spring.social.signinmvc.user.model.User; @@ -29,7 +28,7 @@ import org.springframework.web.context.WebApplicationContext; import static net.petrikainulainen.spring.social.signinmvc.security.util.SecurityContextAssert.assertThat; -import static net.petrikainulainen.spring.social.signinmvc.user.controller.ProviderSignInAttemptStubAssert.assertThatSignIn; +import static net.petrikainulainen.spring.social.signinmvc.user.controller.TestProviderSignInAttemptAssert.assertThatSignIn; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.Matchers.hasProperty; import static org.hamcrest.Matchers.is; @@ -71,6 +70,8 @@ public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext) .build(); + + SecurityContextHolder.getContext().setAuthentication(null); } @Test @@ -93,16 +94,15 @@ public void showRegistrationForm_NormalRegistration_ShouldRenderRegistrationPage @Test public void showRegistrationForm_SocialSignUpWithAllValues_ShouldRenderRegistrationPageWithAllValuesSet() throws Exception { - TestConnection socialConnection = new TestConnectionBuilder() - .providerId(SOCIAL_MEDIA_SERVICE) + TestProviderSignInAttempt socialSignIn = new TestProviderSignInAttemptBuilder() + .connectionData() + .providerId(SOCIAL_MEDIA_SERVICE) .userProfile() .email(EMAIL) .firstName(FIRST_NAME) .lastName(LAST_NAME) .build(); - ProviderSignInAttempt socialSignIn = new ProviderSignInAttemptStub(socialConnection); - mockMvc.perform(get("/user/register") .sessionAttr(ProviderSignInAttempt.SESSION_ATTRIBUTE, socialSignIn) ) @@ -121,13 +121,12 @@ public void showRegistrationForm_SocialSignUpWithAllValues_ShouldRenderRegistrat @Test public void showRegistrationForm_SocialSignUpWithNoValues_ShouldRenderRegistrationPageWithProviderDetails() throws Exception { - TestConnection socialConnection = new TestConnectionBuilder() - .providerId(SOCIAL_MEDIA_SERVICE) + TestProviderSignInAttempt socialSignIn = new TestProviderSignInAttemptBuilder() + .connectionData() + .providerId(SOCIAL_MEDIA_SERVICE) .userProfile() .build(); - ProviderSignInAttempt socialSignIn = new ProviderSignInAttemptStub(socialConnection); - mockMvc.perform(get("/user/register") .sessionAttr(ProviderSignInAttempt.SESSION_ATTRIBUTE, socialSignIn) ) @@ -173,6 +172,7 @@ public void registerUserAccount_NormalRegistrationAndEmptyForm_ShouldRenderRegis "passwordVerification" )); + assertThat(SecurityContextHolder.getContext()).userIsAnonymous(); verifyZeroInteractions(userServiceMock); } @@ -208,6 +208,7 @@ public void registerUserAccount_NormalRegistrationAndTooLongValues_ShouldRenderR ))) .andExpect(model().attributeHasFieldErrors("user", "email", "firstName", "lastName")); + assertThat(SecurityContextHolder.getContext()).userIsAnonymous(); verifyZeroInteractions(userServiceMock); } @@ -239,6 +240,7 @@ public void registerUserAccount_NormalRegistrationAndPasswordMismatch_ShouldRend ))) .andExpect(model().attributeHasFieldErrors("user", "password", "passwordVerification")); + assertThat(SecurityContextHolder.getContext()).userIsAnonymous(); verifyZeroInteractions(userServiceMock); } @@ -272,6 +274,8 @@ public void registerUserAccount_NormalRegistrationAndEmailExists_ShouldRenderReg ))) .andExpect(model().attributeHasFieldErrors("user", "email")); + assertThat(SecurityContextHolder.getContext()).userIsAnonymous(); + verify(userServiceMock, times(1)).registerNewUserAccount(userAccountData); verifyNoMoreInteractions(userServiceMock); } @@ -304,6 +308,8 @@ public void registerUserAccount_NormalRegistrationAndMalformedEmail_ShouldRender ))) .andExpect(model().attributeHasFieldErrors("user", "email")); + assertThat(SecurityContextHolder.getContext()).userIsAnonymous(); + verifyZeroInteractions(userServiceMock); } @@ -346,16 +352,15 @@ public void registerUserAccount_NormalRegistration_ShouldCreateNewUserAccountAnd @Test public void registerUserAccount_SignUpViaSocialProviderAndEmptyForm_ShouldRenderRegistrationFormWithValidationErrors() throws Exception { - TestConnection socialConnection = new TestConnectionBuilder() - .providerId(SOCIAL_MEDIA_SERVICE) + TestProviderSignInAttempt socialSignIn = new TestProviderSignInAttemptBuilder() + .connectionData() + .providerId(SOCIAL_MEDIA_SERVICE) .userProfile() .email(EMAIL) .firstName(FIRST_NAME) .lastName(LAST_NAME) .build(); - ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection); - RegistrationForm userAccountData = new RegistrationFormBuilder() .signInProvider(SIGN_IN_PROVIDER) .build(); @@ -379,22 +384,22 @@ public void registerUserAccount_SignUpViaSocialProviderAndEmptyForm_ShouldRender ))) .andExpect(model().attributeHasFieldErrors("user", "email", "firstName", "lastName")); + assertThat(SecurityContextHolder.getContext()).userIsAnonymous(); assertThatSignIn(socialSignIn).createdNoConnections(); verifyZeroInteractions(userServiceMock); } @Test public void registerUserAccount_SocialSignInAndTooLongValues_ShouldRenderRegistrationFormWithValidationErrors() throws Exception { - TestConnection socialConnection = new TestConnectionBuilder() - .providerId(SOCIAL_MEDIA_SERVICE) + TestProviderSignInAttempt socialSignIn = new TestProviderSignInAttemptBuilder() + .connectionData() + .providerId(SOCIAL_MEDIA_SERVICE) .userProfile() .email(EMAIL) .firstName(FIRST_NAME) .lastName(LAST_NAME) .build(); - ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection); - String email = TestUtil.createStringWithLength(101); String firstName = TestUtil.createStringWithLength(101); String lastName = TestUtil.createStringWithLength(101); @@ -425,22 +430,22 @@ public void registerUserAccount_SocialSignInAndTooLongValues_ShouldRenderRegistr ))) .andExpect(model().attributeHasFieldErrors("user", "email", "firstName", "lastName")); + assertThat(SecurityContextHolder.getContext()).userIsAnonymous(); assertThatSignIn(socialSignIn).createdNoConnections(); verifyZeroInteractions(userServiceMock); } @Test public void registerUserAccount_SocialSignInAndMalformedEmail_ShouldRenderRegistrationFormWithValidationError() throws Exception { - TestConnection socialConnection = new TestConnectionBuilder() - .providerId(SOCIAL_MEDIA_SERVICE) + TestProviderSignInAttempt socialSignIn = new TestProviderSignInAttemptBuilder() + .connectionData() + .providerId(SOCIAL_MEDIA_SERVICE) .userProfile() .email(EMAIL) .firstName(FIRST_NAME) .lastName(LAST_NAME) .build(); - ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection); - RegistrationForm userAccountData = new RegistrationFormBuilder() .email(MALFORMED_EMAIL) .firstName(FIRST_NAME) @@ -467,22 +472,22 @@ public void registerUserAccount_SocialSignInAndMalformedEmail_ShouldRenderRegist ))) .andExpect(model().attributeHasFieldErrors("user", "email")); + assertThat(SecurityContextHolder.getContext()).userIsAnonymous(); assertThatSignIn(socialSignIn).createdNoConnections(); verifyZeroInteractions(userServiceMock); } @Test public void registerUserAccount_SocialSignInAndEmailExist_ShouldRenderRegistrationFormWithFieldError() throws Exception { - TestConnection socialConnection = new TestConnectionBuilder() - .providerId(SOCIAL_MEDIA_SERVICE) + TestProviderSignInAttempt socialSignIn = new TestProviderSignInAttemptBuilder() + .connectionData() + .providerId(SOCIAL_MEDIA_SERVICE) .userProfile() .email(EMAIL) .firstName(FIRST_NAME) .lastName(LAST_NAME) .build(); - ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection); - RegistrationForm userAccountData = new RegistrationFormBuilder() .email(EMAIL) .firstName(FIRST_NAME) @@ -511,6 +516,7 @@ public void registerUserAccount_SocialSignInAndEmailExist_ShouldRenderRegistrati ))) .andExpect(model().attributeHasFieldErrors("user", "email")); + assertThat(SecurityContextHolder.getContext()).userIsAnonymous(); assertThatSignIn(socialSignIn).createdNoConnections(); verify(userServiceMock, times(1)).registerNewUserAccount(userAccountData); @@ -519,16 +525,15 @@ public void registerUserAccount_SocialSignInAndEmailExist_ShouldRenderRegistrati @Test public void registerUserAccount_SocialSignIn_ShouldCreateNewUserAccountAndRenderHomePage() throws Exception { - TestConnection socialConnection = new TestConnectionBuilder() - .providerId(SOCIAL_MEDIA_SERVICE) + TestProviderSignInAttempt socialSignIn = new TestProviderSignInAttemptBuilder() + .connectionData() + .providerId(SOCIAL_MEDIA_SERVICE) .userProfile() .email(EMAIL) .firstName(FIRST_NAME) .lastName(LAST_NAME) .build(); - ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection); - RegistrationForm userAccountData = new RegistrationFormBuilder() .email(EMAIL) .firstName(FIRST_NAME) diff --git a/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/ProviderSignInAttemptStubAssert.java b/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/TestProviderSignInAttemptAssert.java similarity index 59% rename from sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/ProviderSignInAttemptStubAssert.java rename to sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/TestProviderSignInAttemptAssert.java index a8a18e6..afc617f 100644 --- a/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/ProviderSignInAttemptStubAssert.java +++ b/sign-in/spring-mvc-normal/src/test/java/net/petrikainulainen/spring/social/signinmvc/user/controller/TestProviderSignInAttemptAssert.java @@ -2,22 +2,22 @@ import org.fest.assertions.Assertions; import org.fest.assertions.GenericAssert; -import org.springframework.social.connect.web.ProviderSignInAttemptStub; +import org.springframework.social.connect.web.TestProviderSignInAttempt; /** * @author Petri Kainulainen */ -public class ProviderSignInAttemptStubAssert extends GenericAssert { +public class TestProviderSignInAttemptAssert extends GenericAssert { - public ProviderSignInAttemptStubAssert(ProviderSignInAttemptStub actual) { - super(ProviderSignInAttemptStubAssert.class, actual); + public TestProviderSignInAttemptAssert(TestProviderSignInAttempt actual) { + super(TestProviderSignInAttemptAssert.class, actual); } - public static ProviderSignInAttemptStubAssert assertThatSignIn(ProviderSignInAttemptStub actual) { - return new ProviderSignInAttemptStubAssert(actual); + public static TestProviderSignInAttemptAssert assertThatSignIn(TestProviderSignInAttempt actual) { + return new TestProviderSignInAttemptAssert(actual); } - public ProviderSignInAttemptStubAssert createdNoConnections() { + public TestProviderSignInAttemptAssert createdNoConnections() { isNotNull(); String error = String.format( @@ -31,7 +31,7 @@ public ProviderSignInAttemptStubAssert createdNoConnections() { return this; } - public ProviderSignInAttemptStubAssert createdConnectionForUserId(String userId) { + public TestProviderSignInAttemptAssert createdConnectionForUserId(String userId) { isNotNull(); String error = String.format( diff --git a/sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/support/TestConnectionBuilder.java b/sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/support/TestProviderSignInAttemptBuilder.java similarity index 55% rename from sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/support/TestConnectionBuilder.java rename to sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/support/TestProviderSignInAttemptBuilder.java index ae783a2..d3059e1 100644 --- a/sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/support/TestConnectionBuilder.java +++ b/sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/support/TestProviderSignInAttemptBuilder.java @@ -1,13 +1,15 @@ package org.springframework.social.connect.support; +import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionData; import org.springframework.social.connect.UserProfile; import org.springframework.social.connect.UserProfileBuilder; +import org.springframework.social.connect.web.TestProviderSignInAttempt; /** * @author Petri Kainulainen */ -public class TestConnectionBuilder { +public class TestProviderSignInAttemptBuilder { private String accessToken; @@ -33,78 +35,79 @@ public class TestConnectionBuilder { private String secret; - private UserProfileBuilder userProfileBuilder; - - public TestConnectionBuilder() { + public TestProviderSignInAttemptBuilder() { } - public TestConnectionBuilder accessToken(String accessToken) { + public TestProviderSignInAttemptBuilder accessToken(String accessToken) { this.accessToken = accessToken; return this; } - public TestConnectionBuilder displayName(String displayName) { + public TestProviderSignInAttemptBuilder connectionData() { + return this; + } + + public TestProviderSignInAttemptBuilder displayName(String displayName) { this.displayName = displayName; return this; } - public TestConnectionBuilder email(String email) { + public TestProviderSignInAttemptBuilder email(String email) { this.email = email; return this; } - public TestConnectionBuilder expireTime(Long expireTime) { + public TestProviderSignInAttemptBuilder expireTime(Long expireTime) { this.expireTime = expireTime; return this; } - public TestConnectionBuilder firstName(String firstName) { + public TestProviderSignInAttemptBuilder firstName(String firstName) { this.firstName = firstName; return this; } - public TestConnectionBuilder imageUrl(String imageUrl) { + public TestProviderSignInAttemptBuilder imageUrl(String imageUrl) { this.imageUrl = imageUrl; return this; } - public TestConnectionBuilder lastName(String lastName) { + public TestProviderSignInAttemptBuilder lastName(String lastName) { this.lastName = lastName; return this; } - public TestConnectionBuilder profileUrl(String profileUrl) { + public TestProviderSignInAttemptBuilder profileUrl(String profileUrl) { this.profileUrl = profileUrl; return this; } - public TestConnectionBuilder providerId(String providerId) { + public TestProviderSignInAttemptBuilder providerId(String providerId) { this.providerId = providerId; return this; } - public TestConnectionBuilder providerUserId(String providerUserId) { + public TestProviderSignInAttemptBuilder providerUserId(String providerUserId) { this.providerUserId = providerUserId; return this; } - public TestConnectionBuilder refreshToken(String refreshToken) { + public TestProviderSignInAttemptBuilder refreshToken(String refreshToken) { this.refreshToken = refreshToken; return this; } - public TestConnectionBuilder secret(String secret) { + public TestProviderSignInAttemptBuilder secret(String secret) { this.secret = secret; return this; } - public TestConnectionBuilder userProfile() { - userProfileBuilder = new UserProfileBuilder(); + public TestProviderSignInAttemptBuilder userProfile() { return this; } - public TestConnection build() { + public TestProviderSignInAttempt build() { ConnectionData connectionData = new ConnectionData(providerId, providerUserId, displayName, @@ -115,12 +118,14 @@ public TestConnection build() { refreshToken, expireTime); - UserProfile userProfile = userProfileBuilder + UserProfile userProfile = new UserProfileBuilder() .setEmail(email) .setFirstName(firstName) .setLastName(lastName) .build(); - return new TestConnection(connectionData, userProfile); + Connection connection = new TestConnection(connectionData, userProfile); + + return new TestProviderSignInAttempt(connection); } } diff --git a/sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/web/ProviderSignInAttemptStub.java b/sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/web/TestProviderSignInAttempt.java similarity index 83% rename from sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/web/ProviderSignInAttemptStub.java rename to sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/web/TestProviderSignInAttempt.java index 12987fc..2548fb1 100644 --- a/sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/web/ProviderSignInAttemptStub.java +++ b/sign-in/spring-mvc-normal/src/test/java/org/springframework/social/connect/web/TestProviderSignInAttempt.java @@ -8,13 +8,13 @@ /** * @author Petri Kainulainen */ -public class ProviderSignInAttemptStub extends ProviderSignInAttempt { +public class TestProviderSignInAttempt extends ProviderSignInAttempt { private Connection connection; private Set connections = new HashSet<>(); - public ProviderSignInAttemptStub(Connection connection) { + public TestProviderSignInAttempt(Connection connection) { super(connection, null, null); this.connection = connection; }