SecureFilter implements Filter { @Override public Result filter(FilterChain chain, Context context) { if (context.getSession() == null || context.getSession().get(USERNAME) == null) { Result result = ninja.getForbiddenResult(context); return result; } else { return chain.next(context); } } @Inject  SecureFilter(Ninja ninja); @Override Result filter(FilterChain chain, Context context); static final String USERNAME; }
@Test public void testSessionIsNotReturingWhenUserNameMissing() { when(context.getSession()).thenReturn(sessionCookie); when(sessionCookie.get(SecureFilter.USERNAME)).thenReturn(null); Result result = secureFilter.filter(filterChain, context); assertEquals(NinjaConstant.LOCATION_VIEW_FTL_HTML_FORBIDDEN, result.getTemplate()); verifyZeroInteractions(filterChain); }
@Test public void testWorkingSessionWhenUsernameIsThere() { when(context.getSession()).thenReturn(sessionCookie); when(sessionCookie.get(SecureFilter.USERNAME)).thenReturn("myname"); secureFilter.filter(filterChain, context); verify(filterChain).next(context); }