use of ninja.session.Session in project ninja by ninjaframework.
the class TemplateEngineFreemarkerTest method before.
@Before
public void before() throws Exception {
//Setup that allows to to execute invoke(...) in a very minimal version.
when(ninjaProperties.getWithDefault(FREEMARKER_CONFIGURATION_FILE_SUFFIX, ".ftl.html")).thenReturn(".ftl.html");
templateEngineFreemarker = new TemplateEngineFreemarker(messages, lang, logger, templateEngineHelper, templateEngineManager, templateEngineFreemarkerReverseRouteMethod, templateEngineFreemarkerAssetsAtMethod, templateEngineFreemarkerWebJarsAtMethod, ninjaProperties);
when(lang.getLanguage(any(Context.class), any(Optional.class))).thenReturn(Optional.<String>empty());
Session session = Mockito.mock(Session.class);
when(session.isEmpty()).thenReturn(true);
when(context.getSession()).thenReturn(session);
when(context.getRoute()).thenReturn(route);
when(lang.getLocaleFromStringOrDefault(any(Optional.class))).thenReturn(Locale.ENGLISH);
FlashScope flashScope = Mockito.mock(FlashScope.class);
Map<String, String> flashScopeData = new HashMap<>();
when(flashScope.getCurrentFlashCookieData()).thenReturn(flashScopeData);
when(context.getFlashScope()).thenReturn(flashScope);
when(templateEngineHelper.getTemplateForResult(any(Route.class), any(Result.class), Mockito.anyString())).thenReturn("views/template.ftl.html");
writer = new StringWriter();
ResponseStreams responseStreams = mock(ResponseStreams.class);
when(context.finalizeHeaders(any(Result.class))).thenReturn(responseStreams);
when(responseStreams.getWriter()).thenReturn(writer);
}
use of ninja.session.Session in project ninja by ninjaframework.
the class LoginLogoutController method loginPost.
public Result loginPost(@Param("username") String username, @Param("password") String password, @Param("rememberMe") Boolean rememberMe, Context context) {
boolean isUserNameAndPasswordValid = userDao.isUserAndPasswordValid(username, password);
if (isUserNameAndPasswordValid) {
Session session = context.getSession();
session.put("username", username);
if (rememberMe != null && rememberMe) {
session.setExpiryTime(24 * 60 * 60 * 1000L);
}
context.getFlashScope().success("login.loginSuccessful");
return Results.redirect("/");
} else {
// something is wrong with the input or password not found.
context.getFlashScope().put("username", username);
context.getFlashScope().put("rememberMe", rememberMe);
context.getFlashScope().error("login.errorLogin");
return Results.redirect("/login");
}
}
Aggregations