1 20 21 package com.methodhead.reg; 22 23 import java.util.*; 24 import java.io.*; 25 import java.sql.*; 26 import junit.framework.*; 27 import org.apache.log4j.*; 28 import com.methodhead.persistable.*; 29 import com.methodhead.aikp.*; 30 import com.methodhead.test.*; 31 import com.methodhead.sitecontext.*; 32 import com.methodhead.*; 33 import com.methodhead.transfer.*; 34 35 public class UserTest extends TestCase { 36 37 private User user = null; 38 private Contact contact = null; 39 private ResultSet rs = null; 40 private List list = null; 41 private SiteContext siteContext = null; 42 private Role role = null; 43 44 static { 45 TestUtils.initLogger(); 46 TestUtils.initDb(); 47 } 48 49 public UserTest( String name ) { 50 super( name ); 51 } 52 53 protected void setUp() { 54 try { 55 ConnectionSingleton.runBatchUpdate( new FileReader( "webapp/WEB-INF/db/transfer-reset.sql" ) ); 56 } 57 catch ( Exception e ) { 58 fail( e.getMessage() ); 59 } 60 } 61 62 protected void tearDown() { 63 } 64 65 public void testProperties() { 66 try { 67 user = new User(); 68 69 assertNotNull( user.getRoles() ); 70 assertEquals( 0, user.getRoles().size() ); 71 72 assertNotNull( user.getContact() ); 73 } 74 catch ( Exception e ) { 75 e.printStackTrace(); 76 fail(); 77 } 78 } 79 80 public void testCompareTo() { 81 TestData.createUsers(); 82 83 assertEquals( -1, TestData.user1.compareTo( TestData.user2 ) ); 84 assertEquals( 0, TestData.user1.compareTo( TestData.user1 ) ); 85 assertEquals( 1, TestData.user2.compareTo( TestData.user1 ) ); 86 } 87 88 public void testGetPublicSecret() { 89 90 user = new User(); 94 user.setString( "password", "password" ); 95 assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.getPublicSecret() ); 96 97 user = new User(); 101 user.setPasswordEncrypted( true ); 102 user.setString( "password", "password" ); 103 assertEquals( "dgx+mS8hYD3yKR2nr1/AwA==", user.getPublicSecret() ); 104 } 105 106 public void testHasRole() { 107 TestData.createRoles(); 108 109 user = new User(); 110 user.getRoles().add( TestData.role1 ); 111 user.getRoles().add( TestData.role2 ); 112 113 assertTrue( user.hasRole( TestData.siteContext1, DefaultTransferPolicy.ROLE_SYSADMIN ) ); 114 assertTrue( !user.hasRole( TestData.siteContext1, DefaultTransferPolicy.ROLE_WEBMASTER ) ); 115 } 116 117 public void testSaveNew() { 118 try { 119 TestData.createRoles(); 120 121 user = new User(); 122 user.getRoles().add( TestData.role1 ); 123 user.getContact().setString( "firstname", "firstname" ); 124 user.saveNew(); 125 126 rs = ConnectionSingleton.runQuery( "SELECT sitecontext_id,name FROM reg_role WHERE user_id=" + user.getInt( "id" ) ); 127 assertTrue( rs.next() ); 128 assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, rs.getString( "name" ) ); 129 assertEquals( 1, rs.getInt( "sitecontext_id" ) ); 130 assertTrue( !rs.next() ); 131 ConnectionSingleton.close( rs ); 132 133 contact = new Contact(); 134 contact.load( new IntKey( user.getInt( "contact_id" ) ) ); 135 assertEquals( "firstname", contact.getString( "firstname" ) ); 136 } 137 catch ( Exception e ) { 138 e.printStackTrace(); 139 fail(); 140 } 141 } 142 143 public void testLoad() { 144 try { 145 TestData.createUsers(); 146 147 user = new User(); 148 user.load( new IntKey( 1 ) ); 149 150 contact = user.getContact(); 151 assertEquals( "User1", contact.getString( "firstname" ) ); 152 153 list = user.getRoles(); 154 assertEquals( 2, list.size() ); 155 156 role = ( Role )list.get( 0 ); 157 assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() ); 158 assertEquals( TestData.siteContext1, role.getSiteContext() ); 159 160 role = ( Role )list.get( 1 ); 161 assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() ); 162 assertEquals( SiteContext.getDefaultContext(), role.getSiteContext() ); 163 } 164 catch ( Exception e ) { 165 e.printStackTrace(); 166 fail(); 167 } 168 } 169 170 public void testSave() { 171 try { 172 TestData.createUsers(); 173 174 user = new User(); 175 user.load( new IntKey( 1 ) ); 176 user.getContact().setString( "firstname", "User1Updated" ); 177 user.getRoles().add( TestData.role2 ); 178 user.save(); 179 180 user = new User(); 181 user.load( new IntKey( 1 ) ); 182 183 contact = user.getContact(); 184 assertEquals( "User1Updated", contact.getString( "firstname" ) ); 185 186 list = user.getRoles(); 187 assertEquals( 3, list.size() ); 188 189 role = ( Role )list.get( 0 ); 190 assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() ); 191 assertEquals( TestData.siteContext1, role.getSiteContext() ); 192 193 role = ( Role )list.get( 1 ); 194 assertEquals( DefaultTransferPolicy.ROLE_SYSADMIN, role.getName() ); 195 assertEquals( SiteContext.getDefaultContext(), role.getSiteContext() ); 196 197 role = ( Role )list.get( 2 ); 198 assertEquals( DefaultTransferPolicy.ROLE_SITEADMIN, role.getName() ); 199 assertEquals( TestData.siteContext1, role.getSiteContext() ); 200 } 201 catch ( Exception e ) { 202 e.printStackTrace(); 203 fail(); 204 } 205 } 206 207 public void testDelete() { 208 try { 209 TestData.createUsers(); 210 211 user = new User(); 212 user.load( new IntKey( 1 ) ); 213 user.delete(); 214 215 rs = ConnectionSingleton.runQuery( "SELECT name FROM reg_role WHERE user_id=" + user.getInt( "id" ) ); 216 assertTrue( !rs.next() ); 217 ConnectionSingleton.close( rs ); 218 219 try { 220 contact = new Contact(); 221 contact.load( new IntKey( user.getInt( "contact_id" ) ) ); 222 fail(); 223 } 224 catch ( PersistableException e ) {} 225 } 226 catch ( Exception e ) { 227 e.printStackTrace(); 228 fail(); 229 } 230 } 231 232 public void testLoadForLogin() { 233 try { 234 TestData.createUsers(); 235 236 user = new User(); 237 238 assertTrue( !user.loadForLogin( "invalidlogin" ) ); 239 assertTrue( user.loadForLogin( "test1@methodhead.com" ) ); 240 241 assertEquals( TestData.user1.getInt( "id" ), user.getInt( "id" ) ); 242 } 243 catch ( Exception e ) { 244 e.printStackTrace(); 245 fail(); 246 } 247 } 248 249 public void testLoadAllForSiteContext() { 250 try { 251 TestData.createUsers(); 252 253 user = new User(); 254 255 list = user.loadAllForSiteContext( TestData.siteContext1 ); 256 assertNotNull( list ); 257 assertEquals( 2, list.size() ); 258 259 user = ( User )list.get( 0 ); 260 assertEquals( 1, user.getInt( "id" ) ); 261 262 user = ( User )list.get( 1 ); 263 assertEquals( 2, user.getInt( "id" ) ); 264 } 265 catch ( Exception e ) { 266 e.printStackTrace(); 267 fail(); 268 } 269 } 270 271 public void testEncryptPassword() { 272 try { 273 TestData.createUsers(); 274 275 user = new User(); 279 assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.encryptPassword( "password" ) ); 280 281 assertEquals( "bLdfZSqbUnmOts8iAQV8cw==", user.encryptPassword( "password2" ) ); 285 286 assertEquals( "f3v9NIcJ3uqs4Z4/U1+MVA==", user.encryptPassword( "0123456789012345678901234567890123456789012345678901234567890123" ) ); 290 } 291 catch ( Exception e ) { 292 e.printStackTrace(); 293 fail(); 294 } 295 } 296 297 public void testSetPasswordEncrypted() { 298 try { 299 user = new User(); 300 301 user.setString( "password", "password" ); 305 assertEquals( "password", user.getString( "password" ) ); 306 307 assertEquals( true, user.authenticate( "password" ) ); 311 assertEquals( false, user.authenticate( "invalidpassword" ) ); 312 313 user.setPasswordEncrypted( true ); 317 318 user.setString( "password", "password" ); 322 assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.getString( "password" ) ); 323 324 assertEquals( true, user.authenticate( "password" ) ); 328 assertEquals( false, user.authenticate( "invalidpassword" ) ); 329 330 user.getContact().setString( "email", "test" ); 334 user.saveNew(); 335 user = new User(); 336 user.setPasswordEncrypted( true ); 337 user.load( new IntKey( 1 ) ); 338 assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.getString( "password" ) ); 339 340 user = new User(); 341 user.load( new IntKey( 1 ) ); 342 user.setPasswordEncrypted( true ); 343 assertEquals( "X03MO1qnZdYdgyfeuILPmQ==", user.getString( "password" ) ); 344 345 user = new User(); 349 user.setPasswordEncrypted( true ); 350 user.loadForLogin( "test" ); 351 assertEquals( true, user.authenticate( "password" ) ); 352 } 353 catch ( Exception e ) { 354 e.printStackTrace(); 355 fail(); 356 } 357 } 358 } 359 | Popular Tags |