Skip to content

Commit

Permalink
RESTWS-954: Ensures session endpoint returns locale even when user is…
Browse files Browse the repository at this point in the history
… not logged in (#621)
  • Loading branch information
mherman22 authored Sep 10, 2024
1 parent 8764f60 commit 07fd727
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public Object get() {
boolean authenticated = Context.isAuthenticated();
SimpleObject session = new SimpleObject();
session.add("authenticated", authenticated);
session.add("locale", Context.getLocale());
session.add("allowedLocales", Context.getAdministrationService().getAllowedLocales());
if (authenticated) {
session.add("user", ConversionUtil.convertToRepresentation(Context.getAuthenticatedUser(),
new CustomRepresentation(USER_CUSTOM_REP)));
session.add("locale", Context.getLocale());
session.add("allowedLocales", Context.getAdministrationService().getAllowedLocales());
session.add("sessionLocation", ConversionUtil.convertToRepresentation(Context.getUserContext().getLocation(), Representation.REF));
session.add("currentProvider", ConversionUtil.convertToRepresentation(getCurrentProvider(), Representation.REF));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -85,6 +83,22 @@ public void get_shouldReturnTheUserIfTheUserIsAuthenticated() throws Exception {
Assert.assertEquals(Context.getAuthenticatedUser().getPerson().getUuid(),
PropertyUtils.getProperty(personProp, "uuid"));
}

@Test
public void get_shouldReturnLocaleInfoIfTheUserIsNotAuthenticated() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
Assert.assertTrue(Context.isAuthenticated());

// log out the current authenticated user
controller.delete(hsr);
Assert.assertFalse(Context.isAuthenticated());
Assert.assertNull(hsr.getSession(false));

// check if the unauthenticated user response has locale and allowedLocales
Object ret = controller.get();
Assert.assertEquals(Context.getLocale(), PropertyUtils.getProperty(ret, "locale"));
Assert.assertArrayEquals(Context.getAdministrationService().getAllowedLocales().toArray(),
((List<Locale>) PropertyUtils.getProperty(ret, "allowedLocales")).toArray());
}

@Test
public void get_shouldReturnLocaleInfoIfTheUserIsAuthenticated() throws Exception {
Expand Down

0 comments on commit 07fd727

Please sign in to comment.