Fix test suite

This commit is contained in:
Nicolas Le Goff
2013-11-22 11:16:13 +01:00
parent d18c5afb10
commit ed4ae71819
97 changed files with 979 additions and 1367 deletions

View File

@@ -63,18 +63,14 @@ class AccountCreatorTest extends \PhraseanetTestCase
public function testCreateWithTemplates()
{
$random = self::$DI['app']['tokens'];
$template1 = self::$DI['app']['manipulator.user']->createUser('template' . $random->generatePassword(), $random->generatePassword());
$template1->setModel(self::$DI['user']);
$template2 = self::$DI['app']['manipulator.user']->createUser('template' . $random->generatePassword(), $random->generatePassword());
$template2->setModel(self::$DI['user']);
$template3 = self::$DI['app']['manipulator.user']->createUser('template' . $random->generatePassword(), $random->generatePassword());
$template3->setModel(self::$DI['user']);
$template1 = self::$DI['app']['manipulator.user']->createTemplate('template1', self::$DI['user']);
$template2 = self::$DI['app']['manipulator.user']->createTemplate('template2', self::$DI['user']);
$template3 = self::$DI['app']['manipulator.user']->createTemplate('template3', self::$DI['user']);
$templates = [$template1, $template2];
$extra = [$template3];
$creator = new AccountCreator($random, self::$DI['app']['phraseanet.appbox'], true, $templates);
$creator = new AccountCreator(self::$DI['app']['tokens'], self::$DI['app']['phraseanet.appbox'], true, $templates);
$user = $creator->create(self::$DI['app'], self::$DI['app']['tokens']->generatePassword(), null, $extra);
$this->assertInstanceOf('Alchemy\Phrasea\Model\Entities\User', $user);

View File

@@ -15,11 +15,7 @@ class AuthenticatorTest extends \PhraseanetTestCase
{
$app = $this->loadApp();
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
$authenticator = new Authenticator($app, $browser, $session, $em);
$authenticator = new Authenticator(self::$DI['app'], $browser, $session, $em);
$this->assertNull($authenticator->getUser());
}
/**
@@ -31,19 +27,19 @@ class AuthenticatorTest extends \PhraseanetTestCase
$user = self::$DI['user'];
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
self::$DI['app']['browser'] = $browser = $this->getBrowserMock();
self::$DI['app']['session'] = $session = $this->getSessionMock();
$sessionEntity = new Session();
$sessionEntity->setUser($user);
$sessionEntity->setUserAgent('');
$app['EM']->persist($sessionEntity);
$app['EM']->flush();
self::$DI['app']['EM']->persist($sessionEntity);
self::$DI['app']['EM']->flush();
$session->set('usr_id', $user->getId());
$session->set('session_id', $sessionEntity->getId());
$authenticator = new Authenticator($app, $browser, $session, $app['EM']);
$authenticator = new Authenticator(self::$DI['app'], $browser, $session, self::$DI['app']['EM']);
$this->assertEquals($user, $authenticator->getUser());
}
@@ -54,15 +50,11 @@ class AuthenticatorTest extends \PhraseanetTestCase
{
$app = $this->loadApp();
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
$user = $this->getMockBuilder('')
$user = $this->getMockBuilder('Alchemy\Phrasea\Model\Entities\User')
->disableOriginalConstructor()
->getMock();
$authenticator = new Authenticator($app, $browser, $session, $em);
$authenticator = new Authenticator(self::$DI['app'], $browser, $session, $em);
$authenticator->setUser($user);
$this->assertEquals($user, $authenticator->getUser());
$authenticator->setUser(null);
@@ -77,9 +69,9 @@ class AuthenticatorTest extends \PhraseanetTestCase
$app = $this->loadApp();
$capturedSession = null;
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
self::$DI['app']['browser'] = $browser = $this->getBrowserMock();
self::$DI['app']['session'] = $session = $this->getSessionMock();
self::$DI['app']['EM'] = $em = $this->getEntityManagerMock();
$user = $this->getMockBuilder('Alchemy\Phrasea\Model\Entities\User')
->disableOriginalConstructor()
@@ -102,7 +94,7 @@ class AuthenticatorTest extends \PhraseanetTestCase
->method('get')
->will($this->returnValue($acl));
$app['acl'] = $aclProvider;
self::$DI['app']['acl'] = $aclProvider;
$em->expects($this->at(0))
->method('persist')
@@ -113,7 +105,7 @@ class AuthenticatorTest extends \PhraseanetTestCase
$em->expects($this->at(1))
->method('flush');
$authenticator = new Authenticator($app, $browser, $session, $em);
$authenticator = new Authenticator(self::$DI['app'], $browser, $session, $em);
$phsession = $authenticator->openAccount($user);
$this->assertInstanceOf('Alchemy\Phrasea\Model\Entities\Session', $phsession);
@@ -129,9 +121,9 @@ class AuthenticatorTest extends \PhraseanetTestCase
$user = self::$DI['user'];
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $SFsession = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
self::$DI['app']['browser'] = $browser = $this->getBrowserMock();
self::$DI['app']['session'] = $SFsession = $this->getSessionMock();
self::$DI['app']['EM'] = $em = $this->getEntityManagerMock();
$sessionId = 4224242;
@@ -157,7 +149,7 @@ class AuthenticatorTest extends \PhraseanetTestCase
->with($this->equalTo('Phraseanet:Session'))
->will($this->returnValue($repo));
$authenticator = new Authenticator($app, $browser, $SFsession, $em);
$authenticator = new Authenticator(self::$DI['app'], $browser, $SFsession, $em);
$this->assertEquals($session, $authenticator->refreshAccount($session));
}
@@ -170,9 +162,9 @@ class AuthenticatorTest extends \PhraseanetTestCase
$user = self::$DI['user'];
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $SFsession = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
self::$DI['app']['browser'] = $browser = $this->getBrowserMock();
self::$DI['app']['session'] = $SFsession = $this->getSessionMock();
self::$DI['app']['EM'] = $em = $this->getEntityManagerMock();
$sessionId = 4224242;
@@ -198,7 +190,7 @@ class AuthenticatorTest extends \PhraseanetTestCase
->with($this->equalTo('Phraseanet:Session'))
->will($this->returnValue($repo));
$authenticator = new Authenticator($app, $browser, $SFsession, $em);
$authenticator = new Authenticator(self::$DI['app'], $browser, $SFsession, $em);
try {
$authenticator->refreshAccount($session);
$this->fail('Should have raised an exception');
@@ -240,19 +232,19 @@ class AuthenticatorTest extends \PhraseanetTestCase
$user = self::$DI['user'];
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
self::$DI['app']['browser'] = $browser = $this->getBrowserMock();
self::$DI['app']['session'] = $session = $this->getSessionMock();
$sessionEntity = new Session();
$sessionEntity->setUser($user);
$sessionEntity->setUserAgent('');
$app['EM']->persist($sessionEntity);
$app['EM']->flush();
self::$DI['app']['EM']->persist($sessionEntity);
self::$DI['app']['EM']->flush();
$session->set('usr_id', $user->getId());
$session->set('session_id', $sessionEntity->getId());
$authenticator = new Authenticator($app, $browser, $session, $app['EM']);
$authenticator = new Authenticator(self::$DI['app'], $browser, $session, self::$DI['app']['EM']);
$this->assertTrue($authenticator->isAuthenticated());
}
@@ -263,11 +255,7 @@ class AuthenticatorTest extends \PhraseanetTestCase
{
$app = $this->loadApp();
$app['browser'] = $browser = $this->getBrowserMock();
$app['session'] = $session = $this->getSessionMock();
$app['EM'] = $em = $this->getEntityManagerMock();
$authenticator = new Authenticator($app, $browser, $session, $em);
$authenticator = new Authenticator(self::$DI['app'], $browser, $session, $em);
$this->assertFalse($authenticator->isAuthenticated());
}

View File

@@ -4,67 +4,51 @@ namespace Alchemy\Tests\Phrasea\Authentication\Phrasea;
use Alchemy\Phrasea\Authentication\Phrasea\NativeAuthentication;
use Alchemy\Phrasea\Authentication\Exception\AccountLockedException;
use Alchemy\Phrasea\Model\Entities\User;
class NativeAuthenticationTest extends \PhraseanetTestCase
{
/**
* @dataProvider provideReservedUsernames
*/
public function testReservedAreValid($username)
public function testAuthenticationSpecialUser()
{
$password = 'popo42';
$encoder = $this->getEncoderMock();
$oldEncoder = $this->getOldEncoderMock();
$conn = $this->getMock('connection_interface');
$request = $this->getRequestMock();
$auth = new NativeAuthentication($encoder, $oldEncoder, $conn);
$this->assertNull($auth->getUsrId($username, $password, $request));
}
$specialUser = $this->getMock('Alchemy\Phrasea\Model\Entities\User');
$specialUser->expects($this->any())->method('isSpecial')->will($this->returnValue(true));
public function provideReservedUsernames()
{
return [
['autoregister'],
['invite'],
];
$manipulator = $this->getUserManipulatorMock($specialUser);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$this->assertNull($auth->getUsrId('a_login', 'a_password', $request));
}
public function testNotFoundIsNotValid()
{
$username = 'romainneutron';
$password = 'popo42';
$encoder = $this->getEncoderMock();
$oldEncoder = $this->getOldEncoderMock();
$conn = $this->getConnectionMock($username, null);
$request = $this->getRequestMock();
$manipulator = $this->getUserManipulatorMock(null);
$auth = new NativeAuthentication($encoder, $oldEncoder, $conn);
$this->assertNull($auth->getUsrId($username, $password, $request));
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$this->assertNull($auth->getUsrId('a_login', 'a_password', $request));
}
public function testLockAccountThrowsAnException()
{
$username = 'romainneutron';
$password = 'popo42';
$encoder = $this->getEncoderMock();
$oldEncoder = $this->getOldEncoderMock();
$conn = $this->getConnectionMock($username, [
'nonce' => 'dfqsdgqsd',
'salted_password' => '1',
'mail_locked' => '1',
'usr_id' => '1',
'usr_password' => 'qsdfsqdfqsd',
]);
$request = $this->getRequestMock();
$auth = new NativeAuthentication($encoder, $oldEncoder, $conn);
$mailLockedUser = $this->getMock('Alchemy\Phrasea\Model\Entities\User');
$mailLockedUser->expects($this->any())->method('isMailLocked')->will($this->returnValue(true));
$manipulator = $this->getUserManipulatorMock($mailLockedUser);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
try {
$auth->getUsrId($username, $password, $request);
$auth->getUsrId('a_login', 'a_password', $request);
$this->fail('Should have raised an exception');
} catch (AccountLockedException $e) {
@@ -73,23 +57,26 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
public function testGetUsrIdWithCorrectCredentials()
{
$username = 'romainneutron';
$password = 'popo42';
$encoded = 'qsdfsqdfqsd';
$nonce = 'dfqsdgqsd';
$usr_id = '42';
$userId = 42;
$encoder = $this->getEncoderMock();
$oldEncoder = $this->getOldEncoderMock();
$conn = $this->getConnectionMock($username, [
'nonce' => $nonce,
'salted_password' => '1',
'mail_locked' => '0',
'usr_id' => $usr_id,
'usr_password' => $encoded,
]);
$request = $this->getRequestMock();
$user = $this->getMock('Alchemy\Phrasea\Model\Entities\User');
$user->expects($this->any())->method('getId')->will($this->returnValue($userId));
$user->expects($this->any())->method('isSpecial')->will($this->returnValue(false));
$user->expects($this->any())->method('isMailLocked')->will($this->returnValue(false));
$user->expects($this->any())->method('isSaltedPassword')->will($this->returnValue(true));
$user->expects($this->any())->method('getPassword')->will($this->returnValue($encoded));
$user->expects($this->any())->method('getNonce')->will($this->returnValue($nonce));
$manipulator = $this->getUserManipulatorMock($user);
$oldEncoder->expects($this->never())
->method('isPasswordValid');
@@ -98,30 +85,33 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
->with($this->equalTo($encoded), $this->equalTo($password), $this->equalTo($nonce))
->will($this->returnValue(true));
$auth = new NativeAuthentication($encoder, $oldEncoder, $conn);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$this->assertEquals($usr_id, $auth->getUsrId($username, $password, $request));
$this->assertEquals($userId, $auth->getUsrId('a_login', $password, $request));
}
public function testIsNotValidWithIncorrectCredentials()
{
$username = 'romainneutron';
$password = 'popo42';
$encoded = 'qsdfsqdfqsd';
$nonce = 'dfqsdgqsd';
$usr_id = '42';
$userId = 42;
$encoder = $this->getEncoderMock();
$oldEncoder = $this->getOldEncoderMock();
$conn = $this->getConnectionMock($username, [
'nonce' => $nonce,
'salted_password' => '1',
'mail_locked' => '0',
'usr_id' => $usr_id,
'usr_password' => $encoded,
]);
$request = $this->getRequestMock();
$user = $this->getMock('Alchemy\Phrasea\Model\Entities\User');
$user->expects($this->any())->method('getId')->will($this->returnValue($userId));
$user->expects($this->any())->method('isSpecial')->will($this->returnValue(false));
$user->expects($this->any())->method('isMailLocked')->will($this->returnValue(false));
$user->expects($this->any())->method('isSaltedPassword')->will($this->returnValue(true));
$user->expects($this->any())->method('getPassword')->will($this->returnValue($encoded));
$user->expects($this->any())->method('getNonce')->will($this->returnValue($nonce));
$manipulator = $this->getUserManipulatorMock($user);
$oldEncoder->expects($this->never())
->method('isPasswordValid');
@@ -130,30 +120,33 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
->with($this->equalTo($encoded), $this->equalTo($password), $this->equalTo($nonce))
->will($this->returnValue(false));
$auth = new NativeAuthentication($encoder, $oldEncoder, $conn);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$this->assertEquals(false, $auth->getUsrId($username, $password, $request));
$this->assertEquals(false, $auth->getUsrId('a_login', $password, $request));
}
public function testIsNotValidWithIncorrectOldCredentials()
{
$username = 'romainneutron';
$password = 'popo42';
$encoded = 'qsdfsqdfqsd';
$nonce = 'dfqsdgqsd';
$usr_id = '42';
$userId = 42;
$encoder = $this->getEncoderMock();
$oldEncoder = $this->getOldEncoderMock();
$conn = $this->getConnectionMock($username, [
'nonce' => $nonce,
'salted_password' => '0',
'mail_locked' => '0',
'usr_id' => $usr_id,
'usr_password' => $encoded,
]);
$request = $this->getRequestMock();
$user = $this->getMock('Alchemy\Phrasea\Model\Entities\User');
$user->expects($this->any())->method('getId')->will($this->returnValue($userId));
$user->expects($this->any())->method('isSpecial')->will($this->returnValue(false));
$user->expects($this->any())->method('isMailLocked')->will($this->returnValue(false));
$user->expects($this->any())->method('isSaltedPassword')->will($this->returnValue(false));
$user->expects($this->any())->method('getPassword')->will($this->returnValue($encoded));
$user->expects($this->any())->method('getNonce')->will($this->returnValue($nonce));
$manipulator = $this->getUserManipulatorMock($user);
$oldEncoder->expects($this->once())
->method('isPasswordValid')
->with($this->equalTo($encoded), $this->equalTo($password), $this->equalTo($nonce))
@@ -164,62 +157,36 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
->with($this->equalTo($encoded), $this->equalTo($password), $this->equalTo($nonce))
->will($this->returnValue(false));
$auth = new NativeAuthentication($encoder, $oldEncoder, $conn);
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$this->assertEquals(false, $auth->getUsrId($username, $password, $request));
$this->assertEquals(false, $auth->getUsrId('a_login', $password, $request));
}
public function testGetUsrIdWithCorrectOldCredentials()
{
$username = 'romainneutron';
$password = 'popo42';
$encoded = 'qsdfsqdfqsd';
$nonce = 'dfqsdgqsd';
$usr_id = '42';
$userId = 42;
$encoder = $this->getEncoderMock();
$oldEncoder = $this->getOldEncoderMock();
$conn = $this->getMock('connection_interface');
$statement = $this->getMock('PDOStatement');
$statement
->expects($this->once())
->method('execute')
->with($this->equalTo([':login' => $username]));
$statement->expects($this->once())
->method('fetch')
->with($this->equalTo(\PDO::FETCH_ASSOC))
->will($this->returnValue([
'nonce' => $nonce,
'salted_password' => '0',
'mail_locked' => '0',
'usr_id' => $usr_id,
'usr_password' => $encoded,
]));
$catchParameters = $catchTestPassword = null;
$statement2 = $this->getMock('PDOStatement');
$statement2
->expects($this->once())
->method('execute')
->will($this->returnCallback(function ($parameters) use (&$catchParameters) {
$catchParameters = $parameters;
}));
$conn->expects($this->at(0))
->method('prepare')
->with($this->isType('string'))
->will($this->returnValue($statement));
$conn->expects($this->at(1))
->method('prepare')
->with($this->isType('string'))
->will($this->returnValue($statement2));
$request = $this->getRequestMock();
$user = $this->getMock('Alchemy\Phrasea\Model\Entities\User');
$user->expects($this->any())->method('getId')->will($this->returnValue($userId));
$user->expects($this->any())->method('isSpecial')->will($this->returnValue(false));
$user->expects($this->any())->method('isMailLocked')->will($this->returnValue(false));
$user->expects($this->any())->method('isSaltedPassword')->will($this->returnValue(false));
$user->expects($this->any())->method('getPassword')->will($this->returnValue($encoded));
$user->expects($this->any())->method('getNonce')->will($this->returnValue($nonce));
$user->expects($this->once())->method('setSaltedPassword')->with($this->equalTo(true));
$manipulator = $this->getUserManipulatorMock($user);
$manipulator->expects($this->once())->method('setPassword')->with($this->equalTo($user), $this->equalTo($password));
$oldEncoder->expects($this->once())
->method('isPasswordValid')
->with($this->equalTo($encoded), $this->equalTo($password), $this->equalTo($nonce))
@@ -237,37 +204,8 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
return true;
}));
$auth = new NativeAuthentication($encoder, $oldEncoder, $conn);
$this->assertEquals($usr_id, $auth->getUsrId($username, $password, $request));
$this->assertEquals($catchParameters[':password'], $catchTestPassword['encoded']);
$this->assertEquals($password, $catchTestPassword['pass']);
$this->assertEquals($catchParameters[':nonce'], $catchTestPassword['nonce']);
$this->assertEquals($usr_id, $catchParameters[':usr_id']);
}
private function getConnectionMock($username, $row = null)
{
$conn = $this->getMock('connection_interface');
$statement = $this->getMock('PDOStatement');
$statement
->expects($this->once())
->method('execute')
->with($this->equalTo([':login' => $username]));
$statement->expects($this->once())
->method('fetch')
->with($this->equalTo(\PDO::FETCH_ASSOC))
->will($this->returnValue($row));
$conn->expects($this->once())
->method('prepare')
->with($this->isType('string'))
->will($this->returnValue($statement));
return $conn;
$auth = new NativeAuthentication($encoder, $oldEncoder, $manipulator);
$this->assertEquals($userId, $auth->getUsrId('a_login', $password, $request));
}
private function getEncoderMock()
@@ -297,4 +235,15 @@ class NativeAuthenticationTest extends \PhraseanetTestCase
->disableOriginalConstructor()
->getMock();
}
private function getUserManipulatorMock(User $user = null)
{
$repoMock = $this->getMockBuilder('Alchemy\Phrasea\Model\Repositories\UserRepository')->disableOriginalConstructor()->getMock();
$repoMock->expects($this->any())->method('findRealUserByLogin')->will($this->returnValue($user));
$manipulator = $this->getMockBuilder('Alchemy\Phrasea\Model\Manipulator\UserManipulator')->disableOriginalConstructor()->getMock();
$manipulator->expects($this->any())->method('getRepository')->will($this->returnValue($repoMock));
return $manipulator;
}
}