UserBizTest.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* ===================================================================
  2. * UserBizTest.java
  3. *
  4. * Created Sep 29, 2004 11:18:28 AM
  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: UserBizTest.java 60 2009-05-11 09:18:28Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.biz.test;
  27. import magoffin.matt.ieat.AbstractIeatTestCase;
  28. import magoffin.matt.ieat.AuthorizationException;
  29. import magoffin.matt.ieat.biz.UserBiz;
  30. import magoffin.matt.ieat.domain.User;
  31. import magoffin.matt.util.DataEncryption;
  32. /**
  33. * Test case for UserBiz.
  34. *
  35. * <p>These tests can only work when the test data is loaded in the database!</p>
  36. *
  37. * @author Matt Magoffin (spamsqr@msqr.us)
  38. * @version $Revision: 60 $ $Date: 2009-05-11 21:18:28 +1200 (Mon, 11 May 2009) $
  39. */
  40. public class UserBizTest extends AbstractIeatTestCase {
  41. /** The DataEncryption. */
  42. protected DataEncryption encryptor;
  43. /** The UserBiz to test. */
  44. protected UserBiz userBiz;
  45. /**
  46. * Test create/remove user.
  47. * @throws AuthorizationException if auth error occurs
  48. */
  49. public void testCreateAndRemoveUser() throws AuthorizationException {
  50. User u = getDummyUser();
  51. User newUser = null;
  52. newUser = userBiz.storeUser(u,null);
  53. assertNotNull("Newly created user should not be null", newUser);
  54. assertNotNull("Newly create user should have an ID", newUser.getUserId());
  55. assertTrue("Newly create user should have same login as dummy user",
  56. u.getLogin().equals(newUser.getLogin()));
  57. }
  58. /**
  59. * Test logon user.
  60. * @throws AuthorizationException if auth error occurs
  61. */
  62. public void testLogonUserGoodPassword() throws AuthorizationException {
  63. User u = getDummyUser();
  64. u = userBiz.storeUser(u,null);
  65. User newUser = null;
  66. try {
  67. newUser = userBiz.logonUser(TEST_USER_LOGIN,TEST_USER_PASS);
  68. assertNotNull("User should not be null", newUser);
  69. assertEquals(TEST_USER_LOGIN,newUser.getLogin());
  70. } catch ( AuthorizationException e ) {
  71. fail("Authentication failuser for login test/test: " +e.toString());
  72. }
  73. }
  74. /**
  75. * Test logon bad user.
  76. */
  77. public void testLogonUserBadUser() {
  78. boolean correct = false;
  79. try {
  80. userBiz.logonUser(System.currentTimeMillis()+"","foo");
  81. } catch ( AuthorizationException e ) {
  82. assertTrue("Authorization error should be 'unknown user'",
  83. AuthorizationException.UNKNOWN_LOGIN == e.getReason());
  84. correct = true;
  85. }
  86. assertTrue("Logon should throw exception for unknown user", correct);
  87. }
  88. /**
  89. * Test bad password logon.
  90. *
  91. * @throws AuthorizationException if auth error occurs
  92. */
  93. public void testLogonUserBadPassword() throws AuthorizationException {
  94. User u = getDummyUser();
  95. u = userBiz.storeUser(u,null);
  96. boolean correct = false;
  97. try {
  98. userBiz.logonUser(TEST_USER_LOGIN,"this.is.not.the.right.password");
  99. } catch ( AuthorizationException e ) {
  100. assertTrue("Authorization error should be 'bad passowrd'",
  101. AuthorizationException.BAD_PASSWORD == e.getReason());
  102. correct = true;
  103. }
  104. assertTrue("Logon should throw exception for bad password", correct);
  105. }
  106. /**
  107. * Test register and confirm user.
  108. *
  109. * @throws AuthorizationException if auth error occurs
  110. */
  111. public void testRegisterAndConfirmUser() throws AuthorizationException {
  112. User u = getDummyUser();
  113. String confirmation = userBiz.registerUser(u, null);
  114. assertNotNull("Confirmation should never be null", confirmation );
  115. assertTrue("Confirmation should not be empty", confirmation.length() > 0 );
  116. // now confirm user
  117. User confirmedUser = userBiz.confirmRegisteredUser(u.getLogin(),confirmation, null);
  118. assertNotNull("Confirmed user should not be null", confirmedUser );
  119. assertTrue("Confirmed user and registered user should have same login",
  120. u.getLogin().equals(confirmedUser.getLogin()));
  121. }
  122. /**
  123. * Test register duplicate user.
  124. *
  125. * @throws AuthorizationException if auth error occurs
  126. */
  127. public void testRegisterDuplicateUser() throws AuthorizationException {
  128. User u = getDummyUser();
  129. userBiz.registerUser(u, null);
  130. boolean ok = false;
  131. try {
  132. User u2 = getDummyUser();
  133. userBiz.registerUser(u2, null);
  134. } catch ( AuthorizationException e ) {
  135. assertTrue("AuthExpt with reason 'duplicate login' should be thrown",
  136. AuthorizationException.DUPLICATE_LOGIN == e.getReason());
  137. ok = true;
  138. }
  139. if ( !ok ) {
  140. fail("AuthorizationException with reason 'duplicate login' was not thrown");
  141. }
  142. }
  143. /**
  144. * Test bad registration confirmation.
  145. *
  146. * @throws AuthorizationException if auth error occurs
  147. */
  148. public void testRegisterBadConfirmation() throws AuthorizationException {
  149. User u = getDummyUser();
  150. userBiz.registerUser(u, null); // don't keep track of conf code!
  151. boolean ok = false;
  152. try {
  153. String badPass = encryptor.encrypt("this.is.not.the.confirmation.code");
  154. userBiz.confirmRegisteredUser(u.getLogin(), badPass, null);
  155. } catch ( AuthorizationException e ) {
  156. assertTrue("AuthExpt with reason 'not confirmed' should be thrown",
  157. AuthorizationException.REGISTRATION_NOT_CONFIRMED == e.getReason());
  158. ok = true;
  159. }
  160. if ( !ok ) {
  161. fail("AuthorizationException with reason 'not confirmed' was not thrown");
  162. }
  163. }
  164. }