Skip to content

Commit d3bdfae

Browse files
committedJun 10, 2013
Validate email address in user options page - TRUNK-2498
1 parent 9037e46 commit d3bdfae

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
 

‎web/src/main/java/org/openmrs/web/controller/OptionsFormController.java

+12
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.commons.lang.StringUtils;
2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
30+
import org.apache.commons.validator.EmailValidator;
3031
import org.openmrs.PersonName;
3132
import org.openmrs.User;
3233
import org.openmrs.api.APIException;
@@ -197,6 +198,17 @@ protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse
197198
errors.rejectValue("secretQuestionPassword", "error.password.incorrect");
198199
}
199200

201+
String notifyType = opts.getNotification();
202+
if (notifyType != null) {
203+
if (notifyType.equals("internal") || notifyType.equals("internalProtected") || notifyType.equals("email")) {
204+
if (StringUtils.isNotEmpty(opts.getNotificationAddress())) {
205+
if (!EmailValidator.getInstance().isValid(opts.getNotificationAddress())) {
206+
errors.reject("error.options.notificationAddress.invalid");
207+
}
208+
}
209+
}
210+
}
211+
200212
if (opts.getUsername().length() > 0 && !errors.hasErrors()) {
201213
try {
202214
Context.addProxyPrivilege(PrivilegeConstants.VIEW_USERS);

‎web/src/test/java/org/openmrs/web/controller/OptionsFormControllerTest.java

+52
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* The contents of this file are subject to the OpenMRS Public License
3+
* Version 1.0 (the "License"); you may not use this file except in
4+
* compliance with the License. You may obtain a copy of the License at
5+
* http://license.openmrs.org
6+
*
7+
* Software distributed under the License is distributed on an "AS IS"
8+
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9+
* License for the specific language governing rights and limitations
10+
* under the License.
11+
*
12+
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
13+
*/
114
package org.openmrs.web.controller;
215

316
import static org.hamcrest.Matchers.is;
@@ -19,11 +32,14 @@
1932
import org.openmrs.api.db.LoginCredential;
2033
import org.openmrs.api.db.UserDAO;
2134
import org.openmrs.util.OpenmrsConstants;
35+
import org.openmrs.web.OptionsForm;
2236
import org.openmrs.web.test.BaseWebContextSensitiveTest;
2337
import org.openmrs.web.test.WebTestHelper;
2438
import org.springframework.beans.factory.annotation.Autowired;
2539
import org.springframework.mock.web.MockHttpServletRequest;
2640
import org.springframework.mock.web.MockHttpServletResponse;
41+
import org.springframework.validation.BeanPropertyBindingResult;
42+
import org.springframework.web.servlet.ModelAndView;
2743

2844
public class OptionsFormControllerTest extends BaseWebContextSensitiveTest {
2945

@@ -169,4 +185,40 @@ public void onSubmit_shouldReject1CharacterAsUsername() throws Exception {
169185
//then
170186
Assert.assertThat("a", is(not(Context.getAuthenticatedUser().getUsername())));
171187
}
188+
189+
@Test
190+
public void shouldRejectInvalidNotificationAddress() throws Exception {
191+
final String incorrectAddress = "gayan@gmail";
192+
MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
193+
request.setParameter("notification", "email");
194+
request.setParameter("notificationAddress", incorrectAddress);
195+
196+
HttpServletResponse response = new MockHttpServletResponse();
197+
ModelAndView modelAndView = controller.handleRequest(request, response);
198+
199+
OptionsForm optionsForm = (OptionsForm) controller.formBackingObject(request);
200+
assertEquals(incorrectAddress, optionsForm.getNotificationAddress());
201+
202+
BeanPropertyBindingResult bindingResult = (BeanPropertyBindingResult) modelAndView.getModel().get(
203+
"org.springframework.validation.BindingResult.opts");
204+
Assert.assertTrue(bindingResult.hasErrors());
205+
}
206+
207+
@Test
208+
public void shouldAcceptValidNotificationAddress() throws Exception {
209+
String notificationTypes[] = { "internal", "internalProtected", "email" };
210+
String correctAddress = "gayan@gmail.com";
211+
212+
for (String notifyType : notificationTypes) {
213+
MockHttpServletRequest request = new MockHttpServletRequest("POST", "");
214+
request.setParameter("notification", notifyType);
215+
request.setParameter("notificationAddress", correctAddress);
216+
217+
HttpServletResponse response = new MockHttpServletResponse();
218+
controller.handleRequest(request, response);
219+
220+
OptionsForm optionsForm = (OptionsForm) controller.formBackingObject(request);
221+
assertEquals(correctAddress, optionsForm.getNotificationAddress());
222+
}
223+
}
172224
}

‎webapp/src/main/webapp/WEB-INF/messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ error.username.weak=Invalid username. Must be at least 6 characters
372372
error.username.invalid=Invalid username. Username must be alphanumeric and cannot start with a number
373373
error.username.email=Invalid username. Username must be a valid e-mail.
374374
error.retired.requireMetadata=Who retired this and why?
375+
error.options.notificationAddress.invalid=Invalid notifications email address
375376

376377
changePassword.hint.password.length=Password should have at least {0} characters
377378
changePassword.hint.password.bothCasesRequired=both upper and lower case characters

0 commit comments

Comments
 (0)
Please sign in to comment.