PageRenderTime 42ms CodeModel.GetById 19ms app.highlight 17ms RepoModel.GetById 2ms app.codeStats 0ms

/sigmah/src/test/java/org/sigmah/server/bootstrap/LoginControllerTest.java

http://sigma-h.googlecode.com/
Java | 109 lines | 68 code | 36 blank | 5 comment | 0 complexity | d3dedc654bbaf67ce30d10d0934b2b33 MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.server.bootstrap;
  7
  8import org.junit.Before;
  9import org.junit.Test;
 10import org.sigmah.server.Cookies;
 11import org.sigmah.server.bootstrap.model.LoginPageModel;
 12
 13import javax.servlet.ServletException;
 14import java.io.IOException;
 15
 16import static org.junit.Assert.*;
 17
 18public class LoginControllerTest extends ControllerTestCase {
 19
 20
 21    @Before
 22    public void setUp() {
 23        controller = new LoginController(injector, templateCfg);
 24
 25        req.setRequestURL("http://activityinfo.org/login");
 26    }
 27
 28    @Test
 29    public void requestShouldReceiveView() throws IOException, ServletException {
 30
 31        get();
 32
 33        assertNull(resp.redirectUrl);
 34        assertTemplateUsed(LoginPageModel.class);
 35    }
 36
 37    @Test
 38    public void urlSuffixIsParsedCorrectly() throws IOException, ServletException {
 39
 40        get("http://activityinfo.org/login#charts");
 41
 42        assertEquals("#charts", controller.parseUrlSuffix(req));
 43    }
 44
 45    @Test
 46    public void bookmarkShouldBeIncludedInModel() throws IOException, ServletException {
 47
 48        get("http://activityinfo.org/login#charts");
 49
 50        assertNull(resp.redirectUrl);
 51        assertTemplateUsed(LoginPageModel.class);
 52        assertEquals("#charts", lastLoginPageModel().getUrlSuffix());
 53    }
 54
 55
 56    @Test
 57    public void invalidEmailShouldResultInLoginPageWithErrorMessage() throws IOException, ServletException {
 58
 59        req.addParameter("email", "notreallyanemail@dot.com");
 60        req.addParameter("password", WRONG_USER_PASS);
 61        try {
 62        	post();
 63        }catch (Exception e) {
 64        	//ignore
 65        }
 66
 67        assertTemplateUsed(LoginPageModel.class);
 68        assertTrue("error message is displayed", lastLoginPageModel().isLoginError());
 69    }
 70
 71
 72    @Test
 73    public void invalidPasswordShouldResultInLoginPageWithErrorMessage() throws IOException, ServletException {
 74
 75        req.addParameter("email", USER_EMAIL);
 76        req.addParameter("password", WRONG_USER_PASS);
 77
 78        post();
 79
 80        assertTemplateUsed(LoginPageModel.class);
 81        assertTrue("error message is displayed", lastLoginPageModel().isLoginError());
 82    }
 83
 84    @Test
 85    public void correctLoginShouldRedirectToHostPage() throws IOException, ServletException {
 86
 87        req.addParameter("email", USER_EMAIL);
 88        req.addParameter("password", CORRECT_USER_PASS);
 89
 90        post();
 91
 92        assertNotNull("redirected", resp.redirectUrl);
 93        assertEquals("new auth token", NEW_AUTH_TOKEN, resp.getCookie(Cookies.AUTH_TOKEN_COOKIE));
 94    }
 95
 96    @Test
 97    public void bookmarkShouldBeSentToHostPage() throws IOException, ServletException {
 98
 99        req.addParameter("email", USER_EMAIL);
100        req.addParameter("password", CORRECT_USER_PASS);
101        req.addParameter("urlSuffix", "#map");
102
103        post();
104
105        assertTrue(resp.redirectUrl.endsWith("#map"));
106    }
107
108
109}