/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. package org.sigmah.server.bootstrap;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.sigmah.server.Cookies;
  9. import org.sigmah.server.bootstrap.model.LoginPageModel;
  10. import javax.servlet.ServletException;
  11. import java.io.IOException;
  12. import static org.junit.Assert.*;
  13. public class LoginControllerTest extends ControllerTestCase {
  14. @Before
  15. public void setUp() {
  16. controller = new LoginController(injector, templateCfg);
  17. req.setRequestURL("http://activityinfo.org/login");
  18. }
  19. @Test
  20. public void requestShouldReceiveView() throws IOException, ServletException {
  21. get();
  22. assertNull(resp.redirectUrl);
  23. assertTemplateUsed(LoginPageModel.class);
  24. }
  25. @Test
  26. public void urlSuffixIsParsedCorrectly() throws IOException, ServletException {
  27. get("http://activityinfo.org/login#charts");
  28. assertEquals("#charts", controller.parseUrlSuffix(req));
  29. }
  30. @Test
  31. public void bookmarkShouldBeIncludedInModel() throws IOException, ServletException {
  32. get("http://activityinfo.org/login#charts");
  33. assertNull(resp.redirectUrl);
  34. assertTemplateUsed(LoginPageModel.class);
  35. assertEquals("#charts", lastLoginPageModel().getUrlSuffix());
  36. }
  37. @Test
  38. public void invalidEmailShouldResultInLoginPageWithErrorMessage() throws IOException, ServletException {
  39. req.addParameter("email", "notreallyanemail@dot.com");
  40. req.addParameter("password", WRONG_USER_PASS);
  41. try {
  42. post();
  43. }catch (Exception e) {
  44. //ignore
  45. }
  46. assertTemplateUsed(LoginPageModel.class);
  47. assertTrue("error message is displayed", lastLoginPageModel().isLoginError());
  48. }
  49. @Test
  50. public void invalidPasswordShouldResultInLoginPageWithErrorMessage() throws IOException, ServletException {
  51. req.addParameter("email", USER_EMAIL);
  52. req.addParameter("password", WRONG_USER_PASS);
  53. post();
  54. assertTemplateUsed(LoginPageModel.class);
  55. assertTrue("error message is displayed", lastLoginPageModel().isLoginError());
  56. }
  57. @Test
  58. public void correctLoginShouldRedirectToHostPage() throws IOException, ServletException {
  59. req.addParameter("email", USER_EMAIL);
  60. req.addParameter("password", CORRECT_USER_PASS);
  61. post();
  62. assertNotNull("redirected", resp.redirectUrl);
  63. assertEquals("new auth token", NEW_AUTH_TOKEN, resp.getCookie(Cookies.AUTH_TOKEN_COOKIE));
  64. }
  65. @Test
  66. public void bookmarkShouldBeSentToHostPage() throws IOException, ServletException {
  67. req.addParameter("email", USER_EMAIL);
  68. req.addParameter("password", CORRECT_USER_PASS);
  69. req.addParameter("urlSuffix", "#map");
  70. post();
  71. assertTrue(resp.redirectUrl.endsWith("#map"));
  72. }
  73. }