From e4ae0d702eb7c060928dd06cccb1856f4d928aa1 Mon Sep 17 00:00:00 2001 From: Manoj Lakshan <48247516+ManojLL@users.noreply.github.com> Date: Wed, 13 Sep 2023 00:38:11 +0530 Subject: [PATCH] RESTWS-919: POST request to /session endpoint should return current session (#588) --- .../openmrs1_9/SessionController1_9.java | 3 ++- .../openmrs1_9/SessionController1_9Test.java | 20 ++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java index 5924ab1dd..e19e6d1fd 100644 --- a/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java +++ b/omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java @@ -80,7 +80,7 @@ public Object get() { @RequestMapping(method = RequestMethod.POST) @ResponseBody @ResponseStatus(value = HttpStatus.OK) - public void post(HttpServletRequest request, @RequestBody Map body) { + public Object post(HttpServletRequest request, @RequestBody Map body) { String localeStr = body.get("locale"); if (localeStr != null) { Locale locale = null; @@ -108,6 +108,7 @@ public void post(HttpServletRequest request, @RequestBody Map bo request.getSession().setAttribute("emrContext.sessionLocationId", location.getId()); } } + return get(); } /** diff --git a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java index 445b40803..5ff929b84 100644 --- a/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java +++ b/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java @@ -116,14 +116,26 @@ public void get_shouldReturnCurrentProviderIfTheUserIsAuthenticated() throws Exc Assert.assertNotNull(currentProvider); Assert.assertTrue(currentProvider.toString().contains("Super User")); } + + @Test + public void post_shouldReturnTheCurrentSession() throws Exception{ + String content = "{}"; + Object ret = controller.post(hsr,new ObjectMapper().readValue(content, HashMap.class)); + Object currentProvider = PropertyUtils.getProperty(ret, "currentProvider"); + Assert.assertNotNull(currentProvider); + Assert.assertTrue(currentProvider.toString().contains("Super User")); + } @Test public void post_shouldSetTheUserLocale() throws Exception { Locale newLocale = new Locale("sp"); String content = "{\"locale\":\"" + newLocale.toString() + "\"}"; Assert.assertNotEquals(newLocale, Context.getLocale()); - controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); + Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); Assert.assertEquals(newLocale, Context.getLocale()); + Assert.assertEquals(Context.getLocale(), PropertyUtils.getProperty(ret, "locale")); + Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(), + ((List) PropertyUtils.getProperty(ret, "allowedLocales")).toArray()); } @Test(expected = APIException.class) @@ -145,8 +157,11 @@ public void post_shouldSetTheSessionLocation() throws Exception { String content = "{\"sessionLocation\":\"" + XANADU_UUID + "\"}"; Location loc = Context.getLocationService().getLocationByUuid(XANADU_UUID); Assert.assertNotEquals(loc, Context.getUserContext().getLocation()); - controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); + Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); Assert.assertEquals(loc, Context.getUserContext().getLocation()); + Object responseLoc = PropertyUtils.getProperty(ret, "sessionLocation"); + Assert.assertTrue(responseLoc.toString() + " should contain 'display=Xanadu'", + responseLoc.toString().contains("display=Xanadu")); } @Test(expected = APIException.class) @@ -154,5 +169,4 @@ public void post_shouldFailWhenSettingNonexistantLocation() throws Exception { String content = "{\"sessionLocation\":\"fake-nonexistant-uuid\"}"; controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class)); } - }