Skip to content

Commit e4ae0d7

Browse files
authored
RESTWS-919: POST request to /session endpoint should return current session (#588)
1 parent 19e37de commit e4ae0d7

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

omod-1.9/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public Object get() {
8080
@RequestMapping(method = RequestMethod.POST)
8181
@ResponseBody
8282
@ResponseStatus(value = HttpStatus.OK)
83-
public void post(HttpServletRequest request, @RequestBody Map<String, String> body) {
83+
public Object post(HttpServletRequest request, @RequestBody Map<String, String> body) {
8484
String localeStr = body.get("locale");
8585
if (localeStr != null) {
8686
Locale locale = null;
@@ -108,6 +108,7 @@ public void post(HttpServletRequest request, @RequestBody Map<String, String> bo
108108
request.getSession().setAttribute("emrContext.sessionLocationId", location.getId());
109109
}
110110
}
111+
return get();
111112
}
112113

113114
/**

omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/SessionController1_9Test.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,26 @@ public void get_shouldReturnCurrentProviderIfTheUserIsAuthenticated() throws Exc
116116
Assert.assertNotNull(currentProvider);
117117
Assert.assertTrue(currentProvider.toString().contains("Super User"));
118118
}
119+
120+
@Test
121+
public void post_shouldReturnTheCurrentSession() throws Exception{
122+
String content = "{}";
123+
Object ret = controller.post(hsr,new ObjectMapper().readValue(content, HashMap.class));
124+
Object currentProvider = PropertyUtils.getProperty(ret, "currentProvider");
125+
Assert.assertNotNull(currentProvider);
126+
Assert.assertTrue(currentProvider.toString().contains("Super User"));
127+
}
119128

120129
@Test
121130
public void post_shouldSetTheUserLocale() throws Exception {
122131
Locale newLocale = new Locale("sp");
123132
String content = "{\"locale\":\"" + newLocale.toString() + "\"}";
124133
Assert.assertNotEquals(newLocale, Context.getLocale());
125-
controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class));
134+
Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class));
126135
Assert.assertEquals(newLocale, Context.getLocale());
136+
Assert.assertEquals(Context.getLocale(), PropertyUtils.getProperty(ret, "locale"));
137+
Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(),
138+
((List<Locale>) PropertyUtils.getProperty(ret, "allowedLocales")).toArray());
127139
}
128140

129141
@Test(expected = APIException.class)
@@ -145,14 +157,16 @@ public void post_shouldSetTheSessionLocation() throws Exception {
145157
String content = "{\"sessionLocation\":\"" + XANADU_UUID + "\"}";
146158
Location loc = Context.getLocationService().getLocationByUuid(XANADU_UUID);
147159
Assert.assertNotEquals(loc, Context.getUserContext().getLocation());
148-
controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class));
160+
Object ret = controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class));
149161
Assert.assertEquals(loc, Context.getUserContext().getLocation());
162+
Object responseLoc = PropertyUtils.getProperty(ret, "sessionLocation");
163+
Assert.assertTrue(responseLoc.toString() + " should contain 'display=Xanadu'",
164+
responseLoc.toString().contains("display=Xanadu"));
150165
}
151166

152167
@Test(expected = APIException.class)
153168
public void post_shouldFailWhenSettingNonexistantLocation() throws Exception {
154169
String content = "{\"sessionLocation\":\"fake-nonexistant-uuid\"}";
155170
controller.post(hsr, new ObjectMapper().readValue(content, HashMap.class));
156171
}
157-
158172
}

0 commit comments

Comments
 (0)