Skip to content

Commit

Permalink
Updated dependencies, renamed classes and improved tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pkainulainen committed Nov 10, 2013
1 parent 62d5a50 commit d528e00
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 64 deletions.
2 changes: 1 addition & 1 deletion sign-in/spring-mvc-normal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@
<dependency>
<groupId>com.googlecode.catch-exception</groupId>
<artifactId>catch-exception</artifactId>
<version>1.0.4</version>
<version>1.2.0</version>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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 <null> but was <%s>.", authentication);
Assertions.assertThat(authentication)
.overridingErrorMessage(errorMessage)
.isNull();

return this;
}

public SecurityContextAssert loggedInUserIs(User user) {
isNotNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -71,6 +70,8 @@ public void setUp() {

mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext)
.build();

SecurityContextHolder.getContext().setAuthentication(null);
}

@Test
Expand All @@ -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)
)
Expand All @@ -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)
)
Expand Down Expand Up @@ -173,6 +172,7 @@ public void registerUserAccount_NormalRegistrationAndEmptyForm_ShouldRenderRegis
"passwordVerification"
));

assertThat(SecurityContextHolder.getContext()).userIsAnonymous();
verifyZeroInteractions(userServiceMock);
}

Expand Down Expand Up @@ -208,6 +208,7 @@ public void registerUserAccount_NormalRegistrationAndTooLongValues_ShouldRenderR
)))
.andExpect(model().attributeHasFieldErrors("user", "email", "firstName", "lastName"));

assertThat(SecurityContextHolder.getContext()).userIsAnonymous();
verifyZeroInteractions(userServiceMock);
}

Expand Down Expand Up @@ -239,6 +240,7 @@ public void registerUserAccount_NormalRegistrationAndPasswordMismatch_ShouldRend
)))
.andExpect(model().attributeHasFieldErrors("user", "password", "passwordVerification"));

assertThat(SecurityContextHolder.getContext()).userIsAnonymous();
verifyZeroInteractions(userServiceMock);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -304,6 +308,8 @@ public void registerUserAccount_NormalRegistrationAndMalformedEmail_ShouldRender
)))
.andExpect(model().attributeHasFieldErrors("user", "email"));

assertThat(SecurityContextHolder.getContext()).userIsAnonymous();

verifyZeroInteractions(userServiceMock);
}

Expand Down Expand Up @@ -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();
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProviderSignInAttemptStubAssert, ProviderSignInAttemptStub> {
public class TestProviderSignInAttemptAssert extends GenericAssert<TestProviderSignInAttemptAssert, TestProviderSignInAttempt> {

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(
Expand All @@ -31,7 +31,7 @@ public ProviderSignInAttemptStubAssert createdNoConnections() {
return this;
}

public ProviderSignInAttemptStubAssert createdConnectionForUserId(String userId) {
public TestProviderSignInAttemptAssert createdConnectionForUserId(String userId) {
isNotNull();

String error = String.format(
Expand Down
Loading

0 comments on commit d528e00

Please sign in to comment.