Search in sources :

Example 6 with CreateUserFormValidator

use of org.asqatasun.webapp.validator.CreateUserFormValidator in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitAddUserFormWithErrors.

/**
     * Test of submitAddUserForm method, of class UserManagementController.
     */
public void testSubmitAddUserFormWithErrors() throws Exception {
    System.out.println("SubmitAddUserFormWithErrors");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, false, false, false, false);
    setUpMockAuthenticationContext();
    instance.setUserDataService(mockUserDataService);
    CreateUserFormValidator createUserFormValidator = new CreateUserFormValidator();
    createUserFormValidator.setUserDataService(mockUserDataService);
    instance.setCreateUserFormValidator(createUserFormValidator);
    // First the form is sent back empty
    CreateUserCommand createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    Model model = new ExtendedModelMap();
    String result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADD_USER_VIEW_NAME, result);
    assertTrue(bindingResult.hasErrors());
    assertEquals(3, bindingResult.getFieldErrors().size());
    assertNotNull(bindingResult.getFieldErrors("email"));
    assertEquals(1, bindingResult.getFieldErrors("email").size());
    assertEquals("sign-up.missingEmail", bindingResult.getFieldErrors("email").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("password"));
    assertEquals(1, bindingResult.getFieldErrors("password").size());
    assertEquals("sign-up.missingPassword", bindingResult.getFieldErrors("password").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("generalErrorMsg"));
    assertEquals(1, bindingResult.getFieldErrors("generalErrorMsg").size());
    assertEquals("sign-up.mandatoryField", bindingResult.getFieldErrors("generalErrorMsg").get(0).getCode());
    // First the form is sent back with passwords that don't match
    createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    createUserCommand.setEmail("user@test.com");
    createUserCommand.setPassword("password");
    createUserCommand.setConfirmPassword("confirmation");
    bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    model = new ExtendedModelMap();
    result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADD_USER_VIEW_NAME, result);
    assertTrue(bindingResult.hasErrors());
    assertEquals(3, bindingResult.getFieldErrors().size());
    assertNotNull(bindingResult.getFieldErrors("email"));
    assertEquals(1, bindingResult.getFieldErrors("email").size());
    assertEquals("sign-up.existingAccountWithEmail", bindingResult.getFieldErrors("email").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("password"));
    assertEquals(1, bindingResult.getFieldErrors("password").size());
    assertEquals("sign-up.passwordNotIdentical", bindingResult.getFieldErrors("password").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("generalErrorMsg"));
    assertEquals(1, bindingResult.getFieldErrors("generalErrorMsg").size());
    assertEquals("sign-up.mandatoryField", bindingResult.getFieldErrors("generalErrorMsg").get(0).getCode());
    // Then the url is not conform but in this context it is not tested, so ignored
    // The Email1 is not conform regardin the pattern EMAIL_CHECKER_REGEXP
    // The passwords match but don't respect the constraints
    createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    createUserCommand.setEmail("null");
    createUserCommand.setPassword("password");
    createUserCommand.setConfirmPassword("password");
    bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    model = new ExtendedModelMap();
    result = instance.submitAddUserForm(createUserCommand, bindingResult, model);
    assertEquals(TgolKeyStore.ADD_USER_VIEW_NAME, result);
    assertTrue(bindingResult.hasErrors());
    assertEquals(3, bindingResult.getFieldErrors().size());
    assertNotNull(bindingResult.getFieldErrors("email"));
    assertEquals(1, bindingResult.getFieldErrors("email").size());
    assertEquals("sign-up.invalidEmail", bindingResult.getFieldErrors("email").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("password"));
    assertEquals(1, bindingResult.getFieldErrors("password").size());
    assertEquals("sign-up.invalidPassword", bindingResult.getFieldErrors("password").get(0).getCode());
    assertNotNull(bindingResult.getFieldErrors("generalErrorMsg"));
    assertEquals(1, bindingResult.getFieldErrors("generalErrorMsg").size());
    assertEquals("sign-up.mandatoryField", bindingResult.getFieldErrors("generalErrorMsg").get(0).getCode());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Example 7 with CreateUserFormValidator

use of org.asqatasun.webapp.validator.CreateUserFormValidator in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitEditUserForm.

/**
     * Test of submitEditUserForm method, of class UserManagementController.
     */
public void testSubmitEditUserForm() throws Exception {
    System.out.println("submitEditUserForm");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, false, false, true, false);
    setUpMockAuthenticationContext();
    instance.setUserDataService(mockUserDataService);
    CreateUserFormValidator createUserFormValidator = new CreateUserFormValidator();
    createUserFormValidator.setUserDataService(mockUserDataService);
    instance.setCreateUserFormValidator(createUserFormValidator);
    // Finally the form is conform and the admin page is returned
    CreateUserCommand createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    createUserCommand.setEmail("user@test.com");
    createUserCommand.setLastName("newName");
    createUserCommand.setFirstName("newFirstName");
    createUserCommand.setPhoneNumber("0102030405");
    createUserCommand.setActivated(false);
    createUserCommand.setAdmin(true);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.getSession().setAttribute(TgolKeyStore.USER_ID_KEY, Long.valueOf(5));
    BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    Model model = new ExtendedModelMap();
    String result = instance.submitEditUserForm(createUserCommand, bindingResult, request, model);
    assertEquals(TgolKeyStore.ADMIN_VIEW_NAME, result);
    assertFalse(bindingResult.hasErrors());
    assertTrue(bindingResult.getFieldErrors().isEmpty());
    assertEquals(2, model.asMap().size());
    assertEquals("user@test.com", model.asMap().get(TgolKeyStore.UPDATED_USER_NAME_KEY));
    assertTrue(((List<User>) model.asMap().get(TgolKeyStore.USER_LIST_KEY)).isEmpty());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) User(org.asqatasun.webapp.entity.user.User) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Aggregations

CreateUserFormValidator (org.asqatasun.webapp.validator.CreateUserFormValidator)7 BindingResult (org.springframework.validation.BindingResult)7 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)6 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)6 Model (org.springframework.ui.Model)6 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)6 User (org.asqatasun.webapp.entity.user.User)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3