@@ -92,8 +92,10 @@ public class LoginPageControllerTest {
9292
9393 private static final String VIEW_LOCATIONS = "View Locations" ;
9494
95- private final UiUtils uiUtils = new UiUtils () {
96-
95+ private final UiUtils uiUtilsWithoutTimezones = new UiUtils () {
96+ @ Override
97+ public boolean convertTimezones () {return false ;}
98+
9799 @ Override
98100 public String pageLink (String providerName , String pageName ) {
99101 return new BasicUiUtils ().pageLink (providerName , pageName );
@@ -104,6 +106,21 @@ public String message(String code, Object... args) {
104106 return null ;
105107 }
106108 };
109+
110+ private final UiUtils uiUtilsWithTimezone = new UiUtils () {
111+ @ Override
112+ public boolean convertTimezones () {return true ;}
113+
114+ @ Override
115+ public String pageLink (String providerName , String pageName ) {
116+ return new BasicUiUtils ().pageLink (providerName , pageName );
117+ }
118+
119+ @ Override
120+ public String message (String code , Object ... args ) {
121+ return null ;
122+ }
123+ };
107124
108125 private UiSessionContext sessionContext ;
109126
@@ -114,6 +131,7 @@ public String message(String code, Object... args) {
114131 @ Before
115132 public void setup () {
116133 mockStatic (Context .class );
134+
117135 locationService = mock (LocationService .class );
118136 sessionContext = mock (UiSessionContext .class );
119137 appFrameworkService = mock (AppFrameworkService .class );
@@ -169,8 +187,8 @@ public void get_shouldRedirectTheUserToTheHomePageIfTheyAreAlreadyAuthenticatedA
169187 throws Exception {
170188 when (Context .isAuthenticated ()).thenReturn (true );
171189 when (Context .getUserContext ()).thenReturn (mock (UserContext .class ));
172- String homeRedirect = "redirect:" + uiUtils .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
173- assertEquals (homeRedirect , new LoginPageController ().get (null , uiUtils , createPageRequest (null , null ), null , null ,
190+ String homeRedirect = "redirect:" + uiUtilsWithoutTimezones .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
191+ assertEquals (homeRedirect , new LoginPageController ().get (null , uiUtilsWithoutTimezones , createPageRequest (null , null ), null , null ,
174192 appFrameworkService , administrationService ));
175193 }
176194
@@ -185,7 +203,7 @@ public void get_shouldRedirectTheUserToTheHomePageIfTheyAreAlreadyAuthenticatedA
185203 @ Verifies (value = "should show the user the login page if they are not authenticated" , method = "get(PageModel,UiUtils,PageRequest)" )
186204 public void get_shouldShowTheUserTheLoginPageIfTheyAreNotAuthenticated () throws Exception {
187205 when (Context .isAuthenticated ()).thenReturn (false );
188- assertNull (new LoginPageController ().get (new PageModel (), uiUtils , createPageRequest (null , null ), null , null ,
206+ assertNull (new LoginPageController ().get (new PageModel (), uiUtilsWithoutTimezones , createPageRequest (null , null ), null , null ,
189207 appFrameworkService , administrationService ));
190208 }
191209
@@ -207,7 +225,7 @@ public void get_shouldSetRedirectUrlInThePageModelIfOpenmrsRelatedUrlWasSpecifie
207225 request .addParameter (REQUEST_PARAMETER_NAME_REDIRECT_URL , redirectUrl );
208226 PageModel pageModel = new PageModel ();
209227
210- new LoginPageController ().get (pageModel , uiUtils , createPageRequest (request , null ), null , null , appFrameworkService ,
228+ new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , createPageRequest (request , null ), null , null , appFrameworkService ,
211229 administrationService );
212230
213231 assertEquals (redirectUrl , pageModel .get (REQUEST_PARAMETER_NAME_REDIRECT_URL ));
@@ -231,7 +249,7 @@ public void get_shouldSetTheRefererAsTheRedirectUrlInThePageModelIfNoRedirectPar
231249 request .addHeader ("Referer" , refererUrl );
232250 PageModel pageModel = new PageModel ();
233251
234- new LoginPageController ().get (pageModel , uiUtils , createPageRequest (request , null ), null , null , appFrameworkService ,
252+ new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , createPageRequest (request , null ), null , null , appFrameworkService ,
235253 administrationService );
236254
237255 assertEquals (refererUrl , pageModel .get (REQUEST_PARAMETER_NAME_REDIRECT_URL ));
@@ -258,7 +276,7 @@ public void get_shouldSetRedirectUrlInThePageModelIfAnyWasSpecifiedInTheSession(
258276 request .setSession (httpSession );
259277
260278 PageModel pageModel = new PageModel ();
261- new LoginPageController ().get (pageModel , uiUtils , pageRequest , null , null , appFrameworkService ,
279+ new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , pageRequest , null , null , appFrameworkService ,
262280 administrationService );
263281
264282 assertEquals (redirectUrl , pageModel .get (REQUEST_PARAMETER_NAME_REDIRECT_URL ));
@@ -286,7 +304,7 @@ public void get_shouldRedirectUserToRequestedUrlIfAuthenticated() throws Excepti
286304
287305 PageModel pageModel = new PageModel ();
288306
289- assertEquals ("redirect:" + redirectUrl , new LoginPageController ().get (pageModel , uiUtils , pageRequest , null , null ,
307+ assertEquals ("redirect:" + redirectUrl , new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , pageRequest , null , null ,
290308 appFrameworkService , administrationService ));
291309 }
292310
@@ -313,7 +331,7 @@ public void get_shouldRedirectUserToHomeIfAuthenticated() throws Exception {
313331
314332 PageModel pageModel = new PageModel ();
315333
316- assertNotEquals ("redirect:" + redirectUrl , new LoginPageController ().get (pageModel , uiUtils , pageRequest , null , null ,
334+ assertNotEquals ("redirect:" + redirectUrl , new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , pageRequest , null , null ,
317335 appFrameworkService , administrationService ));
318336 }
319337
@@ -326,7 +344,7 @@ public void get_shouldRedirectUserToHomeIfAuthenticated() throws Exception {
326344 public void get_shouldNotSetRedirectUrlParamAfterManualLogout () throws Exception {
327345 when (Context .isAuthenticated ()).thenReturn (false );
328346
329- final String homeRedirect = "redirect:" + uiUtils .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
347+ final String homeRedirect = "redirect:" + uiUtilsWithoutTimezones .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
330348
331349 MockHttpServletRequest request = new MockHttpServletRequest ();
332350 request .addHeader ("Referer" , "somePage/weDont/wantTo/beRedirected" );
@@ -337,7 +355,7 @@ public void get_shouldNotSetRedirectUrlParamAfterManualLogout() throws Exception
337355
338356 PageModel pageModel = new PageModel ();
339357
340- new LoginPageController ().get (pageModel , uiUtils , pageRequest , null , null , appFrameworkService ,
358+ new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , pageRequest , null , null , appFrameworkService ,
341359 administrationService );
342360
343361 assertEquals ("" , pageModel .getAttribute (ReferenceApplicationWebConstants .REQUEST_PARAMETER_NAME_REDIRECT_URL ));
@@ -353,7 +371,7 @@ public void get_shouldNotSetRedirectUrlParamAfterManualLogout() throws Exception
353371 public void post_shouldRedirectNewUserToHome () throws Exception {
354372 setupMocksForSuccessfulAuthentication (true );
355373
356- final String homeRedirect = "redirect:" + uiUtils .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
374+ final String homeRedirect = "redirect:" + uiUtilsWithoutTimezones .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
357375 String redirectUrl = TEST_CONTEXT_PATH + "/referenceapplication/patient.page" ;
358376
359377 MockHttpServletRequest request = new MockHttpServletRequest ();
@@ -365,8 +383,8 @@ public void post_shouldRedirectNewUserToHome() throws Exception {
365383
366384 mockAuthenticatedUser ();
367385
368- assertEquals (homeRedirect , new LoginPageController ().post (USERNAME , PASSWORD , SESSION_LOCATION_ID , locationService ,
369- administrationService , uiUtils , null , pageRequest , sessionContext ));
386+ assertEquals (homeRedirect , new LoginPageController ().post (USERNAME , PASSWORD , SESSION_LOCATION_ID , null , locationService ,
387+ administrationService , uiUtilsWithoutTimezones , null , pageRequest , sessionContext ));
370388 }
371389
372390 /**
@@ -389,7 +407,7 @@ public void post_shouldRedirectOldUserToRedirectUrl() throws Exception {
389407 mockAuthenticatedUser ();
390408
391409 assertEquals ("redirect:" + redirectUrl , new LoginPageController ().post (USERNAME , PASSWORD , SESSION_LOCATION_ID ,
392- locationService , administrationService , uiUtils , null , pageRequest , sessionContext ));
410+ null , locationService , administrationService , uiUtilsWithoutTimezones , null , pageRequest , sessionContext ));
393411 }
394412
395413 /**
@@ -401,17 +419,17 @@ public void post_shouldRedirectOldUserToRedirectUrl() throws Exception {
401419 public void post_shouldRedirectTheUserToTheHomePageIfTheRedirectUrlIsTheLoginPage () throws Exception {
402420 setupMocksForSuccessfulAuthentication (true );
403421
404- final String redirectUrl = uiUtils .pageLink (ReferenceApplicationConstants .MODULE_ID , "login" );
405- final String homeRedirect = "redirect:" + uiUtils .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
422+ final String redirectUrl = uiUtilsWithoutTimezones .pageLink (ReferenceApplicationConstants .MODULE_ID , "login" );
423+ final String homeRedirect = "redirect:" + uiUtilsWithoutTimezones .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
406424 MockHttpServletRequest request = new MockHttpServletRequest ();
407425 request .setContextPath ("/openmrs" );
408426 request .setParameter (REQUEST_PARAMETER_NAME_REDIRECT_URL , redirectUrl );
409427 PageRequest pageRequest = createPageRequest (request , null );
410428
411429 mockAuthenticatedUser ();
412430
413- assertEquals (homeRedirect , new LoginPageController ().post (USERNAME , PASSWORD , SESSION_LOCATION_ID , locationService ,
414- administrationService , uiUtils , null , pageRequest , sessionContext ));
431+ assertEquals (homeRedirect , new LoginPageController ().post (USERNAME , PASSWORD , SESSION_LOCATION_ID , null , locationService ,
432+ administrationService , uiUtilsWithoutTimezones , null , pageRequest , sessionContext ));
415433
416434 }
417435
@@ -434,9 +452,9 @@ private void mockAuthenticatedUser() {
434452 public void post_shouldSendTheUserBackToTheLoginPageWhenAuthenticationFails () throws Exception {
435453 when (Context .isAuthenticated ()).thenReturn (false );
436454 MockHttpServletRequest request = new MockHttpServletRequest ();
437- String page = new LoginPageController ().post (null , null , SESSION_LOCATION_ID , locationService , administrationService ,
438- uiUtils , appFrameworkService , createPageRequest (request , null ), sessionContext );
439- assertEquals ("redirect:" + uiUtils .pageLink ("referenceapplication" , "login" ), page );
455+ String page = new LoginPageController ().post (null , null , SESSION_LOCATION_ID , null , locationService , administrationService ,
456+ uiUtilsWithoutTimezones , appFrameworkService , createPageRequest (request , null ), sessionContext );
457+ assertEquals ("redirect:" + uiUtilsWithoutTimezones .pageLink ("referenceapplication" , "login" ), page );
440458 }
441459
442460 /**
@@ -448,9 +466,9 @@ public void post_shouldSendTheUserBackToTheLoginPageWhenAuthenticationFails() th
448466 public void post_shouldSendTheUserBackToTheLoginPageIfAnInvalidLocationIsSelected () throws Exception {
449467 setupMocksForSuccessfulAuthentication (false );
450468 MockHttpServletRequest request = new MockHttpServletRequest ();
451- String page = new LoginPageController ().post (USERNAME , PASSWORD , SESSION_LOCATION_ID , locationService ,
452- administrationService , uiUtils , null , createPageRequest (request , null ), sessionContext );
453- assertEquals ("redirect:" + uiUtils .pageLink ("referenceapplication" , "login" ), page );
469+ String page = new LoginPageController ().post (USERNAME , PASSWORD , SESSION_LOCATION_ID , null , locationService ,
470+ administrationService , uiUtilsWithoutTimezones , null , createPageRequest (request , null ), sessionContext );
471+ assertEquals ("redirect:" + uiUtilsWithoutTimezones .pageLink ("referenceapplication" , "login" ), page );
454472 }
455473
456474 /**
@@ -468,7 +486,7 @@ public void get_shouldNotSetTheRefererAsTheRedirectUrlInThePageModelIfRefererUrl
468486 request .setContextPath (TEST_CONTEXT_PATH );
469487 request .addHeader ("Referer" , refererUrl );
470488 PageModel pageModel = new PageModel ();
471- new LoginPageController ().get (pageModel , uiUtils , createPageRequest (request , null ), null , null , appFrameworkService ,
489+ new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , createPageRequest (request , null ), null , null , appFrameworkService ,
472490 administrationService );
473491
474492 assertEquals ("" , pageModel .get (REQUEST_PARAMETER_NAME_REDIRECT_URL ));
@@ -490,7 +508,7 @@ public void get_shouldSetTheRefererAsTheRedirectUrlInThePageModelIfRefererUrlIsW
490508 request .setContextPath (TEST_CONTEXT_PATH );
491509 request .addHeader ("Referer" , refererUrl );
492510 PageModel pageModel = new PageModel ();
493- new LoginPageController ().get (pageModel , uiUtils , createPageRequest (request , null ), null , null , appFrameworkService ,
511+ new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , createPageRequest (request , null ), null , null , appFrameworkService ,
494512 administrationService );
495513
496514 assertEquals (redirectUrl , pageModel .get (REQUEST_PARAMETER_NAME_REDIRECT_URL ));
@@ -521,7 +539,7 @@ public void get_shouldChooseRedirectUrlOverReferer() throws Exception {
521539
522540 PageModel pageModel = new PageModel ();
523541
524- assertEquals ("redirect:" + redirectUrl , new LoginPageController ().get (pageModel , uiUtils , pageRequest , null , null ,
542+ assertEquals ("redirect:" + redirectUrl , new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , pageRequest , null , null ,
525543 appFrameworkService , administrationService ));
526544 }
527545
@@ -542,9 +560,9 @@ public void post_shouldSendUserToLocationSelectionPageIfTheyAreAssociatedWithMul
542560 final String expectedPage = "redirect:/openmrs/" + ReferenceApplicationConstants .MODULE_ID + "/login.page" ;
543561 ConversionService conversionService = mock (ConversionService .class );
544562 when (conversionService .convert (eq (true ), eq (String .class ))).thenReturn ("true" );
545- Whitebox .setInternalState (uiUtils , "conversionService" , conversionService );
546- String page = new LoginPageController ().post (USERNAME , PASSWORD , null , locationService , administrationService ,
547- uiUtils , appFrameworkService , createPageRequest (request , null ), sessionContext );
563+ Whitebox .setInternalState (uiUtilsWithoutTimezones , "conversionService" , conversionService );
564+ String page = new LoginPageController ().post (USERNAME , PASSWORD , null , null , locationService , administrationService ,
565+ uiUtilsWithoutTimezones , appFrameworkService , createPageRequest (request , null ), sessionContext );
548566 assertEquals (expectedPage , page );
549567 }
550568
@@ -574,7 +592,7 @@ public void get_shouldScaleDownLoginLocationsToUserSpecificOnesInCaseMultipleLoc
574592 PageRequest pageRequest = createPageRequest (request , null );
575593 PageModel pageModel = new PageModel ();
576594
577- assertNull (new LoginPageController ().get (pageModel , uiUtils , pageRequest , null , locationService , appFrameworkService ,
595+ assertNull (new LoginPageController ().get (pageModel , uiUtilsWithoutTimezones , pageRequest , null , locationService , appFrameworkService ,
578596 administrationService ));
579597 List <Location > locations = (List ) pageModel .getAttribute ("locations" );
580598 assertEquals (2 , locations .size ());
@@ -587,7 +605,7 @@ public void post_shouldAutoSelectALocationAfterAuthenticationIfLoginLocationsSiz
587605 when (Context .isAuthenticated ()).thenReturn (false ).thenReturn (true );
588606 when (Context .getAuthenticatedUser ()).thenReturn (mock (User .class ));
589607 final int locationId = 1 ;
590- final String homeRedirect = "redirect:" + uiUtils .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
608+ final String homeRedirect = "redirect:" + uiUtilsWithoutTimezones .pageLink (ReferenceApplicationConstants .MODULE_ID , "home" );
591609 PageRequest pageRequest = createPageRequest (new MockHttpServletRequest (), null );
592610
593611 Location location = new Location (locationId );
@@ -596,10 +614,29 @@ public void post_shouldAutoSelectALocationAfterAuthenticationIfLoginLocationsSiz
596614 when (administrationService .getGlobalProperty (eq (ReferenceApplicationConstants .LOCATION_USER_PROPERTY_NAME )))
597615 .thenReturn ("someValue" );
598616
599- assertEquals (homeRedirect , new LoginPageController ().post (USERNAME , PASSWORD , null , locationService ,
600- administrationService , uiUtils , appFrameworkService , pageRequest , sessionContext ));
617+ assertEquals (homeRedirect , new LoginPageController ().post (USERNAME , PASSWORD , null , null , locationService ,
618+ administrationService , uiUtilsWithoutTimezones , appFrameworkService , pageRequest , sessionContext ));
601619
602620 verify (sessionContext , times (1 )).setSessionLocation (eq (location ));
603621 }
604-
622+
623+ @ Test
624+ public void post_shouldNotAutoSelectALocationAfterAuthenticationIfLoginLocationsSizeIsOneAndUsingTimezonesWithoutClientTimezone () throws Exception {
625+ when (Context .isAuthenticated ()).thenReturn (false ).thenReturn (true );
626+ when (Context .getAuthenticatedUser ()).thenReturn (mock (User .class ));
627+ final int LOCATION_ID = 1 ;
628+ final String LOCATION_SELECTION_PAGE = "redirect:/openmrs/" + ReferenceApplicationConstants .MODULE_ID + "/login.page" ;
629+ PageRequest pageRequest = createPageRequest (new MockHttpServletRequest (), null );
630+
631+ Location location = new Location (LOCATION_ID );
632+ location .addTag (new LocationTag (EmrApiConstants .LOCATION_TAG_SUPPORTS_LOGIN , null ));
633+ when (appFrameworkService .getLoginLocations ()).thenReturn (Collections .singletonList (location ));
634+ when (administrationService .getGlobalProperty (eq (ReferenceApplicationConstants .LOCATION_USER_PROPERTY_NAME )))
635+ .thenReturn ("someValue" );
636+
637+ assertEquals (LOCATION_SELECTION_PAGE , new LoginPageController ().post (USERNAME , PASSWORD , null , null , locationService ,
638+ administrationService , uiUtilsWithTimezone , appFrameworkService , pageRequest , sessionContext ));
639+
640+ }
641+
605642}
0 commit comments