package com.dodo.blog.server.impl.it;
import com.comvai.auth.exception.AuthenticatedException;
import com.comvai.auth.model.LoginType;
import com.comvai.auth.model.SessionUser;
import com.comvai.auth.server.AuthService;
import com.dodo.blog.exception.UserNotFoundForEmail;
import com.dodo.blog.exception.UserNotFoundForLogin;
import com.dodo.blog.model.Account;
import com.dodo.blog.server.AccountService;
import com.google.guiceberry.junit4.GuiceBerryRule;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import javax.inject.Inject;
/**
* @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
*/
public class AuthServiceTest
extends ServicesEnvironment
{
@Rule
public final GuiceBerryRule guiceBerry = new GuiceBerryRule( ServicesEnvironment.class );
@Inject
private AuthService authService;
@Inject
private AccountService accountService;
@Test
public void testAuthenticateEmailAccount()
{
Account account = new Account();
account.setLoginType( LoginType.OPEN_ID );
account.setEmail( "pohorelecjozef@gmail.com" );
accountService.saveAccount( account );
SessionUser sessionUser = new SessionUser();
sessionUser.setEmail( account.getEmail() );
sessionUser.setLoginType( LoginType.OPEN_ID );
try
{
Account authorizedAccount = ( Account ) authService.authenticate( sessionUser );
Assert.assertEquals( "pohorelecjozef@gmail.com", authorizedAccount.getEmail() );
}
catch ( AuthenticatedException e )
{
Assert.fail( "This exception should not be thrown: " + e );
}
}
@Test
public void testAuthenticateBadEmailAccount()
{
SessionUser sessionUser = new SessionUser();
sessionUser.setLoginType( LoginType.OPEN_ID );
sessionUser.setEmail( "john@foo.com" );
try
{
authService.authenticate( sessionUser );
}
catch ( AuthenticatedException e )
{
Assert.assertEquals( UserNotFoundForEmail.class, e.getClass() );
}
}
@Test
public void testAuthenticateLoginAccount()
{
Account account = new Account();
account.setLoginType( LoginType.LOGIN );
account.setLogin( "dodo" );
account.setPassword( "1111" );
try
{
accountService.register( account );
}
catch ( Exception e )
{
Assert.fail( "This exception should not be thrown: " + e );
}
SessionUser sessionUser = new SessionUser();
sessionUser.setLogin( account.getLogin() );
sessionUser.setPassword( "1111" );
sessionUser.setLoginType( LoginType.LOGIN );
try
{
Account authorizedAccount = ( Account ) authService.authenticate( sessionUser );
Assert.assertEquals( "dodo", authorizedAccount.getLogin() );
Assert.assertEquals( "b59c67bf196a4758191e42f76670ceba", authorizedAccount.getPassword() );
}
catch ( AuthenticatedException e )
{
Assert.fail( "This exception should not be thrown: " + e );
}
}
@Test
public void testAuthenticateBadLoginAccount()
{
SessionUser sessionUser = new SessionUser();
sessionUser.setLoginType( LoginType.LOGIN );
sessionUser.setLogin( "john" );
sessionUser.setPassword( "foo" );
try
{
authService.authenticate( sessionUser );
}
catch ( AuthenticatedException e )
{
Assert.assertEquals( UserNotFoundForLogin.class, e.getClass() );
}
}
}