|
| 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 | + */ |
1 | 14 | package org.openmrs.web.controller;
|
2 | 15 |
|
3 | 16 | import static org.hamcrest.Matchers.is;
|
|
19 | 32 | import org.openmrs.api.db.LoginCredential;
|
20 | 33 | import org.openmrs.api.db.UserDAO;
|
21 | 34 | import org.openmrs.util.OpenmrsConstants;
|
| 35 | +import org.openmrs.web.OptionsForm; |
22 | 36 | import org.openmrs.web.test.BaseWebContextSensitiveTest;
|
23 | 37 | import org.openmrs.web.test.WebTestHelper;
|
24 | 38 | import org.springframework.beans.factory.annotation.Autowired;
|
25 | 39 | import org.springframework.mock.web.MockHttpServletRequest;
|
26 | 40 | import org.springframework.mock.web.MockHttpServletResponse;
|
| 41 | +import org.springframework.validation.BeanPropertyBindingResult; |
| 42 | +import org.springframework.web.servlet.ModelAndView; |
27 | 43 |
|
28 | 44 | public class OptionsFormControllerTest extends BaseWebContextSensitiveTest {
|
29 | 45 |
|
@@ -169,4 +185,40 @@ public void onSubmit_shouldReject1CharacterAsUsername() throws Exception {
|
169 | 185 | //then
|
170 | 186 | Assert.assertThat("a", is(not(Context.getAuthenticatedUser().getUsername())));
|
171 | 187 | }
|
| 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 | + } |
172 | 224 | }
|
0 commit comments