ForgotPasswordTest.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* ===================================================================
  2. * ForgotPasswordTest.java
  3. *
  4. * Created Feb 11, 2005 6:18:43 PM
  5. *
  6. * Copyright (c) 2004 Matt Magoffin (spamsqr@msqr.us)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. * ===================================================================
  23. * $Id: ForgotPasswordTest.java 60 2009-05-11 09:18:28Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.biz.test;
  27. import magoffin.matt.ieat.AuthorizationException;
  28. import magoffin.matt.ieat.biz.UserBiz;
  29. import magoffin.matt.ieat.domain.User;
  30. import magoffin.matt.ieat.test.AbstractSpringEnabledTest;
  31. /**
  32. * Test case for forgot password process.
  33. *
  34. * @author Matt Magoffin (spamsqr@msqr.us)
  35. * @version $Revision: 60 $ $Date: 2009-05-11 21:18:28 +1200 (Mon, 11 May 2009) $
  36. */
  37. public class ForgotPasswordTest extends AbstractSpringEnabledTest {
  38. /**
  39. * Test forgot password.
  40. * @throws Exception if any error occurs
  41. */
  42. public void testForgotPassword() throws Exception {
  43. doTestWithRegisteredDummyUser(new TestWithDummyUser() {
  44. public void test(User user) throws Exception {
  45. String currPassword = user.getPassword();
  46. UserBiz userBiz = getUserBiz();
  47. String confirmCode = userBiz.forgotPassword(user.getLogin(),null);
  48. user = userBiz.getUserById(user.getUserId(),null);
  49. assertTrue("User's password should have changed in forgotPassword()",
  50. !currPassword.equals(user.getPassword()));
  51. assertTrue("Confirmation code from forgotPassword() should not be null",
  52. confirmCode != null);
  53. }
  54. });
  55. }
  56. /**
  57. * Test forgot password for login that does not exist.
  58. * @throws Exception if any error occurs
  59. */
  60. public void testForgotPasswordNonExistentUser() throws Exception {
  61. try {
  62. UserBiz userBiz = getUserBiz();
  63. userBiz.forgotPassword("this.user.does.not.exist",null);
  64. fail("An AuthorizationException should be thrown when trying to call forgotPassword() with an unkown user login");
  65. } catch ( AuthorizationException e ) {
  66. if ( AuthorizationException.UNKNOWN_LOGIN != e.getReason() ) {
  67. fail("The AuthorizationException reason code should be UNKNOWN_LOGIN");
  68. }
  69. } catch ( Exception e ) {
  70. fail("An AuthorizationException should be thrown when trying to call forgotPassword() with an unkown user login");
  71. }
  72. }
  73. /**
  74. * Test confirm forgot password.
  75. * @throws Exception if any error occurs
  76. */
  77. public void testConfirmForgotPassword() throws Exception {
  78. doTestWithRegisteredDummyUser(new TestWithDummyUser() {
  79. public void test(User user) throws Exception {
  80. UserBiz userBiz = getUserBiz();
  81. // first, forget password
  82. String confirmCode = userBiz.forgotPassword(user.getLogin(),null);
  83. // get user so we can confirm their password gets changed
  84. User forgetfulUser = userBiz.getUserById(user.getUserId(),null);
  85. // now confirm forgot password
  86. User confirmedUser = userBiz.confirmForgotPassword(
  87. user.getLogin(),confirmCode,"mynewpass",null);
  88. assertNotNull("User returned from confirmForgotPassword() should not be null",
  89. confirmedUser );
  90. assertTrue("User's password should have changed in confirmForgotPassword()",
  91. !forgetfulUser.getPassword().equals(confirmedUser.getPassword()));
  92. }
  93. });
  94. }
  95. /**
  96. * Test confirm forgot password for login that does not exist.
  97. * @throws Exception if any error occurs
  98. */
  99. public void testConfirmForgotPasswordNonExistentUser() throws Exception {
  100. try {
  101. UserBiz userBiz = getUserBiz();
  102. userBiz.confirmForgotPassword("this.user.does.not.exist","does.not.matter","does.not.matter",null);
  103. fail("An AuthorizationException should be thrown when trying to call confirmForgotPassword() with an unkown user login");
  104. } catch ( AuthorizationException e ) {
  105. if ( AuthorizationException.UNKNOWN_LOGIN != e.getReason() ) {
  106. fail("The AuthorizationException reason code should be UNKNOWN_LOGIN");
  107. }
  108. } catch ( Exception e ) {
  109. fail("An AuthorizationException should be thrown when trying to call confirmForgotPassword() with an unkown user login");
  110. }
  111. }
  112. /**
  113. * Test forgot password for bad confirmation code.
  114. * @throws Exception if any error occurs
  115. */
  116. public void testConfirmForgotPasswordBadConfirmationCode() throws Exception {
  117. doTestWithRegisteredDummyUser(new TestWithDummyUser() {
  118. public void test(User user) throws Exception {
  119. UserBiz userBiz = getUserBiz();
  120. // first, forget password
  121. String confirmCode = userBiz.forgotPassword(user.getLogin(),null);
  122. // now confirm forgot password, but with not same confirmation code
  123. try {
  124. userBiz.confirmForgotPassword(
  125. user.getLogin(),"NOT"+confirmCode,"does.not.matter",null);
  126. fail("An AuthorizationException should be thrown when trying to call confirmForgotPassword() with a bad confirmation code");
  127. } catch ( AuthorizationException e ) {
  128. if ( AuthorizationException.FORGOTTEN_PASSWORD_NOT_CONFIRMED != e.getReason() ) {
  129. fail("The AuthorizationException reason code should be FORGOTTEN_PASSWORD_NOT_CONFIRMED");
  130. }
  131. } catch ( Exception e ) {
  132. fail("An AuthorizationException should be thrown when trying to call confirmForgotPassword() with a bad confirmation code");
  133. }
  134. }
  135. });
  136. }
  137. }