ConfigAuthorizationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain) throws ServletException, IOException { logger.info("Checking authorization"); if (application.isConfigured()) { if (application.isEditableConfig()) { if (isAlreadyLoggedIn(request)) { dispatchRequest(request, response, filterChain); } else { redirectToLoginPage(request, response); } } else { logger.debug("Already configured - returning to list repos view"); response.sendRedirect(request.getContextPath() + "/repos/list"); } } else { filterChain.doFilter(request, response); } } ConfigAuthorizationFilter(final Application application);   }
@Test public void testDoFilterInternalApplicationNotConfigured() throws Exception { final ConfigAuthorizationFilter filter = new ConfigAuthorizationFilter(application); final HttpServletRequest request = new MockHttpServletRequest(); final HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class); final MockFilterChain filterChain = new MockFilterChain(); application.setConfigured(false); EasyMock.replay(response); filter.doFilterInternal(request, response, filterChain); EasyMock.verify(response); assertSame(request, filterChain.getRequest()); assertSame(response, filterChain.getResponse()); }
@Test public void testDoFilterInternalApplicationConfiguredEditDisabled() throws Exception { final ConfigAuthorizationFilter filter = new ConfigAuthorizationFilter(application); final HttpServletRequest request = new MockHttpServletRequest(); final HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class); final MockFilterChain filterChain = new MockFilterChain(); application.setConfigured(true); application.setEditableConfig(false); response.sendRedirect("/repos/list"); EasyMock.replay(response); filter.doFilterInternal(request, response, filterChain); EasyMock.verify(response); assertNull(filterChain.getRequest()); assertNull(filterChain.getResponse()); }
@Test public void testDoFilterInternalApplicationConfiguredEditEnabledAlreadyLoggedIn() throws Exception { final ConfigAuthorizationFilter filter = new ConfigAuthorizationFilter(application); final MockHttpSession session = new MockHttpSession(); session.setAttribute("isAdminLoggedIn", true); final MockHttpServletRequest request = new MockHttpServletRequest(); request.setSession(session); final HttpServletResponse response = new MockHttpServletResponse(); final MockFilterChain filterChain = new MockFilterChain(); application.setConfigured(true); application.setEditableConfig(true); filter.doFilterInternal(request, response, filterChain); assertSame(request, filterChain.getRequest()); assertSame(response, filterChain.getResponse()); assertTrue((Boolean) request.getAttribute("isEdit")); }