Advertisement
Guest User

patch2_TRUNK-2498

a guest
Apr 3rd, 2012
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.80 KB | None | 0 0
  1. Index: webapp/src/main/webapp/WEB-INF/messages.properties
  2. ===================================================================
  3. --- webapp/src/main/webapp/WEB-INF/messages.properties (revision 26253)
  4. +++ webapp/src/main/webapp/WEB-INF/messages.properties (revision )
  5. @@ -339,6 +339,9 @@
  6. error.options.secretAnswer.match=Answers do not match
  7. error.options.secretAnswer.empty=Answer should not be empty
  8. error.options.secretQuestion.empty=Question should not be empty
  9. +error.options.notificationAddress.empty=Notifications-Email address should not be empty
  10. +error.options.notificationAddress.invalid=Notifications-Email address should be a valid one
  11. +
  12. error.username.weak=Invalid username. Must be at least 6 characters
  13. error.username.invalid=Invalid username. Username must be alphanumeric and cannot start with a number
  14. error.retired.requireMetadata=Who retired this and why?
  15. Index: web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java
  16. ===================================================================
  17. --- web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java (revision 17337)
  18. +++ web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java (revision )
  19. @@ -1,20 +1,26 @@
  20. package org.openmrs.web.controller;
  21.  
  22. -import static org.junit.Assert.assertEquals;
  23. -import static org.junit.Assert.assertNull;
  24. -
  25. import javax.servlet.http.HttpServletResponse;
  26.  
  27. +import org.databene.commons.Assert;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.openmrs.User;
  31. import org.openmrs.api.context.Context;
  32. import org.openmrs.api.db.LoginCredential;
  33. import org.openmrs.api.db.UserDAO;
  34. +import org.openmrs.test.Verifies;
  35. +import org.openmrs.util.OpenmrsConstants;
  36. +import org.openmrs.web.controller.encounter.LocationFormController;
  37. import org.openmrs.web.test.BaseWebContextSensitiveTest;
  38. import org.springframework.mock.web.MockHttpServletRequest;
  39. import org.springframework.mock.web.MockHttpServletResponse;
  40. +import org.springframework.validation.BeanPropertyBindingResult;
  41. +import org.springframework.validation.BindingResult;
  42. +import org.springframework.web.servlet.ModelAndView;
  43.  
  44. +import static org.junit.Assert.*;
  45. +
  46. public class OptionsFormControllerTest extends BaseWebContextSensitiveTest {
  47.  
  48. private User user;
  49. @@ -31,57 +37,108 @@
  50. userDao = (UserDAO) applicationContext.getBean("userDAO");
  51. }
  52.  
  53. - @Test
  54. - public void shouldChangeSecretQuestionAndAnswer() throws Exception {
  55. - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  56. - request.setParameter("secretQuestionPassword", "test");
  57. - request.setParameter("secretQuestionNew", "test_question");
  58. -
  59. - String answer = "test_answer";
  60. - request.setParameter("secretAnswerNew", answer);
  61. - request.setParameter("secretAnswerConfirm", answer);
  62. -
  63. - HttpServletResponse response = new MockHttpServletResponse();
  64. - controller.handleRequest(request, response);
  65. -
  66. - LoginCredential loginCredential = userDao.getLoginCredential(user);
  67. - assertEquals(answer, loginCredential.getSecretAnswer());
  68. - }
  69. -
  70. - @Test
  71. - public void shouldRejectEmptySecretAnswer() throws Exception {
  72. - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  73. - request.setParameter("secretQuestionPassword", "test");
  74. - request.setParameter("secretQuestionNew", "test_question");
  75. -
  76. - String emptyAnswer = "";
  77. - request.setParameter("secretAnswerNew", emptyAnswer);
  78. - request.setParameter("secretAnswerConfirm", emptyAnswer);
  79. -
  80. - HttpServletResponse response = new MockHttpServletResponse();
  81. - controller.handleRequest(request, response);
  82. -
  83. - LoginCredential loginCredential = userDao.getLoginCredential(user);
  84. - assertNull(loginCredential.getSecretAnswer());
  85. - }
  86. -
  87. - @Test
  88. - public void shouldRejectEmptySecretQuestion() throws Exception {
  89. - LoginCredential loginCredential = userDao.getLoginCredential(user);
  90. - String originalQuestion = loginCredential.getSecretQuestion();
  91. -
  92. - MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  93. - request.setParameter("secretQuestionPassword", "test");
  94. - request.setParameter("secretQuestionNew", "");
  95. -
  96. - String emptyAnswer = "test_answer";
  97. - request.setParameter("secretAnswerNew", emptyAnswer);
  98. - request.setParameter("secretAnswerConfirm", emptyAnswer);
  99. -
  100. - HttpServletResponse response = new MockHttpServletResponse();
  101. - controller.handleRequest(request, response);
  102. -
  103. - loginCredential = userDao.getLoginCredential(user);
  104. - assertEquals(originalQuestion, loginCredential.getSecretQuestion());
  105. - }
  106. + @Test
  107. + public void shouldChangeSecretQuestionAndAnswer() throws Exception {
  108. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  109. + request.setParameter("secretQuestionPassword", "test");
  110. + request.setParameter("secretQuestionNew", "test_question");
  111. +
  112. + String answer = "test_answer";
  113. + request.setParameter("secretAnswerNew", answer);
  114. + request.setParameter("secretAnswerConfirm", answer);
  115. +
  116. + HttpServletResponse response = new MockHttpServletResponse();
  117. + controller.handleRequest(request, response);
  118. +
  119. + LoginCredential loginCredential = userDao.getLoginCredential(user);
  120. + assertEquals(answer, loginCredential.getSecretAnswer());
  121. + }
  122. +
  123. + @Test
  124. + public void shouldRejectEmptySecretAnswer() throws Exception {
  125. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  126. + request.setParameter("secretQuestionPassword", "test");
  127. + request.setParameter("secretQuestionNew", "test_question");
  128. +
  129. + String emptyAnswer = "";
  130. + request.setParameter("secretAnswerNew", emptyAnswer);
  131. + request.setParameter("secretAnswerConfirm", emptyAnswer);
  132. +
  133. + HttpServletResponse response = new MockHttpServletResponse();
  134. + controller.handleRequest(request, response);
  135. +
  136. + LoginCredential loginCredential = userDao.getLoginCredential(user);
  137. + assertNull(loginCredential.getSecretAnswer());
  138. + }
  139. +
  140. + @Test
  141. + public void shouldRejectEmptySecretQuestion() throws Exception {
  142. + LoginCredential loginCredential = userDao.getLoginCredential(user);
  143. + String originalQuestion = loginCredential.getSecretQuestion();
  144. +
  145. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  146. + request.setParameter("secretQuestionPassword", "test");
  147. + request.setParameter("secretQuestionNew", "");
  148. +
  149. + String emptyAnswer = "test_answer";
  150. + request.setParameter("secretAnswerNew", emptyAnswer);
  151. + request.setParameter("secretAnswerConfirm", emptyAnswer);
  152. +
  153. + HttpServletResponse response = new MockHttpServletResponse();
  154. + controller.handleRequest(request, response);
  155. +
  156. + loginCredential = userDao.getLoginCredential(user);
  157. + assertEquals(originalQuestion, loginCredential.getSecretQuestion());
  158. + }
  159. +
  160. + @Test
  161. + public void shouldRejectEmptyNotificationAddress() throws Exception {
  162. + String emptyAddress = "";
  163. +
  164. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  165. + request.setParameter("notification", "internal");
  166. + request.setParameter("notificationAddress", emptyAddress);
  167. +
  168. + HttpServletResponse response = new MockHttpServletResponse();
  169. + ModelAndView modelAndView = controller.handleRequest(request, response);
  170. + assertEquals("", request.getParameter("notificationAddress"));
  171. +
  172. + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
  173. + "org.springframework.validation.BindingResult.location");
  174. + org.junit.Assert.assertTrue(bindingResult.hasErrors());
  175. -}
  176. + }
  177. +
  178. + @Test
  179. + public void shouldRejectInvalidNotificationAddress() throws Exception {
  180. + String incorrectAddress = "gayan@gmail";
  181. +
  182. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  183. + request.setParameter("notification", "internal");
  184. + request.setParameter("notificationAddress", incorrectAddress);
  185. +
  186. + HttpServletResponse response = new MockHttpServletResponse();
  187. + ModelAndView modelAndView = controller.handleRequest(request, response);
  188. + assertNotNull(modelAndView.getModel().get("notificationAddress"));
  189. +
  190. + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
  191. + "org.springframework.validation.BindingResult.location");
  192. + org.junit.Assert.assertTrue(bindingResult.hasErrors());
  193. + }
  194. +
  195. + @Test
  196. + public void shouldAcceptValidNotificationAddress() throws Exception {
  197. + String correctAddress = "gayan@gmail.com";
  198. +
  199. + MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
  200. + request.setParameter("notification", "internal");
  201. + request.setParameter("notificationAddress", correctAddress);
  202. +
  203. + HttpServletResponse response = new MockHttpServletResponse();
  204. + ModelAndView modelAndView = controller.handleRequest(request, response);
  205. + assertNotNull(modelAndView.getModel().get("notificationAddress"));
  206. +
  207. + BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
  208. + "org.springframework.validation.BindingResult.location");
  209. + org.junit.Assert.assertTrue(!bindingResult.hasErrors());
  210. + }
  211. +}
  212. Index: web/src/main/java/org/openmrs/web/controller/OptionsFormController.java
  213. ===================================================================
  214. --- web/src/main/java/org/openmrs/web/controller/OptionsFormController.java (revision 23745)
  215. +++ web/src/main/java/org/openmrs/web/controller/OptionsFormController.java (revision )
  216. @@ -17,6 +17,8 @@
  217. import java.util.HashMap;
  218. import java.util.Locale;
  219. import java.util.Map;
  220. +import java.util.regex.Matcher;
  221. +import java.util.regex.Pattern;
  222.  
  223. import javax.servlet.ServletException;
  224. import javax.servlet.http.HttpServletRequest;
  225. @@ -47,7 +49,7 @@
  226. /**
  227. * This is the controller for the "My Profile" page. This lets logged in users set personal
  228. * preferences, update their own information, etc.
  229. - *
  230. + *
  231. * @see OptionsForm
  232. */
  233. public class OptionsFormController extends SimpleFormController {
  234. @@ -97,13 +99,28 @@
  235. }
  236. }
  237.  
  238. + String notifyType = opts.getNotification();
  239. +
  240. + if (notifyType.equals("internal") || notifyType.equals("internalProtected") || notifyType.equals("email")) {
  241. + if (opts.getNotificationAddress().isEmpty()) {
  242. + errors.reject("error.options.notificationAddress.empty");
  243. + } else {
  244. + String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  245. + Pattern pattern = Pattern.compile(EMAIL_PATTERN);
  246. + Matcher matcher = pattern.matcher(opts.getNotificationAddress());
  247. + if (matcher.matches() == false) {
  248. + errors.reject("error.options.notificationAddress.invalid");
  249. + }
  250. + }
  251. + }
  252. +
  253. return super.processFormSubmission(request, response, object, errors);
  254. }
  255.  
  256. /**
  257. * The onSubmit function receives the form/command object that was modified by the input form
  258. * and saves it to the db
  259. - *
  260. + *
  261. * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
  262. * javax.servlet.http.HttpServletResponse, java.lang.Object,
  263. * org.springframework.validation.BindException)
  264. @@ -260,7 +277,7 @@
  265. /**
  266. * This is called prior to displaying a form for the first time. It tells Spring the
  267. * form/command object to load into the request
  268. - *
  269. + *
  270. * @see org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
  271. */
  272. protected Object formBackingObject(HttpServletRequest request) throws ServletException {
  273. @@ -292,7 +309,7 @@
  274.  
  275. /**
  276. * Called prior to form display. Allows for data to be put in the request to be used in the view
  277. - *
  278. + *
  279. * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest)
  280. */
  281. protected Map<String, Object> referenceData(HttpServletRequest request) throws Exception {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement