/* ===================================================================
 * UserBizTest.java
 * 
 * Created Sep 29, 2004 11:18:28 AM
 * 
 * Copyright (c) 2004 Matt Magoffin (spamsqr@msqr.us)
 * 
 * This program is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU General Public License as 
 * published by the Free Software Foundation; either version 2 of 
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
 * 02111-1307 USA
 * ===================================================================
 * $Id: UserBizTest.java 60 2009-05-11 09:18:28Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.biz.test;

import magoffin.matt.ieat.AbstractIeatTestCase;
import magoffin.matt.ieat.AuthorizationException;
import magoffin.matt.ieat.biz.UserBiz;
import magoffin.matt.ieat.domain.User;
import magoffin.matt.util.DataEncryption;

/**
 * Test case for UserBiz.
 * 
 * <p>These tests can only work when the test data is loaded in the database!</p>
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 60 $ $Date: 2009-05-11 21:18:28 +1200 (Mon, 11 May 2009) $
 */
public class UserBizTest extends AbstractIeatTestCase {
	
	/** The DataEncryption. */
	protected DataEncryption encryptor;

	/** The UserBiz to test. */
	protected UserBiz userBiz;
	
	/**
	 * Test create/remove user.
	 * @throws AuthorizationException if auth error occurs
	 */
	public void testCreateAndRemoveUser() throws AuthorizationException {
		User u = getDummyUser();
		
		User newUser = null;
		newUser = userBiz.storeUser(u,null);
		
		assertNotNull("Newly created user should not be null", newUser);
		assertNotNull("Newly create user should have an ID", newUser.getUserId());
		assertTrue("Newly create user should have same login as dummy user", 
				u.getLogin().equals(newUser.getLogin()));
	}
	
	/**
	 * Test logon user.
	 * @throws AuthorizationException if auth error occurs
	 */
	public void testLogonUserGoodPassword() throws AuthorizationException {
		
		User u = getDummyUser();
		u = userBiz.storeUser(u,null);
		
		User newUser = null;
		try {
			newUser = userBiz.logonUser(TEST_USER_LOGIN,TEST_USER_PASS);
			assertNotNull("User should not be null", newUser);
			assertEquals(TEST_USER_LOGIN,newUser.getLogin());
		} catch ( AuthorizationException e ) {
			fail("Authentication failuser for login test/test: " +e.toString());
		}
	}
	
	/**
	 * Test logon bad user.
	 */
	public void testLogonUserBadUser() {
		boolean correct = false;
		try {
			userBiz.logonUser(System.currentTimeMillis()+"","foo");
		} catch ( AuthorizationException e ) {
			assertTrue("Authorization error should be 'unknown user'", 
					AuthorizationException.UNKNOWN_LOGIN == e.getReason());
			correct = true;
		}
		assertTrue("Logon should throw exception for unknown user", correct);
	}
	
	/**
	 * Test bad password logon.
	 * 
	 * @throws AuthorizationException if auth error occurs
	 */
	public void testLogonUserBadPassword() throws AuthorizationException {
		User u = getDummyUser();
		u = userBiz.storeUser(u,null);
		
		boolean correct = false;
		try {
			userBiz.logonUser(TEST_USER_LOGIN,"this.is.not.the.right.password");
		} catch ( AuthorizationException e ) {
			assertTrue("Authorization error should be 'bad passowrd'", 
					AuthorizationException.BAD_PASSWORD == e.getReason());
			correct = true;
		}
		assertTrue("Logon should throw exception for bad password", correct);
	}
	
	/**
	 * Test register and confirm user.
	 * 
	 * @throws AuthorizationException if auth error occurs
	 */
	public void testRegisterAndConfirmUser() throws AuthorizationException {
		User u = getDummyUser();
		String confirmation = userBiz.registerUser(u, null);
		
		assertNotNull("Confirmation should never be null", confirmation );
		assertTrue("Confirmation should not be empty", confirmation.length() > 0 );
		
		// now confirm user
		User confirmedUser = userBiz.confirmRegisteredUser(u.getLogin(),confirmation, null);
		
		assertNotNull("Confirmed user should not be null", confirmedUser );
		assertTrue("Confirmed user and registered user should have same login", 
				u.getLogin().equals(confirmedUser.getLogin()));
	}
	
	/**
	 * Test register duplicate user.
	 * 
	 * @throws AuthorizationException if auth error occurs
	 */
	public void testRegisterDuplicateUser() throws AuthorizationException {
		User u = getDummyUser();
		userBiz.registerUser(u, null);
		boolean ok = false;
		try {
			User u2 = getDummyUser();
			userBiz.registerUser(u2, null);
		} catch ( AuthorizationException e ) {
			assertTrue("AuthExpt with reason 'duplicate login' should be thrown", 
					AuthorizationException.DUPLICATE_LOGIN == e.getReason());
			ok = true;
		}
		if ( !ok ) {
			fail("AuthorizationException with reason 'duplicate login' was not thrown");
		}
	}
	
	/**
	 * Test bad registration confirmation.
	 * 
	 * @throws AuthorizationException if auth error occurs
	 */
	public void testRegisterBadConfirmation() throws AuthorizationException {
		User u = getDummyUser();
		userBiz.registerUser(u, null); // don't keep track of conf code!
		boolean ok = false;
		try {
			String badPass = encryptor.encrypt("this.is.not.the.confirmation.code");
			userBiz.confirmRegisteredUser(u.getLogin(), badPass, null);
		} catch ( AuthorizationException e ) {
			assertTrue("AuthExpt with reason 'not confirmed' should be thrown", 
					AuthorizationException.REGISTRATION_NOT_CONFIRMED == e.getReason());
			ok = true;
		}
		if ( !ok ) {
			fail("AuthorizationException with reason 'not confirmed' was not thrown");
		}
	}
	
}
