Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index: webapp/src/main/webapp/WEB-INF/messages.properties
- ===================================================================
- --- webapp/src/main/webapp/WEB-INF/messages.properties (revision 26253)
- +++ webapp/src/main/webapp/WEB-INF/messages.properties (revision )
- @@ -339,6 +339,9 @@
- error.options.secretAnswer.match=Answers do not match
- error.options.secretAnswer.empty=Answer should not be empty
- error.options.secretQuestion.empty=Question should not be empty
- +error.options.notificationAddress.empty=Notifications-Email address should not be empty
- +error.options.notificationAddress.invalid=Notifications-Email address should be a valid one
- +
- error.username.weak=Invalid username. Must be at least 6 characters
- error.username.invalid=Invalid username. Username must be alphanumeric and cannot start with a number
- error.retired.requireMetadata=Who retired this and why?
- Index: web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java
- ===================================================================
- --- web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java (revision 17337)
- +++ web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java (revision )
- @@ -1,20 +1,26 @@
- package org.openmrs.web.controller;
- -import static org.junit.Assert.assertEquals;
- -import static org.junit.Assert.assertNull;
- -
- import javax.servlet.http.HttpServletResponse;
- +import org.databene.commons.Assert;
- import org.junit.Before;
- import org.junit.Test;
- import org.openmrs.User;
- import org.openmrs.api.context.Context;
- import org.openmrs.api.db.LoginCredential;
- import org.openmrs.api.db.UserDAO;
- +import org.openmrs.test.Verifies;
- +import org.openmrs.util.OpenmrsConstants;
- +import org.openmrs.web.controller.encounter.LocationFormController;
- import org.openmrs.web.test.BaseWebContextSensitiveTest;
- import org.springframework.mock.web.MockHttpServletRequest;
- import org.springframework.mock.web.MockHttpServletResponse;
- +import org.springframework.validation.BeanPropertyBindingResult;
- +import org.springframework.validation.BindingResult;
- +import org.springframework.web.servlet.ModelAndView;
- +import static org.junit.Assert.*;
- +
- public class OptionsFormControllerTest extends BaseWebContextSensitiveTest {
- private User user;
- @@ -31,57 +37,108 @@
- userDao = (UserDAO) applicationContext.getBean("userDAO");
- }
- - @Test
- - public void shouldChangeSecretQuestionAndAnswer() throws Exception {
- - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- - request.setParameter("secretQuestionPassword", "test");
- - request.setParameter("secretQuestionNew", "test_question");
- -
- - String answer = "test_answer";
- - request.setParameter("secretAnswerNew", answer);
- - request.setParameter("secretAnswerConfirm", answer);
- -
- - HttpServletResponse response = new MockHttpServletResponse();
- - controller.handleRequest(request, response);
- -
- - LoginCredential loginCredential = userDao.getLoginCredential(user);
- - assertEquals(answer, loginCredential.getSecretAnswer());
- - }
- -
- - @Test
- - public void shouldRejectEmptySecretAnswer() throws Exception {
- - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- - request.setParameter("secretQuestionPassword", "test");
- - request.setParameter("secretQuestionNew", "test_question");
- -
- - String emptyAnswer = "";
- - request.setParameter("secretAnswerNew", emptyAnswer);
- - request.setParameter("secretAnswerConfirm", emptyAnswer);
- -
- - HttpServletResponse response = new MockHttpServletResponse();
- - controller.handleRequest(request, response);
- -
- - LoginCredential loginCredential = userDao.getLoginCredential(user);
- - assertNull(loginCredential.getSecretAnswer());
- - }
- -
- - @Test
- - public void shouldRejectEmptySecretQuestion() throws Exception {
- - LoginCredential loginCredential = userDao.getLoginCredential(user);
- - String originalQuestion = loginCredential.getSecretQuestion();
- -
- - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- - request.setParameter("secretQuestionPassword", "test");
- - request.setParameter("secretQuestionNew", "");
- -
- - String emptyAnswer = "test_answer";
- - request.setParameter("secretAnswerNew", emptyAnswer);
- - request.setParameter("secretAnswerConfirm", emptyAnswer);
- -
- - HttpServletResponse response = new MockHttpServletResponse();
- - controller.handleRequest(request, response);
- -
- - loginCredential = userDao.getLoginCredential(user);
- - assertEquals(originalQuestion, loginCredential.getSecretQuestion());
- - }
- + @Test
- + public void shouldChangeSecretQuestionAndAnswer() throws Exception {
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("secretQuestionPassword", "test");
- + request.setParameter("secretQuestionNew", "test_question");
- +
- + String answer = "test_answer";
- + request.setParameter("secretAnswerNew", answer);
- + request.setParameter("secretAnswerConfirm", answer);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + controller.handleRequest(request, response);
- +
- + LoginCredential loginCredential = userDao.getLoginCredential(user);
- + assertEquals(answer, loginCredential.getSecretAnswer());
- + }
- +
- + @Test
- + public void shouldRejectEmptySecretAnswer() throws Exception {
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("secretQuestionPassword", "test");
- + request.setParameter("secretQuestionNew", "test_question");
- +
- + String emptyAnswer = "";
- + request.setParameter("secretAnswerNew", emptyAnswer);
- + request.setParameter("secretAnswerConfirm", emptyAnswer);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + controller.handleRequest(request, response);
- +
- + LoginCredential loginCredential = userDao.getLoginCredential(user);
- + assertNull(loginCredential.getSecretAnswer());
- + }
- +
- + @Test
- + public void shouldRejectEmptySecretQuestion() throws Exception {
- + LoginCredential loginCredential = userDao.getLoginCredential(user);
- + String originalQuestion = loginCredential.getSecretQuestion();
- +
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("secretQuestionPassword", "test");
- + request.setParameter("secretQuestionNew", "");
- +
- + String emptyAnswer = "test_answer";
- + request.setParameter("secretAnswerNew", emptyAnswer);
- + request.setParameter("secretAnswerConfirm", emptyAnswer);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + controller.handleRequest(request, response);
- +
- + loginCredential = userDao.getLoginCredential(user);
- + assertEquals(originalQuestion, loginCredential.getSecretQuestion());
- + }
- +
- + @Test
- + public void shouldRejectEmptyNotificationAddress() throws Exception {
- + String emptyAddress = "";
- +
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("notification", "internal");
- + request.setParameter("notificationAddress", emptyAddress);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + ModelAndView modelAndView = controller.handleRequest(request, response);
- + assertEquals("", request.getParameter("notificationAddress"));
- +
- + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
- + "org.springframework.validation.BindingResult.location");
- + org.junit.Assert.assertTrue(bindingResult.hasErrors());
- -}
- + }
- +
- + @Test
- + public void shouldRejectInvalidNotificationAddress() throws Exception {
- + String incorrectAddress = "gayan@gmail";
- +
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("notification", "internal");
- + request.setParameter("notificationAddress", incorrectAddress);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + ModelAndView modelAndView = controller.handleRequest(request, response);
- + assertNotNull(modelAndView.getModel().get("notificationAddress"));
- +
- + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
- + "org.springframework.validation.BindingResult.location");
- + org.junit.Assert.assertTrue(bindingResult.hasErrors());
- + }
- +
- + @Test
- + public void shouldAcceptValidNotificationAddress() throws Exception {
- + String correctAddress = "gayan@gmail.com";
- +
- + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
- + request.setParameter("notification", "internal");
- + request.setParameter("notificationAddress", correctAddress);
- +
- + HttpServletResponse response = new MockHttpServletResponse();
- + ModelAndView modelAndView = controller.handleRequest(request, response);
- + assertNotNull(modelAndView.getModel().get("notificationAddress"));
- +
- + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
- + "org.springframework.validation.BindingResult.location");
- + org.junit.Assert.assertTrue(!bindingResult.hasErrors());
- + }
- +}
- Index: web/src/main/java/org/openmrs/web/controller/OptionsFormController.java
- ===================================================================
- --- web/src/main/java/org/openmrs/web/controller/OptionsFormController.java (revision 23745)
- +++ web/src/main/java/org/openmrs/web/controller/OptionsFormController.java (revision )
- @@ -17,6 +17,8 @@
- import java.util.HashMap;
- import java.util.Locale;
- import java.util.Map;
- +import java.util.regex.Matcher;
- +import java.util.regex.Pattern;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- @@ -47,7 +49,7 @@
- /**
- * This is the controller for the "My Profile" page. This lets logged in users set personal
- * preferences, update their own information, etc.
- - *
- + *
- * @see OptionsForm
- */
- public class OptionsFormController extends SimpleFormController {
- @@ -97,13 +99,28 @@
- }
- }
- + String notifyType = opts.getNotification();
- +
- + if (notifyType.equals("internal") || notifyType.equals("internalProtected") || notifyType.equals("email")) {
- + if (opts.getNotificationAddress().isEmpty()) {
- + errors.reject("error.options.notificationAddress.empty");
- + } else {
- + String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
- + Pattern pattern = Pattern.compile(EMAIL_PATTERN);
- + Matcher matcher = pattern.matcher(opts.getNotificationAddress());
- + if (matcher.matches() == false) {
- + errors.reject("error.options.notificationAddress.invalid");
- + }
- + }
- + }
- +
- return super.processFormSubmission(request, response, object, errors);
- }
- /**
- * The onSubmit function receives the form/command object that was modified by the input form
- * and saves it to the db
- - *
- + *
- * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
- * javax.servlet.http.HttpServletResponse, java.lang.Object,
- * org.springframework.validation.BindException)
- @@ -260,7 +277,7 @@
- /**
- * This is called prior to displaying a form for the first time. It tells Spring the
- * form/command object to load into the request
- - *
- + *
- * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
- */
- protected Object formBackingObject(HttpServletRequest request) throws ServletException {
- @@ -292,7 +309,7 @@
- /**
- * Called prior to form display. Allows for data to be put in the request to be used in the view
- - *
- + *
- * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest)
- */
- protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement