Skip to content
Permalink
Browse files

tests: Fixed `LockedUserSubscriber` tests

  • Loading branch information...
tarlepp committed May 30, 2019
1 parent b9644b8 commit 5171ce33fc7724647b67c220702786c11c7058d3
@@ -13,17 +13,14 @@
use App\Repository\UserRepository;
use App\Resource\LogLoginFailureResource;
use App\Security\SecurityUser;
use Doctrine\Common\Collections\ArrayCollection;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Throwable;
use function range;
/**
* Class LockedUserSubscriberTest
@@ -69,34 +66,6 @@ public function testThatOnAuthenticationFailureCallsExpectedServiceMethod(): voi
$subscriber->onAuthenticationFailure(new AuthenticationFailureEvent($authenticationException, new Response()));
}
/**
* @throws Throwable
*/
public function testThatOnJWTAuthenticatedCallsExpectedServiceMethod(): void
{
$user = new User();
$user->setUsername('test-user');
$token = new UsernamePasswordToken('test-user', 'password', 'providerKey');
/**
* @var MockObject|UserRepository $userRepository
* @var MockObject|LogLoginFailureResource $logLoginFailureResource
*/
$userRepository = $this->getMockBuilder(UserRepository::class)->disableOriginalConstructor()->getMock();
$logLoginFailureResource = $this->getMockBuilder(LogLoginFailureResource::class)
->disableOriginalConstructor()->getMock();
$userRepository
->expects(static::once())
->method('loadUserByUsername')
->with('test-user')
->willReturn($user);
$subscriber = new LockedUserSubscriber($userRepository, $logLoginFailureResource);
$subscriber->onJWTAuthenticated(new JWTAuthenticatedEvent([], $token));
}
/**
* @throws Throwable
*/
@@ -105,8 +74,8 @@ public function testThatOnAuthenticationSuccessCallsExpectedServiceMethod(): voi
$user = new User();
$user->setUsername('test-user');
$token = new UsernamePasswordToken('test-user', 'password', 'providerKey');
$event = new AuthenticationEvent($token);
$securityUser = new SecurityUser($user);
$event = new AuthenticationSuccessEvent([], $securityUser, new Response());
/**
* @var MockObject|UserRepository $userRepository
@@ -119,7 +88,7 @@ public function testThatOnAuthenticationSuccessCallsExpectedServiceMethod(): voi
$userRepository
->expects(static::once())
->method('loadUserByUsername')
->with('test-user')
->with($user->getId())
->willReturn($user);
$logLoginFailureResource
@@ -130,43 +99,4 @@ public function testThatOnAuthenticationSuccessCallsExpectedServiceMethod(): voi
$subscriber = new LockedUserSubscriber($userRepository, $logLoginFailureResource);
$subscriber->onAuthenticationSuccess($event);
}
/**
* @throws Throwable
*/
public function testThatOnJWTAuthenticatedDoesNotCallResetServiceMethodIfUserHasEnoughLoginFailures(): void
{
$token = new UsernamePasswordToken('test-user', 'password', 'providerKey');
$event = new JWTAuthenticatedEvent([], $token);
/**
* @var MockObject|UserRepository $userRepository
* @var MockObject|LogLoginFailureResource $logLoginFailureResource
*/
$userRepository = $this->getMockBuilder(UserRepository::class)->disableOriginalConstructor()->getMock();
$logLoginFailureResource = $this->getMockBuilder(LogLoginFailureResource::class)
->disableOriginalConstructor()->getMock();
$user = $this->getMockBuilder(User::class)->getMock();
$userRepository
->expects(static::once())
->method('loadUserByUsername')
->with('test-user')
->willReturn($user);
$logLoginFailureResource
->expects(static::never())
->method('reset')
->with($user);
$user
->expects(static::once())
->method('getLogsLoginFailure')
->willReturn(new ArrayCollection(range(0, 11)));
$subscriber = new LockedUserSubscriber($userRepository, $logLoginFailureResource);
$subscriber->onJWTAuthenticated($event);
static::assertFalse($event->getToken()->isAuthenticated());
}
}
@@ -22,9 +22,8 @@ class LockedUserSubscriberTest extends KernelTestCase
public function testThatGetSubscribedEventsReturnsExpected(): void
{
$expected = [
'lexik_jwt_authentication.on_authentication_success' => 'onAuthenticationSuccess',
'lexik_jwt_authentication.on_authentication_failure' => 'onAuthenticationFailure',
'lexik_jwt_authentication.on_jwt_authenticated' => 'onJWTAuthenticated',
'security.authentication.success' => 'onAuthenticationSuccess',
];
static::assertSame($expected, LockedUserSubscriber::getSubscribedEvents());

0 comments on commit 5171ce3

Please sign in to comment.
You can’t perform that action at this time.