Skip to content

Commit

Permalink
The TestConnection class species a better DSL for creating connections
Browse files Browse the repository at this point in the history
  • Loading branch information
pkainulainen committed Nov 2, 2013
1 parent c03d29c commit 5b99617
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,12 @@ public void showRegistrationForm_NormalRegistration_ShouldRenderRegistrationPage

@Test
public void showRegistrationForm_SocialSignUpWithAllValues_ShouldRenderRegistrationPageWithAllValuesSet() throws Exception {
UserProfile socialUser = new UserProfileBuilder()
.setEmail(EMAIL)
.setFirstName(FIRST_NAME)
.setLastName(LAST_NAME)
.setName(FIRST_NAME + " " + LAST_NAME)
.build();

TestConnection socialConnection = new TestConnectionBuilder()
.providerId(SOCIAL_MEDIA_SERVICE)
.userProfile(socialUser)
.userProfile()
.email(EMAIL)
.firstName(FIRST_NAME)
.lastName(LAST_NAME)
.build();

ProviderSignInAttempt socialSignIn = new ProviderSignInAttemptStub(socialConnection);
Expand All @@ -127,11 +123,9 @@ public void showRegistrationForm_SocialSignUpWithAllValues_ShouldRenderRegistrat

@Test
public void showRegistrationForm_SocialSignUpWithNoValues_ShouldRenderRegistrationPageWithProviderDetails() throws Exception {
UserProfile socialUser = new UserProfileBuilder().build();

TestConnection socialConnection = new TestConnectionBuilder()
.providerId(SOCIAL_MEDIA_SERVICE)
.userProfile(socialUser)
.userProfile()
.build();

ProviderSignInAttempt socialSignIn = new ProviderSignInAttemptStub(socialConnection);
Expand Down Expand Up @@ -343,24 +337,20 @@ public void registerUserAccount_NormalRegistration_ShouldCreateNewUserAccountAnd
.andExpect(status().isMovedTemporarily())
.andExpect(redirectedUrl("/"));

assertThat(SecurityContextHolder.getContext()).loggedInUserIsSignedInByUsingSocialProvider(registered);
assertThat(SecurityContextHolder.getContext()).loggedInUserIsRegisteredByUsingRegistrationForm(registered);

verify(userServiceMock, times(1)).registerNewUserAccount(userAccountData);
verifyNoMoreInteractions(userServiceMock);
}

@Test
public void registerUserAccount_SignUpViaSocialProviderAndEmptyForm_ShouldRenderRegistrationFormWithValidationErrors() throws Exception {
UserProfile socialUser = new UserProfileBuilder()
.setEmail(EMAIL)
.setFirstName(FIRST_NAME)
.setLastName(LAST_NAME)
.setName(FIRST_NAME + " " + LAST_NAME)
.build();

TestConnection socialConnection = new TestConnectionBuilder()
.providerId(SOCIAL_MEDIA_SERVICE)
.userProfile(socialUser)
.userProfile()
.email(EMAIL)
.firstName(FIRST_NAME)
.lastName(LAST_NAME)
.build();

ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection);
Expand Down Expand Up @@ -394,16 +384,12 @@ public void registerUserAccount_SignUpViaSocialProviderAndEmptyForm_ShouldRender

@Test
public void registerUserAccount_SocialSignInAndTooLongValues_ShouldRenderRegistrationFormWithValidationErrors() throws Exception {
UserProfile socialUser = new UserProfileBuilder()
.setEmail(EMAIL)
.setFirstName(FIRST_NAME)
.setLastName(LAST_NAME)
.setName(FIRST_NAME + " " + LAST_NAME)
.build();

TestConnection socialConnection = new TestConnectionBuilder()
.providerId(SOCIAL_MEDIA_SERVICE)
.userProfile(socialUser)
.userProfile()
.email(EMAIL)
.firstName(FIRST_NAME)
.lastName(LAST_NAME)
.build();

ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection);
Expand Down Expand Up @@ -444,16 +430,12 @@ public void registerUserAccount_SocialSignInAndTooLongValues_ShouldRenderRegistr

@Test
public void registerUserAccount_SocialSignInAndMalformedEmail_ShouldRenderRegistrationFormWithValidationError() throws Exception {
UserProfile socialUser = new UserProfileBuilder()
.setEmail(EMAIL)
.setFirstName(FIRST_NAME)
.setLastName(LAST_NAME)
.setName(FIRST_NAME + " " + LAST_NAME)
.build();

TestConnection socialConnection = new TestConnectionBuilder()
.providerId(SOCIAL_MEDIA_SERVICE)
.userProfile(socialUser)
.userProfile()
.email(EMAIL)
.firstName(FIRST_NAME)
.lastName(LAST_NAME)
.build();

ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection);
Expand Down Expand Up @@ -489,17 +471,13 @@ public void registerUserAccount_SocialSignInAndMalformedEmail_ShouldRenderRegist
}

@Test
public void registerUserAccount_SocialSignInAndEmailExist_ShouldRenderRegistrationFormWithFieldErrror() throws Exception {
UserProfile socialUser = new UserProfileBuilder()
.setEmail(EMAIL)
.setFirstName(FIRST_NAME)
.setLastName(LAST_NAME)
.setName(FIRST_NAME + " " + LAST_NAME)
.build();

public void registerUserAccount_SocialSignInAndEmailExist_ShouldRenderRegistrationFormWithFieldError() throws Exception {
TestConnection socialConnection = new TestConnectionBuilder()
.providerId(SOCIAL_MEDIA_SERVICE)
.userProfile(socialUser)
.userProfile()
.email(EMAIL)
.firstName(FIRST_NAME)
.lastName(LAST_NAME)
.build();

ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection);
Expand Down Expand Up @@ -540,16 +518,12 @@ public void registerUserAccount_SocialSignInAndEmailExist_ShouldRenderRegistrati

@Test
public void registerUserAccount_SocialSignIn_ShouldCreateNewUserAccountAndRenderHomePage() throws Exception {
UserProfile socialUser = new UserProfileBuilder()
.setEmail(EMAIL)
.setFirstName(FIRST_NAME)
.setLastName(LAST_NAME)
.setName(FIRST_NAME + " " + LAST_NAME)
.build();

TestConnection socialConnection = new TestConnectionBuilder()
.providerId(SOCIAL_MEDIA_SERVICE)
.userProfile(socialUser)
.userProfile()
.email(EMAIL)
.firstName(FIRST_NAME)
.lastName(LAST_NAME)
.build();

ProviderSignInAttemptStub socialSignIn = new ProviderSignInAttemptStub(socialConnection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.social.connect.ConnectionData;
import org.springframework.social.connect.UserProfile;
import org.springframework.social.connect.UserProfileBuilder;

/**
* @author Petri Kainulainen
Expand All @@ -10,15 +11,21 @@ public class TestConnectionBuilder {

private String displayName;

private String email;

private String firstName;

private String imageUrl;

private String lastName;

private String profileUrl;

private String providerId;

private String providerUserId;

private UserProfile userProfile;
private UserProfileBuilder userProfileBuilder;

public TestConnectionBuilder() {

Expand All @@ -29,11 +36,26 @@ public TestConnectionBuilder displayName(String displayName) {
return this;
}

public TestConnectionBuilder email(String email) {
this.email = email;
return this;
}

public TestConnectionBuilder firstName(String firstName) {
this.firstName = firstName;
return this;
}

public TestConnectionBuilder imageUrl(String imageUrl) {
this.imageUrl = imageUrl;
return this;
}

public TestConnectionBuilder lastName(String lastName) {
this.lastName = lastName;
return this;
}

public TestConnectionBuilder profileUrl(String profileUrl) {
this.profileUrl = profileUrl;
return this;
Expand All @@ -49,8 +71,8 @@ public TestConnectionBuilder providerUserId(String providerUserId) {
return this;
}

public TestConnectionBuilder userProfile(UserProfile userProfile) {
this.userProfile = userProfile;
public TestConnectionBuilder userProfile() {
userProfileBuilder = new UserProfileBuilder();
return this;
}

Expand All @@ -64,6 +86,13 @@ public TestConnection build() {
null,
null,
null);

UserProfile userProfile = new UserProfileBuilder()
.setEmail(email)
.setFirstName(firstName)
.setLastName(lastName)
.build();

return new TestConnection(connectionData, userProfile);
}
}

0 comments on commit 5b99617

Please sign in to comment.