Fix FailureManager tests and dependent AuthenticationManagerServiceProviderTest

This commit is contained in:
Benoît Burnichon
2016-03-15 12:58:44 +01:00
parent 03e0dbae52
commit 9078835ae8
3 changed files with 100 additions and 67 deletions

View File

@@ -4,6 +4,7 @@ namespace Alchemy\Tests\Phrasea\Authentication\Phrasea;
use Alchemy\Phrasea\Authentication\Phrasea\FailureManager; use Alchemy\Phrasea\Authentication\Phrasea\FailureManager;
use Alchemy\Phrasea\Model\Entities\AuthFailure; use Alchemy\Phrasea\Model\Entities\AuthFailure;
use Alchemy\Phrasea\Model\Repositories\AuthFailureRepository;
use Gedmo\Timestampable\TimestampableListener; use Gedmo\Timestampable\TimestampableListener;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
@@ -18,7 +19,7 @@ class FailureManagerTest extends \PhraseanetTestCase
*/ */
public function testSaveFailure() public function testSaveFailure()
{ {
$repo = $this->getRepo(); $repo = $this->createAuthFailureRepositoryMock();
$em = $this->createEntityManagerMock(); $em = $this->createEntityManagerMock();
$recaptcha = $this->getReCaptchaMock(null); $recaptcha = $this->getReCaptchaMock(null);
@@ -31,8 +32,8 @@ class FailureManagerTest extends \PhraseanetTestCase
->will($this->returnValue($ip)); ->will($this->returnValue($ip));
$oldFailures = [ $oldFailures = [
$this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'), $this->getMock(AuthFailure::class),
$this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure') $this->getMock(AuthFailure::class)
]; ];
$repo->expects($this->once()) $repo->expects($this->once())
@@ -41,12 +42,12 @@ class FailureManagerTest extends \PhraseanetTestCase
$em->expects($this->exactly(count($oldFailures))) $em->expects($this->exactly(count($oldFailures)))
->method('remove') ->method('remove')
->with($this->isInstanceOf('Alchemy\Phrasea\Model\Entities\AuthFailure')); ->with($this->isInstanceOf(AuthFailure::class));
$catchFailure = null; $catchFailure = null;
$em->expects($this->once()) $em->expects($this->once())
->method('persist') ->method('persist')
->with($this->isInstanceOf('Alchemy\Phrasea\Model\Entities\AuthFailure')) ->with($this->isInstanceOf(AuthFailure::class))
->will($this->returnCallback(function ($failure) use (&$catchFailure) { ->will($this->returnCallback(function ($failure) use (&$catchFailure) {
$catchFailure = $failure; $catchFailure = $failure;
})); }));
@@ -54,6 +55,8 @@ class FailureManagerTest extends \PhraseanetTestCase
$manager = new FailureManager($repo, $em, $recaptcha, 9); $manager = new FailureManager($repo, $em, $recaptcha, 9);
$manager->saveFailure($username, $request); $manager->saveFailure($username, $request);
/** @var null|AuthFailure $catchFailure */
$this->assertInstanceOf(AuthFailure::class, $catchFailure);
$this->assertEquals($ip, $catchFailure->getIp()); $this->assertEquals($ip, $catchFailure->getIp());
$this->assertEquals(true, $catchFailure->getLocked()); $this->assertEquals(true, $catchFailure->getLocked());
$this->assertEquals($username, $catchFailure->getUsername()); $this->assertEquals($username, $catchFailure->getUsername());
@@ -64,7 +67,7 @@ class FailureManagerTest extends \PhraseanetTestCase
*/ */
public function testCheckFailures() public function testCheckFailures()
{ {
$repo = $this->getRepo(); $repo = $this->createAuthFailureRepositoryMock();
$em = $this->createEntityManagerMock(); $em = $this->createEntityManagerMock();
$recaptcha = $this->getReCaptchaMock(null); $recaptcha = $this->getReCaptchaMock(null);
$request = $this->getRequestMock(); $request = $this->getRequestMock();
@@ -86,7 +89,7 @@ class FailureManagerTest extends \PhraseanetTestCase
*/ */
public function testCheckFailuresLessThan9() public function testCheckFailuresLessThan9()
{ {
$repo = $this->getRepo(); $repo = $this->createAuthFailureRepositoryMock();
$em = $this->createEntityManagerMock(); $em = $this->createEntityManagerMock();
$recaptcha = $this->getReCaptchaMock(null); $recaptcha = $this->getReCaptchaMock(null);
$request = $this->getRequestMock(); $request = $this->getRequestMock();
@@ -94,7 +97,7 @@ class FailureManagerTest extends \PhraseanetTestCase
$username = 'romainneutron'; $username = 'romainneutron';
$oldFailures = $this->ArrayIze(function () { $oldFailures = $this->ArrayIze(function () {
return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); return $this->getMock(AuthFailure::class);
}, 8); }, 8);
$repo->expects($this->once()) $repo->expects($this->once())
@@ -110,7 +113,7 @@ class FailureManagerTest extends \PhraseanetTestCase
*/ */
public function testCheckFailuresMoreThan9WithoutCaptcha() public function testCheckFailuresMoreThan9WithoutCaptcha()
{ {
$repo = $this->getRepo(); $repo = $this->createAuthFailureRepositoryMock();
$em = $this->createEntityManagerMock(); $em = $this->createEntityManagerMock();
$recaptcha = $this->getReCaptchaMock(false); $recaptcha = $this->getReCaptchaMock(false);
$request = $this->getRequestMock(); $request = $this->getRequestMock();
@@ -118,7 +121,7 @@ class FailureManagerTest extends \PhraseanetTestCase
$username = 'romainneutron'; $username = 'romainneutron';
$oldFailures = $this->ArrayIze(function () { $oldFailures = $this->ArrayIze(function () {
return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); return $this->getMock(AuthFailure::class);
}, 10); }, 10);
$repo->expects($this->once()) $repo->expects($this->once())
@@ -134,7 +137,7 @@ class FailureManagerTest extends \PhraseanetTestCase
*/ */
public function testCheckFailuresMoreThan9WithCorrectCaptcha() public function testCheckFailuresMoreThan9WithCorrectCaptcha()
{ {
$repo = $this->getRepo(); $repo = $this->createAuthFailureRepositoryMock();
$em = $this->createEntityManagerMock(); $em = $this->createEntityManagerMock();
$request = $this->getRequestMock(); $request = $this->getRequestMock();
$recaptcha = $this->getReCaptchaMock(true, $request, true); $recaptcha = $this->getReCaptchaMock(true, $request, true);
@@ -142,7 +145,7 @@ class FailureManagerTest extends \PhraseanetTestCase
$username = 'romainneutron'; $username = 'romainneutron';
$oldFailures = $this->ArrayIze(function () { $oldFailures = $this->ArrayIze(function () {
$failure = $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); $failure = $this->getMock(AuthFailure::class);
$failure->expects($this->once()) $failure->expects($this->once())
->method('setLocked') ->method('setLocked')
->with($this->equalTo(false)); ->with($this->equalTo(false));
@@ -164,7 +167,7 @@ class FailureManagerTest extends \PhraseanetTestCase
*/ */
public function testCheckFailuresMoreThan9WithIncorrectCaptcha() public function testCheckFailuresMoreThan9WithIncorrectCaptcha()
{ {
$repo = $this->getRepo(); $repo = $this->createAuthFailureRepositoryMock();
$em = $this->createEntityManagerMock(); $em = $this->createEntityManagerMock();
$request = $this->getRequestMock(); $request = $this->getRequestMock();
$recaptcha = $this->getReCaptchaMock(true, $request, false); $recaptcha = $this->getReCaptchaMock(true, $request, false);
@@ -172,7 +175,7 @@ class FailureManagerTest extends \PhraseanetTestCase
$username = 'romainneutron'; $username = 'romainneutron';
$oldFailures = $this->ArrayIze(function () { $oldFailures = $this->ArrayIze(function () {
return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); return $this->getMock(AuthFailure::class);
}, 10); }, 10);
$repo->expects($this->once()) $repo->expects($this->once())
@@ -185,7 +188,7 @@ class FailureManagerTest extends \PhraseanetTestCase
public function testCheckFailuresTrialsIsConfigurableUnderThreshold() public function testCheckFailuresTrialsIsConfigurableUnderThreshold()
{ {
$repo = $this->getRepo(); $repo = $this->createAuthFailureRepositoryMock();
$em = $this->createEntityManagerMock(); $em = $this->createEntityManagerMock();
$recaptcha = $this->getReCaptchaMock(null); $recaptcha = $this->getReCaptchaMock(null);
$request = $this->getRequestMock(); $request = $this->getRequestMock();
@@ -193,7 +196,7 @@ class FailureManagerTest extends \PhraseanetTestCase
$username = 'romainneutron'; $username = 'romainneutron';
$oldFailures = $this->ArrayIze(function () { $oldFailures = $this->ArrayIze(function () {
return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); return $this->getMock(AuthFailure::class);
}, 2); }, 2);
$repo->expects($this->once()) $repo->expects($this->once())
@@ -207,10 +210,9 @@ class FailureManagerTest extends \PhraseanetTestCase
public function testTrialsIsConfigurable() public function testTrialsIsConfigurable()
{ {
$em = $this->createEntityManagerMock(); $em = $this->createEntityManagerMock();
$recaptcha = $this->getReCaptchaMock(null); $recaptcha = $this->getReCaptchaMock(null);
$manager = new FailureManager($this->createEntityRepositoryMock(), $em, $recaptcha, 2); $manager = new FailureManager($this->createAuthFailureRepositoryMock(), $em, $recaptcha, 2);
$this->assertEquals(2, $manager->getTrials()); $this->assertEquals(2, $manager->getTrials());
} }
@@ -220,7 +222,7 @@ class FailureManagerTest extends \PhraseanetTestCase
*/ */
public function testCheckFailuresTrialsIsConfigurableOverThreshold() public function testCheckFailuresTrialsIsConfigurableOverThreshold()
{ {
$repo = $this->getRepo(); $repo = $this->createAuthFailureRepositoryMock();
$em = $this->createEntityManagerMock(); $em = $this->createEntityManagerMock();
$request = $this->getRequestMock(); $request = $this->getRequestMock();
$recaptcha = $this->getReCaptchaMock(true, $request, false); $recaptcha = $this->getReCaptchaMock(true, $request, false);
@@ -228,7 +230,7 @@ class FailureManagerTest extends \PhraseanetTestCase
$username = 'romainneutron'; $username = 'romainneutron';
$oldFailures = $this->ArrayIze(function () { $oldFailures = $this->ArrayIze(function () {
return $this->getMock('Alchemy\Phrasea\Model\Entities\AuthFailure'); return $this->getMock(AuthFailure::class);
}, 3); }, 3);
$repo->expects($this->once()) $repo->expects($this->once())
@@ -311,14 +313,17 @@ class FailureManagerTest extends \PhraseanetTestCase
private function getRequestMock() private function getRequestMock()
{ {
return $this->getMockBuilder('Symfony\Component\HttpFoundation\Request') return $this->getMockBuilder(Request::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
} }
private function getRepo() /**
* @return AuthFailureRepository|\PHPUnit_Framework_MockObject_MockObject
*/
private function createAuthFailureRepositoryMock()
{ {
return $this->getMockBuilder('Alchemy\Phrasea\Model\Repositories\AuthFailureRepository') return $this->getMockBuilder(AuthFailureRepository::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
} }

View File

@@ -2,11 +2,26 @@
namespace Alchemy\Tests\Phrasea\Core\Provider; namespace Alchemy\Tests\Phrasea\Core\Provider;
use Alchemy\Phrasea\Authentication\AccountCreator;
use Alchemy\Phrasea\Authentication\Authenticator;
use Alchemy\Phrasea\Authentication\Manager as AuthenticationManager;
use Alchemy\Phrasea\Authentication\PersistentCookie\Manager as PersistentCookieManager;
use Alchemy\Phrasea\Authentication\Phrasea\FailureHandledNativeAuthentication;
use Alchemy\Phrasea\Authentication\Phrasea\FailureManager;
use Alchemy\Phrasea\Authentication\Phrasea\NativeAuthentication;
use Alchemy\Phrasea\Authentication\Phrasea\OldPasswordEncoder;
use Alchemy\Phrasea\Authentication\Phrasea\PasswordAuthenticationInterface;
use Alchemy\Phrasea\Authentication\Phrasea\PasswordEncoder;
use Alchemy\Phrasea\Authentication\Provider\Factory;
use Alchemy\Phrasea\Authentication\ProvidersCollection;
use Alchemy\Phrasea\Authentication\SuggestionFinder;
use Alchemy\Phrasea\Core\Provider\RepositoriesServiceProvider; use Alchemy\Phrasea\Core\Provider\RepositoriesServiceProvider;
use Alchemy\Phrasea\Core\Provider\TokensServiceProvider;
use Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider; use Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider;
use Alchemy\Phrasea\Core\Provider\ConfigurationServiceProvider; use Alchemy\Phrasea\Core\Provider\ConfigurationServiceProvider;
use Alchemy\Phrasea\Model\Entities\User;
use Alchemy\Phrasea\Model\Repositories\AuthFailureRepository;
use Alchemy\Phrasea\Model\Repositories\UserRepository; use Alchemy\Phrasea\Model\Repositories\UserRepository;
use Neutron\ReCaptcha\ReCaptcha;
/** /**
* @group functional * @group functional
@@ -19,59 +34,59 @@ class AuthenticationManagerServiceProviderTest extends ServiceProviderTestCase
{ {
return [ return [
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'authentication', 'authentication',
'Alchemy\\Phrasea\\Authentication\\Authenticator', Authenticator::class,
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'authentication.persistent-manager', 'authentication.persistent-manager',
'Alchemy\Phrasea\Authentication\PersistentCookie\Manager' PersistentCookieManager::class
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'authentication.suggestion-finder', 'authentication.suggestion-finder',
'Alchemy\Phrasea\Authentication\SuggestionFinder' SuggestionFinder::class
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'authentication.providers.factory', 'authentication.providers.factory',
'Alchemy\Phrasea\Authentication\Provider\Factory' Factory::class
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'authentication.providers', 'authentication.providers',
'Alchemy\Phrasea\Authentication\ProvidersCollection' ProvidersCollection::class
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'authentication.manager', 'authentication.manager',
'Alchemy\Phrasea\Authentication\Manager' AuthenticationManager::class
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'auth.password-encoder', 'auth.password-encoder',
'Alchemy\Phrasea\Authentication\Phrasea\PasswordEncoder' PasswordEncoder::class
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'auth.old-password-encoder', 'auth.old-password-encoder',
'Alchemy\Phrasea\Authentication\Phrasea\OldPasswordEncoder' OldPasswordEncoder::class
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'auth.native.failure-manager', 'auth.native.failure-manager',
'Alchemy\Phrasea\Authentication\Phrasea\FailureManager' FailureManager::class
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'auth.native', 'auth.native',
'Alchemy\Phrasea\Authentication\Phrasea\PasswordAuthenticationInterface' PasswordAuthenticationInterface::class
], ],
[ [
'Alchemy\Phrasea\Core\Provider\AuthenticationManagerServiceProvider', AuthenticationManagerServiceProvider::class,
'authentication.providers.account-creator', 'authentication.providers.account-creator',
'Alchemy\Phrasea\Authentication\AccountCreator' AccountCreator::class
], ],
]; ];
} }
@@ -83,9 +98,7 @@ class AuthenticationManagerServiceProviderTest extends ServiceProviderTestCase
$app['conf']->set(['authentication', 'captcha', 'trials-before-display'], 42); $app['conf']->set(['authentication', 'captcha', 'trials-before-display'], 42);
//$app['orm.em'] = $this->createEntityManagerMock(); //$app['orm.em'] = $this->createEntityManagerMock();
$app['recaptcha'] = $this->getMockBuilder('Neutron\ReCaptcha\ReCaptcha') $app['recaptcha'] = $this->createReCaptchaMock();
->disableOriginalConstructor()
->getMock();
$manager = $app['auth.native.failure-manager']; $manager = $app['auth.native.failure-manager'];
$this->assertEquals(42, $manager->getTrials()); $this->assertEquals(42, $manager->getTrials());
@@ -93,9 +106,10 @@ class AuthenticationManagerServiceProviderTest extends ServiceProviderTestCase
public function testFailureAccountCreator() public function testFailureAccountCreator()
{ {
self::$DI['app']->register(new ConfigurationServiceProvider()); $app = $this->getApplication();
self::$DI['app']['conf']->set(['authentication', 'auto-create'], ['templates' => []]); $app->register(new ConfigurationServiceProvider());
self::$DI['app']['authentication.providers.account-creator']; $app['conf']->set(['authentication', 'auto-create'], ['templates' => []]);
$app['authentication.providers.account-creator'];
} }
public function testAuthNativeWithCaptchaEnabled() public function testAuthNativeWithCaptchaEnabled()
@@ -111,12 +125,12 @@ class AuthenticationManagerServiceProviderTest extends ServiceProviderTestCase
$app['orm.em'] = $this->createEntityManagerMock(); $app['orm.em'] = $this->createEntityManagerMock();
$app['repo.users'] = $this->createUserRepositoryMock(); $app['repo.users'] = $this->createUserRepositoryMock();
$app['repo.auth-failures'] = $this->createEntityRepositoryMock(); $app['repo.auth-failures'] = $this->getMockBuilder(AuthFailureRepository::class)
$app['recaptcha'] = $this->getMockBuilder('Neutron\ReCaptcha\ReCaptcha')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$app['recaptcha'] = $this->createReCaptchaMock();
$this->assertInstanceOf('Alchemy\Phrasea\Authentication\Phrasea\FailureHandledNativeAuthentication', $app['auth.native']); $this->assertInstanceOf(FailureHandledNativeAuthentication::class, $app['auth.native']);
} }
public function testAuthNativeWithCaptchaDisabled() public function testAuthNativeWithCaptchaDisabled()
@@ -131,26 +145,25 @@ class AuthenticationManagerServiceProviderTest extends ServiceProviderTestCase
$app['orm.em'] = $this->createEntityManagerMock(); $app['orm.em'] = $this->createEntityManagerMock();
$app['repo.users'] = $this->createUserRepositoryMock(); $app['repo.users'] = $this->createUserRepositoryMock();
$app['recaptcha'] = $this->getMockBuilder('Neutron\ReCaptcha\ReCaptcha') $app['recaptcha'] = $this->createReCaptchaMock();
->disableOriginalConstructor()
->getMock();
$this->assertInstanceOf('Alchemy\Phrasea\Authentication\Phrasea\NativeAuthentication', $app['auth.native']); $this->assertInstanceOf(NativeAuthentication::class, $app['auth.native']);
} }
public function testAccountCreator() public function testAccountCreator()
{ {
$template1 = $user = self::$DI['app']['manipulator.user']->createTemplate('template1', self::$DI['user']); $app = $this->getApplication();
$template2 = $user = self::$DI['app']['manipulator.user']->createTemplate('template2', self::$DI['user']); $template1 = $user = $app['manipulator.user']->createTemplate('template1', self::$DI['user']);
$template2 = $user = $app['manipulator.user']->createTemplate('template2', self::$DI['user']);
self::$DI['app']['conf']->set(['authentication', 'auto-create'], ['templates' => [$template1->getId(), $template2->getId()]]); $app['conf']->set(['authentication', 'auto-create'], ['templates' => [$template1->getId(), $template2->getId()]]);
$this->assertEquals([$template1->getLogin(), $template2->getLogin()], array_map(function ($u) { $this->assertEquals([$template1->getLogin(), $template2->getLogin()], array_map(function (User $user) {
return $u->getLogin(); return $user->getLogin();
}, self::$DI['app']['authentication.providers.account-creator']->getTemplates())); }, $app['authentication.providers.account-creator']->getTemplates()));
$this->removeUser(self::$DI['app'], $template1); $this->removeUser($app, $template1);
$this->removeUser(self::$DI['app'], $template2); $this->removeUser($app, $template2);
} }
private function createUserRepositoryMock() private function createUserRepositoryMock()
@@ -159,4 +172,14 @@ class AuthenticationManagerServiceProviderTest extends ServiceProviderTestCase
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
} }
/**
* @return ReCaptcha|\PHPUnit_Framework_MockObject_MockObject
*/
protected function createReCaptchaMock()
{
return $this->getMockBuilder(ReCaptcha::class)
->disableOriginalConstructor()
->getMock();
}
} }

View File

@@ -336,6 +336,11 @@ abstract class PhraseanetTestCase extends WebTestCase
return $cli; return $cli;
} }
/**
* @param null|string $path
* @param string $environment
* @return Application
*/
protected function loadApp($path = null, $environment = Application::ENV_TEST) protected function loadApp($path = null, $environment = Application::ENV_TEST)
{ {
if (null !== $path) { if (null !== $path) {