Skip to content

Commit

Permalink
Changed the API of the SecurityContextAssert class so that tests can …
Browse files Browse the repository at this point in the history
…actually fail
  • Loading branch information
pkainulainen committed Nov 2, 2013
1 parent 5bc0484 commit df452a0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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 <not null> but was <null>");
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 <not null> but was <null>");
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 <null>", user);
String errorMessage = String.format("Expected logged in user to be <not null> but was <null>");
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public void logInUser_UserRegisteredByUsingFormRegistration_ShouldAddUserDetails
SecurityUtil.logInUser(user);

assertThat(SecurityContextHolder.getContext())
.loggedInUserIsRegisteredByUsingRegistrationForm(user);
.loggedInUserIs(user)
.loggedInUserHasPassword(PASSWORD)
.loggedInUserIsRegisteredByUsingNormalRegistration();
}

@Test
Expand All @@ -48,6 +50,7 @@ public void logInUser_UserSignInByUsingSocialSignInProvider_ShouldAddUserDetails
SecurityUtil.logInUser(user);

assertThat(SecurityContextHolder.getContext())
.loggedInUserIsSignedInByUsingSocialProvider(user);
.loggedInUserIs(user)
.loggedInUserIsSignedInByUsingSocialProvider(SocialMediaService.TWITTER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -540,7 +541,6 @@ public void registerUserAccount_SocialSignIn_ShouldCreateNewUserAccountAndRender
.email(EMAIL)
.firstName(FIRST_NAME)
.lastName(LAST_NAME)
.password(PASSWORD)
.signInProvider(SIGN_IN_PROVIDER)
.build();

Expand All @@ -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);
Expand Down

0 comments on commit df452a0

Please sign in to comment.