Skip to content

Commit

Permalink
tests: Fixed LockedUserSubscriber tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tarlepp committed May 30, 2019
1 parent b9644b8 commit 5171ce3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 76 deletions.
78 changes: 4 additions & 74 deletions tests/Integration/EventSubscriber/LockedUserSubscriberTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
use App\Repository\UserRepository; use App\Repository\UserRepository;
use App\Resource\LogLoginFailureResource; use App\Resource\LogLoginFailureResource;
use App\Security\SecurityUser; use App\Security\SecurityUser;
use Doctrine\Common\Collections\ArrayCollection;
use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent; use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationFailureEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTAuthenticatedEvent; use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\Exception\AuthenticationException; use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Throwable; use Throwable;
use function range;


/** /**
* Class LockedUserSubscriberTest * Class LockedUserSubscriberTest
Expand Down Expand Up @@ -69,34 +66,6 @@ public function testThatOnAuthenticationFailureCallsExpectedServiceMethod(): voi
$subscriber->onAuthenticationFailure(new AuthenticationFailureEvent($authenticationException, new Response())); $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 * @throws Throwable
*/ */
Expand All @@ -105,8 +74,8 @@ public function testThatOnAuthenticationSuccessCallsExpectedServiceMethod(): voi
$user = new User(); $user = new User();
$user->setUsername('test-user'); $user->setUsername('test-user');


$token = new UsernamePasswordToken('test-user', 'password', 'providerKey'); $securityUser = new SecurityUser($user);
$event = new AuthenticationEvent($token); $event = new AuthenticationSuccessEvent([], $securityUser, new Response());


/** /**
* @var MockObject|UserRepository $userRepository * @var MockObject|UserRepository $userRepository
Expand All @@ -119,7 +88,7 @@ public function testThatOnAuthenticationSuccessCallsExpectedServiceMethod(): voi
$userRepository $userRepository
->expects(static::once()) ->expects(static::once())
->method('loadUserByUsername') ->method('loadUserByUsername')
->with('test-user') ->with($user->getId())
->willReturn($user); ->willReturn($user);


$logLoginFailureResource $logLoginFailureResource
Expand All @@ -130,43 +99,4 @@ public function testThatOnAuthenticationSuccessCallsExpectedServiceMethod(): voi
$subscriber = new LockedUserSubscriber($userRepository, $logLoginFailureResource); $subscriber = new LockedUserSubscriber($userRepository, $logLoginFailureResource);
$subscriber->onAuthenticationSuccess($event); $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());
}
} }
3 changes: 1 addition & 2 deletions tests/Unit/EventSubscriber/LockedUserSubscriberTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ class LockedUserSubscriberTest extends KernelTestCase
public function testThatGetSubscribedEventsReturnsExpected(): void public function testThatGetSubscribedEventsReturnsExpected(): void
{ {
$expected = [ $expected = [
'lexik_jwt_authentication.on_authentication_success' => 'onAuthenticationSuccess',
'lexik_jwt_authentication.on_authentication_failure' => 'onAuthenticationFailure', 'lexik_jwt_authentication.on_authentication_failure' => 'onAuthenticationFailure',
'lexik_jwt_authentication.on_jwt_authenticated' => 'onJWTAuthenticated',
'security.authentication.success' => 'onAuthenticationSuccess',
]; ];


static::assertSame($expected, LockedUserSubscriber::getSubscribedEvents()); static::assertSame($expected, LockedUserSubscriber::getSubscribedEvents());
Expand Down

0 comments on commit 5171ce3

Please sign in to comment.