package edu.northwestern.bioinformatics.studycalendar.web;

import static edu.northwestern.bioinformatics.studycalendar.utils.accesscontrol.LoginCheckInterceptor.REQUESTED_URL_ATTRIBUTE;
import gov.nih.nci.security.exceptions.CSException;
import static org.easymock.classextension.EasyMock.expect;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Padmaja Vedula
 * @author Rhett Sutphin
 */
public class LoginControllerTest extends ControllerTestCase {
    private LoginController controller;
    private LoginCommand command;

    protected void setUp() throws Exception {
        super.setUp();

        command = registerMockFor(LoginCommand.class, LoginCommand.class.getMethod("login"));

        controller = new LoginController() {
            protected Object formBackingObject(HttpServletRequest request) throws Exception {
                return command;
            }
        };
    }

    public void testViewOnGet() throws Exception {
        request.setMethod("GET");
        ModelAndView view = controller.handleRequest(request, response);
        assertEquals("login", view.getViewName());
    }

    public void testPostWithValidCredentialsAndNoRequestedUrl() throws Exception {
        expectLogin(true);
        session.setAttribute(REQUESTED_URL_ATTRIBUTE, null);

        replayMocks();
        ModelAndView actual = controller.handleRequest(request, response);
        verifyMocks();

        assertTrue("Default view not a redirect", actual.getView() instanceof RedirectView);
        RedirectView actualView = (RedirectView) actual.getView();
        assertEquals("/pages/studyList", actualView.getUrl());
    }

    public void testPostWithValidCredentialsAndRequestedUrl() throws Exception {
        expectLogin(true);
        String expectedRequestedUrl = "http://go/here/now";
        session.setAttribute(REQUESTED_URL_ATTRIBUTE, expectedRequestedUrl);

        replayMocks();
        ModelAndView actual = controller.handleRequest(request, response);
        verifyMocks();

        assertTrue("View not a redirect", actual.getView() instanceof RedirectView);
        RedirectView actualView = (RedirectView) actual.getView();
        assertEquals(expectedRequestedUrl, actualView.getUrl());
        assertNull(session.getAttribute(REQUESTED_URL_ATTRIBUTE));
    }

    public void testPostWithBadCredentials() throws Exception {
        expectLogin(false);

        replayMocks();
        ModelAndView actual = controller.handleRequest(request, response);
        verifyMocks();

        assertTrue("failed indicator missing", (Boolean) actual.getModel().get("failed"));
        assertEquals("login", actual.getViewName());
    }

    private void expectLogin(boolean success) throws CSException {
        expect(command.login()).andReturn(success);
    }

}

@@ -2775,24 +2775,698 @@
 ());%0A    %7D%0A%0A
+    public void testAjaxGet() throws Exception %7B%0A        request.setParameter(%22ajax%22, %22 %22);%0A        request.setMethod(%22GET%22);%0A        ModelAndView view = controller.handleRequest(request, response);%0A        assertEquals(%22relogin%22, view.getViewName());%0A    %7D%0A    %0A    public void testAjaxPostBadCredentials() throws Exception %7B%0A        request.setParameter(%22ajax%22, %22 %22);%0A        expectLogin(false);%0A%0A        replayMocks();%0A        ModelAndView actual = controller.handleRequest(request, response);%0A        verifyMocks();%0A%0A        assertTrue(%22failed indicator missing%22, (Boolean) actual.getModel().get(%22failed%22));%0A        assertEquals(%22relogin%22, actual.getViewName());%0A    %7D%0A%0A
     private 

Add tests for ajax mode