Skip to content

Commit

Permalink
Ensure that only the fields from the form can be filled when registering
Browse files Browse the repository at this point in the history
  • Loading branch information
arteymix committed Dec 1, 2023
1 parent d1d1b4d commit 740290f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/ubc/pavlab/rdp/controllers/LoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
Expand Down Expand Up @@ -42,6 +44,11 @@ public class LoginController {
@Autowired
private ApplicationSettings applicationSettings;

@InitBinder("user")
public void configureUserDataBinder( WebDataBinder dataBinder ) {
dataBinder.setAllowedFields( "email", "password", "profile.name", "profile.lastName" );
}

@GetMapping("/login")
public ModelAndView login() {
ModelAndView modelAndView = new ModelAndView( "login" );
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/ubc/pavlab/rdp/controllers/LoginControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand Down Expand Up @@ -30,6 +31,7 @@

import java.util.Locale;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -108,6 +110,24 @@ public void register_thenReturnSuccess() throws Exception {
.andExpect( view().name( "registration" ) )
.andExpect( model().attribute( "user", new User() ) );
when( userService.create( any() ) ).thenAnswer( answer -> answer.getArgument( 0, User.class ) );
mvc.perform( post( "/registration" )
.param( "profile.name", "Bob" )
.param( "profile.lastName", "Smith" )
.param( "email", "bob@example.com" )
.param( "password", "123456" )
.param( "passwordConfirm", "123456" )
.param( "id", "27" ) ) // this field is ignored
.andExpect( status().is3xxRedirection() )
.andExpect( redirectedUrl( "/login" ) );
ArgumentCaptor<User> captor = ArgumentCaptor.forClass( User.class );
verify( userService ).create( captor.capture() );
verify( userService ).createVerificationTokenForUser( eq( captor.getValue() ), any() );
assertThat( captor.getValue() ).satisfies( user -> {
assertThat( user.getId() ).isNull();
assertThat( user.getEmail() ).isEqualTo( "bob@example.com" );
assertThat( user.isEnabled() ).isFalse();
assertThat( user.getAnonymousId() ).isNull();
} );
}

@Test
Expand Down

0 comments on commit 740290f

Please sign in to comment.