/*
 * Copyright (C) 2004-2008  exedio GmbH (www.exedio.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

package com.exedio.cope.pattern;

import java.util.Arrays;
import java.util.Date;

import com.exedio.cope.AbstractRuntimeTest;
import com.exedio.cope.Feature;
import com.exedio.cope.Model;

public class PasswordRecoveryTest extends AbstractRuntimeTest
{
	private static final Model MODEL = new Model(PasswordRecoveryItem.TYPE);
	
	public PasswordRecoveryTest()
	{
		super(MODEL);
	}
	
	PasswordRecoveryItem i;
	
	@Override
	protected void setUp() throws Exception
	{
		super.setUp();
		i = deleteOnTearDown(new PasswordRecoveryItem("oldpass"));
	}
	
	public void testIt() throws Exception
	{
		// test model
		assertEquals(Arrays.asList(new Feature[]{
				i.TYPE.getThis(),
				i.password,
				i.password.getStorage(),
				i.passwordRecovery,
				i.passwordRecovery.getToken(),
				i.passwordRecovery.getDate(),
			}), i.TYPE.getFeatures());
		assertEquals(Arrays.asList(new Feature[]{
				i.TYPE.getThis(),
				i.password,
				i.password.getStorage(),
				i.passwordRecovery,
				i.passwordRecovery.getToken(),
				i.passwordRecovery.getDate(),
			}), i.TYPE.getDeclaredFeatures());
		
		assertEquals(i.TYPE, i.password.getType());
		assertEquals(i.TYPE, i.passwordRecovery.getToken().getType());
		assertEquals(i.TYPE, i.passwordRecovery.getDate().getType());
		assertEquals("password", i.password.getName());
		assertEquals("passwordRecoveryToken", i.passwordRecovery.getToken().getName());
		assertEquals("passwordRecoveryDate", i.passwordRecovery.getDate().getName());
		
		assertEquals(list(i.passwordRecovery.getToken(), i.passwordRecovery.getDate()), i.passwordRecovery.getSourceFields());
		assertEquals(i.passwordRecovery, i.passwordRecovery.getToken().getPattern());
		assertEquals(i.passwordRecovery, i.passwordRecovery.getDate().getPattern());
		
		assertSame(i.password, i.passwordRecovery.getPassword());
		assertEquals(15*60*1000, i.passwordRecovery.getExpiryMillis());
		
		try
		{
			new PasswordRecovery(null, 1);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("password must not be null", e.getMessage());
		}
		try
		{
			new PasswordRecovery(i.password, 0);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("expiryMillis must be greater zero, but was 0", e.getMessage());
		}
		
		// test persistence
		assertTrue(i.checkPassword("oldpass"));
		assertEquals(0, i.getPasswordRecoveryToken());
		assertEquals(null, i.getPasswordRecoveryDate());
		
		final Date before = new Date();
		final long token = i.issuePasswordRecovery();
		final Date after = new Date();
		assertTrue(i.checkPassword("oldpass"));
		assertEquals(token, i.getPasswordRecoveryToken());
		assertWithin(before, after, i.getPasswordRecoveryDate());
		
		assertTrue(i.checkPassword("oldpass"));
		assertEquals(token, i.getPasswordRecoveryToken());
		assertWithin(before, after, i.getPasswordRecoveryDate());
		
		assertEquals(null, i.redeemPasswordRecovery(token+1));
		assertTrue(i.checkPassword("oldpass"));
		assertEquals(token, i.getPasswordRecoveryToken());
		assertWithin(before, after, i.getPasswordRecoveryDate());
		
		final String newPassword = i.redeemPasswordRecovery(token);
		assertNotNull(newPassword);
		assertTrue(i.checkPassword(newPassword));
		assertEquals(0, i.getPasswordRecoveryToken());
		assertEquals(null, i.getPasswordRecoveryDate());
		
		assertEquals(null, i.redeemPasswordRecovery(token));
		assertNotNull(newPassword);
		assertTrue(i.checkPassword(newPassword));
		assertEquals(0, i.getPasswordRecoveryToken());
		assertEquals(null, i.getPasswordRecoveryDate());
		
		try
		{
			i.redeemPasswordRecovery(0);
			fail();
		}
		catch(IllegalArgumentException e)
		{
			assertEquals("not a valid token: 0", e.getMessage());
		}
	}
}

@@ -63,455 +63,265 @@
 %0A * 
-Permission is hereby granted,
+This library is
  free 
+s
 of
- charge, to any person obtaining a%0A * copy of this software and associated documentation files (the %22Software%22),%0A * to deal in the Software without restriction, including without limitation%0A * the rights to use, copy, modify, merge, publish, distribute, sublicense,%0A * and/or sell copies of the Software, and to permit p
+tware; you can redistribute it and/or%0A * modify it under the terms of the GNU Lesser General Public%0A * License as published by the Free Software Foundation; either%0A * v
 ers
+i
 on
-s to whom the%0A * Software is furnished to do so, subject to the following conditions:
+ 2.1 of the License, or (at your option) any later version.
 %0A *%0A
@@ -329,175 +329,76 @@
 * Th
-e above copyright notice and this permission notice shall be included in%0A * all copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22,
+is library is distributed in the hope that it will be useful,%0A * but
  WIT
@@ -406,16 +406,20 @@
 OUT 
+ANY 
 WARRANTY
  OF 
@@ -418,91 +418,49 @@
 ANTY
- OF ANY KIND, EXPRESS OR%0A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+; without even the implied warranty of%0A *
  MER
@@ -471,20 +471,19 @@
 TABILITY
-,%0A *
+ or
  FITNESS
@@ -511,300 +511,292 @@
 POSE
- AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING%0A * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER%0A * DEALINGS IN THE SOFTWARE.
+.  See the GNU%0A * Lesser General Public License for more details.%0A *%0A * You should have received a copy of the GNU Lesser General Public%0A * License along with this library; if not, write to the Free Software%0A * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 %0A */

fix licence header after r11362: add PasswordRecovery